From 33d29e01b1c90990039fb39781455d5a4c63b5ae Mon Sep 17 00:00:00 2001 From: Manuel Stahl Date: Tue, 16 Apr 2024 12:45:51 +0200 Subject: [PATCH] Add authProvider test Change-Id: Ia5acce659a386437687e38ae03d578e3bccb9324 --- src/synapse/authProvider.test.js | 137 +++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/synapse/authProvider.test.js diff --git a/src/synapse/authProvider.test.js b/src/synapse/authProvider.test.js new file mode 100644 index 0000000..1fecbc3 --- /dev/null +++ b/src/synapse/authProvider.test.js @@ -0,0 +1,137 @@ +import { waitFor } from "@testing-library/react"; + +import authProvider from "./authProvider"; + +describe("authProvider", () => { + beforeEach(() => { + fetch.resetMocks(); + localStorage.clear(); + }); + + describe("login", () => { + it("should successfully login with username and password", async () => { + fetch.once( + JSON.stringify({ + home_server: "example.com", + user_id: "@user:example.com", + access_token: "foobar", + device_id: "some_device", + }) + ); + + const ret = await authProvider.login({ + base_url: "http://example.com", + username: "@user:example.com", + password: "secret", + }); + + expect(ret).toBe(undefined); + expect(fetch).toBeCalledWith( + "http://example.com/_matrix/client/r0/login", + { + body: '{"device_id":null,"initial_device_display_name":"Synapse Admin","type":"m.login.password","user":"@user:example.com","password":"secret"}', + headers: new Headers({ + Accept: ["application/json"], + "Content-Type": ["application/json"], + }), + method: "POST", + } + ); + expect(localStorage.getItem("base_url")).toEqual("http://example.com"); + expect(localStorage.getItem("user_id")).toEqual("@user:example.com"); + expect(localStorage.getItem("access_token")).toEqual("foobar"); + expect(localStorage.getItem("device_id")).toEqual("some_device"); + }); + }); + + it("should successfully login with token", async () => { + fetch.once( + JSON.stringify({ + home_server: "example.com", + user_id: "@user:example.com", + access_token: "foobar", + device_id: "some_device", + }) + ); + + const ret = await authProvider.login({ + base_url: "https://example.com/", + loginToken: "login_token", + }); + + expect(ret).toBe(undefined); + expect(fetch).toBeCalledWith( + "https://example.com/_matrix/client/r0/login", + { + body: '{"device_id":null,"initial_device_display_name":"Synapse Admin","type":"m.login.token","token":"login_token"}', + headers: new Headers({ + Accept: ["application/json"], + "Content-Type": ["application/json"], + }), + method: "POST", + } + ); + expect(localStorage.getItem("base_url")).toEqual("https://example.com"); + expect(localStorage.getItem("user_id")).toEqual("@user:example.com"); + expect(localStorage.getItem("access_token")).toEqual("foobar"); + expect(localStorage.getItem("device_id")).toEqual("some_device"); + }); + + describe("logout", () => { + it("should remove the access_token from localStorage", async () => { + localStorage.setItem("base_url", "example.com"); + localStorage.setItem("access_token", "foo"); + fetch.mockResponse(JSON.stringify({})); + + await authProvider.logout(); + + expect(fetch).toBeCalledWith("example.com/_matrix/client/r0/logout", { + headers: new Headers({ + Accept: ["application/json"], + Authorization: ["Bearer foo"], + }), + method: "POST", + user: { authenticated: true, token: "Bearer foo" }, + }); + waitFor(() => expect(localStorage.getItem("access_token")).toBeNull()); + }); + }); + + describe("checkError", () => { + it("should resolve if error.status is not 401 or 403", async () => { + await expect( + authProvider.checkError({ status: 200 }) + ).resolves.toBeUndefined(); + }); + + it("should reject if error.status is 401", async () => { + await expect( + authProvider.checkError({ status: 401 }) + ).rejects.toBeUndefined(); + }); + + it("should reject if error.status is 403", async () => { + await expect( + authProvider.checkError({ status: 403 }) + ).rejects.toBeUndefined(); + }); + }); + + describe("checkAuth", () => { + it("should reject when not logged in", async () => { + await expect(authProvider.checkAuth({})).rejects.toBeUndefined(); + }); + + it("should resolve when logged in", async () => { + localStorage.setItem("access_token", "foobar"); + + await expect(authProvider.checkAuth({})).resolves.toBeUndefined(); + }); + }); + + describe("getPermissions", () => { + it("should do nothing", async () => { + await expect(authProvider.getPermissions()).resolves.toBeUndefined(); + }); + }); +});