From 206fa760ff4feb5fafb71a61bde8e5bcd77c1a5f Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Thu, 11 Apr 2024 10:20:18 +0200 Subject: [PATCH 1/2] fix users js tests --- web/src/client/users.test.js | 180 +++++++++++++++++++++-------------- 1 file changed, 106 insertions(+), 74 deletions(-) diff --git a/web/src/client/users.test.js b/web/src/client/users.test.js index 220a3b5313..e49604999c 100644 --- a/web/src/client/users.test.js +++ b/web/src/client/users.test.js @@ -21,71 +21,91 @@ // @ts-check -import DBusClient from "./dbus"; +import { HTTPClient } from "./http"; import { UsersClient } from "./users"; -jest.mock("./dbus"); +const mockJsonFn = jest.fn(); -const USERS_IFACE = "org.opensuse.Agama.Users1"; +const mockGetFn = jest.fn().mockImplementation(() => { + return { + ok: true, + json: mockJsonFn, + }; +}); -let setFirstUserResult = [true, []]; +const mockPutFn = jest.fn().mockImplementation(() => { + return { + ok: true, + }; +}); -const usersProxy = { - wait: jest.fn(), - FirstUser: ["Jane Doe", "jane", "12345", false, {}], - SetFirstUser: jest.fn().mockResolvedValue(setFirstUserResult), - RemoveFirstUser: jest.fn().mockResolvedValue(0), - SetRootPassword: jest.fn().mockResolvedValue(0), - RemoveRootPassword: jest.fn().mockResolvedValue(0), - RootPasswordSet: false, - SetRootSSHKey: jest.fn().mockResolvedValue(0), - RootSSHKey: "ssh-key" -}; +const mockDeleteFn = jest.fn().mockImplementation(() => { + return { + ok: true, + }; +}); -beforeEach(() => { - // @ts-ignore - DBusClient.mockImplementation(() => { - return { - proxy: (iface) => { - if (iface === USERS_IFACE) return usersProxy; - } - }; - }); +const mockPatchFn = jest.fn().mockImplementation(() => { + return { + ok: true, + }; +}); + +jest.mock("./http", () => { + return { + HTTPClient: jest.fn().mockImplementation(() => { + return { + get: mockGetFn, + put: mockPutFn, + delete: mockDeleteFn, + patch: mockPatchFn, + }; + }), + }; }); +const FirstUser = { + fullName: "Jane Doe", + userName: "jane", + password: "12345", + autologin: false, + data: {} +}; +const RootPasswordSet = false; +const RootSSHKey = "ssh-key"; + describe("#getUser", () => { it("returns the defined first user", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + mockJsonFn.mockResolvedValue(FirstUser); const user = await client.getUser(); expect(user).toEqual({ fullName: "Jane Doe", userName: "jane", password: "12345", - autologin: false + autologin: false, + data: {}, }); }); }); describe("#isRootPasswordSet", () => { describe("when the root password is set", () => { - beforeEach(() => { - usersProxy.RootPasswordSet = true; - }); - it("returns true", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + mockJsonFn.mockResolvedValue({ password: true }); const result = await client.isRootPasswordSet(); expect(result).toEqual(true); }); }); describe("when the root password is not set", () => { - beforeEach(() => { - usersProxy.RootPasswordSet = false; - }); - it("returns false", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + mockJsonFn.mockResolvedValue({ password: false }); const result = await client.isRootPasswordSet(); expect(result).toEqual(false); }); @@ -94,7 +114,9 @@ describe("#isRootPasswordSet", () => { describe("#getRootSSHKey", () => { it("returns the SSH key for the root user", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + mockJsonFn.mockResolvedValue({ sshkey: RootSSHKey }); const result = await client.getRootSSHKey(); expect(result).toEqual("ssh-key"); }); @@ -102,51 +124,58 @@ describe("#getRootSSHKey", () => { describe("#setUser", () => { it("sets the values of the first user and returns whether succeeded or not an errors found", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.setUser({ fullName: "Jane Doe", userName: "jane", password: "12345", - autologin: false + autologin: false, + data: [] }); - expect(usersProxy.SetFirstUser).toHaveBeenCalledWith("Jane Doe", "jane", "12345", false, {}); + expect(mockPutFn.mock.calls[0][1]).toEqual( + { + fullName: "Jane Doe", + userName: "jane", + password: "12345", + autologin: false, + data: [] + }); expect(result).toEqual({ result: true, issues: [] }); }); describe("when setting the user fails because some issue", () => { - beforeEach(() => { - setFirstUserResult = [false, ["There is an error"]]; - usersProxy.SetFirstUser = jest.fn().mockResolvedValue(setFirstUserResult); - }); - it("returns an object with the result as false and the issues found", async () => { - const client = new UsersClient(); + mockPutFn.mockImplementationOnce(() => { return { ok: false, } }); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.setUser({ fullName: "Jane Doe", userName: "jane", password: "12345", - autologin: false + autologin: false, + data: [] }); - expect(result).toEqual({ result: false, issues: ["There is an error"] }); + expect(result).toEqual({ result: false, issues: [] }); // TODO: test when we start detecting issues }); }); }); describe("#removeUser", () => { it("removes the first user and returns true", async () => { - const client = new UsersClient(); - const result = await client.removeUser(); - expect(usersProxy.RemoveFirstUser).toHaveBeenCalled(); - expect(result).toEqual(true); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + client.removeUser(); + expect(mockDeleteFn).toHaveBeenCalled(); }); describe("when removing the user fails", () => { - beforeEach(() => (usersProxy.RemoveFirstUser = jest.fn().mockResolvedValue(1))); - it("returns false", async () => { - const client = new UsersClient(); + mockDeleteFn.mockImplementationOnce(() => { return { ok: false, } }); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.removeUser(); expect(result).toEqual(false); }); @@ -155,17 +184,18 @@ describe("#removeUser", () => { describe("#setRootPassword", () => { it("sets the root password and returns true", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.setRootPassword("12345"); - expect(usersProxy.SetRootPassword).toHaveBeenCalledWith("12345", false); + expect(mockPatchFn.mock.calls[0][1]).toEqual({ password: "12345", password_encrypted: false }); expect(result).toEqual(true); }); describe("when setting the password fails", () => { - beforeEach(() => (usersProxy.SetRootPassword = jest.fn().mockResolvedValue(1))); - it("returns false", async () => { - const client = new UsersClient(); + mockPatchFn.mockImplementationOnce(() => { return { ok: false, } }); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.setRootPassword("12345"); expect(result).toEqual(false); }); @@ -174,17 +204,18 @@ describe("#setRootPassword", () => { describe("#removeRootPassword", () => { it("removes the root password", async () => { - const client = new UsersClient(); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.removeRootPassword(); - expect(usersProxy.RemoveRootPassword).toHaveBeenCalled(); + expect(mockPatchFn.mock.calls[0][1]).toEqual({ password: "", password_encrypted: false }); expect(result).toEqual(true); }); - describe("when setting the user fails", () => { - beforeEach(() => (usersProxy.RemoveRootPassword = jest.fn().mockResolvedValue(1))); - + describe("when setting the root password fails", () => { it("returns false", async () => { - const client = new UsersClient(); + mockPatchFn.mockImplementationOnce(() => { return { ok: false, } }); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.removeRootPassword(); expect(result).toEqual(false); }); @@ -192,19 +223,20 @@ describe("#removeRootPassword", () => { }); describe("#setRootSSHKey", () => { - it("sets the root password and returns true", async () => { - const client = new UsersClient(); + it("sets the root SSH key and returns true", async () => { + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); const result = await client.setRootSSHKey("ssh-key"); - expect(usersProxy.SetRootSSHKey).toHaveBeenCalledWith("ssh-key"); + expect(mockPatchFn.mock.calls[0][1]).toEqual({ sshkey: "ssh-key" }); expect(result).toEqual(true); }); - describe("when setting the user fails", () => { - beforeEach(() => (usersProxy.SetRootSSHKey = jest.fn().mockResolvedValue(1))); - + describe("when setting the root SSH key fails", () => { it("returns false", async () => { - const client = new UsersClient(); - const result = await client.setRootSSHKey("ssh-key"); + mockPatchFn.mockImplementationOnce(() => { return { ok: false, } }); + const http = new HTTPClient(new URL("http://localhost")); + const client = new UsersClient(http); + const result = await client.setRootSSHKey("12345"); expect(result).toEqual(false); }); }); From a2bf8078698a0691a454e3730f1a33e85658c78d Mon Sep 17 00:00:00 2001 From: Josef Reidinger Date: Thu, 11 Apr 2024 10:37:11 +0200 Subject: [PATCH 2/2] fix case --- web/src/client/users.test.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/web/src/client/users.test.js b/web/src/client/users.test.js index e49604999c..9121a3c39b 100644 --- a/web/src/client/users.test.js +++ b/web/src/client/users.test.js @@ -64,21 +64,20 @@ jest.mock("./http", () => { }; }); -const FirstUser = { +const firstUser = { fullName: "Jane Doe", userName: "jane", password: "12345", autologin: false, data: {} }; -const RootPasswordSet = false; -const RootSSHKey = "ssh-key"; +const rootSSHKey = "ssh-key"; describe("#getUser", () => { it("returns the defined first user", async () => { const http = new HTTPClient(new URL("http://localhost")); const client = new UsersClient(http); - mockJsonFn.mockResolvedValue(FirstUser); + mockJsonFn.mockResolvedValue(firstUser); const user = await client.getUser(); expect(user).toEqual({ fullName: "Jane Doe", @@ -116,7 +115,7 @@ describe("#getRootSSHKey", () => { it("returns the SSH key for the root user", async () => { const http = new HTTPClient(new URL("http://localhost")); const client = new UsersClient(http); - mockJsonFn.mockResolvedValue({ sshkey: RootSSHKey }); + mockJsonFn.mockResolvedValue({ sshkey: rootSSHKey }); const result = await client.getRootSSHKey(); expect(result).toEqual("ssh-key"); });