Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix users JavaScript tests #1144

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 105 additions & 74 deletions web/src/client/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,71 +21,90 @@

// @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 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);
});
Expand All @@ -94,59 +113,68 @@ 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");
});
});

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);
});
Expand All @@ -155,17 +183,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);
});
Expand All @@ -174,37 +203,39 @@ 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);
});
});
});

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);
});
});
Expand Down
Loading