-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
57 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
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 |
---|---|---|
@@ -1,16 +1,20 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { JwtModule, JwtService } from '@nestjs/jwt'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { NotFoundException } from '@nestjs/common'; | ||
import { RefreshToken } from '@prisma/client'; | ||
import { AuthService } from './auth.service'; | ||
import { RefreshTokensService } from '../refresh-tokens/refresh-tokens.service'; | ||
import { UsersService } from '../users/users.service'; | ||
import { ProfilesService } from '../profiles/profiles.service'; | ||
|
||
const fetchSpy = jest.spyOn(global, 'fetch'); | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
let jwtService: JwtService; | ||
let refreshTokensService: RefreshTokensService; | ||
let usersService: UsersService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
|
@@ -24,6 +28,14 @@ describe('AuthService', () => { | |
findRefreshToken: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: UsersService, | ||
useValue: { getOrCreateUser: jest.fn() }, | ||
}, | ||
{ | ||
provide: ProfilesService, | ||
useValue: { getOrCreateProfile: jest.fn() }, | ||
}, | ||
], | ||
}) | ||
.overrideProvider(JwtService) | ||
|
@@ -34,12 +46,41 @@ describe('AuthService', () => { | |
jwtService = module.get<JwtService>(JwtService); | ||
refreshTokensService = | ||
module.get<RefreshTokensService>(RefreshTokensService); | ||
usersService = module.get<UsersService>(UsersService); | ||
}); | ||
|
||
afterEach(() => { | ||
fetchSpy.mockRestore(); | ||
}); | ||
|
||
describe('kakaoLogin', () => { | ||
const kakaoUserId = 0; | ||
const kakaoUser = { email: '[email protected]' }; | ||
const user = { uuid: 'user uuid' }; | ||
const tokens = { | ||
access_token: 'access token', | ||
refresh_token: 'refresh token', | ||
}; | ||
|
||
it('kakao user exist', async () => { | ||
jest.spyOn(service, 'getKakaoAccount').mockResolvedValue(kakaoUser); | ||
jest.spyOn(service, 'login').mockResolvedValue(tokens); | ||
(usersService.getOrCreateUser as jest.Mock).mockResolvedValue(user); | ||
|
||
const tokenData = service.kakaoLogin(kakaoUserId); | ||
|
||
await expect(tokenData).resolves.toEqual(tokens); | ||
}); | ||
|
||
it('kakao user not exist', async () => { | ||
jest.spyOn(service, 'getKakaoAccount').mockResolvedValue(null); | ||
|
||
const tokenData = service.kakaoLogin(kakaoUserId); | ||
|
||
await expect(tokenData).rejects.toThrow(NotFoundException); | ||
}); | ||
}); | ||
|
||
it('login success', async () => { | ||
jest.spyOn(jwtService, 'signAsync').mockResolvedValue('access token'); | ||
jest.spyOn(refreshTokensService, 'createRefreshToken').mockResolvedValue({ | ||
|
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