Skip to content

Commit

Permalink
Merge pull request #242 from Quickchive/test-#240/google-login
Browse files Browse the repository at this point in the history
Test #240/google login
  • Loading branch information
stae1102 authored Jan 28, 2024
2 parents c28f6e1 + 99c9605 commit 7060c15
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/auth/get-google-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 결합도 낮추기
Expand Down
67 changes: 67 additions & 0 deletions test/auth/get-google-login.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
6 changes: 4 additions & 2 deletions test/mock/google-auth-guard.mock.ts
Original file line number Diff line number Diff line change
@@ -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;
},
Expand Down

0 comments on commit 7060c15

Please sign in to comment.