add e2e
This commit is contained in:
8
test/client/dvbooking.api-context.ts
Normal file
8
test/client/dvbooking.api-context.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { User } from '../../src/entity/user';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
|
||||
export interface DvbookingApiContext {
|
||||
token: string;
|
||||
user: User;
|
||||
app: INestApplication<any>;
|
||||
}
|
||||
78
test/client/dvbooking.api.ts
Normal file
78
test/client/dvbooking.api.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { DvbookingApiContext } from './dvbooking.api-context';
|
||||
import { CreateUserDto } from '../../src/user/dto/create-user.dto';
|
||||
import { TestingModule } from '@nestjs/testing';
|
||||
import { UserService } from '../../src/user/user.service';
|
||||
import { UserGroupService } from '../../src/user/user-group.service';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import request from 'supertest';
|
||||
|
||||
export class DvbookingApi {
|
||||
public context: DvbookingApiContext;
|
||||
|
||||
systemUser: { username: string; password: string; token?: string } = {
|
||||
username: 'admin',
|
||||
password: '123456',
|
||||
token: undefined,
|
||||
};
|
||||
|
||||
private userService: UserService;
|
||||
private userGroupService: UserGroupService;
|
||||
constructor(
|
||||
private app: INestApplication<any>,
|
||||
private moduleFixture: TestingModule,
|
||||
) {
|
||||
this.userService = moduleFixture.get<UserService>(UserService);
|
||||
this.userGroupService =
|
||||
moduleFixture.get<UserGroupService>(UserGroupService);
|
||||
}
|
||||
|
||||
public async init() {
|
||||
const response = await request(this.app.getHttpServer())
|
||||
.post('/auth/login')
|
||||
.send({
|
||||
username: this.systemUser.username,
|
||||
password: this.systemUser.password,
|
||||
});
|
||||
|
||||
this.systemUser.token = (
|
||||
response.body as { access_token: string }
|
||||
).access_token;
|
||||
}
|
||||
|
||||
public async destroy() {
|
||||
// do cleanup
|
||||
}
|
||||
|
||||
public async loginWithGroup(
|
||||
username?: string,
|
||||
groupName?: string,
|
||||
): Promise<void> {
|
||||
const group = await this.userGroupService.findByName(groupName);
|
||||
if (!username) {
|
||||
username = 'e2e-user-' + Math.floor(100 * Math.random());
|
||||
}
|
||||
const password = 'password';
|
||||
const user = await this.createUser({
|
||||
username: username,
|
||||
email: 'user@dvbooking.hu',
|
||||
password: 'password',
|
||||
groups: [{ id: group!.id }],
|
||||
});
|
||||
|
||||
const response = await request(this.app.getHttpServer())
|
||||
.post('/auth/login')
|
||||
.send({ username, password });
|
||||
|
||||
const token = (response.body as { access_token: string }).access_token;
|
||||
|
||||
this.context = {
|
||||
user,
|
||||
token,
|
||||
app: this.app,
|
||||
};
|
||||
}
|
||||
|
||||
public async createUser(user: CreateUserDto) {
|
||||
return await this.userService.create(user);
|
||||
}
|
||||
}
|
||||
17
test/client/dvbooking.http-client.ts
Normal file
17
test/client/dvbooking.http-client.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import request from 'supertest';
|
||||
import { DvbookingApiContext } from './dvbooking.api-context';
|
||||
|
||||
export class DvBookingHttpClient {
|
||||
constructor(private context: DvbookingApiContext) {}
|
||||
|
||||
private createRequest() {
|
||||
return request(this.context.app.getHttpServer());
|
||||
}
|
||||
public async httpPost(path: string, data?: string | object) {
|
||||
return await this.createRequest().post(path).send(data);
|
||||
}
|
||||
|
||||
public async httpGet(path: string) {
|
||||
return await this.createRequest().get(path).send();
|
||||
}
|
||||
}
|
||||
21
test/client/user.api-client.ts
Normal file
21
test/client/user.api-client.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { DvbookingApiContext } from './dvbooking.api-context';
|
||||
import { DvBookingHttpClient } from './dvbooking.http-client';
|
||||
import { User } from '../../src/entity/user';
|
||||
|
||||
export class UserApiClient {
|
||||
private http: DvBookingHttpClient;
|
||||
|
||||
constructor(context: DvbookingApiContext) {
|
||||
this.http = new DvBookingHttpClient(context);
|
||||
}
|
||||
|
||||
public async find() {
|
||||
const response = await this.http.httpGet('/users');
|
||||
return response.body as User[];
|
||||
}
|
||||
|
||||
public async findById(id: number) {
|
||||
const response = await this.http.httpGet('/users/' + id);
|
||||
return response.body as User[];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user