ToolJet/server/test/controllers/users.e2e-spec.ts
Midhun G S fadf025365
[BUG] Account level security issue (#3150)
* bug fixes

* changes

* changes for single workspace support

* added guards for signup route

* test cases fixes

* Workspace invite and user onboarding flow changes (#3190)

* invite user flow changes

* review comments

* cleanup

* testcase fix
2022-06-02 15:20:51 +05:30

101 lines
3.4 KiB
TypeScript

import * as request from 'supertest';
import { INestApplication } from '@nestjs/common';
import { authHeaderForUser, clearDB, createUser, createNestAppInstance } from '../test.helper';
import { getManager } from 'typeorm';
import { User } from 'src/entities/user.entity';
const path = require('path');
describe('users controller', () => {
let app: INestApplication;
beforeEach(async () => {
await clearDB();
});
beforeAll(async () => {
app = await createNestAppInstance();
});
afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});
describe('PATCH /api/users/change_password', () => {
it('should allow users to update their password', async () => {
const userData = await createUser(app, { email: 'admin@tooljet.io' });
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);
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
expect(updatedUser.password).not.toEqual(oldPassword);
});
it('should not allow users to update their password if entered current password is wrong', async () => {
const userData = await createUser(app, { email: 'admin@tooljet.io' });
const { user } = userData;
const oldPassword = user.password;
const response = await request(app.getHttpServer())
.patch('/api/users/change_password')
.set('Authorization', authHeaderForUser(user))
.send({
currentPassword: 'wrong password',
newPassword: 'new password',
});
expect(response.statusCode).toBe(403);
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
expect(updatedUser.password).toEqual(oldPassword);
});
});
describe('PATCH /api/users/update', () => {
it('should allow users to update their firstName, lastName and password', async () => {
const userData = await createUser(app, { email: 'admin@tooljet.io' });
const { user } = userData;
const [firstName, lastName] = ['Daenerys', 'Targaryen'];
const response = await request(app.getHttpServer())
.patch('/api/users/update')
.set('Authorization', authHeaderForUser(user))
.send({ first_name: firstName, last_name: lastName });
expect(response.statusCode).toBe(200);
const updatedUser = await getManager().findOneOrFail(User, { where: { email: user.email } });
expect(updatedUser.firstName).toEqual(firstName);
expect(updatedUser.lastName).toEqual(lastName);
});
});
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');
const response = await request(app.getHttpServer())
.post('/api/users/avatar')
.set('Authorization', authHeaderForUser(user))
.attach('file', filePath);
expect(response.statusCode).toBe(201);
});
});
afterAll(async () => {
await app.close();
});
});