Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

userController&test #56

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions server/src/app/user/dto/signUp.dto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {SignUpDto} from './signUp.dto';

describe('SignUpPayload', () => {
it('should be defined', () => {
expect(new SignUpDto({address: 'address', message: 'message'}, 'signature')).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class SignUpPayload {
export class SignUpDto {

data: {
address: string,
Expand Down
7 changes: 0 additions & 7 deletions server/src/app/user/dto/signUp.payload.spec.ts

This file was deleted.

9 changes: 6 additions & 3 deletions server/src/app/user/user.service.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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 {UserDto} from './dto/user.dto';
import {UserRepository} from '../../port/persistence/repository/user.repository.impl';
import {BadRequestException} from '@nestjs/common';

describe('UserServiceImpl', () => {
const address = 'testAddress';
Expand Down Expand Up @@ -44,11 +45,13 @@ describe('UserServiceImpl', () => {

expect(await service.get(id)).toBe(user);
});
it('should return undefined', async () => {
it('should throw BadRequestException', async () => {
when(mockRepository.findById(null)).thenReturn(undefined);
service = new UserServiceImpl(instance(mockRepository));

expect(await service.get(null)).toBeUndefined();
await expect(service.get(null))
.rejects
.toThrowError(BadRequestException);
});
});
describe('#create()', () => {
Expand Down
8 changes: 6 additions & 2 deletions server/src/app/user/user.service.impl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Inject, Injectable} from '@nestjs/common';
import {BadRequestException, Inject, Injectable} from '@nestjs/common';
import {UserService} from './user.service';
import {UserDto} from '../../domain/user/dto/user.dto';
import {UserDto} from './dto/user.dto';
import {User} from '../../domain/user/user.entity';
import {DeleteResult} from 'typeorm';
import {IUserRepository} from '../../port/persistence/repository/user.repository';
Expand All @@ -11,6 +11,10 @@ export class UserServiceImpl implements UserService {
constructor(@Inject('IUserRepository') private userRepository: IUserRepository) {}

async get(id: number): Promise<User> {
const user = await this.userRepository.findById(id);
if (user === undefined) {
throw new BadRequestException('no user with the id');
}
return await this.userRepository.findById(id);
}
async create(userDto: UserDto): Promise<User> {
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,5 +1,5 @@
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../domain/user/dto/user.dto';
import {UserDto} from './dto/user.dto';
import {DeleteResult} from 'typeorm';

export interface UserService {
Expand Down
4 changes: 1 addition & 3 deletions server/src/domain/user/validation.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {Data} from './data';

export interface ValidationService {
verify(data: Data, signature: string): boolean;
verify(data: {address: string, message: string}, signature: string): boolean;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {BridgeService} from '../../../../../../domain/user/bridge.service';
import {Inject, Injectable} from '@nestjs/common';
import Web3 from 'web3';
import {SignUpPayload} from '../../../../../../app/user/dto/signUp.payload';

@Injectable()
export class Web3BridgeService implements BridgeService {
Expand Down
4 changes: 3 additions & 1 deletion server/src/port/module/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Module } from '@nestjs/common';
import {UserServiceImpl} from '../../app/user/user.service.impl';
import {UserRepository} from '../persistence/repository/user.repository.impl';
import {UserController} from '../../web/user/user.controller';

@Module({
controllers: [UserController],
providers: [
UserServiceImpl,
{provide: 'UserService', useClass: UserServiceImpl},
{provide: 'IUserRepository', useClass: UserRepository},
],
})
Expand Down
150 changes: 150 additions & 0 deletions server/src/web/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { Test, TestingModule } from '@nestjs/testing';
import { UserController } from './user.controller';
import {instance, mock, when} from 'ts-mockito';
import {UserService} from '../../app/user/user.service';
import {UserServiceImpl} from '../../app/user/user.service.impl';
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../app/user/dto/user.dto';
import {BadRequestException} from '@nestjs/common';

describe('User Controller', () => {
const mockUserService = mock(UserServiceImpl);
const address = 'testAddress';
const name = 'name';
let user: User;
let controller: UserController;

beforeEach(() => {
user = new User(address, name);
});

describe('dependency resolve', () => {
it('should be defined', async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [
UserController,
],
providers: [
{
provide: 'UserService',
useValue: instance(mockUserService),
},
],
}).compile();

controller = module.get<UserController>(UserController);
expect(controller).toBeDefined();
});
});
describe('#get()', () => {
const id = 1;

it('should return user', async () => {
when(mockUserService.get(id)).thenReturn(new Promise((resolve) => {
resolve(user);
}));
controller = new UserController(instance(mockUserService));

expect(await controller.get(id)).toBe(user);

});
it('should return undefined', async () => {
when(mockUserService.get(id)).thenThrow(new BadRequestException('no user with the id'));
controller = new UserController(instance(mockUserService));

await expect(controller.get(id))
.rejects
.toThrowError(BadRequestException);
});
});
describe('#create()', () => {
let userDto: UserDto;

it('should return user', async () => {
userDto = new UserDto(address, name);
when(mockUserService.create(userDto)).thenReturn(new Promise((resolve => {
resolve(user);
})));
controller = new UserController(instance(mockUserService));

expect(await controller.create(userDto)).toBe(user);

});
it('should return undefined', async () => {
userDto = new UserDto();
when(mockUserService.create(userDto)).thenThrow(new Error('some errors'));
controller = new UserController(instance(mockUserService));

await expect(controller.create(userDto))
.rejects
.toThrowError('some errors');
});
});
describe('#delete()', () => {
const id = 1;

it('should return undefined', async () => {
when(mockUserService.delete(id)).thenReturn(new Promise((resolve => resolve(undefined))));
controller = new UserController(instance(mockUserService));

expect(await controller.delete(id)).toBe(undefined);
});
});
describe('#increaseLevel()', () => {
const id = 1;
const amount = 10;

it('should return user', async () => {
when(mockUserService.increaseLevel(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.increaseLevel(id, {amount})).toBe(user);
});
it('should throw error', async () => {
when(mockUserService.increaseLevel(id, amount)).thenThrow(new Error('some errors'));
controller = new UserController(instance(mockUserService));

await expect(controller.increaseLevel(id, {amount}))
.rejects
.toThrowError('some errors');
});
});
describe('#increasePoint()', () => {
const id = 1;
const amount = 10;

it('should return user', async () => {
when(mockUserService.increasePoint(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.increasePoint(id, {amount})).toBe(user);
});
it('should throw error', async () => {
when(mockUserService.increasePoint(id, amount)).thenThrow(new Error('some errors'));
controller = new UserController(instance(mockUserService));

await expect(controller.increasePoint(id, {amount}))
.rejects
.toThrowError('some errors');
});
});
describe('#decreasePoint()', () => {
const id = 1;
const amount = 10;

it('should return user', async () => {
when(mockUserService.decreasePoint(id, amount)).thenReturn(new Promise((resolve => resolve(user))));
controller = new UserController(instance(mockUserService));

expect(await controller.decreasePoint(id, {amount})).toBe(user);
});
it('should throw error', async () => {
when(mockUserService.decreasePoint(id, amount)).thenThrow(new Error('some errors'));
controller = new UserController(instance(mockUserService));

await expect(controller.decreasePoint(id, {amount}))
.rejects
.toThrowError('some errors');
});
});
});
46 changes: 46 additions & 0 deletions server/src/web/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Body,
Controller,
Delete,
Get,
Inject,
Injectable,
Param,
Post, Put,
Query,
} from '@nestjs/common';
import {User} from '../../domain/user/user.entity';
import {UserDto} from '../../app/user/dto/user.dto';
import {DeleteResult} from 'typeorm';
import {UserService} from '../../app/user/user.service';

@Injectable()
@Controller('users')
export class UserController {
constructor(@Inject('UserService') private service: UserService) {}

@Get(':id')
async get(@Param('id') id: number): Promise<User> {
return await this.service.get(id);
}
@Post()
async create(@Body() userDto: UserDto): Promise<User> {
return await this.service.create(userDto);
}
@Delete(':id')
async delete(@Param('id') id: number): Promise<DeleteResult> {
return await this.service.delete(id);
}
@Put(':id/increase-level')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need Comment: @junbeomlee
not sure http method and url is good.

i referenced: github-api
PUT /repos/:owner/:repo/pulls/:pull_number/update-branch

it was using verb in url

async increaseLevel(@Param('id') id: number, @Query() query): Promise<User> {
return await this.service.increaseLevel(id, query.amount);
}
@Put(':id/increase-point')
async increasePoint(@Param('id') id: number, @Query() query): Promise<User> {
return await this.service.increasePoint(id, query.amount);
}
@Put(':id/decrease-point')
async decreasePoint(@Param('id') id: number, @Query() query): Promise<User> {
return await this.service.decreasePoint(id, query.amount);
}
}