diff --git a/test/auth/get-google-auth.spec.ts b/test/auth/get-google-auth.spec.ts new file mode 100644 index 0000000..f190abf --- /dev/null +++ b/test/auth/get-google-auth.spec.ts @@ -0,0 +1,60 @@ +import { AuthGuard } from '@nestjs/passport'; +import { getBuilder } from '../common/application-builder'; +import { HttpStatus, INestApplication } from '@nestjs/common'; +import { StartedPostgreSqlContainer } from '@testcontainers/postgresql'; +import * as request from 'supertest'; +import { DataSource } from 'typeorm'; +import { googleAuthGuardMock } from '../mock/google-auth-guard.mock'; + +const emailStub = 'test@email.com'; +const nameStub = 'test'; +const accessTokenStub = 'testToken'; + +jest.setTimeout(30_000); +describe('[GET] /api/oauth/kakao-auth', () => { + // Application + let app: INestApplication; + let container: StartedPostgreSqlContainer; // TODO 결합도 낮추기 + let dataSource: DataSource; + + beforeAll(async () => { + const { + builder, + container: _container, + dataSource: _dataSource, + } = await getBuilder(); + container = _container; + dataSource = _dataSource; + + const module = await builder + .overrideProvider(DataSource) + .useValue(dataSource) + .overrideGuard(AuthGuard('google')) + .useValue( + googleAuthGuardMock({ + email: emailStub, + name: nameStub, + accessToken: accessTokenStub, + }), + ) + .compile(); + + app = module.createNestApplication(); + await app.init(); + }); + + afterAll(async () => { + await app.close(); + await container.stop(); + }); + + describe('구글 계정 로그인 요청에 성공한다.', () => { + it('302를 반환한다.', async () => { + const { status } = await request(app.getHttpServer()).get( + '/oauth/google-auth', + ); + + expect(status).toBe(HttpStatus.FOUND); + }); + }); +}); diff --git a/test/mock/google-auth-guard.mock.ts b/test/mock/google-auth-guard.mock.ts new file mode 100644 index 0000000..23b2708 --- /dev/null +++ b/test/mock/google-auth-guard.mock.ts @@ -0,0 +1,15 @@ +import { ExecutionContext } from '@nestjs/common'; +import { googleUserInfo } from '../../src/auth/dtos/google.dto'; + +export const googleAuthGuardMock = (googleUserInfo: googleUserInfo) => ({ + canActivate: (context: ExecutionContext) => { + const request = context.switchToHttp().getRequest(); + const response = context.switchToHttp().getResponse(); + + request.user = googleUserInfo; + + response.redirect(); + + return true; + }, +});