Skip to content

Commit

Permalink
Merge pull request #241 from Quickchive/test-#239/google-auth
Browse files Browse the repository at this point in the history
test: create get googla auth e2e test
  • Loading branch information
stae1102 authored Jan 28, 2024
2 parents 50a57f1 + 10bd9b1 commit c28f6e1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/auth/get-google-auth.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
15 changes: 15 additions & 0 deletions test/mock/google-auth-guard.mock.ts
Original file line number Diff line number Diff line change
@@ -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;
},
});

0 comments on commit c28f6e1

Please sign in to comment.