From d9419e2fdf1c317b5bdbacebb8b1a1724b928019 Mon Sep 17 00:00:00 2001 From: Seongtae Date: Sun, 28 Jan 2024 18:35:59 +0900 Subject: [PATCH 1/2] fix: add argument to google auth guard mock --- test/mock/google-auth-guard.mock.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/mock/google-auth-guard.mock.ts b/test/mock/google-auth-guard.mock.ts index 23b2708..8aa95b0 100644 --- a/test/mock/google-auth-guard.mock.ts +++ b/test/mock/google-auth-guard.mock.ts @@ -1,14 +1,16 @@ import { ExecutionContext } from '@nestjs/common'; import { googleUserInfo } from '../../src/auth/dtos/google.dto'; -export const googleAuthGuardMock = (googleUserInfo: googleUserInfo) => ({ +export const googleAuthGuardMock = (googleUserInfo?: googleUserInfo) => ({ canActivate: (context: ExecutionContext) => { const request = context.switchToHttp().getRequest(); const response = context.switchToHttp().getResponse(); request.user = googleUserInfo; - response.redirect(); + if (!googleUserInfo) { + response.redirect(); + } return true; }, From 99c96054eb4501ae8a9b2b21279e51835ec7472c Mon Sep 17 00:00:00 2001 From: Seongtae Date: Sun, 28 Jan 2024 18:36:11 +0900 Subject: [PATCH 2/2] test: create get google login e2e test --- test/auth/get-google-auth.spec.ts | 2 +- test/auth/get-google-login.spec.ts | 67 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/auth/get-google-login.spec.ts diff --git a/test/auth/get-google-auth.spec.ts b/test/auth/get-google-auth.spec.ts index f190abf..2511be6 100644 --- a/test/auth/get-google-auth.spec.ts +++ b/test/auth/get-google-auth.spec.ts @@ -11,7 +11,7 @@ const nameStub = 'test'; const accessTokenStub = 'testToken'; jest.setTimeout(30_000); -describe('[GET] /api/oauth/kakao-auth', () => { +describe('[GET] /api/oauth/google-auth', () => { // Application let app: INestApplication; let container: StartedPostgreSqlContainer; // TODO 결합도 낮추기 diff --git a/test/auth/get-google-login.spec.ts b/test/auth/get-google-login.spec.ts new file mode 100644 index 0000000..4e3fbd9 --- /dev/null +++ b/test/auth/get-google-login.spec.ts @@ -0,0 +1,67 @@ +import { AuthGuard } from '@nestjs/passport'; +import { getBuilder } from '../common/application-builder'; +import { CACHE_MANAGER, 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'; +import { cacheManagerMock } from '../mock/cache-manager.mock'; + +const emailStub = 'test@email.com'; +const nameStub = 'test'; +const accessTokenStub = 'testToken'; + +jest.setTimeout(30_000); +describe('[GET] /api/oauth/google-login', () => { + // 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, + }), + ) + .overrideProvider(CACHE_MANAGER) + .useValue(cacheManagerMock) + .compile(); + + app = module.createNestApplication(); + await app.init(); + }); + + afterAll(async () => { + await app.close(); + await container.stop(); + }); + + describe('구글 로그인에 성공한다', () => { + it('토큰과 함께 200을 반환한다.', async () => { + const { status, body } = await request(app.getHttpServer()).get( + '/oauth/google-login', + ); + + console.log(body); + + expect(status).toBe(HttpStatus.OK); + expect(typeof body.access_token).toBe('string'); + expect(typeof body.refresh_token).toBe('string'); + }); + }); +});