Skip to content

Commit

Permalink
Merge pull request #238 from Quickchive/test-#237/kakao-login
Browse files Browse the repository at this point in the history
Test #237/kakao login
  • Loading branch information
stae1102 authored Jan 28, 2024
2 parents dc45dd8 + 5c94989 commit 50a57f1
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.preferences.importModuleSpecifier": "project-relative"
"typescript.preferences.importModuleSpecifier": "relative"
}
1 change: 0 additions & 1 deletion src/auth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class OAuthController {
})
@Get('kakao-login')
async kakaoOauth(@Query('code') code: string): Promise<LoginOutput> {
console.log(code);
return this.oauthService.kakaoOauth({ code });
}

Expand Down
14 changes: 12 additions & 2 deletions test/auth/get-kakao-auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@ 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';

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 } = await getBuilder();
const {
builder,
container: _container,
dataSource: _dataSource,
} = await getBuilder();
container = _container;
dataSource = _dataSource;

const module = await builder.compile();
const module = await builder
.overrideProvider(DataSource)
.useValue(dataSource)
.compile();

app = module.createNestApplication();
await app.init();
Expand Down
161 changes: 161 additions & 0 deletions test/auth/get-kakao-login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { getBuilder } from '../common/application-builder';
import {
BadRequestException,
CACHE_MANAGER,
HttpStatus,
INestApplication,
UnauthorizedException,
} from '@nestjs/common';
import { StartedPostgreSqlContainer } from '@testcontainers/postgresql';
import { OAuthUtil } from '../../src/auth/util/oauth.util';
import { User } from '../../src/users/entities/user.entity';
import * as request from 'supertest';
import { cacheManagerMock } from '../mock/cache-manager.mock';
import { oAuthUtilMock } from '../mock/oauth-util.mock';
import { Seeder } from '../seeder/seeder.interface';
import { UserSeeder } from '../seeder/user.seeder';
import { DataSource, Repository } from 'typeorm';

jest.setTimeout(30_000);
describe('[GET] /api/oauth/kakao-login', () => {
// Application
let app: INestApplication;
let container: StartedPostgreSqlContainer; // TODO 결합도 낮추기
let dataSource: DataSource;

// Service
let oauthUtil: OAuthUtil;

// Database
let userRepository: Repository<User>;

// Seeder
const userSeeder: Seeder<User> = new UserSeeder();

// Stub
let userStub: User;

beforeAll(async () => {
const {
builder,
container: _container,
dataSource: _dataSource,
} = await getBuilder();
container = _container;
dataSource = _dataSource;

const module = await builder
.overrideProvider(DataSource)
.useValue(dataSource)
.overrideProvider(OAuthUtil)
.useValue(oAuthUtilMock)
.overrideProvider(CACHE_MANAGER)
.useValue(cacheManagerMock)
.compile();

app = module.createNestApplication();
await app.init();

oauthUtil = app.get(OAuthUtil);
userRepository = dataSource.getRepository(User);
});

beforeEach(async () => {
await dataSource.synchronize(true);
});

afterAll(async () => {
await app.close();
await container.stop();
});

/**
* 1. 카카오 로그인에 성공한다.
* 2. 카카오 인증을 받지 못해 실패한다.
* 3. 이메일 수집에 동의하지 않아 실패한다.
*/

describe('카카오 로그인에 성공한다.', () => {
it('200을 반환한다.', async () => {
const emailStub = 'test@email.com';
const nicknameStub = 'test';
oauthUtil.getKakaoAccessToken = jest
.fn()
.mockImplementationOnce(async () => ({ access_token: '' }));
oauthUtil.getKakaoUserInfo = jest
.fn()
.mockImplementationOnce(async () => ({
userInfo: {
kakao_account: {
email: emailStub,
profile: { nickname: nicknameStub },
},
},
}));

const { status, body } = await request(app.getHttpServer())
.get('/oauth/kakao-login')
.query({ code: '' });

expect(status).toBe(HttpStatus.OK);
expect(typeof body.access_token).toBe('string');
expect(typeof body.refresh_token).toBe('string');
});
});

describe('카카오 인증을 받지 못해 실패한다.', () => {
it('401 예외를 던진다.', async () => {
oauthUtil.getKakaoAccessToken = jest
.fn()
.mockImplementationOnce(async () => {
throw new UnauthorizedException();
});

const { status } = await request(app.getHttpServer())
.get('/oauth/kakao-login')
.query({ code: '' });

expect(status).toBe(HttpStatus.UNAUTHORIZED);
});

it('400 예외를 던진다.', async () => {
oauthUtil.getKakaoAccessToken = jest
.fn()
.mockImplementationOnce(async () => {
throw new BadRequestException();
});

const { status } = await request(app.getHttpServer())
.get('/oauth/kakao-login')
.query({ code: '' });

expect(status).toBe(HttpStatus.BAD_REQUEST);
});
});

describe('이메일 동의를 하지 않아 실패한다.', () => {
it('400 예외를 던진다.', async () => {
const nicknameStub = 'test';
oauthUtil.getKakaoAccessToken = jest
.fn()
.mockImplementationOnce(async () => ({ access_token: '' }));
oauthUtil.getKakaoUserInfo = jest
.fn()
.mockImplementationOnce(async () => ({
userInfo: {
kakao_account: {
email: null,
profile: { nickname: nicknameStub },
},
},
}));

const { status, body } = await request(app.getHttpServer())
.get('/oauth/kakao-login')
.query({ code: '' });

expect(status).toBe(HttpStatus.BAD_REQUEST);
expect(body.message).toBe('Please Agree to share your email');
});
});
});
4 changes: 4 additions & 0 deletions test/mock/oauth-util.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const oAuthUtilMock = {
getKakaoAccessToken: jest.fn(),
getKakaoUserInfo: jest.fn(),
};

0 comments on commit 50a57f1

Please sign in to comment.