Skip to content

Commit

Permalink
Merge pull request #116 from classbinu/jinhwan
Browse files Browse the repository at this point in the history
Jinhwan
  • Loading branch information
jinjung0101 authored Feb 17, 2024
2 parents 30b69f5 + e316b4d commit 48764ef
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UseGuards,
Patch,
Res,
ValidationPipe,
} from '@nestjs/common';
import { Response } from 'express';
import { CreateUserDto } from 'src/users/dto/create-user.dto';
Expand All @@ -25,7 +26,7 @@ export class AuthController {
constructor(private authService: AuthService) {}

@Post('register')
async register(@Body() createUserDto: CreateUserDto) {
async register(@Body(new ValidationPipe()) createUserDto: CreateUserDto) {
return await this.authService.register(createUserDto);
}

Expand Down
8 changes: 7 additions & 1 deletion src/filter/GlobalExceptionFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export class GlobalExceptionFilter implements ExceptionFilter {

if (exception instanceof HttpException) {
status = exception.getStatus();
userMessage = 'A request error occurred';
const exceptionResponse = exception.getResponse();
userMessage =
typeof exceptionResponse === 'string'
? exceptionResponse
: Array.isArray((exceptionResponse as any).message)
? (exceptionResponse as any).message[0]
: (exceptionResponse as any).message || 'A request error occurred';
} else if (exception.name === 'CastError') {
status = HttpStatus.BAD_REQUEST;
this.logger.error(`CastError: ${exception.message}`);
Expand Down
48 changes: 48 additions & 0 deletions src/users/__test__/users.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Test, TestingModule } from '@nestjs/testing';

import { UsersController } from '../users.controller';
import { UsersService } from '../users.service';
import { CreateUserDto } from '../dto/create-user.dto';

describe('UsersController', () => {
let controller: UsersController;
let service: UsersService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [UsersController],
providers: [
{
provide: UsersService,
useValue: {
createUser: jest
.fn()
.mockResolvedValue({ id: '1', userId: 'test', password: 'test' }),
},
},
],
}).compile();

controller = module.get<UsersController>(UsersController);
service = module.get<UsersService>(UsersService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('createUser', () => {
it('should return a user', async () => {
const createUserDto: CreateUserDto = {
userId: 'test',
password: 'test',
};
expect(await controller.createUser(createUserDto)).toEqual({
id: '1',
userId: 'test',
password: 'test',
});
expect(service.createUser).toHaveBeenCalledWith(createUserDto);
});
});
});
19 changes: 10 additions & 9 deletions src/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import { IsNotEmpty, IsString, Matches } from 'class-validator';
import {
IsNotEmpty,
IsString,
Matches,
MaxLength,
MinLength,
} from 'class-validator';

export class CreateUserDto {
@IsString()
@IsNotEmpty()
@MinLength(3, { message: '아이디를 3글자 이상 입력해 주세요.' })
@MaxLength(10, { message: '아이디를 10글자 이내로 입력해 주세요.' })
@Matches(/^[A-Za-z0-9]*$/, {
message: 'userId should not contain spaces or special characters',
})
userId: string;

@IsString()
@MinLength(4, { message: '비밀번호를 4글자 이상 입력해 주세요.' })
@IsNotEmpty()
password: string;

// @IsString()
// @IsEmail()
// @IsOptional()
// email: string;

// @IsOptional()
// refreshToken?: string | null;
}
5 changes: 4 additions & 1 deletion src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Req,
UseInterceptors,
UploadedFile,
ValidationPipe,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { UsersService } from './users.service';
Expand All @@ -32,7 +33,9 @@ export class UsersController {
constructor(private readonly usersService: UsersService) {}

@Post()
createUser(@Body() createUserDto: CreateUserDto): Promise<User> {
createUser(
@Body(new ValidationPipe()) createUserDto: CreateUserDto,
): Promise<User> {
return this.usersService.createUser(createUserDto);
}

Expand Down

0 comments on commit 48764ef

Please sign in to comment.