Skip to content

Commit

Permalink
Merge pull request #271 from boostcampwm2023/test/230-test-db-refactor
Browse files Browse the repository at this point in the history
[Test] 백엔드 e2e 테스트 코드 트랜잭션 반영 및 테스트 DB 더미데이터 활용
  • Loading branch information
mingxoxo authored Dec 14, 2023
2 parents d1601e3 + 76dd7af commit 246bc87
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 365 deletions.
7 changes: 7 additions & 0 deletions BE/src/auth/guard/auth.diary-guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BadRequestException,
ExecutionContext,
Injectable,
NotFoundException,
Expand All @@ -24,11 +25,17 @@ export class PrivateDiaryGuard extends JwtAuthGuard {
}

const request = context.switchToHttp().getRequest();

// GET, DELETE 요청인 경우 params.uuid를 사용
// PUT 요청인 경우 body.uuid를 사용
const requestUuid = request.params.uuid
? request.params.uuid
: request.body.uuid;

if (requestUuid === undefined) {
throw new BadRequestException("일기 uuid는 비어있지 않아야 합니다.");
}

const requestDiary =
await this.diariesRepository.getDiaryByUuid(requestUuid);

Expand Down
4 changes: 4 additions & 0 deletions BE/src/purchase/purchase.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export class PurchaseController {
constructor(private purchaseService: PurchaseService) {}

@Get("/credit")
@HttpCode(200)
async getCreditBalanceByUser(@GetUser() user: User): Promise<CreditDto> {
return new CreditDto(user.credit);
}

@Post("/design")
@HttpCode(200)
async purchaseDesign(
@GetUser() user: User,
@Body() purchaseDesignDto: PurchaseDesignDto,
Expand All @@ -39,11 +41,13 @@ export class PurchaseController {
}

@Post("/premium")
@HttpCode(200)
async purchasePremium(@GetUser() user: User): Promise<CreditDto> {
return this.purchaseService.purchasePremium(user);
}

@Get("/premium")
@HttpCode(200)
async getPremiumStatus(@GetUser() user: User): Promise<PremiumDto> {
return this.purchaseService.getPremiumStatus(user);
}
Expand Down
2 changes: 1 addition & 1 deletion BE/src/purchase/purchase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PurchaseService {
const domain = domainEnum[purchaseDesignDto.domain];
const design = designEnum[purchaseDesignDto.design];

if (await this.isDesignAlreadyPurchased(user.userId, design, design)) {
if (await this.isDesignAlreadyPurchased(user.userId, domain, design)) {
throw new BadRequestException(`이미 구매한 디자인입니다.`);
}

Expand Down
45 changes: 0 additions & 45 deletions BE/src/utils/clearDb.ts

This file was deleted.

17 changes: 9 additions & 8 deletions BE/test/e2e/auth.reissue.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { ValidationPipe } from "@nestjs/common";
import { AuthModule } from "src/auth/auth.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMTestConfig } from "src/configs/typeorm.test.config";
import { User } from "src/auth/users.entity";
import { UsersRepository } from "src/auth/users.repository";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { DataSource } from "typeorm";
import { TransactionalTestContext } from "typeorm-transactional-tests";

describe("[액세스 토큰 재발급] /auth/reissue POST e2e 테스트", () => {
let app: INestApplication;
let dataSource: DataSource;
let transactionalContext: TransactionalTestContext;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand All @@ -34,6 +37,10 @@ describe("[액세스 토큰 재발급] /auth/reissue POST e2e 테스트", () =>

await app.init();

dataSource = moduleFixture.get<DataSource>(DataSource);
transactionalContext = new TransactionalTestContext(dataSource);
await transactionalContext.start();

await request(app.getHttpServer())
.post("/auth/signup")
.send({
Expand All @@ -46,13 +53,7 @@ describe("[액세스 토큰 재발급] /auth/reissue POST e2e 테스트", () =>
});

afterAll(async () => {
const usersRepository = new UsersRepository();

const testUser = await usersRepository.getUserByUserId("SignoutTestUserId");
if (testUser) {
await User.remove(testUser);
}

await transactionalContext.finish();
await app.close();
});

Expand Down
9 changes: 9 additions & 0 deletions BE/test/e2e/auth.signin.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import { AuthModule } from "src/auth/auth.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMTestConfig } from "src/configs/typeorm.test.config";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { DataSource } from "typeorm";
import { TransactionalTestContext } from "typeorm-transactional-tests";

describe("[로그인] /auth/signin POST e2e 테스트", () => {
let app: INestApplication;
let dataSource: DataSource;
let transactionalContext: TransactionalTestContext;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand All @@ -30,9 +34,14 @@ describe("[로그인] /auth/signin POST e2e 테스트", () => {
app.useGlobalPipes(new ValidationPipe());

await app.init();

dataSource = moduleFixture.get<DataSource>(DataSource);
transactionalContext = new TransactionalTestContext(dataSource);
await transactionalContext.start();
});

afterAll(async () => {
await transactionalContext.finish();
await app.close();
});

Expand Down
17 changes: 9 additions & 8 deletions BE/test/e2e/auth.signout.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { ValidationPipe } from "@nestjs/common";
import { AuthModule } from "src/auth/auth.module";
import { TypeOrmModule } from "@nestjs/typeorm";
import { typeORMTestConfig } from "src/configs/typeorm.test.config";
import { User } from "src/auth/users.entity";
import { UsersRepository } from "src/auth/users.repository";
import { RedisModule } from "@liaoliaots/nestjs-redis";
import { DataSource } from "typeorm";
import { TransactionalTestContext } from "typeorm-transactional-tests";

describe("[로그아웃] /auth/signout POST e2e 테스트", () => {
let app: INestApplication;
let dataSource: DataSource;
let transactionalContext: TransactionalTestContext;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand All @@ -34,6 +37,10 @@ describe("[로그아웃] /auth/signout POST e2e 테스트", () => {

await app.init();

dataSource = moduleFixture.get<DataSource>(DataSource);
transactionalContext = new TransactionalTestContext(dataSource);
await transactionalContext.start();

await request(app.getHttpServer())
.post("/auth/signup")
.send({
Expand All @@ -46,13 +53,7 @@ describe("[로그아웃] /auth/signout POST e2e 테스트", () => {
});

afterAll(async () => {
const usersRepository = new UsersRepository();

const testUser = await usersRepository.getUserByUserId("SignoutTestUserId");
if (testUser) {
await User.remove(testUser);
}

await transactionalContext.finish();
await app.close();
});

Expand Down
Loading

0 comments on commit 246bc87

Please sign in to comment.