86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
|
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
|
||
|
import { MockCrypto } from "./crypto.ts";
|
||
|
import { MockDb } from "./mockdb.ts";
|
||
|
import { LoginReq, Sessions } from "./sessions.ts";
|
||
|
import { Users } from "./users.ts";
|
||
|
|
||
|
const loginReq: LoginReq = {
|
||
|
username: "testuser",
|
||
|
password: "1234",
|
||
|
};
|
||
|
|
||
|
Deno.test("login: unknown user", async () => {
|
||
|
const db = new MockDb();
|
||
|
const sessions = new Sessions(db, new MockCrypto());
|
||
|
const result = await sessions.login(loginReq);
|
||
|
assertEquals(result.ok, false);
|
||
|
if (result.ok) return;
|
||
|
assertEquals(result.error, "wrong username/password");
|
||
|
});
|
||
|
|
||
|
Deno.test("login: wrong username", async () => {
|
||
|
const db = new MockDb();
|
||
|
const users = new Users(db, new MockCrypto());
|
||
|
const sessions = new Sessions(db, new MockCrypto());
|
||
|
{
|
||
|
const result = await users.createUser({
|
||
|
username: "testuser",
|
||
|
password: "1234",
|
||
|
});
|
||
|
assertEquals(result.ok, true);
|
||
|
}
|
||
|
{
|
||
|
const result = await sessions.login({
|
||
|
...loginReq,
|
||
|
username: "wrong",
|
||
|
});
|
||
|
assertEquals(result.ok, false);
|
||
|
if (result.ok) return;
|
||
|
assertEquals(result.error, "wrong username/password");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Deno.test("login: wrong password", async () => {
|
||
|
const db = new MockDb();
|
||
|
const users = new Users(db, new MockCrypto());
|
||
|
const sessions = new Sessions(db, new MockCrypto());
|
||
|
{
|
||
|
const result = await users.createUser({
|
||
|
username: "testuser",
|
||
|
password: "1234",
|
||
|
});
|
||
|
assertEquals(result.ok, true);
|
||
|
}
|
||
|
{
|
||
|
const result = await sessions.login({
|
||
|
...loginReq,
|
||
|
password: "wrong",
|
||
|
});
|
||
|
assertEquals(result.ok, false);
|
||
|
if (result.ok) return;
|
||
|
assertEquals(result.error, "wrong username/password");
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Deno.test("login: add session to database", async () => {
|
||
|
const db = new MockDb();
|
||
|
const users = new Users(db, new MockCrypto());
|
||
|
const sessions = new Sessions(db, new MockCrypto());
|
||
|
{
|
||
|
const result = await users.createUser({
|
||
|
username: "testuser",
|
||
|
password: "1234",
|
||
|
});
|
||
|
assertEquals(result.ok, true);
|
||
|
}
|
||
|
{
|
||
|
const result = await sessions.login(loginReq);
|
||
|
assertEquals(result.ok, true);
|
||
|
if (!result.ok) return;
|
||
|
const sessionResult = await db.sessionWithId(result.value.sessionId);
|
||
|
assertEquals(sessionResult.ok, true);
|
||
|
if (!sessionResult.ok) return;
|
||
|
assertEquals(sessionResult.value.some, true);
|
||
|
}
|
||
|
});
|