From 227561ecc73fb8ef0bf0520c2e4563410b03a775 Mon Sep 17 00:00:00 2001 From: eldpswp99 Date: Sun, 15 Oct 2023 15:32:14 +0900 Subject: [PATCH] fix test --- .../controller/signup-dtos/createuser.dto.ts | 14 ----- backend/src/auth/strategies/local.strategy.ts | 4 +- backend/src/core/ormconfig.ts | 4 +- backend/src/core/user-request.ts | 4 +- backend/src/test/auth/login.spec.ts | 7 +-- backend/src/test/auth/signup.spec.ts | 53 ------------------ backend/src/test/fixture/user.fixture.ts | 4 +- backend/src/test/user/signup.spec.ts | 56 +++++++++++++++++++ backend/src/user/in-dtos/createuser.dto.ts | 14 +++++ backend/src/user/models/user.entity.ts | 2 +- .../src/user/repostiories/user.repository.ts | 11 ++++ backend/src/user/user.controller.ts | 28 ++++++---- backend/src/user/user.module.ts | 10 ++-- backend/src/user/user.service.ts | 27 +++------ 14 files changed, 123 insertions(+), 115 deletions(-) delete mode 100644 backend/src/auth/controller/signup-dtos/createuser.dto.ts delete mode 100644 backend/src/test/auth/signup.spec.ts create mode 100644 backend/src/test/user/signup.spec.ts create mode 100644 backend/src/user/in-dtos/createuser.dto.ts create mode 100644 backend/src/user/repostiories/user.repository.ts diff --git a/backend/src/auth/controller/signup-dtos/createuser.dto.ts b/backend/src/auth/controller/signup-dtos/createuser.dto.ts deleted file mode 100644 index 52a928b..0000000 --- a/backend/src/auth/controller/signup-dtos/createuser.dto.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { IsString, IsNotEmpty, IsAlphanumeric, MinLength } from 'class-validator'; - -export class CreateUserDto { - @IsString() - name: string; - - @IsString() - @IsNotEmpty() - username: string; - - @IsString() - @IsNotEmpty() - password: string; -} \ No newline at end of file diff --git a/backend/src/auth/strategies/local.strategy.ts b/backend/src/auth/strategies/local.strategy.ts index 9f6fb29..3f2ab09 100644 --- a/backend/src/auth/strategies/local.strategy.ts +++ b/backend/src/auth/strategies/local.strategy.ts @@ -1,7 +1,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { Strategy } from 'passport-local'; import { PassportStrategy } from '@nestjs/passport'; -import { User } from '../../user/models/user.entity'; +import { UserEntity } from '../../user/models/user.entity'; @Injectable() export class LocalStrategy extends PassportStrategy(Strategy) { @@ -10,7 +10,7 @@ export class LocalStrategy extends PassportStrategy(Strategy) { } async validate(username: string, password: string): Promise { - const user = await User.findOne({ + const user = await UserEntity.findOne({ where: { username, }, diff --git a/backend/src/core/ormconfig.ts b/backend/src/core/ormconfig.ts index bad4a38..23f71c7 100644 --- a/backend/src/core/ormconfig.ts +++ b/backend/src/core/ormconfig.ts @@ -3,7 +3,7 @@ import { DataSourceOptions } from 'typeorm'; import { SnakeNamingStrategy } from 'typeorm-naming-strategies'; import { getEnvFilePath } from './getEnvFilePath'; import * as path from 'path'; -import { User } from '../user/models/user.entity'; +import { UserEntity } from '../user/models/user.entity'; const envFilePath = getEnvFilePath(); config({ path: path.resolve(process.cwd(), envFilePath) }); @@ -15,7 +15,7 @@ const ormConfig: DataSourceOptions = { username: process.env.PG_USER, password: process.env.PG_PASSWORD, database: process.env.PG_DBNAME, - entities: [User], + entities: [UserEntity], synchronize: true, migrationsTableName: 'migrations', migrations: ['dist/migrations/*.js'], diff --git a/backend/src/core/user-request.ts b/backend/src/core/user-request.ts index 8d6bff4..a3875e9 100644 --- a/backend/src/core/user-request.ts +++ b/backend/src/core/user-request.ts @@ -1,5 +1,5 @@ -import { User } from '../user/models/user.entity'; +import { UserEntity } from '../user/models/user.entity'; export interface UserRequest extends Request { - user: User; + user: UserEntity; } diff --git a/backend/src/test/auth/login.spec.ts b/backend/src/test/auth/login.spec.ts index 3d5e6e6..d1e4b93 100644 --- a/backend/src/test/auth/login.spec.ts +++ b/backend/src/test/auth/login.spec.ts @@ -4,7 +4,7 @@ import { Test } from '@nestjs/testing'; import { DataSource } from 'typeorm'; import { appSetting } from '../../main'; import * as supertest from 'supertest'; -import { User } from '../../user/models/user.entity'; +import { UserEntity } from '../../user/models/user.entity'; import { UserFixture } from '../fixture/user.fixture'; import { HttpStatus } from '@nestjs/common'; import { validateDtoKeys } from '../utils'; @@ -12,7 +12,7 @@ import { validateDtoKeys } from '../utils'; describe('login test', () => { let testServer: NestExpressApplication; let dataSource: DataSource; - let user: User; + let user: UserEntity; beforeAll(async () => { const module = await Test.createTestingModule({ @@ -46,7 +46,7 @@ describe('login test', () => { password: 'world2', }) .expect(HttpStatus.UNAUTHORIZED); - }) + }); it('존재하지 않는 유저로 찾기', async () => { await supertest(testServer.getHttpServer()) .post('/auth/login') @@ -62,7 +62,6 @@ describe('login test', () => { await supertest(testServer.getHttpServer()) .post('/auth/login') .send({ - name: 'hi', username: 'hello', password: 'world', }) diff --git a/backend/src/test/auth/signup.spec.ts b/backend/src/test/auth/signup.spec.ts deleted file mode 100644 index d192192..0000000 --- a/backend/src/test/auth/signup.spec.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { NestExpressApplication } from '@nestjs/platform-express'; -import { AppModule } from '../../app.module'; -import { Test } from '@nestjs/testing'; -import { DataSource } from 'typeorm'; -import { appSetting } from '../../main'; -import * as supertest from 'supertest'; -import { User } from '../../user/models/user.entity'; -import { UserFixture } from '../fixture/user.fixture'; -import { HttpStatus } from '@nestjs/common'; - -describe('signup test', () => { - let testServer: NestExpressApplication; - let dataSource: DataSource; - let user: User; - - beforeAll(async () => { - const module = await Test.createTestingModule({ - imports: [AppModule], - }).compile(); - - testServer = module.createNestApplication(); - dataSource = testServer.get(DataSource); - await dataSource.synchronize(true); - appSetting(testServer); - - await testServer.init(); - }); - - beforeEach(async () => { - await dataSource.synchronize(true); - - user = await UserFixture.create({ - name: 'hi', - username: 'hello', - password: 'world', - }); - }); - - it('Check if the username already exists', async () => { - await supertest(testServer.getHttpServer()) - .post('/user/signup') - .send({ - name: 'John Doe', - username: 'hello', - password: 'password123', - }) - .expect(HttpStatus.CONFLICT); - }); - - afterAll(async () => { - await testServer.close(); - }); -}); diff --git a/backend/src/test/fixture/user.fixture.ts b/backend/src/test/fixture/user.fixture.ts index 93d57d6..327d168 100644 --- a/backend/src/test/fixture/user.fixture.ts +++ b/backend/src/test/fixture/user.fixture.ts @@ -1,4 +1,4 @@ -import { User } from '../../user/models/user.entity'; +import { UserEntity } from '../../user/models/user.entity'; export type UserFixtureProps = { name: string; @@ -8,7 +8,7 @@ export type UserFixtureProps = { export class UserFixture { static create({ name, username, password }: Partial) { - return User.create({ + return UserEntity.create({ name: name ?? 'test', username: username ?? 'test', password: password ?? 'test', diff --git a/backend/src/test/user/signup.spec.ts b/backend/src/test/user/signup.spec.ts new file mode 100644 index 0000000..7fe9da6 --- /dev/null +++ b/backend/src/test/user/signup.spec.ts @@ -0,0 +1,56 @@ +import { NestExpressApplication } from '@nestjs/platform-express'; +import { AppModule } from '../../app.module'; +import { Test } from '@nestjs/testing'; +import { DataSource } from 'typeorm'; +import { appSetting } from '../../main'; +import * as supertest from 'supertest'; +import { UserEntity } from '../../user/models/user.entity'; +import { UserFixture } from '../fixture/user.fixture'; +import { HttpStatus } from '@nestjs/common'; + +describe('signup test', () => { + let testServer: NestExpressApplication; + let dataSource: DataSource; + + let user: UserEntity; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + testServer = module.createNestApplication(); + dataSource = testServer.get(DataSource); + await dataSource.synchronize(true); + appSetting(testServer); + + await testServer.init(); + }); + + beforeEach(async () => { + await dataSource.synchronize(true); + + user = await UserFixture.create({ + name: 'hi', + username: 'hello', + password: 'world', + }); + }); + + it('Check if the username already exists', async () => { + await supertest(testServer.getHttpServer()) + .post('/user/signup') + .send({ + name: 'John Doe', + username: 'hello', + password: 'password123', + }) + .expect(HttpStatus.CONFLICT); + }); + + afterAll(async () => { + await dataSource.synchronize(true); + + await testServer.close(); + }); +}); diff --git a/backend/src/user/in-dtos/createuser.dto.ts b/backend/src/user/in-dtos/createuser.dto.ts new file mode 100644 index 0000000..4eb0e40 --- /dev/null +++ b/backend/src/user/in-dtos/createuser.dto.ts @@ -0,0 +1,14 @@ +import { IsString, IsNotEmpty } from 'class-validator'; + +export class CreateUserDto { + @IsString() + name: string; + + @IsString() + @IsNotEmpty() + username: string; + + @IsString() + @IsNotEmpty() + password: string; +} diff --git a/backend/src/user/models/user.entity.ts b/backend/src/user/models/user.entity.ts index ce3ce4a..edd9e23 100644 --- a/backend/src/user/models/user.entity.ts +++ b/backend/src/user/models/user.entity.ts @@ -5,7 +5,7 @@ import { sign } from 'jsonwebtoken'; import * as process from 'process'; @Entity() -export class User extends IssuedAtMetaEntity { +export class UserEntity extends IssuedAtMetaEntity { @Column({ type: 'varchar' }) name: string; @Column({ type: 'varchar' }) diff --git a/backend/src/user/repostiories/user.repository.ts b/backend/src/user/repostiories/user.repository.ts new file mode 100644 index 0000000..e51bba6 --- /dev/null +++ b/backend/src/user/repostiories/user.repository.ts @@ -0,0 +1,11 @@ +import { CustomRepository } from '../../typeorm-ex/typeorm-ex.decorator'; +import { Repository } from 'typeorm'; +import { UserEntity } from '../models/user.entity'; + +@CustomRepository(UserEntity) +export class UserRepository extends Repository { + async findByUsername(username: string): Promise { + const user = await this.findOne({ where: { username } }); + return user ? user : undefined; // Return undefined when no user is found + } +} diff --git a/backend/src/user/user.controller.ts b/backend/src/user/user.controller.ts index dd6b30a..6bba424 100644 --- a/backend/src/user/user.controller.ts +++ b/backend/src/user/user.controller.ts @@ -1,20 +1,24 @@ import { Controller, Post, Body, ConflictException } from '@nestjs/common'; import { UserService } from './user.service'; -import { User } from './models/user.entity'; -import {CreateUserDto} from "../auth/controller/signup-dtos/createuser.dto"; +import { UserEntity } from './models/user.entity'; +import { CreateUserDto } from './in-dtos/createuser.dto'; +import { UserRepository } from './repostiories/user.repository'; @Controller('user') export class UserController { - constructor(private readonly userService: UserService) {} + constructor( + private readonly userService: UserService, + private readonly userRepository: UserRepository, + ) {} - - @Post('signup') - async signUp(@Body() createUserDto: CreateUserDto): Promise { - const existingUser = await this.userService.findByUsername(createUserDto.username); - if (existingUser) { - throw new ConflictException('Username already exists'); - } - return this.userService.create(createUserDto); + @Post('signup') + async signUp(@Body() createUserDto: CreateUserDto): Promise { + const existingUser = await this.userRepository.findByUsername( + createUserDto.username, + ); + if (existingUser) { + throw new ConflictException('Username already exists'); } - + return this.userService.create(createUserDto); + } } diff --git a/backend/src/user/user.module.ts b/backend/src/user/user.module.ts index 88b1ecf..e8bc7c5 100644 --- a/backend/src/user/user.module.ts +++ b/backend/src/user/user.module.ts @@ -1,12 +1,12 @@ import { Module } from '@nestjs/common'; -import { TypeOrmModule } from '@nestjs/typeorm'; import { UserController } from './user.controller'; import { UserService } from './user.service'; -import { User } from './models/user.entity'; +import { TypeOrmExModule } from '../typeorm-ex/typeorm-ex.module'; +import { UserRepository } from './repostiories/user.repository'; @Module({ - imports: [TypeOrmModule.forFeature([User])], - controllers: [UserController], - providers: [UserService], + imports: [TypeOrmExModule.forCustomRepository([UserRepository])], + controllers: [UserController], + providers: [UserService], }) export class UserModule {} diff --git a/backend/src/user/user.service.ts b/backend/src/user/user.service.ts index 8a486b3..ad4653b 100644 --- a/backend/src/user/user.service.ts +++ b/backend/src/user/user.service.ts @@ -1,24 +1,15 @@ import { Injectable } from '@nestjs/common'; -import { InjectRepository } from '@nestjs/typeorm'; -import { Repository } from 'typeorm'; -import { User } from './models/user.entity'; -import { CreateUserDto } from '../auth/controller/signup-dtos/createuser.dto'; +import { UserEntity } from './models/user.entity'; +import { CreateUserDto } from './in-dtos/createuser.dto'; +import { UserRepository } from './repostiories/user.repository'; @Injectable() export class UserService { - constructor( - @InjectRepository(User) - private usersRepository: Repository, - ) {} + constructor(private userRepository: UserRepository) {} - async create(createUserDto: CreateUserDto): Promise { - const { name, username, password } = createUserDto; - const user = this.usersRepository.create({ name, username, password }); - return this.usersRepository.save(user); - } - - async findByUsername(username: string): Promise { - const user = await this.usersRepository.findOne({ where: { username } }); - return user ? user : undefined; // Return undefined when no user is found - } + async create(createUserDto: CreateUserDto): Promise { + const { name, username, password } = createUserDto; + const user = this.userRepository.create({ name, username, password }); + return this.userRepository.save(user); + } }