|
| 1 | +import { HttpStatus, INestApplication } from '@nestjs/common'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { AuthModule } from '../src/auth/auth.module'; |
| 4 | +import * as request from 'supertest'; |
| 5 | +import { ConfigModule } from '@nestjs/config'; |
| 6 | + |
| 7 | +describe('AuthController (e2e)', () => { |
| 8 | + let app: INestApplication; |
| 9 | + let fetchSpy: jest.SpyInstance; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + fetchSpy = jest.spyOn(global, 'fetch'); |
| 13 | + }); |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 17 | + imports: [AuthModule, ConfigModule.forRoot({ isGlobal: true })], |
| 18 | + }).compile(); |
| 19 | + |
| 20 | + app = moduleFixture.createNestApplication(); |
| 21 | + |
| 22 | + await app.init(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + fetchSpy.mockRestore(); |
| 27 | + |
| 28 | + await app.close(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('/auth/kakao-oauth (POST)', () => { |
| 32 | + fetchSpy.mockResolvedValue({ |
| 33 | + json: async () => { |
| 34 | + return { kakao_account: { email: '[email protected]' } }; |
| 35 | + }, |
| 36 | + ok: true, |
| 37 | + }); |
| 38 | + |
| 39 | + return request(app.getHttpServer()) |
| 40 | + .post('/auth/kakao-oauth') |
| 41 | + .send({ kakaoUserId: 1 }) |
| 42 | + .expect(HttpStatus.CREATED) |
| 43 | + .expect((res) => { |
| 44 | + expect(res.body.statusCode).toBe(HttpStatus.OK); |
| 45 | + expect(res.body.message).toBe('Success'); |
| 46 | + expect(res.body.data.access_token).toMatch( |
| 47 | + /^[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+$/, |
| 48 | + ); |
| 49 | + expect(res.body.data.refresh_token).toMatch( |
| 50 | + /^[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+?\.[A-Za-z0-9-_]+$/, |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('/auth/kakao-oauth (POST) received wrong kakao user id', () => { |
| 56 | + fetchSpy.mockResolvedValue({ |
| 57 | + json: async () => null, |
| 58 | + ok: false, |
| 59 | + }); |
| 60 | + |
| 61 | + return request(app.getHttpServer()) |
| 62 | + .post('/auth/kakao-oauth') |
| 63 | + .send({ kakaoUserId: 1 }) |
| 64 | + .expect(HttpStatus.NOT_FOUND) |
| 65 | + .expect({ statusCode: HttpStatus.NOT_FOUND, message: 'Not Found' }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments