@flusys/nestjs-auth 7.0.2 → 8.0.0-beta.1
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.
- package/README.md +68 -1
- package/config/auth.constants.d.ts +2 -0
- package/config/event-actions.d.ts +2 -0
- package/config/index.d.ts +3 -3
- package/config/message-keys.d.ts +23 -0
- package/controllers/authentication.controller.d.ts +5 -5
- package/controllers/branch.controller.d.ts +2 -2
- package/controllers/company-selection.controller.d.ts +4 -4
- package/controllers/company.controller.d.ts +2 -2
- package/controllers/email-verification.controller.d.ts +6 -6
- package/controllers/index.d.ts +9 -7
- package/controllers/social-auth-config.controller.d.ts +22 -0
- package/controllers/social-auth.controller.d.ts +11 -0
- package/controllers/user-permission.controller.d.ts +5 -4
- package/controllers/user.controller.d.ts +7 -7
- package/docs/auth-swagger.config.d.ts +2 -2
- package/docs/index.d.ts +1 -1
- package/dtos/index.d.ts +6 -5
- package/dtos/social-auth.dto.d.ts +42 -0
- package/entities/company-branch.entity.d.ts +1 -1
- package/entities/company.entity.d.ts +1 -1
- package/entities/index.d.ts +15 -13
- package/entities/pending-registration.entity.d.ts +1 -1
- package/entities/social-auth-config.entity.d.ts +9 -0
- package/entities/user-company-permission.entity.d.ts +4 -4
- package/enums/index.d.ts +2 -1
- package/enums/social-provider-type.enum.d.ts +6 -0
- package/fesm/118.js +1230 -0
- package/fesm/120.js +2197 -0
- package/fesm/24.js +4389 -0
- package/fesm/370.js +1163 -0
- package/fesm/446.js +877 -0
- package/fesm/chunks/117.js +69 -0
- package/fesm/config/index.js +244 -3
- package/fesm/controllers/index.js +27 -7
- package/fesm/docs/index.js +479 -1
- package/fesm/dtos/index.js +123 -5
- package/fesm/entities/index.js +68 -19
- package/fesm/enums/index.js +53 -1
- package/fesm/helpers/index.js +115 -2
- package/fesm/index.js +1437 -12
- package/fesm/interceptors/index.js +508 -2
- package/fesm/interfaces/index.js +75 -7
- package/fesm/modules/index.js +546 -1
- package/fesm/runtime.js +134 -0
- package/fesm/services/index.js +67 -9
- package/helpers/auth-response.mapper.d.ts +3 -3
- package/helpers/index.d.ts +2 -2
- package/helpers/token.helper.d.ts +2 -2
- package/index.d.ts +13 -12
- package/interceptors/clear-token.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +2 -2
- package/interceptors/set-token.interceptor.d.ts +2 -2
- package/interfaces/auth-module-options.interface.d.ts +3 -3
- package/interfaces/authentication.interface.d.ts +3 -3
- package/interfaces/company-branch.interface.d.ts +2 -2
- package/interfaces/company.interface.d.ts +1 -1
- package/interfaces/index.d.ts +8 -7
- package/interfaces/social-auth.interface.d.ts +43 -0
- package/interfaces/user-enricher.interface.d.ts +3 -3
- package/modules/auth.module.d.ts +4 -4
- package/modules/index.d.ts +1 -1
- package/package.json +32 -39
- package/providers/facebook.social-provider.d.ts +10 -0
- package/providers/google.social-provider.d.ts +10 -0
- package/providers/index.d.ts +1 -0
- package/providers/linkedin.social-provider.d.ts +10 -0
- package/providers/microsoft.social-provider.d.ts +10 -0
- package/providers/oauth-http.util.d.ts +10 -0
- package/providers/social-provider.registry.d.ts +3 -0
- package/services/auth-config.service.d.ts +4 -3
- package/services/auth-datasource.provider.d.ts +4 -3
- package/services/auth-email.service.d.ts +3 -3
- package/services/authentication.service.d.ts +15 -11
- package/services/branch.service.d.ts +7 -7
- package/services/company.service.d.ts +6 -6
- package/services/index.d.ts +11 -9
- package/services/social-auth-config.service.d.ts +21 -0
- package/services/social-auth.service.d.ts +24 -0
- package/services/user-permission.service.d.ts +8 -4
- package/services/user.service.d.ts +7 -7
- package/strategies/jwt.strategy.d.ts +4 -4
- package/validators/password.validator.d.ts +1 -1
- package/cjs/config/auth.constants.js +0 -41
- package/cjs/config/event-actions.js +0 -45
- package/cjs/config/index.js +0 -20
- package/cjs/config/message-keys.js +0 -108
- package/cjs/controllers/authentication.controller.js +0 -427
- package/cjs/controllers/authentication.controller.spec.js +0 -219
- package/cjs/controllers/branch.controller.js +0 -102
- package/cjs/controllers/branch.controller.spec.js +0 -134
- package/cjs/controllers/company-selection.controller.js +0 -198
- package/cjs/controllers/company-selection.controller.spec.js +0 -84
- package/cjs/controllers/company.controller.js +0 -102
- package/cjs/controllers/company.controller.spec.js +0 -131
- package/cjs/controllers/email-verification.controller.js +0 -224
- package/cjs/controllers/email-verification.controller.spec.js +0 -142
- package/cjs/controllers/index.js +0 -24
- package/cjs/controllers/user-permission.controller.js +0 -223
- package/cjs/controllers/user-permission.controller.spec.js +0 -138
- package/cjs/controllers/user.controller.js +0 -438
- package/cjs/controllers/user.controller.spec.js +0 -204
- package/cjs/docs/auth-swagger.config.js +0 -453
- package/cjs/docs/auth-swagger.config.spec.js +0 -130
- package/cjs/docs/index.js +0 -18
- package/cjs/dtos/authentication.dto.js +0 -755
- package/cjs/dtos/company-branch.dto.js +0 -294
- package/cjs/dtos/company.dto.js +0 -298
- package/cjs/dtos/index.js +0 -22
- package/cjs/dtos/user-permission.dto.js +0 -358
- package/cjs/dtos/user.dto.js +0 -413
- package/cjs/entities/app-user.entity.js +0 -25
- package/cjs/entities/company-branch.entity.js +0 -163
- package/cjs/entities/company.entity.js +0 -146
- package/cjs/entities/index.js +0 -65
- package/cjs/entities/index.spec.js +0 -73
- package/cjs/entities/pending-registration.entity.js +0 -135
- package/cjs/entities/user-company-permission.entity.js +0 -153
- package/cjs/entities/user-token.entity.js +0 -120
- package/cjs/enums/index.js +0 -18
- package/cjs/enums/user-org-permission-type.enum.js +0 -15
- package/cjs/helpers/auth-response.mapper.js +0 -46
- package/cjs/helpers/auth-response.mapper.spec.js +0 -139
- package/cjs/helpers/index.js +0 -19
- package/cjs/helpers/token.helper.js +0 -58
- package/cjs/helpers/token.helper.spec.js +0 -86
- package/cjs/index.js +0 -29
- package/cjs/interceptors/clear-token.interceptor.js +0 -73
- package/cjs/interceptors/clear-token.interceptor.spec.js +0 -107
- package/cjs/interceptors/index.js +0 -19
- package/cjs/interceptors/set-token.interceptor.js +0 -107
- package/cjs/interceptors/set-token.interceptor.spec.js +0 -165
- package/cjs/interfaces/auth-email-provider.interface.js +0 -45
- package/cjs/interfaces/auth-module-options.interface.js +0 -4
- package/cjs/interfaces/authentication.interface.js +0 -4
- package/cjs/interfaces/company-branch.interface.js +0 -4
- package/cjs/interfaces/company.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -24
- package/cjs/interfaces/user-enricher.interface.js +0 -4
- package/cjs/interfaces/user.interface.js +0 -4
- package/cjs/modules/auth.module.js +0 -202
- package/cjs/modules/auth.module.spec.js +0 -243
- package/cjs/modules/index.js +0 -18
- package/cjs/services/auth-config.service.js +0 -110
- package/cjs/services/auth-config.service.spec.js +0 -97
- package/cjs/services/auth-datasource.provider.js +0 -134
- package/cjs/services/auth-datasource.provider.spec.js +0 -161
- package/cjs/services/auth-email.service.js +0 -539
- package/cjs/services/auth-email.service.spec.js +0 -456
- package/cjs/services/authentication.service.js +0 -1173
- package/cjs/services/authentication.service.spec.js +0 -962
- package/cjs/services/branch.service.js +0 -153
- package/cjs/services/branch.service.spec.js +0 -169
- package/cjs/services/company-selection-session.service.js +0 -121
- package/cjs/services/company-selection-session.service.spec.js +0 -90
- package/cjs/services/company.service.js +0 -140
- package/cjs/services/company.service.spec.js +0 -127
- package/cjs/services/index.js +0 -26
- package/cjs/services/user-permission.service.js +0 -236
- package/cjs/services/user-permission.service.spec.js +0 -254
- package/cjs/services/user.service.js +0 -671
- package/cjs/services/user.service.spec.js +0 -718
- package/cjs/strategies/jwt.strategy.js +0 -140
- package/cjs/strategies/jwt.strategy.spec.js +0 -166
- package/cjs/validators/password.validator.js +0 -45
- package/cjs/validators/password.validator.spec.js +0 -161
- package/fesm/config/auth.constants.js +0 -11
- package/fesm/config/event-actions.js +0 -24
- package/fesm/config/message-keys.js +0 -78
- package/fesm/controllers/authentication.controller.js +0 -417
- package/fesm/controllers/authentication.controller.spec.js +0 -215
- package/fesm/controllers/branch.controller.js +0 -92
- package/fesm/controllers/branch.controller.spec.js +0 -130
- package/fesm/controllers/company-selection.controller.js +0 -188
- package/fesm/controllers/company-selection.controller.spec.js +0 -80
- package/fesm/controllers/company.controller.js +0 -92
- package/fesm/controllers/company.controller.spec.js +0 -127
- package/fesm/controllers/email-verification.controller.js +0 -214
- package/fesm/controllers/email-verification.controller.spec.js +0 -138
- package/fesm/controllers/user-permission.controller.js +0 -213
- package/fesm/controllers/user-permission.controller.spec.js +0 -134
- package/fesm/controllers/user.controller.js +0 -428
- package/fesm/controllers/user.controller.spec.js +0 -200
- package/fesm/docs/auth-swagger.config.js +0 -446
- package/fesm/docs/auth-swagger.config.spec.js +0 -126
- package/fesm/dtos/authentication.dto.js +0 -683
- package/fesm/dtos/company-branch.dto.js +0 -279
- package/fesm/dtos/company.dto.js +0 -283
- package/fesm/dtos/user-permission.dto.js +0 -339
- package/fesm/dtos/user.dto.js +0 -391
- package/fesm/entities/app-user.entity.js +0 -15
- package/fesm/entities/company-branch.entity.js +0 -153
- package/fesm/entities/company.entity.js +0 -136
- package/fesm/entities/index.spec.js +0 -69
- package/fesm/entities/pending-registration.entity.js +0 -125
- package/fesm/entities/user-company-permission.entity.js +0 -136
- package/fesm/entities/user-token.entity.js +0 -104
- package/fesm/enums/user-org-permission-type.enum.js +0 -5
- package/fesm/helpers/auth-response.mapper.js +0 -25
- package/fesm/helpers/auth-response.mapper.spec.js +0 -135
- package/fesm/helpers/token.helper.js +0 -40
- package/fesm/helpers/token.helper.spec.js +0 -82
- package/fesm/interceptors/clear-token.interceptor.js +0 -63
- package/fesm/interceptors/clear-token.interceptor.spec.js +0 -103
- package/fesm/interceptors/set-token.interceptor.js +0 -98
- package/fesm/interceptors/set-token.interceptor.spec.js +0 -161
- package/fesm/interfaces/auth-email-provider.interface.js +0 -38
- package/fesm/interfaces/auth-module-options.interface.js +0 -3
- package/fesm/interfaces/authentication.interface.js +0 -1
- package/fesm/interfaces/company-branch.interface.js +0 -1
- package/fesm/interfaces/company.interface.js +0 -1
- package/fesm/interfaces/user-enricher.interface.js +0 -4
- package/fesm/interfaces/user.interface.js +0 -1
- package/fesm/modules/auth.module.js +0 -192
- package/fesm/modules/auth.module.spec.js +0 -239
- package/fesm/services/auth-config.service.js +0 -100
- package/fesm/services/auth-config.service.spec.js +0 -93
- package/fesm/services/auth-datasource.provider.js +0 -83
- package/fesm/services/auth-datasource.provider.spec.js +0 -157
- package/fesm/services/auth-email.service.js +0 -488
- package/fesm/services/auth-email.service.spec.js +0 -411
- package/fesm/services/authentication.service.js +0 -1122
- package/fesm/services/authentication.service.spec.js +0 -917
- package/fesm/services/branch.service.js +0 -143
- package/fesm/services/branch.service.spec.js +0 -165
- package/fesm/services/company-selection-session.service.js +0 -111
- package/fesm/services/company-selection-session.service.spec.js +0 -86
- package/fesm/services/company.service.js +0 -130
- package/fesm/services/company.service.spec.js +0 -123
- package/fesm/services/user-permission.service.js +0 -226
- package/fesm/services/user-permission.service.spec.js +0 -250
- package/fesm/services/user.service.js +0 -620
- package/fesm/services/user.service.spec.js +0 -673
- package/fesm/strategies/jwt.strategy.js +0 -130
- package/fesm/strategies/jwt.strategy.spec.js +0 -162
- package/fesm/validators/password.validator.js +0 -48
- package/fesm/validators/password.validator.spec.js +0 -157
|
@@ -1,673 +0,0 @@
|
|
|
1
|
-
import { BadRequestException, ForbiddenException, NotFoundException } from '@nestjs/common';
|
|
2
|
-
import { ApiService } from '@flusys/nestjs-shared/classes';
|
|
3
|
-
import * as bcrypt from 'bcrypt';
|
|
4
|
-
import { createMockRepository } from '@test-utils/mocks/repository.mock';
|
|
5
|
-
import { buildMockUser } from '@test-utils/mocks/logged-user.mock';
|
|
6
|
-
import { UserService } from './user.service';
|
|
7
|
-
import { AppUser } from '../entities/app-user.entity';
|
|
8
|
-
import { PermissionType, UserCompanyPermission } from '../entities/user-company-permission.entity';
|
|
9
|
-
jest.mock('bcrypt', ()=>({
|
|
10
|
-
hash: jest.fn().mockResolvedValue('hashed-password'),
|
|
11
|
-
compare: jest.fn().mockResolvedValue(true)
|
|
12
|
-
}));
|
|
13
|
-
function buildAppUser(overrides = {}) {
|
|
14
|
-
return {
|
|
15
|
-
id: 'user-1',
|
|
16
|
-
name: 'Test User',
|
|
17
|
-
email: 'user@example.com',
|
|
18
|
-
phone: null,
|
|
19
|
-
password: 'old-hashed',
|
|
20
|
-
isActive: true,
|
|
21
|
-
emailVerified: false,
|
|
22
|
-
phoneVerified: false,
|
|
23
|
-
profilePictureId: null,
|
|
24
|
-
lastLoginAt: null,
|
|
25
|
-
additionalFields: null,
|
|
26
|
-
createdAt: new Date(),
|
|
27
|
-
updatedAt: new Date(),
|
|
28
|
-
deletedAt: null,
|
|
29
|
-
createdById: null,
|
|
30
|
-
updatedById: null,
|
|
31
|
-
deletedById: null,
|
|
32
|
-
...overrides
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
describe('UserService', ()=>{
|
|
36
|
-
let service;
|
|
37
|
-
let userRepo;
|
|
38
|
-
let permissionRepo;
|
|
39
|
-
let mockUtilsService;
|
|
40
|
-
let mockAuthConfig;
|
|
41
|
-
let mockDataSourceProvider;
|
|
42
|
-
let mockUserEnricher;
|
|
43
|
-
let companyFeatureEnabled;
|
|
44
|
-
let mockDataSource;
|
|
45
|
-
beforeEach(()=>{
|
|
46
|
-
jest.clearAllMocks();
|
|
47
|
-
bcrypt.compare.mockResolvedValue(true);
|
|
48
|
-
bcrypt.hash.mockResolvedValue('hashed-password');
|
|
49
|
-
userRepo = createMockRepository();
|
|
50
|
-
permissionRepo = createMockRepository();
|
|
51
|
-
companyFeatureEnabled = false;
|
|
52
|
-
mockUtilsService = {
|
|
53
|
-
clearCache: jest.fn()
|
|
54
|
-
};
|
|
55
|
-
mockAuthConfig = {
|
|
56
|
-
isCompanyFeatureEnabled: jest.fn().mockImplementation(()=>companyFeatureEnabled),
|
|
57
|
-
isPermissionModeFound: jest.fn().mockReturnValue(false)
|
|
58
|
-
};
|
|
59
|
-
mockDataSource = {
|
|
60
|
-
createQueryRunner: jest.fn()
|
|
61
|
-
};
|
|
62
|
-
mockDataSourceProvider = {
|
|
63
|
-
getRepository: jest.fn().mockImplementation(async (entity)=>{
|
|
64
|
-
if (entity === AppUser) return userRepo;
|
|
65
|
-
if (entity === UserCompanyPermission) return permissionRepo;
|
|
66
|
-
throw new Error('unexpected entity');
|
|
67
|
-
}),
|
|
68
|
-
getDataSource: jest.fn().mockResolvedValue(mockDataSource)
|
|
69
|
-
};
|
|
70
|
-
mockUserEnricher = {
|
|
71
|
-
enrichListItems: jest.fn(),
|
|
72
|
-
getProfileExtras: jest.fn(),
|
|
73
|
-
getProfileSections: jest.fn(),
|
|
74
|
-
getProfileSectionData: jest.fn(),
|
|
75
|
-
updateProfileSection: jest.fn(),
|
|
76
|
-
calculateProfileCompletion: jest.fn(),
|
|
77
|
-
validateProfileExtras: jest.fn(),
|
|
78
|
-
updateProfileExtras: jest.fn()
|
|
79
|
-
};
|
|
80
|
-
service = new UserService({}, mockUtilsService, mockDataSourceProvider, mockAuthConfig, mockUserEnricher);
|
|
81
|
-
});
|
|
82
|
-
/**
|
|
83
|
-
* Every write path has to drop both the list cache and the row's own cache, or a
|
|
84
|
-
* stale user keeps being served from `get-all`.
|
|
85
|
-
*/ function expectCacheCleared(id) {
|
|
86
|
-
expect(mockUtilsService.clearCache).toHaveBeenCalledWith('appuser', expect.anything());
|
|
87
|
-
expect(mockUtilsService.clearCache).toHaveBeenCalledWith('appuser', expect.anything(), id);
|
|
88
|
-
}
|
|
89
|
-
describe('ensureDataSourceRepository', ()=>{
|
|
90
|
-
it('does not load the permission repository when the company feature is disabled', async ()=>{
|
|
91
|
-
await service.ensureDataSourceRepository();
|
|
92
|
-
expect(mockDataSourceProvider.getRepository).toHaveBeenCalledWith(AppUser);
|
|
93
|
-
expect(mockDataSourceProvider.getRepository).not.toHaveBeenCalledWith(UserCompanyPermission);
|
|
94
|
-
});
|
|
95
|
-
it('loads the permission repository when the company feature is enabled', async ()=>{
|
|
96
|
-
companyFeatureEnabled = true;
|
|
97
|
-
await service.ensureDataSourceRepository();
|
|
98
|
-
expect(mockDataSourceProvider.getRepository).toHaveBeenCalledWith(UserCompanyPermission);
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
describe('verifyEmail / verifyPhone', ()=>{
|
|
102
|
-
it('marks the email as verified and clears the cache', async ()=>{
|
|
103
|
-
await service.verifyEmail('user-1');
|
|
104
|
-
expect(userRepo.update).toHaveBeenCalledWith('user-1', {
|
|
105
|
-
emailVerified: true
|
|
106
|
-
});
|
|
107
|
-
expectCacheCleared('user-1');
|
|
108
|
-
});
|
|
109
|
-
it('marks the phone as verified and clears the cache', async ()=>{
|
|
110
|
-
await service.verifyPhone('user-1');
|
|
111
|
-
expect(userRepo.update).toHaveBeenCalledWith('user-1', {
|
|
112
|
-
phoneVerified: true
|
|
113
|
-
});
|
|
114
|
-
expectCacheCleared('user-1');
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
describe('updateStatus', ()=>{
|
|
118
|
-
it('updates the user directly when the company feature is disabled', async ()=>{
|
|
119
|
-
await service.updateStatus('user-1', false, null);
|
|
120
|
-
expect(userRepo.update).toHaveBeenCalledWith('user-1', {
|
|
121
|
-
isActive: false
|
|
122
|
-
});
|
|
123
|
-
expectCacheCleared('user-1');
|
|
124
|
-
});
|
|
125
|
-
it('throws NotFoundException when the company permission is missing', async ()=>{
|
|
126
|
-
companyFeatureEnabled = true;
|
|
127
|
-
permissionRepo.findOne.mockResolvedValue(null);
|
|
128
|
-
await expect(service.updateStatus('user-1', false, buildMockUser({
|
|
129
|
-
companyId: 'company-1'
|
|
130
|
-
}))).rejects.toThrow(NotFoundException);
|
|
131
|
-
});
|
|
132
|
-
it('updates the company permission status when the company feature is enabled', async ()=>{
|
|
133
|
-
companyFeatureEnabled = true;
|
|
134
|
-
const permission = {
|
|
135
|
-
isActive: true
|
|
136
|
-
};
|
|
137
|
-
permissionRepo.findOne.mockResolvedValue(permission);
|
|
138
|
-
permissionRepo.save.mockResolvedValue(permission);
|
|
139
|
-
await service.updateStatus('user-1', false, buildMockUser({
|
|
140
|
-
companyId: 'company-1'
|
|
141
|
-
}));
|
|
142
|
-
expect(permissionRepo.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
143
|
-
isActive: false
|
|
144
|
-
}));
|
|
145
|
-
expect(userRepo.update).not.toHaveBeenCalled();
|
|
146
|
-
expectCacheCleared('user-1');
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
|
-
describe('convertSingleDtoToEntity', ()=>{
|
|
150
|
-
it('hashes the password when present in the DTO', async ()=>{
|
|
151
|
-
service.repository = userRepo;
|
|
152
|
-
userRepo.create.mockReturnValue({
|
|
153
|
-
password: null
|
|
154
|
-
});
|
|
155
|
-
const entity = await service.convertSingleDtoToEntity({
|
|
156
|
-
name: 'New User',
|
|
157
|
-
email: 'new@example.com',
|
|
158
|
-
password: 'Password1!'
|
|
159
|
-
}, null);
|
|
160
|
-
expect(bcrypt.hash).toHaveBeenCalledWith('Password1!', 12);
|
|
161
|
-
expect(entity.password).toBe('hashed-password');
|
|
162
|
-
});
|
|
163
|
-
it('does not touch the password field when absent from the DTO', async ()=>{
|
|
164
|
-
service.repository = userRepo;
|
|
165
|
-
userRepo.create.mockReturnValue({
|
|
166
|
-
password: null
|
|
167
|
-
});
|
|
168
|
-
const entity = await service.convertSingleDtoToEntity({
|
|
169
|
-
name: 'New User',
|
|
170
|
-
email: 'new@example.com'
|
|
171
|
-
}, null);
|
|
172
|
-
expect(bcrypt.hash).not.toHaveBeenCalled();
|
|
173
|
-
expect(entity.password).toBeNull();
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
describe('convertEntityToResponseDto', ()=>{
|
|
177
|
-
it('maps a plain entity when isRaw is false', ()=>{
|
|
178
|
-
const user = buildAppUser();
|
|
179
|
-
const dto = service.convertEntityToResponseDto(user, false);
|
|
180
|
-
expect(dto).toEqual(expect.objectContaining({
|
|
181
|
-
id: 'user-1',
|
|
182
|
-
email: 'user@example.com'
|
|
183
|
-
}));
|
|
184
|
-
});
|
|
185
|
-
it('maps a raw query result and derives isActive from the company permission when present', ()=>{
|
|
186
|
-
const raw = {
|
|
187
|
-
appuser_id: 'user-1',
|
|
188
|
-
appuser_name: 'Test User',
|
|
189
|
-
appuser_phone: null,
|
|
190
|
-
appuser_email: 'user@example.com',
|
|
191
|
-
appuser_profile_picture_id: null,
|
|
192
|
-
appuser_is_active: true,
|
|
193
|
-
appuser_companyIsActive: false,
|
|
194
|
-
appuser_email_verified: false,
|
|
195
|
-
appuser_phone_verified: false,
|
|
196
|
-
appuser_last_login_at: null,
|
|
197
|
-
appuser_additional_fields: null,
|
|
198
|
-
appuser_created_at: new Date(),
|
|
199
|
-
appuser_updated_at: new Date(),
|
|
200
|
-
appuser_deleted_at: null,
|
|
201
|
-
appuser_created_by_id: null,
|
|
202
|
-
appuser_updated_by_id: null,
|
|
203
|
-
appuser_deleted_by_id: null
|
|
204
|
-
};
|
|
205
|
-
const dto = service.convertEntityToResponseDto(raw, true);
|
|
206
|
-
expect(dto.isActive).toBe(false);
|
|
207
|
-
});
|
|
208
|
-
it('falls back to the base isActive column when no company permission flag is present in the raw row', ()=>{
|
|
209
|
-
const raw = {
|
|
210
|
-
appuser_id: 'user-1',
|
|
211
|
-
appuser_name: 'Test User',
|
|
212
|
-
appuser_phone: null,
|
|
213
|
-
appuser_email: 'user@example.com',
|
|
214
|
-
appuser_profile_picture_id: null,
|
|
215
|
-
appuser_is_active: true,
|
|
216
|
-
appuser_email_verified: false,
|
|
217
|
-
appuser_phone_verified: false,
|
|
218
|
-
appuser_last_login_at: null,
|
|
219
|
-
appuser_additional_fields: null,
|
|
220
|
-
appuser_created_at: new Date(),
|
|
221
|
-
appuser_updated_at: new Date(),
|
|
222
|
-
appuser_deleted_at: null,
|
|
223
|
-
appuser_created_by_id: null,
|
|
224
|
-
appuser_updated_by_id: null,
|
|
225
|
-
appuser_deleted_by_id: null
|
|
226
|
-
};
|
|
227
|
-
const dto = service.convertEntityToResponseDto(raw, true);
|
|
228
|
-
expect(dto.isActive).toBe(true);
|
|
229
|
-
});
|
|
230
|
-
});
|
|
231
|
-
describe('getFilterQuery', ()=>{
|
|
232
|
-
it('skips actionCode and isActive keys but applies other filters', async ()=>{
|
|
233
|
-
const qb = {
|
|
234
|
-
andWhere: jest.fn().mockReturnThis()
|
|
235
|
-
};
|
|
236
|
-
await service.getFilterQuery(qb, {
|
|
237
|
-
actionCode: 'x',
|
|
238
|
-
isActive: true,
|
|
239
|
-
name: 'John'
|
|
240
|
-
}, null);
|
|
241
|
-
expect(qb.andWhere).toHaveBeenCalledTimes(1);
|
|
242
|
-
expect(qb.andWhere).toHaveBeenCalledWith('appuser.name = :value', {
|
|
243
|
-
value: 'John'
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
describe('getGlobalSearchQuery', ()=>{
|
|
248
|
-
it('adds a LIKE search clause across name/email/phone', async ()=>{
|
|
249
|
-
const qb = {
|
|
250
|
-
andWhere: jest.fn().mockReturnThis()
|
|
251
|
-
};
|
|
252
|
-
await service.getGlobalSearchQuery(qb, 'john', null);
|
|
253
|
-
expect(qb.andWhere).toHaveBeenCalledWith(expect.stringContaining('appuser.name LIKE'), {
|
|
254
|
-
search: '%john%'
|
|
255
|
-
});
|
|
256
|
-
});
|
|
257
|
-
});
|
|
258
|
-
describe('getExtraManipulateQuery', ()=>{
|
|
259
|
-
function buildQb() {
|
|
260
|
-
return {
|
|
261
|
-
andWhere: jest.fn().mockReturnThis(),
|
|
262
|
-
addSelect: jest.fn().mockReturnThis(),
|
|
263
|
-
setParameter: jest.fn().mockReturnThis()
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
it('short-circuits with 1=0 when company feature is enabled but the user has no companyId', async ()=>{
|
|
267
|
-
companyFeatureEnabled = true;
|
|
268
|
-
const qb = buildQb();
|
|
269
|
-
const { isRaw } = await service.getExtraManipulateQuery(qb, {
|
|
270
|
-
filter: {
|
|
271
|
-
actionCode: 'user.read'
|
|
272
|
-
}
|
|
273
|
-
}, buildMockUser({
|
|
274
|
-
companyId: undefined
|
|
275
|
-
}));
|
|
276
|
-
expect(qb.andWhere).toHaveBeenCalledWith('1 = 0');
|
|
277
|
-
expect(isRaw).toBe(true);
|
|
278
|
-
});
|
|
279
|
-
it('applies isActive filter directly on the non-company path', async ()=>{
|
|
280
|
-
const qb = buildQb();
|
|
281
|
-
await service.getExtraManipulateQuery(qb, {
|
|
282
|
-
filter: {
|
|
283
|
-
isActive: true
|
|
284
|
-
}
|
|
285
|
-
}, null);
|
|
286
|
-
expect(qb.andWhere).toHaveBeenCalledWith('appuser.isActive = :value', {
|
|
287
|
-
value: true
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
it('restricts results to the current company when the company feature is enabled', async ()=>{
|
|
291
|
-
companyFeatureEnabled = true;
|
|
292
|
-
const qb = buildQb();
|
|
293
|
-
service.permissionRepository = permissionRepo;
|
|
294
|
-
const { isRaw } = await service.getExtraManipulateQuery(qb, {
|
|
295
|
-
filter: {}
|
|
296
|
-
}, buildMockUser({
|
|
297
|
-
companyId: 'company-1'
|
|
298
|
-
}));
|
|
299
|
-
expect(qb.addSelect).toHaveBeenCalled();
|
|
300
|
-
expect(qb.andWhere).toHaveBeenCalledWith(expect.stringContaining('user_company_permissions'), expect.objectContaining({
|
|
301
|
-
permissionType: PermissionType.COMPANY,
|
|
302
|
-
companyId: 'company-1'
|
|
303
|
-
}));
|
|
304
|
-
expect(isRaw).toBe(true);
|
|
305
|
-
});
|
|
306
|
-
});
|
|
307
|
-
describe('insert', ()=>{
|
|
308
|
-
it('delegates to the base insert when the company feature is disabled', async ()=>{
|
|
309
|
-
const baseInsertSpy = jest.spyOn(ApiService.prototype, 'insert').mockResolvedValue({
|
|
310
|
-
id: 'user-1'
|
|
311
|
-
});
|
|
312
|
-
const result = await service.insert({
|
|
313
|
-
name: 'A',
|
|
314
|
-
email: 'a@example.com',
|
|
315
|
-
password: 'Password1!'
|
|
316
|
-
}, null);
|
|
317
|
-
expect(baseInsertSpy).toHaveBeenCalled();
|
|
318
|
-
expect(result).toEqual({
|
|
319
|
-
id: 'user-1'
|
|
320
|
-
});
|
|
321
|
-
baseInsertSpy.mockRestore();
|
|
322
|
-
});
|
|
323
|
-
it('creates the user and company/branch permissions transactionally when the company feature is enabled', async ()=>{
|
|
324
|
-
companyFeatureEnabled = true;
|
|
325
|
-
service.dataSource = mockDataSource;
|
|
326
|
-
service.permissionRepository = permissionRepo;
|
|
327
|
-
service.repository = userRepo;
|
|
328
|
-
userRepo.create.mockReturnValue({});
|
|
329
|
-
const savedUser = buildAppUser();
|
|
330
|
-
const queryRunner = {
|
|
331
|
-
connect: jest.fn(),
|
|
332
|
-
startTransaction: jest.fn(),
|
|
333
|
-
commitTransaction: jest.fn(),
|
|
334
|
-
rollbackTransaction: jest.fn(),
|
|
335
|
-
release: jest.fn(),
|
|
336
|
-
manager: {
|
|
337
|
-
save: jest.fn().mockResolvedValue(savedUser),
|
|
338
|
-
findOne: jest.fn().mockResolvedValue(null),
|
|
339
|
-
create: jest.fn().mockImplementation((_target, data)=>data)
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
343
|
-
const result = await service.insert({
|
|
344
|
-
name: 'A',
|
|
345
|
-
email: 'a@example.com',
|
|
346
|
-
password: 'Password1!',
|
|
347
|
-
isActive: true
|
|
348
|
-
}, buildMockUser({
|
|
349
|
-
companyId: 'company-1',
|
|
350
|
-
branchId: 'branch-1'
|
|
351
|
-
}));
|
|
352
|
-
expect(queryRunner.manager.save).toHaveBeenCalled();
|
|
353
|
-
expect(queryRunner.commitTransaction).toHaveBeenCalled();
|
|
354
|
-
expect(result).toEqual(expect.objectContaining({
|
|
355
|
-
id: 'user-1'
|
|
356
|
-
}));
|
|
357
|
-
expectCacheCleared(savedUser.id);
|
|
358
|
-
});
|
|
359
|
-
it('rolls back the transaction if permission assignment fails', async ()=>{
|
|
360
|
-
companyFeatureEnabled = true;
|
|
361
|
-
service.dataSource = mockDataSource;
|
|
362
|
-
service.permissionRepository = permissionRepo;
|
|
363
|
-
service.repository = userRepo;
|
|
364
|
-
userRepo.create.mockReturnValue({});
|
|
365
|
-
const queryRunner = {
|
|
366
|
-
connect: jest.fn(),
|
|
367
|
-
startTransaction: jest.fn(),
|
|
368
|
-
commitTransaction: jest.fn(),
|
|
369
|
-
rollbackTransaction: jest.fn(),
|
|
370
|
-
release: jest.fn(),
|
|
371
|
-
manager: {
|
|
372
|
-
save: jest.fn().mockResolvedValue(buildAppUser()),
|
|
373
|
-
findOne: jest.fn().mockRejectedValue(new Error('db failure')),
|
|
374
|
-
create: jest.fn()
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
378
|
-
await expect(service.insert({
|
|
379
|
-
name: 'A',
|
|
380
|
-
email: 'a@example.com',
|
|
381
|
-
password: 'Password1!'
|
|
382
|
-
}, buildMockUser({
|
|
383
|
-
companyId: 'company-1'
|
|
384
|
-
}))).rejects.toThrow('db failure');
|
|
385
|
-
expect(queryRunner.rollbackTransaction).toHaveBeenCalled();
|
|
386
|
-
});
|
|
387
|
-
});
|
|
388
|
-
describe('getProfileWithExtras', ()=>{
|
|
389
|
-
it('throws ForbiddenException when requesting another user profile', async ()=>{
|
|
390
|
-
await expect(service.getProfileWithExtras('other-user', buildMockUser({
|
|
391
|
-
id: 'user-1'
|
|
392
|
-
}))).rejects.toThrow(ForbiddenException);
|
|
393
|
-
});
|
|
394
|
-
it('throws NotFoundException when the user does not exist', async ()=>{
|
|
395
|
-
userRepo.findOne.mockResolvedValue(null);
|
|
396
|
-
await expect(service.getProfileWithExtras('user-1', buildMockUser({
|
|
397
|
-
id: 'user-1'
|
|
398
|
-
}))).rejects.toThrow(NotFoundException);
|
|
399
|
-
});
|
|
400
|
-
it('returns the profile with enricher-provided extras', async ()=>{
|
|
401
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
402
|
-
mockUserEnricher.getProfileExtras.mockResolvedValue({
|
|
403
|
-
completionPercentage: 80
|
|
404
|
-
});
|
|
405
|
-
const result = await service.getProfileWithExtras('user-1', buildMockUser({
|
|
406
|
-
id: 'user-1'
|
|
407
|
-
}));
|
|
408
|
-
expect(result.user).toEqual(expect.objectContaining({
|
|
409
|
-
id: 'user-1'
|
|
410
|
-
}));
|
|
411
|
-
expect(result.extras).toEqual({
|
|
412
|
-
completionPercentage: 80
|
|
413
|
-
});
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
describe('getAll (enriched)', ()=>{
|
|
417
|
-
it('enriches the list returned by the base getAll', async ()=>{
|
|
418
|
-
jest.spyOn(ApiService.prototype, 'getAll').mockResolvedValue({
|
|
419
|
-
data: [
|
|
420
|
-
{
|
|
421
|
-
id: 'user-1'
|
|
422
|
-
}
|
|
423
|
-
],
|
|
424
|
-
total: 1
|
|
425
|
-
});
|
|
426
|
-
mockUserEnricher.enrichListItems.mockResolvedValue([
|
|
427
|
-
{
|
|
428
|
-
id: 'user-1',
|
|
429
|
-
completionPercentage: 50
|
|
430
|
-
}
|
|
431
|
-
]);
|
|
432
|
-
const result = await service.getAll('', {}, null);
|
|
433
|
-
expect(result.data).toEqual([
|
|
434
|
-
{
|
|
435
|
-
id: 'user-1',
|
|
436
|
-
completionPercentage: 50
|
|
437
|
-
}
|
|
438
|
-
]);
|
|
439
|
-
expect(result.total).toBe(1);
|
|
440
|
-
jest.restoreAllMocks();
|
|
441
|
-
});
|
|
442
|
-
});
|
|
443
|
-
describe('getEnrichedList', ()=>{
|
|
444
|
-
it('returns users unchanged when no enricher is configured', async ()=>{
|
|
445
|
-
service = new UserService({}, mockUtilsService, mockDataSourceProvider, mockAuthConfig, undefined);
|
|
446
|
-
const users = [
|
|
447
|
-
{
|
|
448
|
-
id: 'user-1'
|
|
449
|
-
}
|
|
450
|
-
];
|
|
451
|
-
const result = await service.getEnrichedList(users, null);
|
|
452
|
-
expect(result).toBe(users);
|
|
453
|
-
});
|
|
454
|
-
});
|
|
455
|
-
describe('getProfileSections / getProfileSectionData / getProfileCompletion', ()=>{
|
|
456
|
-
it('returns sections from the enricher', async ()=>{
|
|
457
|
-
mockUserEnricher.getProfileSections.mockReturnValue([
|
|
458
|
-
{
|
|
459
|
-
id: 's1',
|
|
460
|
-
name: 'Basic',
|
|
461
|
-
order: 1
|
|
462
|
-
}
|
|
463
|
-
]);
|
|
464
|
-
const result = await service.getProfileSections();
|
|
465
|
-
expect(result).toEqual([
|
|
466
|
-
{
|
|
467
|
-
id: 's1',
|
|
468
|
-
name: 'Basic',
|
|
469
|
-
order: 1
|
|
470
|
-
}
|
|
471
|
-
]);
|
|
472
|
-
});
|
|
473
|
-
it('returns an empty array when no enricher is configured', async ()=>{
|
|
474
|
-
service = new UserService({}, mockUtilsService, mockDataSourceProvider, mockAuthConfig, undefined);
|
|
475
|
-
expect(await service.getProfileSections()).toEqual([]);
|
|
476
|
-
});
|
|
477
|
-
it('returns section data via the enricher after verifying profile ownership', async ()=>{
|
|
478
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
479
|
-
mockUserEnricher.getProfileSectionData.mockResolvedValue({
|
|
480
|
-
sectionId: 's1',
|
|
481
|
-
data: {},
|
|
482
|
-
isComplete: true
|
|
483
|
-
});
|
|
484
|
-
const result = await service.getProfileSectionData('user-1', 's1', buildMockUser({
|
|
485
|
-
id: 'user-1'
|
|
486
|
-
}));
|
|
487
|
-
expect(result).toEqual(expect.objectContaining({
|
|
488
|
-
sectionId: 's1'
|
|
489
|
-
}));
|
|
490
|
-
});
|
|
491
|
-
it('returns the default 100% completion when no enricher is configured', async ()=>{
|
|
492
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
493
|
-
service = new UserService({}, mockUtilsService, mockDataSourceProvider, mockAuthConfig, undefined);
|
|
494
|
-
const result = await service.getProfileCompletion('user-1', buildMockUser({
|
|
495
|
-
id: 'user-1'
|
|
496
|
-
}));
|
|
497
|
-
expect(result).toBe(100);
|
|
498
|
-
});
|
|
499
|
-
it('delegates completion calculation to the enricher when configured', async ()=>{
|
|
500
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
501
|
-
mockUserEnricher.calculateProfileCompletion.mockResolvedValue(65);
|
|
502
|
-
const result = await service.getProfileCompletion('user-1', buildMockUser({
|
|
503
|
-
id: 'user-1'
|
|
504
|
-
}));
|
|
505
|
-
expect(result).toBe(65);
|
|
506
|
-
});
|
|
507
|
-
});
|
|
508
|
-
describe('updateProfileSection', ()=>{
|
|
509
|
-
it('throws BadRequestException when the data source is unavailable', async ()=>{
|
|
510
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
511
|
-
service.dataSource = undefined;
|
|
512
|
-
mockDataSourceProvider.getDataSource.mockResolvedValue(undefined);
|
|
513
|
-
await expect(service.updateProfileSection('user-1', 's1', {}, buildMockUser({
|
|
514
|
-
id: 'user-1'
|
|
515
|
-
}))).rejects.toThrow(BadRequestException);
|
|
516
|
-
});
|
|
517
|
-
it('throws BadRequestException when profile sections are not supported', async ()=>{
|
|
518
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
519
|
-
service.dataSource = mockDataSource;
|
|
520
|
-
service = new UserService({}, mockUtilsService, mockDataSourceProvider, mockAuthConfig, undefined);
|
|
521
|
-
service.dataSource = mockDataSource;
|
|
522
|
-
await expect(service.updateProfileSection('user-1', 's1', {}, buildMockUser({
|
|
523
|
-
id: 'user-1'
|
|
524
|
-
}))).rejects.toThrow(BadRequestException);
|
|
525
|
-
});
|
|
526
|
-
it('updates the section within a transaction and commits on success', async ()=>{
|
|
527
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
528
|
-
service.dataSource = mockDataSource;
|
|
529
|
-
const queryRunner = {
|
|
530
|
-
connect: jest.fn(),
|
|
531
|
-
startTransaction: jest.fn(),
|
|
532
|
-
commitTransaction: jest.fn(),
|
|
533
|
-
rollbackTransaction: jest.fn(),
|
|
534
|
-
release: jest.fn()
|
|
535
|
-
};
|
|
536
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
537
|
-
await service.updateProfileSection('user-1', 's1', {
|
|
538
|
-
foo: 'bar'
|
|
539
|
-
}, buildMockUser({
|
|
540
|
-
id: 'user-1'
|
|
541
|
-
}));
|
|
542
|
-
expect(mockUserEnricher.updateProfileSection).toHaveBeenCalledWith('user-1', 's1', {
|
|
543
|
-
foo: 'bar'
|
|
544
|
-
}, queryRunner);
|
|
545
|
-
expect(queryRunner.commitTransaction).toHaveBeenCalled();
|
|
546
|
-
expectCacheCleared('user-1');
|
|
547
|
-
});
|
|
548
|
-
it('rolls back the transaction when the enricher throws', async ()=>{
|
|
549
|
-
userRepo.findOne.mockResolvedValue(buildAppUser());
|
|
550
|
-
service.dataSource = mockDataSource;
|
|
551
|
-
const queryRunner = {
|
|
552
|
-
connect: jest.fn(),
|
|
553
|
-
startTransaction: jest.fn(),
|
|
554
|
-
commitTransaction: jest.fn(),
|
|
555
|
-
rollbackTransaction: jest.fn(),
|
|
556
|
-
release: jest.fn()
|
|
557
|
-
};
|
|
558
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
559
|
-
mockUserEnricher.updateProfileSection.mockRejectedValue(new Error('enrich failed'));
|
|
560
|
-
await expect(service.updateProfileSection('user-1', 's1', {}, buildMockUser({
|
|
561
|
-
id: 'user-1'
|
|
562
|
-
}))).rejects.toThrow('enrich failed');
|
|
563
|
-
expect(queryRunner.rollbackTransaction).toHaveBeenCalled();
|
|
564
|
-
});
|
|
565
|
-
});
|
|
566
|
-
describe('profile', ()=>{
|
|
567
|
-
function buildQueryRunner(findUser) {
|
|
568
|
-
return {
|
|
569
|
-
connect: jest.fn(),
|
|
570
|
-
startTransaction: jest.fn(),
|
|
571
|
-
commitTransaction: jest.fn(),
|
|
572
|
-
rollbackTransaction: jest.fn(),
|
|
573
|
-
release: jest.fn(),
|
|
574
|
-
manager: {
|
|
575
|
-
findOne: jest.fn().mockResolvedValue(findUser),
|
|
576
|
-
save: jest.fn().mockResolvedValue(findUser)
|
|
577
|
-
}
|
|
578
|
-
};
|
|
579
|
-
}
|
|
580
|
-
it('throws BadRequestException when the data source is unavailable', async ()=>{
|
|
581
|
-
service.dataSource = undefined;
|
|
582
|
-
mockDataSourceProvider.getDataSource.mockResolvedValue(undefined);
|
|
583
|
-
await expect(service.profile({
|
|
584
|
-
id: 'user-1'
|
|
585
|
-
}, buildMockUser({
|
|
586
|
-
id: 'user-1'
|
|
587
|
-
}))).rejects.toThrow(BadRequestException);
|
|
588
|
-
});
|
|
589
|
-
it('throws NotFoundException when the target user does not exist', async ()=>{
|
|
590
|
-
service.dataSource = mockDataSource;
|
|
591
|
-
const queryRunner = buildQueryRunner(null);
|
|
592
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
593
|
-
await expect(service.profile({
|
|
594
|
-
id: 'user-1'
|
|
595
|
-
}, buildMockUser({
|
|
596
|
-
id: 'user-1'
|
|
597
|
-
}))).rejects.toThrow(NotFoundException);
|
|
598
|
-
expect(queryRunner.rollbackTransaction).toHaveBeenCalled();
|
|
599
|
-
});
|
|
600
|
-
it('requires the current password when changing your own password', async ()=>{
|
|
601
|
-
service.dataSource = mockDataSource;
|
|
602
|
-
const queryRunner = buildQueryRunner(buildAppUser());
|
|
603
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
604
|
-
await expect(service.profile({
|
|
605
|
-
id: 'user-1',
|
|
606
|
-
password: 'NewPassword1!'
|
|
607
|
-
}, buildMockUser({
|
|
608
|
-
id: 'user-1'
|
|
609
|
-
}))).rejects.toThrow(BadRequestException);
|
|
610
|
-
});
|
|
611
|
-
it('rejects an incorrect current password when changing your own password', async ()=>{
|
|
612
|
-
service.dataSource = mockDataSource;
|
|
613
|
-
const queryRunner = buildQueryRunner(buildAppUser());
|
|
614
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
615
|
-
bcrypt.compare.mockResolvedValue(false);
|
|
616
|
-
await expect(service.profile({
|
|
617
|
-
id: 'user-1',
|
|
618
|
-
password: 'NewPassword1!',
|
|
619
|
-
oldPassword: 'wrong'
|
|
620
|
-
}, buildMockUser({
|
|
621
|
-
id: 'user-1'
|
|
622
|
-
}))).rejects.toThrow(BadRequestException);
|
|
623
|
-
});
|
|
624
|
-
it('updates the profile and hashes the new password on success', async ()=>{
|
|
625
|
-
service.dataSource = mockDataSource;
|
|
626
|
-
const queryRunner = buildQueryRunner(buildAppUser());
|
|
627
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
628
|
-
const result = await service.profile({
|
|
629
|
-
id: 'user-1',
|
|
630
|
-
name: 'Updated Name',
|
|
631
|
-
password: 'NewPassword1!',
|
|
632
|
-
oldPassword: 'old-password'
|
|
633
|
-
}, buildMockUser({
|
|
634
|
-
id: 'user-1'
|
|
635
|
-
}));
|
|
636
|
-
expect(bcrypt.hash).toHaveBeenCalledWith('NewPassword1!', 12);
|
|
637
|
-
expect(queryRunner.manager.save).toHaveBeenCalled();
|
|
638
|
-
expect(queryRunner.commitTransaction).toHaveBeenCalled();
|
|
639
|
-
expect(result).toBe('user-1');
|
|
640
|
-
expectCacheCleared('user-1');
|
|
641
|
-
});
|
|
642
|
-
it('does not require the current password when an admin updates another user profile', async ()=>{
|
|
643
|
-
service.dataSource = mockDataSource;
|
|
644
|
-
const targetUser = buildAppUser({
|
|
645
|
-
id: 'other-user'
|
|
646
|
-
});
|
|
647
|
-
const queryRunner = buildQueryRunner(targetUser);
|
|
648
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
649
|
-
await service.profile({
|
|
650
|
-
id: 'other-user',
|
|
651
|
-
password: 'NewPassword1!'
|
|
652
|
-
}, buildMockUser({
|
|
653
|
-
id: 'admin-1'
|
|
654
|
-
}));
|
|
655
|
-
expect(queryRunner.manager.save).toHaveBeenCalled();
|
|
656
|
-
expect(queryRunner.commitTransaction).toHaveBeenCalled();
|
|
657
|
-
expectCacheCleared('other-user');
|
|
658
|
-
});
|
|
659
|
-
it('rolls back the transaction when save fails', async ()=>{
|
|
660
|
-
service.dataSource = mockDataSource;
|
|
661
|
-
const queryRunner = buildQueryRunner(buildAppUser());
|
|
662
|
-
queryRunner.manager.save.mockRejectedValue(new Error('save failed'));
|
|
663
|
-
mockDataSource.createQueryRunner.mockReturnValue(queryRunner);
|
|
664
|
-
await expect(service.profile({
|
|
665
|
-
id: 'user-1',
|
|
666
|
-
name: 'X'
|
|
667
|
-
}, buildMockUser({
|
|
668
|
-
id: 'user-1'
|
|
669
|
-
}))).rejects.toThrow('save failed');
|
|
670
|
-
expect(queryRunner.rollbackTransaction).toHaveBeenCalled();
|
|
671
|
-
});
|
|
672
|
-
});
|
|
673
|
-
});
|