2021-08-12 15:33:52 +00:00
|
|
|
import * as request from 'supertest';
|
|
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
|
import { authHeaderForUser, clearDB, createUser, createNestAppInstance } from '../test.helper';
|
2021-12-14 11:02:03 +00:00
|
|
|
import { getManager } from 'typeorm';
|
|
|
|
|
import { User } from 'src/entities/user.entity';
|
2021-08-12 15:33:52 +00:00
|
|
|
|
|
|
|
|
describe('users controller', () => {
|
|
|
|
|
let app: INestApplication;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
await clearDB();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
app = await createNestAppInstance();
|
2021-12-14 11:02:03 +00:00
|
|
|
});
|
|
|
|
|
|
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-04-20 09:16:57 +00:00
|
|
|
const userData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
role: 'admin',
|
|
|
|
|
});
|
2021-12-14 11:02:03 +00:00
|
|
|
const { user } = userData;
|
|
|
|
|
|
|
|
|
|
const oldPassword = user.password;
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.patch('/api/users/change_password')
|
|
|
|
|
.set('Authorization', authHeaderForUser(user))
|
|
|
|
|
.send({ currentPassword: 'password', newPassword: 'new password' });
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
|
|
|
|
2022-04-20 09:16:57 +00:00
|
|
|
const updatedUser = await getManager().findOne(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-04-20 09:16:57 +00:00
|
|
|
const userData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
role: 'admin',
|
|
|
|
|
});
|
2021-12-14 11:02:03 +00:00
|
|
|
const { user } = userData;
|
|
|
|
|
|
|
|
|
|
const oldPassword = user.password;
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
|
.patch('/api/users/change_password')
|
|
|
|
|
.set('Authorization', authHeaderForUser(user))
|
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-04-20 09:16:57 +00:00
|
|
|
const updatedUser = await getManager().findOne(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-04-20 09:16:57 +00:00
|
|
|
const userData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
role: 'admin',
|
|
|
|
|
});
|
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
|
|
|
|
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')
|
2021-08-12 15:33:52 +00:00
|
|
|
.set('Authorization', authHeaderForUser(user))
|
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-04-20 09:16:57 +00:00
|
|
|
const updatedUser = await getManager().findOne(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-04-20 09:16:57 +00:00
|
|
|
describe('POST /api/users/set_password_from_token', () => {
|
|
|
|
|
it('should allow users to set password from token', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
role: 'admin',
|
|
|
|
|
});
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
const anotherUserData = await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
groups: ['all_users'],
|
|
|
|
|
invitationToken: 'token',
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer()).post('/api/users/set_password_from_token').send({
|
|
|
|
|
first_name: 'Khal',
|
|
|
|
|
last_name: 'Drogo',
|
|
|
|
|
token: 'token',
|
|
|
|
|
organization: 'Dothraki Pvt Limited',
|
|
|
|
|
password: 'Khaleesi',
|
|
|
|
|
new_signup: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(201);
|
|
|
|
|
|
|
|
|
|
const updatedUser = await getManager().findOne(User, {
|
|
|
|
|
where: { email: anotherUserData.user.email },
|
|
|
|
|
});
|
|
|
|
|
expect(updatedUser.firstName).toEqual('Khal');
|
|
|
|
|
expect(updatedUser.lastName).toEqual('Drogo');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return error if required params are not present', async () => {
|
|
|
|
|
const adminUserData = await createUser(app, {
|
|
|
|
|
email: 'admin@tooljet.io',
|
|
|
|
|
role: 'admin',
|
|
|
|
|
});
|
|
|
|
|
const organization = adminUserData.organization;
|
|
|
|
|
await createUser(app, {
|
|
|
|
|
email: 'developer@tooljet.io',
|
|
|
|
|
groups: ['all_users'],
|
|
|
|
|
invitationToken: 'token',
|
|
|
|
|
organization,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await request(app.getHttpServer()).post('/api/users/set_password_from_token');
|
|
|
|
|
|
|
|
|
|
expect(response.statusCode).toBe(400);
|
|
|
|
|
expect(response.body.message).toStrictEqual([
|
|
|
|
|
'password should not be empty',
|
|
|
|
|
'password must be a string',
|
|
|
|
|
'token should not be empty',
|
|
|
|
|
'token must be a string',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-12 15:33:52 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
|
await app.close();
|
|
|
|
|
});
|
|
|
|
|
});
|