summary refs log tree commit diff
path: root/src/db/dbAccess/user.test.js
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-06-01 01:03:40 +0200
committerRory& <root@rory.gay>2025-06-01 01:03:40 +0200
commit99220d73469210f94493ef92a9edc64ab50eb0d9 (patch)
tree765781a300db5c1e1a15afaa8bd09f48712a5313 /src/db/dbAccess/user.test.js
parentInit test frontend (diff)
downloadnodejs-final-assignment-99220d73469210f94493ef92a9edc64ab50eb0d9.tar.xz
Add some tests
Diffstat (limited to 'src/db/dbAccess/user.test.js')
-rw-r--r--src/db/dbAccess/user.test.js44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/db/dbAccess/user.test.js b/src/db/dbAccess/user.test.js
new file mode 100644

index 0000000..e8e1af5 --- /dev/null +++ b/src/db/dbAccess/user.test.js
@@ -0,0 +1,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 { username, password, email, 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();