79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
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);
|
|
}
|
|
}
|