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

곽소정 #3

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 0 additions & 11 deletions .env.example

This file was deleted.

73 changes: 50 additions & 23 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.13",
"@nestjs/typeorm": "^10.0.0",
"@nestjs/swagger": "^7.1.16",
"@nestjs/typeorm": "^10.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"mysql2": "^3.6.1",
"mysql2": "^3.6.4",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.0",
"ts-mockito": "^2.6.1",
"typeorm": "^0.3.17",
"typeorm-transactional": "^0.4.1"
Expand Down
6 changes: 4 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { addTransactionalDataSource } from 'typeorm-transactional';
import { DataSource } from 'typeorm';
import { PhoneController } from './phone/phone-controller/phone-controller.controller';
import {PhoneService} from './phone/phone-service/phone-service.service';

@Module({
imports: [
Expand All @@ -29,7 +31,7 @@ import { DataSource } from 'typeorm';
},
}),
],
controllers: [],
providers: [],
controllers: [PhoneController],
providers: [PhoneService],
})
export class AppModule {}
20 changes: 13 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { initializeTransactionalContext } from 'typeorm-transactional';

async function bootstrap() {
initializeTransactionalContext();

const app = await NestFactory.create(AppModule);
// TODO: 프로그램 구현
await app.listen(process.env.PORT || 8000);

console.log(`Application is running on: ${await app.getUrl()}`);
}
const config = new DocumentBuilder()
.setTitle('Phone Verification API')
.setDescription('The phone verification API description')
.setVersion('1.0')
.addTag('phone-verification')
.build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(3001);
}
bootstrap();

10 changes: 10 additions & 0 deletions src/notificaiton/dto/notificaiton-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, Length } from "class-validator";

export class CheckCodeDto {
@IsNotEmpty()
@IsString()
@Length(6, 6)
@ApiProperty({ example: '123456', description: '인증 코드' })
code: string;
}
10 changes: 10 additions & 0 deletions src/phone/dto/GetAllVerificationsDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { VerifyCodeDto } from './VerifyCodeDto';

export class GetAllVerificationsDto {
@ApiProperty({
type: [VerifyCodeDto],
description: '저장된 모든 전화번호와 그 인증 코드 목록'
})
verifications: VerifyCodeDto[];
}
10 changes: 10 additions & 0 deletions src/phone/dto/SendCodeDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNotEmpty, IsString, Matches } from 'class-validator';

export class SendCodeDto {
@IsNotEmpty()
@IsString()
@ApiProperty({ example: '010-1234-5678', description: '전화번호' })
@Matches(/^010-\d{4}-\d{4}$/, { message: '잘못된 전화번호 형식입니다' })
phoneNumber: string;
}
16 changes: 16 additions & 0 deletions src/phone/dto/VerifyCodeDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsNotEmpty, IsString, Length, Matches } from 'class-validator';
import { ApiProperty } from "@nestjs/swagger";

export class VerifyCodeDto {
@IsNotEmpty()
@IsString()
@ApiProperty({ example: '010-1234-5678', description: '전화번호' })
@Matches(/^010-\d{4}-\d{4}$/, { message: '잘못된 전화번호 형식입니다' })
phoneNumber: string;

@IsNotEmpty()
@IsString()
@Length(6, 6, { message: '코드는 6자리 숫자여야 합니다' })
@ApiProperty({ example: '123456', description: '코드' })
code: string;
}
14 changes: 14 additions & 0 deletions src/phone/entity/PhoneVerification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Entity, Column } from "typeorm";

@Entity()
export class PhoneVerification {
@Column("전화번호")
phoneNumber: string;

@Column("코드")
code: string;

@Column("시간")
timestamp: Date;
}

7 changes: 7 additions & 0 deletions src/phone/exception/CodeNotFound.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from "@nestjs/common";

export class CodeNotFound extends HttpException {
constructor() {
super('인증번호가 맞지 않습니다', HttpStatus.BAD_REQUEST);
}
}
7 changes: 7 additions & 0 deletions src/phone/exception/TimeOver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from "@nestjs/common";

export class TimeOver extends HttpException {
constructor() {
super('인증번호 유효시간이 지났습니다', HttpStatus.BAD_REQUEST);
}
}
7 changes: 7 additions & 0 deletions src/phone/exception/TitleNullException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from "@nestjs/common";

export class TitleNullException extends HttpException {
constructor() {
super('전화번호를 입력해주세요', HttpStatus.BAD_REQUEST);
}
}
49 changes: 49 additions & 0 deletions src/phone/phone-controller/phone-controller.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PhoneController } from './phone-controller.controller';
import { PhoneService } from '../phone-service/phone-service.service';
import { SendCodeDto } from '../dto/SendCodeDto';
import { CheckCodeDto } from '../../notificaiton/dto/notificaiton-response.dto';

describe('PhoneController', () => {
let controller: PhoneController;
let mockPhoneService: PhoneService;

beforeEach(async () => {
mockPhoneService = {
sendCode: jest.fn(),
checkCode: jest.fn(),
getAllVerifications: jest.fn()
} as any;

const module: TestingModule = await Test.createTestingModule({
controllers: [PhoneController],
providers: [{ provide: PhoneService, useValue: mockPhoneService }],
}).compile();

controller = module.get<PhoneController>(PhoneController);
});

describe('sendCode', () => {
it('PhoneService의 sendCode 메서드를 호출해야 함', async () => {
const sendCodeDto: SendCodeDto = { phoneNumber: '010-1234-5678' };
await controller.sendCode(sendCodeDto);
expect(mockPhoneService.sendCode).toHaveBeenCalledWith(sendCodeDto);
});
});

describe('checkCode', () => {
it('정확한 매개변수와 함께 PhoneService의 checkCode 메서드를 호출해야 함', async () => {
const phoneNumber = '010-1234-5678';
const checkCodeDto: CheckCodeDto = { code: '123456' };
await controller.checkCode(phoneNumber, checkCodeDto);
expect(mockPhoneService.checkCode).toHaveBeenCalledWith(phoneNumber, checkCodeDto.code);
});
});

describe('getAllVerifications', () => {
it('PhoneService의 getAllVerifications 메서드를 호출해야 함', async () => {
await controller.getAllVerifications();
expect(mockPhoneService.getAllVerifications).toHaveBeenCalled();
});
});
});
Loading