134 lines
4.0 KiB
TypeScript
134 lines
4.0 KiB
TypeScript
import { Test, TestingModule } from '@nestjs/testing';
|
|
import { INestApplication, ValidationPipe } from '@nestjs/common';
|
|
import request from 'supertest';
|
|
import { AppModule } from '../src/app.module';
|
|
import { CreateUserDto } from '../src/user/dto/create-user.dto';
|
|
import { Role } from '../src/auth/role.enum';
|
|
import { UserService } from '../src/user/user.service';
|
|
import { User } from '../src/entity/user';
|
|
import { UpdateUserDto } from '../src/user/dto/update-user.dto';
|
|
|
|
describe('UserController (e2e)', () => {
|
|
process.env.DATA_SOURCE_ENV = '.env.e2e';
|
|
let app: INestApplication;
|
|
let jwtToken: string;
|
|
let adminUserId: number;
|
|
|
|
beforeAll(async () => {
|
|
const moduleFixture: TestingModule = await Test.createTestingModule({
|
|
imports: [AppModule],
|
|
}).compile();
|
|
|
|
// process.env.DATA_SOURCE_ENV=".env.e2e";
|
|
app = moduleFixture.createNestApplication();
|
|
app.useGlobalPipes(new ValidationPipe());
|
|
await app.init();
|
|
|
|
const userService = moduleFixture.get<UserService>(UserService);
|
|
const adminUser = await userService.create({
|
|
username: 'e2e_admin',
|
|
password: 'password',
|
|
email: "admin@dvbooking.hu",
|
|
roles: [Role.Admin],
|
|
});
|
|
adminUserId = adminUser.id;
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.post('/auth/login')
|
|
.send({ username: 'admin', password: '123456' });
|
|
|
|
jwtToken = response.body.access_token;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
const userService = app.get<UserService>(UserService);
|
|
await userService.remove(adminUserId);
|
|
await app.close();
|
|
});
|
|
|
|
describe('/users', () => {
|
|
it('(GET) should get all users', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get('/users')
|
|
.set('Authorization', `Bearer ${jwtToken}`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(Array.isArray(response.body)).toBe(true);
|
|
});
|
|
|
|
it('(POST) should create a user', async () => {
|
|
const createUserDto: CreateUserDto = {
|
|
username: 'e2e_user',
|
|
password: 'password',
|
|
roles: [Role.User],
|
|
};
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.post('/users')
|
|
.set('Authorization', `Bearer ${jwtToken}`)
|
|
.send(createUserDto);
|
|
|
|
expect(response.status).toBe(201);
|
|
expect(response.body.username).toEqual(createUserDto.username);
|
|
|
|
const userService = app.get<UserService>(UserService);
|
|
await userService.remove(response.body.id);
|
|
});
|
|
});
|
|
|
|
describe('/users/:id', () => {
|
|
let user: User;
|
|
let userService: UserService;
|
|
|
|
beforeEach(async () => {
|
|
userService = app.get<UserService>(UserService);
|
|
user = await userService.create({
|
|
username: 'e2e_test_user',
|
|
password: 'password',
|
|
roles: [Role.User],
|
|
});
|
|
});
|
|
|
|
afterEach(async () => {
|
|
const userExists = await userService.findOne(user.id);
|
|
if (userExists) {
|
|
await userService.remove(user.id);
|
|
}
|
|
});
|
|
|
|
it('(GET) should get a user by id', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.get(`/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${jwtToken}`);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.id).toEqual(user.id);
|
|
});
|
|
|
|
it('(PATCH) should update a user', async () => {
|
|
const updateUserDto: UpdateUserDto = {
|
|
username: 'e2e_updated_user',
|
|
};
|
|
|
|
const response = await request(app.getHttpServer())
|
|
.patch(`/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${jwtToken}`)
|
|
.send(updateUserDto);
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body.username).toEqual(updateUserDto.username);
|
|
});
|
|
|
|
it('(DELETE) should delete a user', async () => {
|
|
const response = await request(app.getHttpServer())
|
|
.delete(`/users/${user.id}`)
|
|
.set('Authorization', `Bearer ${jwtToken}`);
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
const deletedUser = await userService.findOne(user.id);
|
|
expect(deletedUser).toBeNull();
|
|
});
|
|
});
|
|
});
|