11import { beforeEach , describe , expect , it } from 'vitest'
2- import { getUser } from './user-service'
2+ import { changeUserPassword , deleteUser , getUser } from './user-service'
33import { createUser } from './user-repository'
44import { runMigration } from '../db/database-migration-util'
55import { db } from '../db/database'
@@ -11,16 +11,18 @@ beforeEach(async () => {
1111} )
1212
1313describe ( 'User Service Integration' , ( ) => {
14+ const newUserPlainPassword = 'securepassword'
15+
16+ const newUser = {
17+ email : 'integration@test.com' ,
18+ first_name : 'Integration' ,
19+ last_name : 'Test' ,
20+ password_hash : hash ( newUserPlainPassword ) ,
21+ role : 'user'
22+ }
23+
1424 describe ( 'getUser' , ( ) => {
1525 it ( 'should return a user when found' , async ( ) => {
16- const newUser = {
17- email : 'integration@test.com' ,
18- first_name : 'Integration' ,
19- last_name : 'Test' ,
20- password_hash : hash ( 'securepassword' ) ,
21- role : 'user'
22- }
23-
2426 const createdUser = await createUser ( newUser )
2527
2628 const result = await getUser ( createdUser . id )
@@ -34,24 +36,60 @@ describe('User Service Integration', () => {
3436 } )
3537 } )
3638
37- it ( 'should throw an error when user is not found' , async ( ) => {
38- await expect ( getUser ( 999 ) ) . rejects . toThrow ( 'User not found' )
39+ it ( 'should return undefined when user is not found' , async ( ) => {
40+ await expect ( getUser ( 999 ) ) . resolves . toEqual ( undefined )
3941 } )
4042
4143 it ( 'should not return the password hash' , async ( ) => {
42- const newUser = {
43- email : 'integration@test.com' ,
44- first_name : 'Integration' ,
45- last_name : 'Test' ,
46- password_hash : hash ( 'securepassword' ) ,
47- role : 'user'
48- }
49-
5044 const createdUser = await createUser ( newUser )
5145
5246 const result = await getUser ( createdUser . id )
5347
5448 expect ( result ) . not . toHaveProperty ( 'password_hash' )
5549 } )
5650 } )
51+
52+ describe ( 'deleteUser' , ( ) => {
53+ it ( 'should correctly delete an existing user' , async ( ) => {
54+ const createdUser = await createUser ( newUser )
55+
56+ await expect ( deleteUser ( createdUser . id ) ) . resolves . not . toThrowError ( )
57+ } )
58+
59+ it ( 'should return false when trying to delete a non-existing-user' , async ( ) => {
60+ await expect ( deleteUser ( 5123 ) ) . rejects . toThrowError ( )
61+ } )
62+ } )
63+
64+ describe ( 'changeUserPassword' , ( ) => {
65+ it ( 'should correctly update a users password on valid input' , async ( ) => {
66+ const createdUser = await createUser ( newUser )
67+
68+ const newPassword = 'new-secure-password-123'
69+ const confirmPassword = newPassword
70+
71+ await expect (
72+ changeUserPassword ( createdUser . id , {
73+ currentPassword : newUserPlainPassword ,
74+ newPassword,
75+ confirmPassword
76+ } )
77+ ) . resolves . not . toThrowError ( )
78+ } )
79+
80+ it ( 'should throw an error when current password hash does not match' , async ( ) => {
81+ const createdUser = await createUser ( newUser )
82+
83+ const newPassword = 'new-secure-password-123'
84+ const confirmPassword = newPassword
85+
86+ await expect (
87+ changeUserPassword ( createdUser . id , {
88+ currentPassword : 'wrong-current-password' ,
89+ newPassword,
90+ confirmPassword
91+ } )
92+ ) . rejects . toThrowError ( 'Invalid password' )
93+ } )
94+ } )
5795} )
0 commit comments