import { assertEquals } from "https://deno.land/std@0.224.0/assert/mod.ts"; import { CreateUserReq, Users } from "./users.ts"; import { MockDb } from "./mockdb.ts"; import { assertNotEquals } from "https://deno.land/std@0.224.0/assert/assert_not_equals.ts"; import { MockCrypto } from "./crypto.ts"; const createUserReq: CreateUserReq = { username: "testuser", password: "1234", }; Deno.test("createUser: no username", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); const result = await users.createUser({ ...createUserReq, username: "", }); assertEquals(result.ok, false); if (result.ok) return; assertEquals(result.error, "username empty"); }); Deno.test("createUser: no password", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); const result = await users.createUser({ ...createUserReq, password: "", }); assertEquals(result.ok, false); if (result.ok) return; assertEquals(result.error, "password empty"); }); Deno.test("createUser: add to database", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); { const result = await db.userWithUsername(createUserReq.username); assertEquals(result.ok, true); if (!result.ok) return; assertEquals(result.value.some, false); } const createResult = await users.createUser(createUserReq); assertEquals(createResult.ok, true); { const result = await db.userWithUsername(createUserReq.username); assertEquals(result.ok, true); if (!result.ok) return; assertEquals(result.value.some, true); } }); Deno.test("createUser: taken username", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); { const result = await users.createUser(createUserReq); assertEquals(result.ok, true); } { const result = await users.createUser(createUserReq); assertEquals(result.ok, false); if (result.ok) return; assertEquals(result.error, "username taken"); } }); Deno.test("createUser: add username to database", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); const createResult = await users.createUser(createUserReq); assertEquals(createResult.ok, true); const result = await db.userWithUsername(createUserReq.username); assertEquals(result.ok, true); if (!result.ok) return; assertEquals(result.value.some, true); if (!result.value.some) return; assertEquals(result.value.value.username, createUserReq.username); }); Deno.test("createUser: add password hash to database", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); const createResult = await users.createUser(createUserReq); assertEquals(createResult.ok, true); const result = await db.userWithUsername(createUserReq.username); assertEquals(result.ok, true); if (!result.ok) return; assertEquals(result.value.some, true); if (!result.value.some) return; assertNotEquals(result.value.value.passwordHash, ""); assertNotEquals(result.value.value.passwordHash, createUserReq.password); }); Deno.test("createUser: database error -> internal error", async () => { const db = new MockDb(); db.inErronousState = true; const users = new Users(db, new MockCrypto()); const result = await users.createUser(createUserReq); assertEquals(result.ok, false); if (result.ok) return; assertEquals(result.error, "internal"); }); Deno.test("userInfo: not found", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); const result = await users.userInfo(0); assertEquals(result.ok, false); if (result.ok) return; assertEquals(result.error, "not found"); }); Deno.test("userInfo: find created", async () => { const db = new MockDb(); const users = new Users(db, new MockCrypto()); { const result = await users.createUser(createUserReq); assertEquals(result.ok, true); } { const result = await users.userInfo(0); assertEquals(result.ok, true); if (!result.ok) return; assertEquals(result.value, { id: 0, username: createUserReq.username, }); } });