Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
eldpswp99 committed Oct 15, 2023
1 parent 781167f commit 227561e
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 115 deletions.
14 changes: 0 additions & 14 deletions backend/src/auth/controller/signup-dtos/createuser.dto.ts

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/auth/strategies/local.strategy.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -10,7 +10,7 @@ export class LocalStrategy extends PassportStrategy(Strategy) {
}

async validate(username: string, password: string): Promise<any> {
const user = await User.findOne({
const user = await UserEntity.findOne({
where: {
username,
},
Expand Down
4 changes: 2 additions & 2 deletions backend/src/core/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand All @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions backend/src/core/user-request.ts
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 3 additions & 4 deletions backend/src/test/auth/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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';

describe('login test', () => {
let testServer: NestExpressApplication;
let dataSource: DataSource;
let user: User;
let user: UserEntity;

beforeAll(async () => {
const module = await Test.createTestingModule({
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('login test', () => {
password: 'world2',
})
.expect(HttpStatus.UNAUTHORIZED);
})
});
it('존재하지 않는 유저로 찾기', async () => {
await supertest(testServer.getHttpServer())
.post('/auth/login')
Expand All @@ -62,7 +62,6 @@ describe('login test', () => {
await supertest(testServer.getHttpServer())
.post('/auth/login')
.send({
name: 'hi',
username: 'hello',
password: 'world',
})
Expand Down
53 changes: 0 additions & 53 deletions backend/src/test/auth/signup.spec.ts

This file was deleted.

4 changes: 2 additions & 2 deletions backend/src/test/fixture/user.fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from '../../user/models/user.entity';
import { UserEntity } from '../../user/models/user.entity';

export type UserFixtureProps = {
name: string;
Expand All @@ -8,7 +8,7 @@ export type UserFixtureProps = {

export class UserFixture {
static create({ name, username, password }: Partial<UserFixtureProps>) {
return User.create({
return UserEntity.create({
name: name ?? 'test',
username: username ?? 'test',
password: password ?? 'test',
Expand Down
56 changes: 56 additions & 0 deletions backend/src/test/user/signup.spec.ts
Original file line number Diff line number Diff line change
@@ -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<NestExpressApplication>();
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();
});
});
14 changes: 14 additions & 0 deletions backend/src/user/in-dtos/createuser.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IsString, IsNotEmpty } from 'class-validator';

export class CreateUserDto {
@IsString()
name: string;

@IsString()
@IsNotEmpty()
username: string;

@IsString()
@IsNotEmpty()
password: string;
}
2 changes: 1 addition & 1 deletion backend/src/user/models/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
11 changes: 11 additions & 0 deletions backend/src/user/repostiories/user.repository.ts
Original file line number Diff line number Diff line change
@@ -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<UserEntity> {
async findByUsername(username: string): Promise<UserEntity | undefined> {
const user = await this.findOne({ where: { username } });
return user ? user : undefined; // Return undefined when no user is found
}
}
28 changes: 16 additions & 12 deletions backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -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<User> {
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<UserEntity> {
const existingUser = await this.userRepository.findByUsername(
createUserDto.username,
);
if (existingUser) {
throw new ConflictException('Username already exists');
}

return this.userService.create(createUserDto);
}
}
10 changes: 5 additions & 5 deletions backend/src/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
27 changes: 9 additions & 18 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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<User>,
) {}
constructor(private userRepository: UserRepository) {}

async create(createUserDto: CreateUserDto): Promise<User> {
const { name, username, password } = createUserDto;
const user = this.usersRepository.create({ name, username, password });
return this.usersRepository.save(user);
}

async findByUsername(username: string): Promise<User | undefined> {
const user = await this.usersRepository.findOne({ where: { username } });
return user ? user : undefined; // Return undefined when no user is found
}
async create(createUserDto: CreateUserDto): Promise<UserEntity> {
const { name, username, password } = createUserDto;
const user = this.userRepository.create({ name, username, password });
return this.userRepository.save(user);
}
}

0 comments on commit 227561e

Please sign in to comment.