diff --git a/backend/src/controllers/user.controller.ts b/backend/src/controllers/user.controller.ts index 1647acc..fc44faf 100644 --- a/backend/src/controllers/user.controller.ts +++ b/backend/src/controllers/user.controller.ts @@ -29,7 +29,7 @@ export const changePassword = async (req: Request, res: Response) => { try { const userService = container.get(UserService); await userService.changePassword(user.username, newPassword); - return res.status(200).json({ message: 'Password changed successfully.' }); + return res.status(200).json({ message: 'Password changed successfully' }); } catch (error) { handleError(res, error, 'changing password'); } diff --git a/backend/tests/helpers/request-helpers.ts b/backend/tests/helpers/request-helpers.ts index a6dd74e..813e483 100644 --- a/backend/tests/helpers/request-helpers.ts +++ b/backend/tests/helpers/request-helpers.ts @@ -145,6 +145,24 @@ export const loginUser = async (): Promise => { return accessTokenCookie; }; +export const getProfile = async (cookie: string) => { + checkAppIsRunning(); + + return await request(app) + .get(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/users/profile`) + .set('Cookie', cookie) + .send(); +}; + +export const changePassword = async (newPassword: string, cookie: string) => { + checkAppIsRunning(); + + return await request(app) + .post(`${INTERNAL_CONFIG.INTERNAL_API_BASE_PATH_V1}/users/change-password`) + .set('Cookie', cookie) + .send({ newPassword }); +}; + export const createRoom = async (options: MeetRoomOptions = {}): Promise => { checkAppIsRunning(); diff --git a/backend/tests/integration/api/users/change-password.test.ts b/backend/tests/integration/api/users/change-password.test.ts new file mode 100644 index 0000000..e3977a2 --- /dev/null +++ b/backend/tests/integration/api/users/change-password.test.ts @@ -0,0 +1,30 @@ +import { afterEach, beforeAll, describe, expect, it } from '@jest/globals'; +import { expectValidationError } from '../../../helpers/assertion-helpers.js'; +import { changePassword, loginUser, startTestServer } from '../../../helpers/request-helpers.js'; + +describe('Users API Tests', () => { + let adminCookie: string; + + beforeAll(async () => { + startTestServer(); + adminCookie = await loginUser(); + }); + + afterEach(async () => { + // Reset password + await changePassword('admin', adminCookie); + }); + + describe('Change Password Tests', () => { + it('should successfully change password', async () => { + const response = await changePassword('newpassword123', adminCookie); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('message', 'Password changed successfully'); + }); + }); + + it('should fail when new password is not 4 characters long', async () => { + const response = await changePassword('123', adminCookie); + expectValidationError(response, 'newPassword', 'New password must be at least 4 characters long'); + }); +}); diff --git a/backend/tests/integration/api/users/get-profile.test.ts b/backend/tests/integration/api/users/get-profile.test.ts new file mode 100644 index 0000000..e954569 --- /dev/null +++ b/backend/tests/integration/api/users/get-profile.test.ts @@ -0,0 +1,21 @@ +import { beforeAll, describe, expect, it } from '@jest/globals'; +import { getProfile, loginUser } from '../../../helpers/request-helpers.js'; + +describe('Users API Tests', () => { + let adminCookie: string; + + beforeAll(async () => { + adminCookie = await loginUser(); + }); + + describe('Profile Tests', () => { + it('should return 200 and admin profile', async () => { + const response = await getProfile(adminCookie); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('username'); + expect(response.body.username).toBe('admin'); + expect(response.body).toHaveProperty('roles'); + expect(response.body.roles).toEqual(expect.arrayContaining(['admin', 'user'])); + }); + }); +});