Skip to content

Commit

Permalink
test: AuthController에 대한 E2E 테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Conut-1 committed Sep 24, 2024
1 parent 58fb6c2 commit 39dfd5a
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions nestjs-BE/server/test/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { HttpStatus, INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { AuthModule } from '../src/auth/auth.module';
import * as request from 'supertest';
import { ConfigModule } from '@nestjs/config';

describe('AuthController (e2e)', () => {
let app: INestApplication;
let fetchSpy: jest.SpyInstance;

beforeAll(() => {
fetchSpy = jest.spyOn(global, 'fetch');
});

beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AuthModule, ConfigModule.forRoot({ isGlobal: true })],
}).compile();

app = moduleFixture.createNestApplication();

await app.init();
});

afterEach(async () => {
fetchSpy.mockRestore();

await app.close();
});

it('/auth/kakao-oauth (POST)', () => {
fetchSpy.mockResolvedValue({
json: async () => {
return { kakao_account: { email: '[email protected]' } };
},
ok: true,
});

return request(app.getHttpServer())
.post('/auth/kakao-oauth')
.send({ kakaoUserId: 1 })
.expect(HttpStatus.CREATED)
.expect((res) => {
expect(res.body.statusCode).toBe(HttpStatus.OK);
expect(res.body.message).toBe('Success');
expect(res.body.data.access_token).toMatch(
/^[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+$/,
);
expect(res.body.data.refresh_token).toMatch(
/^[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+$/,
);
});
});

it('/auth/kakao-oauth (POST) received wrong kakao user id', () => {
fetchSpy.mockResolvedValue({
json: async () => null,
ok: false,
});

return request(app.getHttpServer())
.post('/auth/kakao-oauth')
.send({ kakaoUserId: 1 })
.expect(HttpStatus.NOT_FOUND)
.expect({ statusCode: HttpStatus.NOT_FOUND, message: 'Not Found' });
});
});

0 comments on commit 39dfd5a

Please sign in to comment.