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

[NDD-103] Member API E2E 테스트 (1h / 1h) #29

Merged
merged 5 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions BE/src/config/cors.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {CorsOptions} from "@nestjs/common/interfaces/external/cors-options.interface";
import {CORS_HEADERS, CORS_ORIGIN} from "./cors.secure";
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { CORS_HEADERS, CORS_ORIGIN } from './cors.secure';

export const CORS_CONFIG: CorsOptions = {
origin: CORS_ORIGIN,
credentials: true,
exposedHeaders: CORS_HEADERS,
}
origin: CORS_ORIGIN,
credentials: true,
exposedHeaders: CORS_HEADERS,
};
2 changes: 1 addition & 1 deletion BE/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common';
import { setupSwagger } from './config/swagger.config';
import {CORS_CONFIG} from "./config/cors.config";
import { CORS_CONFIG } from './config/cors.config';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
Expand Down
48 changes: 47 additions & 1 deletion BE/src/member/controller/member.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { Test } from '@nestjs/testing';
import { Test, TestingModule } from '@nestjs/testing';
import { MemberController } from './member.controller';
import { MemberResponse } from '../dto/memberResponse';
import { Request } from 'express';
import { Member } from '../entity/member';
import { ManipulatedTokenNotFiltered } from 'src/token/exception/token.exception';
import { INestApplication } from '@nestjs/common';
import { AppModule } from 'src/app.module';
import * as request from 'supertest';
import { TokenModule } from 'src/token/token.module';
import { TokenService } from 'src/token/service/token.service';

describe('MemberController', () => {
let memberController: MemberController;
Expand Down Expand Up @@ -47,3 +52,44 @@ describe('MemberController', () => {
).toThrow(ManipulatedTokenNotFiltered);
});
});

describe('MemberController (E2E Test)', () => {
let app: INestApplication;
let tokenService: TokenService;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule, TokenModule],
}).compile();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분의 반복구조를 빼낼 수 있을 것 같습니다!!!

const createTestApp = async (moduleOptions: 타입은 직접 들어가서 봐야겠군요!) => {
    const moduleFixture: TestingModule = await Test.createTestingModule(moduleOptions).compile();
    return moduleFixture.createNestApplication();
}

이렇게 한 후에, 따로 const로 옵션들을 가져와서 테스트 app의 반복구조를 줄이는건 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 좋은 생각입니다 한번 시도해보겠습니다!!!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


app = moduleFixture.createNestApplication();
await app.init();

tokenService = moduleFixture.get<TokenService>(TokenService);
});

it('GET /api/member (회원 정보 반환 성공)', async () => {
const memberId = 1; // 항상 DB에 들어있는 회원의 ID로의 설정이 필요해보입니다.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에 대한 고민이 많습니다... 이렇게 진행하면 DB에 영구적으로 1이라는 ID를 가지는 Member를 항상 넣어놔야 하는데.. 이게 말이 되나 싶구요..
소셜 로그인을 코드로 해결할 수가 없다는게 이런 문제를 발생시키는 것 같습니다.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇다면, 테스트시에 DB의 모든 데이터를 지우는 로직을 테스트 전체 후에 넣는 것은 어떨까요???
beforeEach, beforeAll 혹은 afterAll로 해당 문제를 해결할 수 있을 것 같습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 좋은 생각입니다 고민 해보겠습니다!!!

const validToken = await tokenService.assignToken(memberId);

const response = await request(app.getHttpServer())
.get('/api/member')
.set('Authorization', `Bearer ${validToken}`)
.expect(200);

expect(response.body.id).toBe(memberId);
});

it('GET /api/member (유효하지 않은 토큰 사용으로 인한 회원 정보 반환 실패)', async () => {
const invalidToken = 'INVALID_TOKEN';

await request(app.getHttpServer())
.get('/api/member')
.set('Authorization', `Bearer ${invalidToken}`)
.expect(401);
});

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