2021-08-12 15:33:52 +00:00
|
|
|
import * as request from 'supertest';
|
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
2023-04-06 11:12:58 +00:00
|
|
|
import { clearDB, createUser, createNestAppInstance, authenticateUser } from '../test.helper';
|
2021-12-14 11:02:03 +00:00
|
|
|
import { getManager } from 'typeorm';
|
|
|
|
|
import { User } from 'src/entities/user.entity';
|
2022-06-02 06:49:49 +00:00
|
|
|
const path = require('path');
|
2021-08-12 15:33:52 +00:00
|
|
|
|
|
|
|
|
describe('users controller', () => {
|
|
|
|
|
let app: INestApplication;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
await clearDB();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-06-02 09:50:51 +00:00
|
|
|
app = await createNestAppInstance();
|
2021-12-14 11:02:03 +00:00
|
|
|
});
|
|
|
|
|
|
2022-05-11 11:00:25 +00:00
|
|
|
afterEach(() => {
|
|
|
|
|
jest.resetAllMocks();
|
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
});
|
|
|
|
|
|
2022-04-20 09:16:57 +00:00
|
|
|
describe('PATCH /api/users/change_password', () => {
|
2021-12-14 11:02:03 +00:00
|
|
|
it('should allow users to update their password', async () => {
|
2022-05-05 07:08:42 +00:00
|
|
|
const userData = await createUser(app, { email: 'admin@tooljet.io' });
|
2021-12-14 11:02:03 +00:00
|
|
|
const { user } = userData;
|
|
|
|
|
|
|
|
|
|
const oldPassword = user.password;
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
userData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-14 11:02:03 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.patch('/api/users/change_password')
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', userData['tokenCookie'])
|
2021-12-14 11:02:03 +00:00
|
|
|
.send({ currentPassword: 'password', newPassword: 'new password' });
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
2022-05-05 07:08:42 +00:00
|
|
|
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
|
2021-12-14 11:02:03 +00:00
|
|
|
expect(updatedUser.password).not.toEqual(oldPassword);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should not allow users to update their password if entered current password is wrong', async () => {
|
2022-05-05 07:08:42 +00:00
|
|
|
const userData = await createUser(app, { email: 'admin@tooljet.io' });
|
2021-12-14 11:02:03 +00:00
|
|
|
const { user } = userData;
|
|
|
|
|
|
|
|
|
|
const oldPassword = user.password;
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
userData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-12-14 11:02:03 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.patch('/api/users/change_password')
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', userData['tokenCookie'])
|
2022-04-20 09:16:57 +00:00
|
|
|
.send({
|
|
|
|
|
currentPassword: 'wrong password',
|
|
|
|
|
newPassword: 'new password',
|
|
|
|
|
});
|
2021-12-14 11:02:03 +00:00
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
|
|
|
|
2022-05-05 07:08:42 +00:00
|
|
|
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
|
2021-12-14 11:02:03 +00:00
|
|
|
expect(updatedUser.password).toEqual(oldPassword);
|
|
|
|
|
});
|
2021-08-12 15:33:52 +00:00
|
|
|
});
|
|
|
|
|
|
2022-04-20 09:16:57 +00:00
|
|
|
describe('PATCH /api/users/update', () => {
|
2021-08-12 15:33:52 +00:00
|
|
|
it('should allow users to update their firstName, lastName and password', async () => {
|
2022-05-05 07:08:42 +00:00
|
|
|
const userData = await createUser(app, { email: 'admin@tooljet.io' });
|
2021-08-12 15:33:52 +00:00
|
|
|
const { user } = userData;
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2022-04-20 09:16:57 +00:00
|
|
|
const [firstName, lastName] = ['Daenerys', 'Targaryen'];
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
userData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
2021-10-15 09:05:11 +00:00
|
|
|
.patch('/api/users/update')
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', userData['tokenCookie'])
|
2022-04-20 09:16:57 +00:00
|
|
|
.send({ first_name: firstName, last_name: lastName });
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
expect(response.statusCode).toBe(200);
|
2021-09-21 13:48:28 +00:00
|
|
|
|
2022-05-05 07:08:42 +00:00
|
|
|
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
|
2021-12-14 11:02:03 +00:00
|
|
|
expect(updatedUser.firstName).toEqual(firstName);
|
|
|
|
|
expect(updatedUser.lastName).toEqual(lastName);
|
2021-08-12 15:33:52 +00:00
|
|
|
});
|
2021-09-21 13:48:28 +00:00
|
|
|
});
|
2021-08-12 15:33:52 +00:00
|
|
|
|
2022-06-02 06:49:49 +00:00
|
|
|
describe('POST /api/users/avatar', () => {
|
|
|
|
|
it('should allow users to add a avatar', async () => {
|
|
|
|
|
const userData = await createUser(app, { email: 'admin@tooljet.io' });
|
|
|
|
|
|
|
|
|
|
const { user } = userData;
|
|
|
|
|
const filePath = path.join(__dirname, '../__mocks__/avatar.png');
|
|
|
|
|
|
2023-04-06 11:12:58 +00:00
|
|
|
const loggedUser = await authenticateUser(app);
|
|
|
|
|
userData['tokenCookie'] = loggedUser.tokenCookie;
|
|
|
|
|
|
2022-06-02 06:49:49 +00:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.post('/api/users/avatar')
|
2023-04-06 11:12:58 +00:00
|
|
|
.set('tj-workspace-id', user.defaultOrganizationId)
|
|
|
|
|
.set('Cookie', userData['tokenCookie'])
|
2022-06-02 06:49:49 +00:00
|
|
|
.attach('file', filePath);
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(201);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
|
|
|
|
});
|