-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(#520): OAuth Signin 테스트 코드 추가 완료
- Loading branch information
1 parent
58698d1
commit 778094c
Showing
13 changed files
with
240 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const OAuthProvider = { | ||
KAKAO: 'KAKAO', | ||
GOOGLE: 'GOOGLE', | ||
NAVER: 'NAVER', | ||
} as const; | ||
|
||
export type OAuthProvider = (typeof OAuthProvider)[keyof typeof OAuthProvider]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ApiOperator } from '@src/common/types/type'; | ||
import { OperationObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface'; | ||
import { HttpStatus, applyDecorators } from '@nestjs/common'; | ||
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; | ||
import { ExceptionResponseDto } from '@src/common/swagger/dtos/exeption-response.dto'; | ||
import { PaginationResponseDto } from '@src/common/swagger/dtos/pagination-response.dto'; | ||
import { LecturerPaymentItemDto } from '@src/payments/dtos/response/lecturer-payment-item.dto'; | ||
import { AuthOAuthController } from '../auth-oauth.controller'; | ||
|
||
export const ApiOAuth: ApiOperator<keyof AuthOAuthController> = { | ||
SignIn: ( | ||
apiOperationOptions: Required<Pick<Partial<OperationObject>, 'summary'>> & | ||
Partial<OperationObject>, | ||
): PropertyDecorator => { | ||
return applyDecorators( | ||
ApiOperation(apiOperationOptions), | ||
ApiBearerAuth(), | ||
PaginationResponseDto.swaggerBuilder( | ||
HttpStatus.OK, | ||
'lecturerPaymentList', | ||
LecturerPaymentItemDto, | ||
), | ||
ExceptionResponseDto.swaggerBuilder(HttpStatus.BAD_REQUEST, [ | ||
{ | ||
error: 'differentSignUpMethod', | ||
description: '다른 방식으로 가입된 이메일 입니다.', | ||
}, | ||
]), | ||
ExceptionResponseDto.swaggerBuilder(HttpStatus.INTERNAL_SERVER_ERROR, [ | ||
{ | ||
error: 'oAuthServerError', | ||
description: 'OAuth 서버 요청 중 오류가 발생했습니다.', | ||
}, | ||
]), | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ValidatorConstraint } from 'class-validator'; | ||
import { BadRequestException, PipeTransform } from '@nestjs/common'; | ||
import { OAuthProvider } from '../constants/const'; | ||
|
||
@ValidatorConstraint() | ||
export class ProviderValidator implements PipeTransform { | ||
transform(provider: string): OAuthProvider { | ||
const convertedProvider = OAuthProvider[provider.toUpperCase()]; | ||
if (!OAuthProvider.hasOwnProperty(convertedProvider)) { | ||
throw new BadRequestException( | ||
'올바르지 않은 OAuthProvider 입니다.', | ||
'InvalidProvider', | ||
); | ||
} | ||
|
||
return convertedProvider; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { AppModule } from '@src/app.module'; | ||
import axios from 'axios'; | ||
import { testUserSignin } from '@test/features/auth/test-user-signin'; | ||
import { randomInt } from 'crypto'; | ||
import { v4 } from 'uuid'; | ||
import { testCreateUser } from '@test/features/user/test-create-user'; | ||
import { OAuthProvider } from '@src/auth/constants/const'; | ||
|
||
jest.mock('axios'); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
describe('AuthOAuthController (e2e)', () => { | ||
let server: INestApplication; | ||
const PORT = randomInt(20000, 50000); | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
server = moduleFixture.createNestApplication(); | ||
await server.init(); | ||
await server.listen(PORT); | ||
}); | ||
|
||
afterAll(async () => { | ||
await server.close(); | ||
}); | ||
|
||
describe('/auth/oauth/signin/kakao (GET)', () => { | ||
//소셜 로그인 테스트 | ||
it('새로운 사용자일 경우 authEmail을 반환해야 한다', async () => { | ||
const testEmail = v4() + '@example.com'; | ||
mockedAxios.post.mockResolvedValue({ | ||
data: { kakao_account: { email: testEmail } }, | ||
}); | ||
|
||
const response = await testUserSignin(PORT, OAuthProvider.KAKAO); | ||
|
||
expect(response.authEmail).toEqual(testEmail); | ||
}); | ||
|
||
// 기존 사용자일 경우 액세스 토큰을 반환하는 테스트 | ||
it('기존 사용자일 경우 액세스 토큰을 반환해야 한다', async () => { | ||
const testEmail = v4() + '@example.com'; | ||
|
||
await testCreateUser(PORT, { | ||
provider: OAuthProvider.KAKAO, | ||
email: testEmail, | ||
}); | ||
|
||
mockedAxios.post.mockResolvedValue({ | ||
data: { kakao_account: { email: testEmail } }, | ||
}); | ||
const response = await testUserSignin(PORT, OAuthProvider.KAKAO); | ||
|
||
expect(response.userAccessToken).toBeDefined(); | ||
expect(typeof response.userAccessToken).toBe('string'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { OAuthProvider } from '@src/auth/constants/const'; | ||
import { AuthOAuthController } from '@src/auth/controllers/auth-oauth.controller'; | ||
import { v4 } from 'uuid'; | ||
|
||
/** | ||
* 유저 OAuth 로그인 테스트 | ||
* 테스트 환경에서는 OAuth 서버와 통신하지 않는다. | ||
* Axios 요청을 모킹하여 OAuth 프로바이더에 맞게 반환값을 할당해야한다. | ||
* OAuth Server는 성공한다는 가정하에 비즈니스 로직 성공 보장 | ||
* | ||
* @param PORT 포트번호 | ||
* @param provider 로그인 할 OAuth provider | ||
* @returns 로그인에 성공하면 토큰을 발급받는다. | ||
*/ | ||
export const testUserSignin = async ( | ||
PORT: number, | ||
provider: OAuthProvider, | ||
): Promise<ReturnType<AuthOAuthController['signIn']>> => { | ||
const accessToken = v4(); | ||
const url = `http://localhost:${PORT}/auth/oauth/signin/${ | ||
provider ?? 'kakao' | ||
}?access-token=${accessToken}`; | ||
|
||
const response = await fetch(url, { | ||
method: 'GET', | ||
}); | ||
const responseBody = await response.json(); | ||
|
||
return responseBody as Awaited<ReturnType<AuthOAuthController['signIn']>>; | ||
}; |
Oops, something went wrong.