@drax/identity-back 1.1.0 → 1.3.0
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/dist/controllers/UserController.js +0 -1
- package/package.json +4 -4
- package/src/controllers/UserController.ts +1 -1
- package/test/endpoints/data/users-data.ts +38 -0
- package/test/{routes → endpoints}/tenant-route.test.ts +91 -63
- package/test/{routes → endpoints}/user-route.test.ts +41 -78
- package/test/repository/mongo/role-mongo-repository.test.ts +4 -3
- package/test/repository/mongo/user-apikey-mongo-repository.test.ts +5 -4
- package/test/repository/mongo/user-mongo-repository.test.ts +35 -48
- package/test/services/role-service.test.ts +5 -6
- package/test/services/user-service.test.ts +1 -1
- package/test/setup/MongoInMemory.ts +21 -12
- package/test/setup/TestSetup.ts +81 -4
- package/test/setup/data/admin-role.ts +5 -2
- package/test/setup/data/basic-user.ts +14 -0
- package/test/setup/data/restricted-role.ts +10 -0
- package/test/setup/data/root-user.ts +3 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/test/db/MongoInMemory.ts +0 -43
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.3.0",
|
|
7
7
|
"description": "Identity module for user management, authentication and authorization.",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "types/index.d.ts",
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"author": "Cristian Incarnato & Drax Team",
|
|
29
29
|
"license": "ISC",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@drax/common-back": "^1.
|
|
32
|
-
"@drax/crud-back": "^1.
|
|
31
|
+
"@drax/common-back": "^1.1.1",
|
|
32
|
+
"@drax/crud-back": "^1.3.0",
|
|
33
33
|
"@drax/crud-share": "^1.0.0",
|
|
34
34
|
"@drax/email-back": "^1.0.0",
|
|
35
35
|
"@drax/identity-share": "^1.0.0",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"debug": "0"
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "45976c72b3d08b4835c3801da839125178ed77a2"
|
|
67
67
|
}
|
|
@@ -180,7 +180,7 @@ class UserController extends AbstractFastifyController<IUser, IUserCreate, IUser
|
|
|
180
180
|
item.password = undefined
|
|
181
181
|
delete item.password
|
|
182
182
|
}
|
|
183
|
-
|
|
183
|
+
|
|
184
184
|
return paginateResult
|
|
185
185
|
} catch (e) {
|
|
186
186
|
this.handleError(e, reply)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {IUserCreate} from "@drax/identity-share";
|
|
2
|
+
|
|
3
|
+
const USER1: IUserCreate = {
|
|
4
|
+
active: true,
|
|
5
|
+
password: "12345678",
|
|
6
|
+
phone: "",
|
|
7
|
+
role: "",
|
|
8
|
+
name: "John Wick",
|
|
9
|
+
username: "johnwick",
|
|
10
|
+
email: "johnwick@example.com"
|
|
11
|
+
}
|
|
12
|
+
const USER2: IUserCreate = {
|
|
13
|
+
active: true,
|
|
14
|
+
password: "12345678",
|
|
15
|
+
phone: "",
|
|
16
|
+
role: "",
|
|
17
|
+
name: "John Rambo",
|
|
18
|
+
username: "rambo",
|
|
19
|
+
email: "rambo@example.com"
|
|
20
|
+
}
|
|
21
|
+
const USER3: IUserCreate = {
|
|
22
|
+
active: true,
|
|
23
|
+
password: "12345678",
|
|
24
|
+
phone: "",
|
|
25
|
+
role: "",
|
|
26
|
+
name: "John Depp",
|
|
27
|
+
username: "depp",
|
|
28
|
+
email: "depp@example.com"
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const USERS : IUserCreate[] = [USER1, USER2, USER3]
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
USER1,
|
|
35
|
+
USER2,
|
|
36
|
+
USER3,
|
|
37
|
+
USERS
|
|
38
|
+
}
|
|
@@ -6,33 +6,35 @@ import TestSetup from "../setup/TestSetup";
|
|
|
6
6
|
describe("Tenant Route Test", function () {
|
|
7
7
|
|
|
8
8
|
let testSetup = new TestSetup()
|
|
9
|
-
let FASTIFY_TEST_SERVER: any;
|
|
10
|
-
let ACCESS_TOKEN: any;
|
|
11
9
|
|
|
12
10
|
beforeAll(async () => {
|
|
13
11
|
await testSetup.setup()
|
|
14
|
-
FASTIFY_TEST_SERVER = testSetup.fastifyInstance
|
|
15
|
-
const {accessToken} = await testSetup.login()
|
|
16
|
-
ACCESS_TOKEN = accessToken
|
|
17
12
|
})
|
|
18
13
|
|
|
19
14
|
afterAll(async () => {
|
|
20
|
-
await testSetup.
|
|
15
|
+
await testSetup.dropAndClose()
|
|
21
16
|
return
|
|
22
17
|
})
|
|
23
18
|
|
|
24
19
|
it("Me", async () => {
|
|
25
20
|
|
|
26
|
-
const
|
|
21
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
22
|
+
expect(accessToken).toBeTruthy()
|
|
23
|
+
|
|
24
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
27
25
|
method: 'get',
|
|
28
26
|
url: '/api/auth/me',
|
|
29
|
-
headers: {Authorization: `Bearer ${
|
|
27
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
30
28
|
});
|
|
31
29
|
let body = resp.json()
|
|
32
30
|
console.log("me", body)
|
|
33
31
|
})
|
|
34
32
|
|
|
35
33
|
it("Should paginate tenant", async () => {
|
|
34
|
+
|
|
35
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
36
|
+
expect(accessToken).toBeTruthy()
|
|
37
|
+
|
|
36
38
|
// First, create a few tenants
|
|
37
39
|
const tenantData = [
|
|
38
40
|
{name: "Tenant1"},
|
|
@@ -41,18 +43,18 @@ describe("Tenant Route Test", function () {
|
|
|
41
43
|
];
|
|
42
44
|
|
|
43
45
|
for (const data of tenantData) {
|
|
44
|
-
await
|
|
46
|
+
await testSetup.fastifyInstance.inject({
|
|
45
47
|
method: 'POST',
|
|
46
48
|
url: '/api/tenants',
|
|
47
49
|
payload: data,
|
|
48
|
-
headers: {Authorization: `Bearer ${
|
|
50
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
49
51
|
});
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
const resp = await
|
|
54
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
53
55
|
method: 'GET',
|
|
54
56
|
url: '/api/tenants',
|
|
55
|
-
headers: {Authorization: `Bearer ${
|
|
57
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
56
58
|
})
|
|
57
59
|
|
|
58
60
|
const result = await resp.json()
|
|
@@ -66,15 +68,19 @@ describe("Tenant Route Test", function () {
|
|
|
66
68
|
})
|
|
67
69
|
|
|
68
70
|
it("should create a new tenant", async () => {
|
|
71
|
+
|
|
72
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
73
|
+
expect(accessToken).toBeTruthy()
|
|
74
|
+
|
|
69
75
|
const newTenant = {
|
|
70
76
|
name: "NewTestTenant"
|
|
71
77
|
};
|
|
72
78
|
|
|
73
|
-
const resp = await
|
|
79
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
74
80
|
method: 'POST',
|
|
75
81
|
url: '/api/tenants',
|
|
76
82
|
payload: newTenant,
|
|
77
|
-
headers: {Authorization: `Bearer ${
|
|
83
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
78
84
|
});
|
|
79
85
|
|
|
80
86
|
const result = await resp.json();
|
|
@@ -83,10 +89,10 @@ describe("Tenant Route Test", function () {
|
|
|
83
89
|
expect(result._id).toBeDefined();
|
|
84
90
|
|
|
85
91
|
// Verify tenant was created by fetching it
|
|
86
|
-
const getResp = await
|
|
92
|
+
const getResp = await testSetup.fastifyInstance.inject({
|
|
87
93
|
method: 'GET',
|
|
88
94
|
url: '/api/tenants/' + result.id,
|
|
89
|
-
headers: {Authorization: `Bearer ${
|
|
95
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
90
96
|
});
|
|
91
97
|
|
|
92
98
|
const getTenant = await getResp.json();
|
|
@@ -96,22 +102,25 @@ describe("Tenant Route Test", function () {
|
|
|
96
102
|
|
|
97
103
|
it("should update an existing tenant", async () => {
|
|
98
104
|
|
|
105
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
106
|
+
expect(accessToken).toBeTruthy()
|
|
107
|
+
|
|
99
108
|
const newTenant = {
|
|
100
109
|
name: "ExistTenant"
|
|
101
110
|
};
|
|
102
111
|
|
|
103
|
-
const resp = await
|
|
112
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
104
113
|
method: 'POST',
|
|
105
114
|
url: '/api/tenants',
|
|
106
115
|
payload: newTenant,
|
|
107
|
-
headers: {Authorization: `Bearer ${
|
|
116
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
108
117
|
});
|
|
109
118
|
|
|
110
119
|
// First, get existing tenants to extract the id
|
|
111
|
-
const getResp = await
|
|
120
|
+
const getResp = await testSetup.fastifyInstance.inject({
|
|
112
121
|
method: 'GET',
|
|
113
122
|
url: '/api/tenants',
|
|
114
|
-
headers: {Authorization: `Bearer ${
|
|
123
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
115
124
|
})
|
|
116
125
|
|
|
117
126
|
const result = await getResp.json()
|
|
@@ -123,11 +132,11 @@ describe("Tenant Route Test", function () {
|
|
|
123
132
|
}
|
|
124
133
|
|
|
125
134
|
// Send update request
|
|
126
|
-
const updateResp = await
|
|
135
|
+
const updateResp = await testSetup.fastifyInstance.inject({
|
|
127
136
|
method: 'PUT',
|
|
128
137
|
url: `/api/tenants/${tenantId}`,
|
|
129
138
|
payload: updateData,
|
|
130
|
-
headers: {Authorization: `Bearer ${
|
|
139
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
131
140
|
})
|
|
132
141
|
|
|
133
142
|
// Verify update response
|
|
@@ -136,10 +145,10 @@ describe("Tenant Route Test", function () {
|
|
|
136
145
|
expect(updatedTenant.name).toBe("UpdatedTenantName")
|
|
137
146
|
|
|
138
147
|
// Verify the tenant was actually updated by fetching it again
|
|
139
|
-
const verifyResp = await
|
|
148
|
+
const verifyResp = await testSetup.fastifyInstance.inject({
|
|
140
149
|
method: 'GET',
|
|
141
150
|
url: `/api/tenants/${tenantId}`,
|
|
142
|
-
headers: {Authorization: `Bearer ${
|
|
151
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
143
152
|
})
|
|
144
153
|
|
|
145
154
|
const verifiedTenant = await verifyResp.json()
|
|
@@ -147,11 +156,11 @@ describe("Tenant Route Test", function () {
|
|
|
147
156
|
expect(verifiedTenant.name).toBe("UpdatedTenantName")
|
|
148
157
|
|
|
149
158
|
// Send update inexistingId should return 404
|
|
150
|
-
const updateRespNotFound = await
|
|
159
|
+
const updateRespNotFound = await testSetup.fastifyInstance.inject({
|
|
151
160
|
method: 'PUT',
|
|
152
161
|
url: `/api/tenants/66761bed94d57a42c3277bab`,
|
|
153
162
|
payload: updateData,
|
|
154
|
-
headers: {Authorization: `Bearer ${
|
|
163
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
155
164
|
})
|
|
156
165
|
|
|
157
166
|
// Verify update response
|
|
@@ -160,16 +169,20 @@ describe("Tenant Route Test", function () {
|
|
|
160
169
|
})
|
|
161
170
|
|
|
162
171
|
it("should delete an existing tenant", async () => {
|
|
172
|
+
|
|
173
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
174
|
+
expect(accessToken).toBeTruthy()
|
|
175
|
+
|
|
163
176
|
// First create a tenant to be deleted
|
|
164
177
|
const newTenant = {
|
|
165
178
|
name: "TenantToDelete"
|
|
166
179
|
};
|
|
167
180
|
|
|
168
|
-
const createResp = await
|
|
181
|
+
const createResp = await testSetup.fastifyInstance.inject({
|
|
169
182
|
method: 'POST',
|
|
170
183
|
url: '/api/tenants',
|
|
171
184
|
payload: newTenant,
|
|
172
|
-
headers: {Authorization: `Bearer ${
|
|
185
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
173
186
|
});
|
|
174
187
|
|
|
175
188
|
const createdTenant = await createResp.json();
|
|
@@ -177,10 +190,10 @@ describe("Tenant Route Test", function () {
|
|
|
177
190
|
const tenantId = createdTenant.id;
|
|
178
191
|
|
|
179
192
|
// Delete the tenant
|
|
180
|
-
const deleteResp = await
|
|
193
|
+
const deleteResp = await testSetup.fastifyInstance.inject({
|
|
181
194
|
method: 'DELETE',
|
|
182
195
|
url: `/api/tenants/${tenantId}`,
|
|
183
|
-
headers: {Authorization: `Bearer ${
|
|
196
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
184
197
|
});
|
|
185
198
|
|
|
186
199
|
// Verify delete response
|
|
@@ -189,10 +202,10 @@ describe("Tenant Route Test", function () {
|
|
|
189
202
|
expect(deleteResult.deleted).toBe(true);
|
|
190
203
|
|
|
191
204
|
// Verify the tenant was actually deleted by trying to fetch it
|
|
192
|
-
const verifyResp = await
|
|
205
|
+
const verifyResp = await testSetup.fastifyInstance.inject({
|
|
193
206
|
method: 'GET',
|
|
194
207
|
url: `/api/tenants/${tenantId}`,
|
|
195
|
-
headers: {Authorization: `Bearer ${
|
|
208
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
196
209
|
});
|
|
197
210
|
|
|
198
211
|
// Should return 404 or empty response
|
|
@@ -200,16 +213,19 @@ describe("Tenant Route Test", function () {
|
|
|
200
213
|
})
|
|
201
214
|
|
|
202
215
|
it("should find a tenant by ID", async () => {
|
|
216
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
217
|
+
expect(accessToken).toBeTruthy()
|
|
218
|
+
|
|
203
219
|
// First create a tenant to ensure we have one with known ID
|
|
204
220
|
const newTenant = {
|
|
205
221
|
name: "FindByIdTenant"
|
|
206
222
|
};
|
|
207
223
|
|
|
208
|
-
const createResp = await
|
|
224
|
+
const createResp = await testSetup.fastifyInstance.inject({
|
|
209
225
|
method: 'POST',
|
|
210
226
|
url: '/api/tenants',
|
|
211
227
|
payload: newTenant,
|
|
212
|
-
headers: {Authorization: `Bearer ${
|
|
228
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
213
229
|
});
|
|
214
230
|
|
|
215
231
|
const createdTenant = await createResp.json();
|
|
@@ -217,10 +233,10 @@ describe("Tenant Route Test", function () {
|
|
|
217
233
|
const tenantId = createdTenant.id;
|
|
218
234
|
|
|
219
235
|
// Now fetch the tenant by ID
|
|
220
|
-
const getResp = await
|
|
236
|
+
const getResp = await testSetup.fastifyInstance.inject({
|
|
221
237
|
method: 'GET',
|
|
222
238
|
url: `/api/tenants/${tenantId}`,
|
|
223
|
-
headers: {Authorization: `Bearer ${
|
|
239
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
224
240
|
});
|
|
225
241
|
|
|
226
242
|
// Verify the response
|
|
@@ -231,6 +247,9 @@ describe("Tenant Route Test", function () {
|
|
|
231
247
|
})
|
|
232
248
|
|
|
233
249
|
it("should search for tenants ", async () => {
|
|
250
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
251
|
+
expect(accessToken).toBeTruthy()
|
|
252
|
+
|
|
234
253
|
// First, create a few tenants for search testing
|
|
235
254
|
const tenantData = [
|
|
236
255
|
{name: "SearchTenant1"},
|
|
@@ -240,23 +259,23 @@ describe("Tenant Route Test", function () {
|
|
|
240
259
|
|
|
241
260
|
// Create the test tenants
|
|
242
261
|
for (const data of tenantData) {
|
|
243
|
-
await
|
|
262
|
+
await testSetup.fastifyInstance.inject({
|
|
244
263
|
method: 'POST',
|
|
245
264
|
url: '/api/tenants',
|
|
246
265
|
payload: data,
|
|
247
|
-
headers: {Authorization: `Bearer ${
|
|
266
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
248
267
|
});
|
|
249
268
|
}
|
|
250
269
|
|
|
251
270
|
// Test searching with a matching term
|
|
252
|
-
const searchResp = await
|
|
271
|
+
const searchResp = await testSetup.fastifyInstance.inject({
|
|
253
272
|
method: 'GET',
|
|
254
273
|
url: '/api/tenants/search?search=Search',
|
|
255
|
-
headers: {Authorization: `Bearer ${
|
|
274
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
256
275
|
});
|
|
257
276
|
|
|
258
277
|
const searchResult = await searchResp.json();
|
|
259
|
-
console.log("searchResult",searchResult)
|
|
278
|
+
console.log("searchResult", searchResult)
|
|
260
279
|
|
|
261
280
|
expect(searchResp.statusCode).toBe(200);
|
|
262
281
|
expect(searchResult.length).toBe(2); // Should find the two tenants with "Search" in their name
|
|
@@ -267,6 +286,9 @@ describe("Tenant Route Test", function () {
|
|
|
267
286
|
})
|
|
268
287
|
|
|
269
288
|
it("Should get all tenants", async () => {
|
|
289
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
290
|
+
expect(accessToken).toBeTruthy()
|
|
291
|
+
|
|
270
292
|
// First, create a few tenants
|
|
271
293
|
const tenantData = [
|
|
272
294
|
{name: "SearchTenant1"},
|
|
@@ -275,23 +297,23 @@ describe("Tenant Route Test", function () {
|
|
|
275
297
|
];
|
|
276
298
|
|
|
277
299
|
for (const data of tenantData) {
|
|
278
|
-
await
|
|
300
|
+
await testSetup.fastifyInstance.inject({
|
|
279
301
|
method: 'POST',
|
|
280
302
|
url: '/api/tenants',
|
|
281
303
|
payload: data,
|
|
282
|
-
headers: {Authorization: `Bearer ${
|
|
304
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
283
305
|
});
|
|
284
306
|
}
|
|
285
307
|
|
|
286
308
|
// Get all tenants
|
|
287
|
-
const resp = await
|
|
309
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
288
310
|
method: 'GET',
|
|
289
311
|
url: '/api/tenants/all',
|
|
290
|
-
headers: {Authorization: `Bearer ${
|
|
312
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
291
313
|
});
|
|
292
314
|
|
|
293
315
|
const result = await resp.json();
|
|
294
|
-
console.log("result",result)
|
|
316
|
+
console.log("result", result)
|
|
295
317
|
|
|
296
318
|
expect(resp.statusCode).toBe(200);
|
|
297
319
|
|
|
@@ -303,6 +325,9 @@ describe("Tenant Route Test", function () {
|
|
|
303
325
|
})
|
|
304
326
|
|
|
305
327
|
it("should find tenants by specific field-value pair", async () => {
|
|
328
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
329
|
+
expect(accessToken).toBeTruthy()
|
|
330
|
+
|
|
306
331
|
// First create tenants with specific field values for testing
|
|
307
332
|
const tenantData = [
|
|
308
333
|
{name: "FieldTenantA"},
|
|
@@ -312,19 +337,19 @@ describe("Tenant Route Test", function () {
|
|
|
312
337
|
|
|
313
338
|
// Create the test tenants
|
|
314
339
|
for (const data of tenantData) {
|
|
315
|
-
await
|
|
340
|
+
await testSetup.fastifyInstance.inject({
|
|
316
341
|
method: 'POST',
|
|
317
342
|
url: '/api/tenants',
|
|
318
343
|
payload: data,
|
|
319
|
-
headers: {Authorization: `Bearer ${
|
|
344
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
320
345
|
});
|
|
321
346
|
}
|
|
322
347
|
|
|
323
348
|
// Test finding by description field with value "Special"
|
|
324
|
-
const findByResp = await
|
|
349
|
+
const findByResp = await testSetup.fastifyInstance.inject({
|
|
325
350
|
method: 'GET',
|
|
326
351
|
url: '/api/tenants/find-by/name/FieldTenantA',
|
|
327
|
-
headers: {Authorization: `Bearer ${
|
|
352
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
328
353
|
});
|
|
329
354
|
|
|
330
355
|
const findByResult = await findByResp.json();
|
|
@@ -337,23 +362,26 @@ describe("Tenant Route Test", function () {
|
|
|
337
362
|
})
|
|
338
363
|
|
|
339
364
|
it("should handle error responses correctly when tenant is not found", async () => {
|
|
340
|
-
|
|
341
|
-
|
|
365
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
366
|
+
expect(accessToken).toBeTruthy()
|
|
342
367
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
url: `/api/tenants/${nonExistentId}`,
|
|
346
|
-
headers: {Authorization: `Bearer ${ACCESS_TOKEN}`}
|
|
347
|
-
});
|
|
368
|
+
// Try to fetch a non-existent tenant
|
|
369
|
+
const nonExistentId = "123456789012345678901234"; // Valid MongoDB ObjectId that doesn't exist
|
|
348
370
|
|
|
349
|
-
|
|
350
|
-
|
|
371
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
372
|
+
method: 'GET',
|
|
373
|
+
url: `/api/tenants/${nonExistentId}`,
|
|
374
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// Verify response status code should be 404 Not Found
|
|
378
|
+
expect(resp.statusCode).toBe(404);
|
|
351
379
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
380
|
+
const result = await resp.json();
|
|
381
|
+
expect(result.error).toBeDefined();
|
|
382
|
+
expect(result.message).toContain("Not found");
|
|
355
383
|
|
|
356
|
-
});
|
|
384
|
+
});
|
|
357
385
|
|
|
358
386
|
|
|
359
387
|
})
|
|
@@ -1,69 +1,37 @@
|
|
|
1
1
|
import {describe, it, beforeAll, afterAll, expect} from "vitest"
|
|
2
2
|
|
|
3
3
|
import {TestSetup} from "../setup/TestSetup"
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const USER1: IUserCreate = {
|
|
10
|
-
active: true,
|
|
11
|
-
password: "12345678",
|
|
12
|
-
phone: "",
|
|
13
|
-
role: "",
|
|
14
|
-
name: "John Wick",
|
|
15
|
-
username: "johnwick",
|
|
16
|
-
email: "johnwick@example.com"
|
|
17
|
-
}
|
|
18
|
-
const USER2: IUserCreate = {
|
|
19
|
-
active: true,
|
|
20
|
-
password: "12345678",
|
|
21
|
-
phone: "",
|
|
22
|
-
role: "",
|
|
23
|
-
name: "John Rambo",
|
|
24
|
-
username: "rambo",
|
|
25
|
-
email: "rambo@example.com"
|
|
26
|
-
}
|
|
27
|
-
const USER3: IUserCreate = {
|
|
28
|
-
active: true,
|
|
29
|
-
password: "12345678",
|
|
30
|
-
phone: "",
|
|
31
|
-
role: "",
|
|
32
|
-
name: "John Depp",
|
|
33
|
-
username: "depp",
|
|
34
|
-
email: "depp@example.com"
|
|
35
|
-
}
|
|
4
|
+
import {USER1} from "./data/users-data"
|
|
5
|
+
|
|
36
6
|
|
|
37
7
|
describe("User Route Test", async function () {
|
|
38
8
|
|
|
39
9
|
let testSetup = new TestSetup()
|
|
40
|
-
let FASTIFY_TEST_SERVER: any;
|
|
41
|
-
let ROOT_USER: any;
|
|
42
|
-
let ADMIN_ROLE: any;
|
|
43
|
-
let ACCESS_TOKEN: any;
|
|
44
10
|
|
|
45
11
|
beforeAll(async () => {
|
|
46
12
|
await testSetup.setup()
|
|
47
|
-
FASTIFY_TEST_SERVER = testSetup.fastifyInstance
|
|
48
|
-
ROOT_USER = testSetup.rootUser
|
|
49
|
-
ADMIN_ROLE = testSetup.adminRole
|
|
50
|
-
const {accessToken} = await testSetup.login()
|
|
51
|
-
ACCESS_TOKEN = accessToken
|
|
52
13
|
})
|
|
53
14
|
|
|
54
15
|
afterAll(async () => {
|
|
55
|
-
await testSetup.
|
|
16
|
+
await testSetup.dropAndClose()
|
|
56
17
|
return
|
|
57
18
|
})
|
|
58
19
|
|
|
59
|
-
it("
|
|
60
|
-
let {accessToken} = await testSetup.
|
|
20
|
+
it("RootLogin", async () => {
|
|
21
|
+
let {accessToken} = await testSetup.rootUserLogin()
|
|
61
22
|
expect(accessToken).toBeTruthy()
|
|
62
23
|
let user = await testSetup.me(accessToken)
|
|
63
24
|
expect(user.username).toBe(testSetup.rootUserData.username)
|
|
64
25
|
})
|
|
65
26
|
|
|
66
|
-
it("
|
|
27
|
+
it("BasicUserLogin", async () => {
|
|
28
|
+
let {accessToken} = await testSetup.basicUserLogin()
|
|
29
|
+
expect(accessToken).toBeTruthy()
|
|
30
|
+
let user = await testSetup.me(accessToken)
|
|
31
|
+
expect(user.username).toBe(testSetup.basicUserData.username)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("Login", async () => {
|
|
67
35
|
|
|
68
36
|
const loginResp = await testSetup.fastifyInstance.inject({
|
|
69
37
|
method: 'POST',
|
|
@@ -98,11 +66,14 @@ describe("User Route Test", async function () {
|
|
|
98
66
|
|
|
99
67
|
it("should create a new user", async () => {
|
|
100
68
|
|
|
101
|
-
const
|
|
69
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
70
|
+
expect(accessToken).toBeTruthy()
|
|
71
|
+
|
|
72
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
102
73
|
method: 'POST',
|
|
103
74
|
url: '/api/users',
|
|
104
|
-
payload: {...USER1, ...{role:
|
|
105
|
-
headers: {Authorization: `Bearer ${
|
|
75
|
+
payload: {...USER1, ...{role: testSetup.adminRole._id}},
|
|
76
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
106
77
|
});
|
|
107
78
|
|
|
108
79
|
const result = await resp.json();
|
|
@@ -111,11 +82,11 @@ describe("User Route Test", async function () {
|
|
|
111
82
|
expect(result._id).toBeDefined();
|
|
112
83
|
|
|
113
84
|
|
|
114
|
-
// Verify
|
|
115
|
-
const getResp = await
|
|
85
|
+
// Verify user was created by fetching it
|
|
86
|
+
const getResp = await testSetup.fastifyInstance.inject({
|
|
116
87
|
method: 'GET',
|
|
117
88
|
url: '/api/users/search?search=' + result._id,
|
|
118
|
-
headers: {Authorization: `Bearer ${
|
|
89
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
119
90
|
});
|
|
120
91
|
|
|
121
92
|
const items = await getResp.json();
|
|
@@ -125,44 +96,35 @@ describe("User Route Test", async function () {
|
|
|
125
96
|
|
|
126
97
|
|
|
127
98
|
it("Should paginate user", async () => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
for (const data of users) {
|
|
134
|
-
await FASTIFY_TEST_SERVER.inject({
|
|
135
|
-
method: 'POST',
|
|
136
|
-
url: '/api/users',
|
|
137
|
-
payload: {...data, ...{role: ADMIN_ROLE._id.toString()}},
|
|
138
|
-
headers: {Authorization: `Bearer ${ACCESS_TOKEN}`}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const resp = await FASTIFY_TEST_SERVER.inject({
|
|
99
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
100
|
+
expect(accessToken).toBeTruthy()
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
const resp = await testSetup.fastifyInstance.inject({
|
|
143
104
|
method: 'GET',
|
|
144
105
|
url: '/api/users',
|
|
145
|
-
headers: {Authorization: `Bearer ${
|
|
106
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
146
107
|
})
|
|
147
108
|
|
|
148
109
|
const result = await resp.json()
|
|
149
110
|
expect(resp.statusCode).toBe(200)
|
|
150
|
-
expect(result.items.length).toBe(
|
|
151
|
-
expect(result.items[0].name).toBe(
|
|
111
|
+
expect(result.items.length).toBe(3)
|
|
112
|
+
expect(result.items[0].name).toBe(testSetup.rootUser.name)
|
|
152
113
|
expect(result.page).toBe(1)
|
|
153
114
|
expect(result.limit).toBe(10)
|
|
154
|
-
expect(result.total).toBe(
|
|
115
|
+
expect(result.total).toBe(3)
|
|
155
116
|
|
|
156
117
|
})
|
|
157
118
|
|
|
158
119
|
it("should change my password", async () => {
|
|
120
|
+
const {accessToken} = await testSetup.basicUserLogin()
|
|
121
|
+
expect(accessToken).toBeTruthy()
|
|
159
122
|
|
|
160
|
-
|
|
161
|
-
const respPassword = await FASTIFY_TEST_SERVER.inject({
|
|
123
|
+
const respPassword = await testSetup.fastifyInstance.inject({
|
|
162
124
|
method: 'POST',
|
|
163
125
|
url: '/api/users/password/change',
|
|
164
|
-
payload: {currentPassword: "
|
|
165
|
-
headers: {Authorization: `Bearer ${
|
|
126
|
+
payload: {currentPassword: "basic.123", newPassword: "newpass"},
|
|
127
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
166
128
|
});
|
|
167
129
|
|
|
168
130
|
const resultPassword = await respPassword.json();
|
|
@@ -173,13 +135,14 @@ describe("User Route Test", async function () {
|
|
|
173
135
|
})
|
|
174
136
|
|
|
175
137
|
it("should change password", async () => {
|
|
138
|
+
const {accessToken} = await testSetup.rootUserLogin()
|
|
139
|
+
expect(accessToken).toBeTruthy()
|
|
176
140
|
|
|
177
|
-
|
|
178
|
-
const respPassword = await FASTIFY_TEST_SERVER.inject({
|
|
141
|
+
const respPassword = await testSetup.fastifyInstance.inject({
|
|
179
142
|
method: 'POST',
|
|
180
|
-
url: '/api/users/password/change/'+
|
|
143
|
+
url: '/api/users/password/change/'+testSetup.rootUser._id,
|
|
181
144
|
payload: {currentPassword: "root.123", newPassword: "newpass"},
|
|
182
|
-
headers: {Authorization: `Bearer ${
|
|
145
|
+
headers: {Authorization: `Bearer ${accessToken}`}
|
|
183
146
|
});
|
|
184
147
|
|
|
185
148
|
const resultPassword = await respPassword.json();
|