add-nest-auth 1.3.1 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,11 @@ import { RefreshToken } from '../users/entities/refresh-token.entity';
|
|
|
12
12
|
{{/if}}
|
|
13
13
|
{{/if}}
|
|
14
14
|
|
|
15
|
+
jest.mock('bcrypt', () => ({
|
|
16
|
+
compare: jest.fn(),
|
|
17
|
+
hash: jest.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
15
20
|
describe('AuthService', () => {
|
|
16
21
|
let authService: AuthService;
|
|
17
22
|
let usersService: UsersService;
|
|
@@ -110,7 +115,7 @@ describe('AuthService', () => {
|
|
|
110
115
|
describe('validateUser', () => {
|
|
111
116
|
it('should return user without password for valid credentials', async () => {
|
|
112
117
|
mockUsersService.findByEmail.mockResolvedValue(mockUser);
|
|
113
|
-
|
|
118
|
+
(bcrypt.compare as jest.Mock).mockResolvedValue(true);
|
|
114
119
|
|
|
115
120
|
const result = await authService.validateUser('test@example.com', 'password123');
|
|
116
121
|
|
|
@@ -129,7 +134,7 @@ describe('AuthService', () => {
|
|
|
129
134
|
|
|
130
135
|
it('should return null if password is invalid', async () => {
|
|
131
136
|
mockUsersService.findByEmail.mockResolvedValue(mockUser);
|
|
132
|
-
|
|
137
|
+
(bcrypt.compare as jest.Mock).mockResolvedValue(false);
|
|
133
138
|
|
|
134
139
|
const result = await authService.validateUser('test@example.com', 'wrongpassword');
|
|
135
140
|
|
|
@@ -139,9 +144,8 @@ describe('AuthService', () => {
|
|
|
139
144
|
|
|
140
145
|
describe('login', () => {
|
|
141
146
|
it('should return access token and user for valid credentials', async () => {
|
|
142
|
-
const userWithoutPassword = { id: mockUser.id, email: mockUser.email{{#if features.useUsername}}, username: mockUser.username{{/if}}{{#if rbac.enabled}}, roles: mockUser.roles{{/if}} };
|
|
143
147
|
mockUsersService.findByEmail.mockResolvedValue(mockUser);
|
|
144
|
-
|
|
148
|
+
(bcrypt.compare as jest.Mock).mockResolvedValue(true);
|
|
145
149
|
{{#if features.refreshTokens}}
|
|
146
150
|
mockJwtService.sign.mockReturnValue('mock-jwt-token');
|
|
147
151
|
{{#if (eq orm "typeorm")}}
|
|
@@ -169,7 +173,7 @@ describe('AuthService', () => {
|
|
|
169
173
|
it('should create user and return access token', async () => {
|
|
170
174
|
const newUser = { ...mockUser };
|
|
171
175
|
mockUsersService.create.mockResolvedValue(newUser);
|
|
172
|
-
|
|
176
|
+
(bcrypt.hash as jest.Mock).mockResolvedValue('$2b$10$hashedpassword');
|
|
173
177
|
{{#if features.refreshTokens}}
|
|
174
178
|
mockJwtService.sign.mockReturnValue('mock-jwt-token');
|
|
175
179
|
{{#if (eq orm "typeorm")}}
|
|
@@ -195,8 +199,8 @@ describe('AuthService', () => {
|
|
|
195
199
|
describe('changePassword', () => {
|
|
196
200
|
it('should update password when current password is valid', async () => {
|
|
197
201
|
mockUsersService.findById.mockResolvedValue(mockUser);
|
|
198
|
-
|
|
199
|
-
|
|
202
|
+
(bcrypt.compare as jest.Mock).mockResolvedValue(true);
|
|
203
|
+
(bcrypt.hash as jest.Mock).mockResolvedValue('$2b$10$newhashedpassword');
|
|
200
204
|
mockUsersService.updatePassword.mockResolvedValue(undefined);
|
|
201
205
|
|
|
202
206
|
await authService.changePassword('test-uuid', {
|
|
@@ -209,7 +213,7 @@ describe('AuthService', () => {
|
|
|
209
213
|
|
|
210
214
|
it('should throw UnauthorizedException for wrong current password', async () => {
|
|
211
215
|
mockUsersService.findById.mockResolvedValue(mockUser);
|
|
212
|
-
|
|
216
|
+
(bcrypt.compare as jest.Mock).mockResolvedValue(false);
|
|
213
217
|
|
|
214
218
|
await expect(
|
|
215
219
|
authService.changePassword('test-uuid', {
|
|
@@ -289,7 +293,7 @@ describe('AuthService', () => {
|
|
|
289
293
|
const futureDate = new Date();
|
|
290
294
|
futureDate.setHours(futureDate.getHours() + 1);
|
|
291
295
|
mockUsersService.findByResetToken.mockResolvedValue({ ...mockUser, passwordResetExpires: futureDate });
|
|
292
|
-
|
|
296
|
+
(bcrypt.hash as jest.Mock).mockResolvedValue('$2b$10$newhashedpassword');
|
|
293
297
|
mockUsersService.updatePassword.mockResolvedValue(undefined);
|
|
294
298
|
mockUsersService.clearResetToken.mockResolvedValue(undefined);
|
|
295
299
|
|