-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauth.controller.spec.ts
143 lines (119 loc) ยท 4.83 KB
/
auth.controller.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
import { PrismaService } from '../prisma/prisma.service';
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';
import { PrismaClient, RefreshToken, User } from '@prisma/client';
import { AuthService } from './auth.service';
import { UsersService } from '../users/users.service';
import { RefreshTokensService } from './refresh-tokens.service';
import { ProfilesService } from '../profiles/profiles.service';
import { BadRequestException, NotFoundException } from '@nestjs/common';
describe('AuthController', () => {
let controller: AuthController;
let refreshTokensService: DeepMockProxy<RefreshTokensService>;
let usersService: DeepMockProxy<UsersService>;
let authService: DeepMockProxy<AuthService>;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [
AuthService,
PrismaService,
UsersService,
ProfilesService,
RefreshTokensService,
],
})
.overrideProvider(AuthService)
.useValue(mockDeep<AuthService>())
.overrideProvider(PrismaService)
.useValue(mockDeep<PrismaClient>())
.overrideProvider(UsersService)
.useValue(mockDeep<UsersService>())
.overrideProvider(ProfilesService)
.useValue(mockDeep<ProfilesService>())
.overrideProvider(RefreshTokensService)
.useValue(mockDeep<RefreshTokensService>())
.compile();
controller = module.get<AuthController>(AuthController);
refreshTokensService = module.get(RefreshTokensService);
authService = module.get(AuthService);
usersService = module.get(UsersService);
});
it('kakaoLogin user have been logged in', async () => {
const requestMock = { kakaoUserId: 0 };
const kakaoUserAccountMock = { email: 'kakao email' };
const tokenMock = {
refresh_token: 'refresh token',
access_token: 'access token',
};
authService.getKakaoAccount.mockResolvedValue(kakaoUserAccountMock);
usersService.findUserByEmailAndProvider.mockResolvedValue({
uuid: 'user uuid',
} as User);
authService.login.mockResolvedValue(tokenMock);
const response = controller.kakaoLogin(requestMock);
await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: tokenMock,
});
expect(usersService.createUser).not.toHaveBeenCalled();
});
it('kakaoLogin user login first time', async () => {
const requestMock = { kakaoUserId: 0 };
const kakaoUserAccountMock = { email: 'kakao email' };
const tokenMock = {
refresh_token: 'refresh token',
access_token: 'access token',
};
authService.getKakaoAccount.mockResolvedValue(kakaoUserAccountMock);
usersService.createUser.mockResolvedValue({ uuid: 'user uuid' } as User);
authService.login.mockResolvedValue(tokenMock);
const response = controller.kakaoLogin(requestMock);
await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: tokenMock,
});
expect(usersService.createUser).toHaveBeenCalled();
});
it('kakaoLogin kakao login fail', async () => {
const requestMock = { kakaoUserId: 0 };
authService.getKakaoAccount.mockResolvedValue(null);
const response = controller.kakaoLogin(requestMock);
await expect(response).rejects.toThrow(NotFoundException);
});
it('renewAccessToken respond new access token', async () => {
const requestMock = { refresh_token: 'refresh token' };
authService.renewAccessToken.mockResolvedValue('new access token');
const response = controller.renewAccessToken(requestMock);
await expect(response).resolves.toEqual({
statusCode: 200,
message: 'Success',
data: { access_token: 'new access token' },
});
});
it('renewAccessToken received expired token', async () => {
const requestMock = { refresh_token: 'refresh token' };
authService.renewAccessToken.mockRejectedValue(new Error());
const response = controller.renewAccessToken(requestMock);
await expect(response).rejects.toThrow(Error);
});
it('logout received token deleted', async () => {
const requestMock = { refresh_token: 'refresh token' };
const token = {} as RefreshToken;
refreshTokensService.deleteRefreshToken.mockResolvedValue(token);
const response = controller.logout(requestMock);
await expect(response).resolves.toEqual({
statusCode: 204,
message: 'No Content',
});
});
it('logout received token not found', async () => {
const requestMock = { refresh_token: 'bad refresh token' };
refreshTokensService.deleteRefreshToken.mockResolvedValue(null);
const response = controller.logout(requestMock);
await expect(response).rejects.toThrow(BadRequestException);
});
});