AstroCol/test/database.test.ts

32 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-06-01 13:58:57 +00:00
import { createUser, deleteUser, getUserByNickOrEmail } from '../src/lib/db/users';
import { connect, disconnect } from '../src/lib/db/mongodb';
import { verify } from 'argon2';
beforeEach(async () => {
await connect();
});
afterEach(async () => {
await disconnect();
});
describe('Test user database functions', () => {
test('getUserByNickOrEmail', async () => {
const user = await getUserByNickOrEmail("gargamel");
expect(user).not.toBeNull();
expect(user?.username).toBe("gargamel");
expect(user?.email).toBe("gargamel@smerfy.pl");
});
test('createAndVerifyUser', async () => {
await createUser("test", "test@example.com", "password");
const user = await getUserByNickOrEmail("test");
expect(user).not.toBeNull();
if(!user) return;
expect(user?.username).toBe("test");
expect(user?.email).toBe("test@example.com");
expect(await verify(user?.password as string, "password")).toBe(true);
await deleteUser(user._id);
});
});