Files
termos/test/core/session_manager.test.ts
T

77 lines
2.2 KiB
TypeScript

import type { UserInfo } from "@core/session_manager";
import { createInitialSessionState, getCurrentSession, login } from "@core/session_manager";
import { createInitialShellState } from "@core/shell_state";
import { buildTestFS } from "@tests/helpers/filesystems_fixture";
import { depth7Tree } from "@tests/helpers/tree_fixture";
import { describe, expect, test } from "vitest";
const root: UserInfo = {
username: "root",
uid: 0,
gid: 0,
group: [0],
home: 0,
};
const testU: UserInfo = {
username: "test",
uid: 1,
gid: 1,
group: [1],
home: 1,
};
const fs = buildTestFS(depth7Tree);
const users = new Map();
users.set(0, root);
users.set(1, testU);
const state = createInitialShellState(fs, users, root);
const passMap = new Map();
passMap.set(0, "123");
passMap.set(1, "321");
test("createInitialSessionState - successfully creates a new session state", () => {
const state = createInitialSessionState(root);
expect(state.cwd).toEqual(root.home);
expect(state.pwd).toEqual(root.home);
expect(state.exitCode).toEqual(0);
expect(state.user).toBe(root);
expect(Object.isFrozen(state)).toBe(true);
});
describe("login", () => {
test("it successfully starts a new session under the user test", () => {
const newState = login(state, "test", "321", passMap);
const pop = getCurrentSession(newState);
expect(pop.user).toEqual(testU);
});
test("fails to start a new session. Cause: no password mapped to the uid", () => {
const nopass: UserInfo = {
username: "no pass",
uid: 2,
gid: 1,
group: [1],
home: 1,
};
users.set(2, nopass);
//TODO: Fix to expect the correct object;
expect(() => login(state, "no pass", "321", passMap)).toThrow();
});
test("fails to start a new session. Cause: Incorrect password", () => {
//TODO: Fix to expect the correct object;
expect(() => login(state, "no pass", "333", passMap)).toThrow();
});
test("fails to start a new session. Cause: Specified user does not exist", () => {
//TODO: Fix to expect the correct object;
expect(() => login(state, "no user", "321", passMap)).toThrow();
});
});