@@ -5,40 +5,77 @@ const expect = chai.expect
5
5
const constants = require ( '../constants.js' )
6
6
const app = require ( '../../../src/index.js' )
7
7
8
- const params = { new_short_name : 'test_org' , name : 'Test Organization' , id_quota : 100000 }
8
+ const params = { name : 'Test Organization' , id_quota : 100 }
9
+ const secretariat_params = { name : 'MITRE Corporation' , id_quota : 100000 }
10
+ const cna_params = { name : 'Adams, Nielsen and Hensley' , id_quota : 1309 }
9
11
10
12
describe ( 'Testing org put endpoint' , ( ) => {
11
13
context ( 'Positive Tests' , ( ) => {
12
14
it ( 'Allows update made by a secretariat to itself' , async ( ) => {
13
15
await chai . request ( app )
14
16
. put ( '/api/org/mitre' )
15
17
. set ( { ...constants . headers } )
16
- . query ( { id_quota : '100000' } )
18
+ . query ( params )
17
19
. send ( )
18
20
. then ( ( res , err ) => {
19
- console . log ( res . text )
20
21
expect ( res ) . to . have . status ( 200 )
22
+ expect ( res . body . updated . name ) . to . equal ( params . name )
21
23
expect ( res . body . updated . policies . id_quota ) . to . equal ( params . id_quota )
22
24
expect ( err ) . to . be . undefined
23
25
} )
26
+ await chai . request ( app )
27
+ . put ( `/api/org/mitre` )
28
+ . set ( { ...constants . headers } )
29
+ . query ( secretariat_params )
30
+ . send ( )
31
+ . then ( ( res , err ) => {
32
+ expect ( res ) . to . have . status ( 200 )
33
+ expect ( res . body . updated . name ) . to . equal ( secretariat_params . name )
34
+ expect ( res . body . updated . policies . id_quota ) . to . equal ( secretariat_params . id_quota )
35
+ expect ( err ) . to . be . undefined
36
+ } )
24
37
} )
25
38
it ( 'Allows update made by a secretariat to another org' , async ( ) => {
26
39
await chai . request ( app )
27
- . put ( '/api/org/cause_8 ' )
40
+ . put ( '/api/org/win_5 ' )
28
41
. set ( { ...constants . headers } )
42
+ . query ( params )
29
43
. send ( )
30
44
. then ( ( res , err ) => {
31
45
expect ( res ) . to . have . status ( 200 )
46
+ expect ( res . body . updated . name ) . to . equal ( params . name )
47
+ expect ( res . body . updated . policies . id_quota ) . to . equal ( params . id_quota )
48
+ expect ( err ) . to . be . undefined
49
+ } )
50
+ await chai . request ( app )
51
+ . put ( '/api/org/win_5' )
52
+ . set ( { ...constants . headers } )
53
+ . query ( cna_params )
54
+ . send ( )
55
+ . then ( ( res , err ) => {
56
+ expect ( res ) . to . have . status ( 200 )
57
+ expect ( res . body . updated . name ) . to . equal ( cna_params . name )
58
+ expect ( res . body . updated . policies . id_quota ) . to . equal ( cna_params . id_quota )
32
59
expect ( err ) . to . be . undefined
33
60
} )
34
61
} )
35
- it ( 'Allows update made by non secretariat org to itself' , async ( ) => {
62
+ it ( 'Update made by non secretariat org to itself ONLY updates last_active field' , async ( ) => {
63
+ let now = Date . now ( )
36
64
await chai . request ( app )
37
65
. put ( '/api/org/win_5' )
38
66
. set ( { ...constants . nonSecretariatUserHeaders } )
67
+ . query ( params )
39
68
. send ( )
40
69
. then ( ( res , err ) => {
70
+ // Assert that that the last_active field was updated under 2 seconds ago
71
+ let last_active = Date . parse ( res . body . updated . last_active )
72
+ let diff = Math . abs ( now - last_active )
73
+ let within_two_seconds = diff < 2000
74
+ expect ( within_two_seconds ) . to . be . true
75
+ // Assert no other fields were changed
41
76
expect ( res ) . to . have . status ( 200 )
77
+ expect ( res . body . updated . name ) . to . equal ( cna_params . name )
78
+ expect ( res . body . updated . policies . id_quota ) . to . equal ( cna_params . id_quota )
42
79
expect ( err ) . to . be . undefined
43
80
} )
44
81
} )
@@ -54,5 +91,15 @@ describe('Testing org put endpoint', () => {
54
91
expect ( err ) . to . be . undefined
55
92
} )
56
93
} )
94
+ it ( 'Fails update made by a non-secretariat org to a secretariat' , async ( ) => {
95
+ await chai . request ( app )
96
+ . put ( '/api/org/mitre' )
97
+ . set ( { ...constants . nonSecretariatUserHeaders } )
98
+ . send ( )
99
+ . then ( ( res , err ) => {
100
+ expect ( res ) . to . have . status ( 401 )
101
+ expect ( err ) . to . be . undefined
102
+ } )
103
+ } )
57
104
} )
58
105
} )
0 commit comments