summary refs log tree commit diff
path: root/src/db/dbAccess/user.test.js
blob: 7b72d2972c0ee6b9d55df78a493829b1c8b39136 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as dotenv from 'dotenv';
import { it } from 'node:test';
import { deleteUser, registerUser } from '#db/index.js';
import * as assert from 'node:assert';
import { initDb } from '#db/db.js';
import { disconnect } from 'mongoose';

dotenv.config();

await initDb();

async function createTestUser() {
    const username = (Math.random() * 1000000).toString();
    const password = (Math.random() * 1000000).toString();
    const email = (Math.random() * 1000000).toString() + '@example.com';
    return {
        username,
        password,
        email,
        user: await registerUser(username, password, email)
    };
}

await it('Can create user', async () => {
    await assert.doesNotReject(createTestUser());
});

await it('Can delete user', async () => {
    const { password, user } = await createTestUser();

    const deletePromise = deleteUser(user._id, password);
    await assert.doesNotReject(deletePromise);
});

await it('Cant delete nonexistant user', async () => {
    await assert.rejects(deleteUser('abc', ''));
});

await it('Cant delete user with invalid password', async () => {
    const user = await createTestUser();
    await assert.rejects(deleteUser(user.user._id, 'wrongpassword'));
});

await disconnect();