1
+ /* eslint-disable no-unused-expressions */
1
2
const express = require ( 'express' )
2
3
const app = express ( )
3
4
const chai = require ( 'chai' )
@@ -38,7 +39,9 @@ class OrgUpdatedAddingRole {
38
39
}
39
40
40
41
async aggregate ( ) {
41
- return [ orgFixtures . owningOrg ]
42
+ const org = orgFixtures . owningOrg
43
+ org . last_active = Date . now ( )
44
+ return [ org ]
42
45
}
43
46
44
47
async updateByOrgUUID ( ) {
@@ -60,7 +63,9 @@ class OrgUpdatedRemovingRole {
60
63
}
61
64
62
65
async aggregate ( ) {
63
- return [ orgFixtures . owningOrg ]
66
+ const org = orgFixtures . owningOrg
67
+ org . last_active = Date . now ( )
68
+ return [ org ]
64
69
}
65
70
66
71
async updateByOrgUUID ( ) {
@@ -172,6 +177,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
172
177
expect ( res . body . updated . name ) . to . equal ( orgFixtures . owningOrg . name )
173
178
expect ( res . body . updated . UUID ) . to . equal ( orgFixtures . owningOrg . UUID )
174
179
expect ( res . body . updated . policies . id_quota ) . to . equal ( orgFixtures . owningOrg . policies . id_quota )
180
+ const now = Date . now ( )
181
+ const lastActive = res . body . updated . last_active
182
+ const diff = Math . abs ( now - lastActive )
183
+ const withinTwoSeconds = diff < 500
184
+ expect ( withinTwoSeconds ) . to . be . true
175
185
done ( )
176
186
} )
177
187
} )
@@ -207,6 +217,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
207
217
expect ( res . body . updated . name ) . to . equal ( orgFixtures . owningOrg . name )
208
218
expect ( res . body . updated . UUID ) . to . equal ( orgFixtures . owningOrg . UUID )
209
219
expect ( res . body . updated . policies . id_quota ) . to . equal ( orgFixtures . owningOrg . policies . id_quota )
220
+ const now = Date . now ( )
221
+ const lastActive = res . body . updated . last_active
222
+ const diff = Math . abs ( now - lastActive )
223
+ const withinTwoSeconds = diff < 500
224
+ expect ( withinTwoSeconds ) . to . be . true
210
225
done ( )
211
226
} )
212
227
} )
@@ -241,6 +256,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
241
256
expect ( res . body . updated . name ) . to . equal ( orgFixtures . owningOrg . name )
242
257
expect ( res . body . updated . UUID ) . to . equal ( orgFixtures . owningOrg . UUID )
243
258
expect ( res . body . updated . policies . id_quota ) . to . equal ( orgFixtures . owningOrg . policies . id_quota )
259
+ const now = Date . now ( )
260
+ const lastActive = res . body . updated . last_active
261
+ const diff = Math . abs ( now - lastActive )
262
+ const withinTwoSeconds = diff < 500
263
+ expect ( withinTwoSeconds ) . to . be . true
244
264
done ( )
245
265
} )
246
266
} )
@@ -275,6 +295,11 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
275
295
expect ( res . body . updated . name ) . to . equal ( orgFixtures . owningOrg . name )
276
296
expect ( res . body . updated . UUID ) . to . equal ( orgFixtures . owningOrg . UUID )
277
297
expect ( res . body . updated . policies . id_quota ) . to . equal ( orgFixtures . owningOrg . policies . id_quota )
298
+ const now = Date . now ( )
299
+ const lastActive = res . body . updated . last_active
300
+ const diff = Math . abs ( now - lastActive )
301
+ const withinTwoSeconds = diff < 500
302
+ expect ( withinTwoSeconds ) . to . be . true
278
303
done ( )
279
304
} )
280
305
} )
@@ -295,7 +320,9 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
295
320
}
296
321
297
322
async aggregate ( ) {
298
- return [ orgFixtures . existentOrg ]
323
+ const org = orgFixtures . existentOrg
324
+ org . last_active = Date . now ( )
325
+ return [ org ]
299
326
}
300
327
301
328
async isSecretariat ( ) {
@@ -328,6 +355,69 @@ describe('Testing the PUT /org/:shortname endpoint in Org Controller', () => {
328
355
expect ( res . body . updated . name ) . to . equal ( orgFixtures . existentOrg . name )
329
356
expect ( res . body . updated . short_name ) . to . equal ( orgFixtures . existentOrg . short_name )
330
357
expect ( res . body . updated . UUID ) . to . equal ( orgFixtures . existentOrg . UUID )
358
+ const now = Date . now ( )
359
+ const lastActive = res . body . updated . last_active
360
+ const diff = Math . abs ( now - lastActive )
361
+ const withinTwoSeconds = diff < 500
362
+ expect ( withinTwoSeconds ) . to . be . true
363
+ } )
364
+
365
+ it ( 'Non-secretariat only last_active field updated' , ( done ) => {
366
+ class NonSecretariat {
367
+ async findOneByShortName ( ) {
368
+ return orgFixtures . owningOrg
369
+ }
370
+
371
+ async aggregate ( ) {
372
+ const org = orgFixtures . owningOrg
373
+ org . last_active = Date . now ( )
374
+ return [ org ]
375
+ }
376
+
377
+ async updateByOrgUUID ( ) {
378
+ return { n : 1 }
379
+ }
380
+
381
+ async getOrgUUID ( ) {
382
+ return null
383
+ }
384
+
385
+ async isSecretariat ( ) {
386
+ return false
387
+ }
388
+ }
389
+ app . route ( '/org-non-secretariat-updates-last-active-field/:shortname' )
390
+ . put ( ( req , res , next ) => {
391
+ const factory = {
392
+ getOrgRepository : ( ) => { return new NonSecretariat ( ) } ,
393
+ getUserRepository : ( ) => { return new NullUserRepo ( ) }
394
+ }
395
+ req . ctx . repositories = factory
396
+ next ( )
397
+ } , orgParams . parsePostParams , orgController . ORG_UPDATE_SINGLE )
398
+
399
+ chai . request ( app )
400
+ . put ( `/org-non-secretariat-updates-last-active-field/${ orgFixtures . owningOrg . short_name } ?name=TestOrg` )
401
+ . set ( orgFixtures . owningOrgHeader )
402
+ . end ( ( err , res ) => {
403
+ if ( err ) {
404
+ done ( err )
405
+ }
406
+ expect ( res ) . to . have . status ( 200 )
407
+ expect ( res ) . to . have . property ( 'body' ) . and . to . be . a ( 'object' )
408
+ expect ( res . body ) . to . have . property ( 'updated' ) . and . to . be . a ( 'object' )
409
+ // Assert that that the last_active field was updated under 0.5 seconds ago
410
+ const now = Date . now ( )
411
+ const lastActive = res . body . updated . last_active
412
+ const diff = Math . abs ( now - lastActive )
413
+ const withinTwoSeconds = diff < 500
414
+ expect ( withinTwoSeconds ) . to . be . true
415
+ // Assert no other fields were changed
416
+ expect ( res . body . updated . active_roles ) . to . be . undefined
417
+ expect ( res . body . updated . name ) . to . be . undefined
418
+ expect ( res . body . updated . policies ) . to . be . undefined
419
+ done ( )
420
+ } )
331
421
} )
332
422
} )
333
423
} )
0 commit comments