Skip to content

Commit

Permalink
user e2e ing
Browse files Browse the repository at this point in the history
  • Loading branch information
ttkmw committed Jun 14, 2019
1 parent 00c2946 commit a866ea9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 8 deletions.
12 changes: 12 additions & 0 deletions server/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.3'

services:
db:
image: postgres
restart: always
environment:
POSTGRES_USER: test
POSTGRES_DB: hatchout
POSTGRES_PASSWORD: test
ports:
- 5432:5432
2 changes: 1 addition & 1 deletion server/src/app/user/user.service.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {UserServiceImpl} from './user.service.impl';
import {anything, instance, mock, when} from 'ts-mockito';
import {TestingModule, Test} from '@nestjs/testing';
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../domain/user/dto/user.dto';
import {UserRepository} from '../../port/persistence/repository/user.repository.impl';
import {BadRequestException} from '@nestjs/common';
import {UserDto} from './dto/user.dto';

describe('UserServiceImpl', () => {
const address = 'testAddress';
Expand Down
2 changes: 1 addition & 1 deletion server/src/app/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../domain/user/dto/user.dto';
import {DeleteResult} from 'typeorm';
import {UserDto} from './dto/user.dto';

export interface UserService {
get(id: number): Promise<User>;
Expand Down
6 changes: 3 additions & 3 deletions server/src/config/database/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ export default {
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'de-labtory',
password: 'de-labtory',
username: 'test',
password: 'test',
database: 'hatchout',
logging: true,
logging: false,
synchronize: true,
entities: ['src/domain/**/*.entity{.ts,.js}'],
};
6 changes: 3 additions & 3 deletions server/src/web/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Get,
Inject,
Injectable,
Param,
Param, ParseIntPipe,
Post, Put,
Query,
} from '@nestjs/common';
Expand All @@ -32,8 +32,8 @@ export class UserController {
return await this.service.delete(id);
}
@Put(':id/increase-level')
async increaseLevel(@Param('id') id: number, @Query() query): Promise<User> {
return await this.service.increaseLevel(id, query.amount);
async increaseLevel(@Param('id') id: number, @Query('amount', new ParseIntPipe()) amount): Promise<User> {
return await this.service.increaseLevel(id, amount);
}
@Put(':id/increase-point')
async increasePoint(@Param('id') id: number, @Query() query): Promise<User> {
Expand Down
131 changes: 131 additions & 0 deletions server/test/user/user.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {INestApplication} from '@nestjs/common';
import {Test} from '@nestjs/testing';
import request from 'supertest';
import {UserModule} from '../../src/port/module/user.module';
import {User} from '../../src/domain/user/user.entity';
import {DatabaseModule} from '../../src/port/module/database.module';
import {ConfigModule, ConfigService} from 'nestjs-config';
import * as path from 'path';

describe('User', () => {
let app: INestApplication;

beforeAll(async () => {
ConfigService.rootPath = path.resolve(__dirname, '../../src');
const module = await Test.createTestingModule({
imports: [
ConfigModule.resolveRootPath(__dirname).load('config/**/!(*.d).{ts,js}'),
UserModule,
DatabaseModule,
],
})
.compile();

app = module.createNestApplication();
await app.init();

});

afterAll(async () => await app.close());

describe('#create()', () => {
const url = '/users';
let response;
it('should return 201', async () => {
response = { address: 'testAddress', name: 'testName', point: 0, level: 0, id: 1};

return request(app.getHttpServer())
.post(url)
.send({
address: 'testAddress',
name: 'testName',
})
.expect(201)
.expect(response);

});
it('should return 500 without address', async () => {

return request(app.getHttpServer())
.post(url)
.send({
name: 'testName',
})
.expect(500)
.expect({
statusCode: 500,
message: 'Internal server error',
});

});
it('should return 500 without name', async () => {

return request(app.getHttpServer())
.post(url)
.send({
address: 'testAddress',
})
.expect(500)
.expect({ statusCode: 500, message: 'Internal server error' });

});
});

describe('#get()', () => {
let url: string;
let response;

it('should return 200', async () => {
url = '/users/1';
response = { address: 'testAddress', name: 'testName', point: 0, level: 0, id: 1 };

return request(app.getHttpServer())
.get(url)
.expect(200)
.expect(response);
});
it('should return 400', async () => {
url = '/users/2';
response = { statusCode: 400, error: 'Bad Request', message: 'no user with the id' };

return request(app.getHttpServer())
.get(url)
.expect(400)
.expect(response);
});
});
describe('#increaseLevel', () => {
let url: string;
let response;

it('should return 200', async () => {
url = '/users/1/increase-level?amount=1';
response = { address: 'testAddress', name: 'testName', point: 0, level: 1, id: 1 };

return request(app.getHttpServer())
.put(url)
.expect(200)
.expect(response);
});
});

describe('#delete()', () => {
let url: string;
it('should return 200 and affected 1', async () => {
url = '/users/1';

return request(app.getHttpServer())
.delete(url)
.expect(200)
.expect({ raw: [], affected: 1 });
});
it('should return 200 but affected 0', async () => {
url = '/users/2';

return request(app.getHttpServer())
.delete(url)
.expect(200)
.expect({ raw: [], affected: 0 });
});
});
});

0 comments on commit a866ea9

Please sign in to comment.