test: add integration tests for change password and get profile functionalities

This commit is contained in:
juancarmore 2025-06-09 22:35:44 +02:00
parent 287148e8fc
commit 129be42152
4 changed files with 70 additions and 1 deletions

View File

@ -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');
}

View File

@ -145,6 +145,24 @@ export const loginUser = async (): Promise<string> => {
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<MeetRoom> => {
checkAppIsRunning();

View File

@ -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');
});
});

View File

@ -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']));
});
});
});