4
4
*/
5
5
6
6
import { module , test } from 'qunit' ;
7
- import {
8
- visit ,
9
- currentURL ,
10
- click ,
11
- fillIn ,
12
- find ,
13
- findAll ,
14
- } from '@ember/test-helpers' ;
7
+ import { visit , currentURL , click , fillIn } from '@ember/test-helpers' ;
15
8
import { setupApplicationTest } from 'admin/tests/helpers' ;
16
9
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage' ;
17
10
import a11yAudit from 'ember-a11y-testing/test-support/audit' ;
18
11
import { Response } from 'miragejs' ;
19
12
import { authenticateSession } from 'ember-simple-auth/test-support' ;
13
+ import * as selectors from './selectors' ;
20
14
import * as commonSelectors from 'admin/tests/helpers/selectors' ;
21
15
22
16
module ( 'Acceptance | roles | grants' , function ( hooks ) {
23
17
setupApplicationTest ( hooks ) ;
24
18
setupMirage ( hooks ) ;
25
19
20
+ // This is unique to permissions tests and only used once here
21
+ const FIELD_GRANT_DISABLED = 'input[disabled]' ;
22
+
23
+ let grantsCount ;
24
+
26
25
const instances = {
27
26
scopes : {
28
27
global : null ,
@@ -31,14 +30,10 @@ module('Acceptance | roles | grants', function (hooks) {
31
30
role : null ,
32
31
} ;
33
32
const urls = {
34
- orgScope : null ,
35
33
roles : null ,
36
34
role : null ,
37
35
newRole : null ,
38
36
} ;
39
- const newGrantForm = 'form:nth-child(1)' ;
40
- const grantsForm = 'form:nth-child(2)' ;
41
- let grantsCount ;
42
37
43
38
hooks . beforeEach ( async function ( ) {
44
39
await authenticateSession ( { username : 'admin' } ) ;
@@ -50,20 +45,19 @@ module('Acceptance | roles | grants', function (hooks) {
50
45
instances . role = this . server . create ( 'role' , {
51
46
scope : instances . scopes . org ,
52
47
} ) ;
53
- grantsCount = this . server . db . roles [ 0 ] . grant_strings . length ;
54
48
urls . roles = `/scopes/${ instances . scopes . org . id } /roles` ;
55
49
urls . role = `${ urls . roles } /${ instances . role . id } ` ;
56
50
urls . grants = `${ urls . role } /grants` ;
51
+ grantsCount = ( ) =>
52
+ this . server . schema . roles . all ( ) . models [ 0 ] . grant_strings . length ;
57
53
} ) ;
58
54
59
55
test ( 'visiting role grants' , async function ( assert ) {
60
56
await visit ( urls . grants ) ;
61
57
await a11yAudit ( ) ;
58
+
62
59
assert . strictEqual ( currentURL ( ) , urls . grants ) ;
63
- assert . strictEqual (
64
- findAll ( `${ grantsForm } [name="grant"]` ) . length ,
65
- grantsCount ,
66
- ) ;
60
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
67
61
} ) ;
68
62
69
63
test ( 'cannot set grants without proper authorization' , async function ( assert ) {
@@ -72,17 +66,13 @@ module('Acceptance | roles | grants', function (hooks) {
72
66
) ;
73
67
instances . role . update ( { authorized_actions } ) ;
74
68
await visit ( urls . grants ) ;
75
- assert . strictEqual (
76
- findAll ( 'main form' ) . length ,
77
- 1 ,
78
- 'New grant form is not displayed.' ,
79
- ) ;
80
- assert . notOk (
81
- find ( 'main form button:not([type="submit"])' ) ,
82
- 'Grant delete button is not displayed.' ,
83
- ) ;
84
- assert . notOk ( find ( '.rose-form-actions' ) , 'Form actions are not displayed.' ) ;
85
- assert . ok ( find ( 'main form input[disabled]' ) , 'Grant fields are disabled.' ) ;
69
+
70
+ // This checks that only the form for existing grants is displayed
71
+ assert . dom ( commonSelectors . FORM ) . exists ( { count : 1 } ) ;
72
+ assert . dom ( selectors . FIELD_NEW_GRANT_ADD_BTN ) . doesNotExist ( ) ;
73
+ assert . dom ( FIELD_GRANT_DISABLED ) . exists ( { count : grantsCount ( ) } ) ;
74
+ assert . dom ( selectors . SAVE_BTN ) . doesNotExist ( ) ;
75
+ assert . dom ( commonSelectors . CANCEL_BTN ) . doesNotExist ( ) ;
86
76
} ) ;
87
77
88
78
test ( 'update a grant' , async function ( assert ) {
@@ -93,26 +83,28 @@ module('Acceptance | roles | grants', function (hooks) {
93
83
const attrs = JSON . parse ( requestBody ) ;
94
84
assert . strictEqual (
95
85
attrs . grant_strings [ 0 ] ,
96
- 'ids=123,action=delete' ,
86
+ selectors . FIELD_GRANT_VALUE ,
97
87
'A grant is updated' ,
98
88
) ;
99
89
const id = idMethod . split ( ':' ) [ 0 ] ;
100
90
return { id } ;
101
91
} ,
102
92
) ;
103
93
await visit ( urls . grants ) ;
104
- await fillIn ( `${ grantsForm } [name="grant"]` , 'ids=123,action=delete' ) ;
105
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
94
+
95
+ await fillIn ( selectors . FIELD_GRANT , selectors . FIELD_GRANT_VALUE ) ;
96
+ await click ( selectors . SAVE_BTN ) ;
106
97
} ) ;
107
98
108
99
test ( 'cancel a grant update' , async function ( assert ) {
109
100
await visit ( urls . grants ) ;
110
- await fillIn ( `${ grantsForm } [name="grant"]` , 'ids=123,action=delete' ) ;
111
- await click ( '.rose-form-actions button:not([type="submit"])' ) ;
112
- assert . notEqual (
113
- find ( `${ grantsForm } [name="grant"]` ) . value ,
114
- 'ids=123,action=delete' ,
115
- ) ;
101
+
102
+ await fillIn ( selectors . FIELD_GRANT , selectors . FIELD_GRANT_VALUE ) ;
103
+ await click ( commonSelectors . CANCEL_BTN ) ;
104
+
105
+ assert
106
+ . dom ( selectors . FIELD_GRANT )
107
+ . doesNotIncludeText ( selectors . FIELD_GRANT_VALUE ) ;
116
108
} ) ;
117
109
118
110
test ( 'shows error message on grant update' , async function ( assert ) {
@@ -129,12 +121,12 @@ module('Acceptance | roles | grants', function (hooks) {
129
121
) ;
130
122
} ) ;
131
123
await visit ( urls . grants ) ;
132
- assert . strictEqual (
133
- findAll ( ` ${ grantsForm } [name="grant"]` ) . length ,
134
- grantsCount ,
135
- ) ;
136
- await fillIn ( ` ${ grantsForm } [name="grant"]` , 'ids=123,action=delete' ) ;
137
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
124
+
125
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
126
+
127
+ await fillIn ( selectors . FIELD_GRANT , selectors . FIELD_GRANT_VALUE ) ;
128
+ await click ( selectors . SAVE_BTN ) ;
129
+
138
130
assert
139
131
. dom ( commonSelectors . ALERT_TOAST_BODY )
140
132
. hasText ( 'The request was invalid.' ) ;
@@ -148,25 +140,31 @@ module('Acceptance | roles | grants', function (hooks) {
148
140
const attrs = JSON . parse ( requestBody ) ;
149
141
assert . strictEqual (
150
142
attrs . grant_strings . length ,
151
- grantsCount + 1 ,
143
+ grantsCount ( ) + 1 ,
152
144
'A grant is created' ,
153
145
) ;
154
146
const id = idMethod . split ( ':' ) [ 0 ] ;
155
147
return { id } ;
156
148
} ,
157
149
) ;
158
150
await visit ( urls . grants ) ;
159
- await fillIn ( `${ newGrantForm } [name="grant"]` , 'ids=123,action=delete' ) ;
160
- await click ( `${ newGrantForm } [type="submit"]:not(:disabled)` ) ;
161
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
151
+
152
+ await fillIn ( selectors . FIELD_NEW_GRANT , selectors . FIELD_GRANT_VALUE ) ;
153
+ await click ( selectors . FIELD_NEW_GRANT_ADD_BTN ) ;
154
+ await click ( selectors . SAVE_BTN ) ;
162
155
} ) ;
163
156
164
157
test ( 'cancel a grant creation' , async function ( assert ) {
165
158
await visit ( urls . grants ) ;
166
- await fillIn ( `${ newGrantForm } [name="grant"]` , 'ids=123,action=delete' ) ;
167
- await click ( `${ newGrantForm } [type="submit"]:not(:disabled)` ) ;
168
- await click ( '.rose-form-actions button:not([type="submit"])' ) ;
169
- assert . notOk ( find ( `${ newGrantForm } [name="grant"]` ) . value ) ;
159
+
160
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
161
+
162
+ await fillIn ( selectors . FIELD_NEW_GRANT , selectors . FIELD_GRANT_VALUE ) ;
163
+ await click ( selectors . FIELD_NEW_GRANT_ADD_BTN ) ;
164
+ await click ( commonSelectors . CANCEL_BTN ) ;
165
+
166
+ assert . dom ( selectors . FIELD_NEW_GRANT ) . hasNoText ( ) ;
167
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
170
168
} ) ;
171
169
172
170
test ( 'shows error message on grant create' , async function ( assert ) {
@@ -183,34 +181,34 @@ module('Acceptance | roles | grants', function (hooks) {
183
181
) ;
184
182
} ) ;
185
183
await visit ( urls . grants ) ;
186
- assert . strictEqual (
187
- findAll ( `${ grantsForm } [name="grant"]` ) . length ,
188
- grantsCount ,
189
- ) ;
190
- await fillIn ( `${ newGrantForm } [name="grant"]` , 'ids=123,action=delete' ) ;
191
- await click ( `${ newGrantForm } [type="submit"]:not(:disabled)` ) ;
192
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
193
- assert . ok ( find ( commonSelectors . ALERT_TOAST_BODY ) ) ;
184
+
185
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
186
+
187
+ await fillIn ( selectors . FIELD_NEW_GRANT , selectors . FIELD_GRANT_VALUE ) ;
188
+ await click ( selectors . FIELD_NEW_GRANT_ADD_BTN ) ;
189
+ await click ( selectors . SAVE_BTN ) ;
190
+
191
+ assert
192
+ . dom ( commonSelectors . ALERT_TOAST_BODY )
193
+ . hasText ( 'The request was invalid.' ) ;
194
194
} ) ;
195
195
196
196
test ( 'delete a grant' , async function ( assert ) {
197
197
await visit ( urls . grants ) ;
198
- await click ( `${ grantsForm } button:not([type="submit"])` ) ;
199
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
200
- assert . strictEqual (
201
- findAll ( `${ grantsForm } [name="grant"]` ) . length ,
202
- grantsCount - 1 ,
203
- ) ;
198
+
199
+ await click ( selectors . FIELD_GRANT_REMOVE_BTN ) ;
200
+ await click ( selectors . SAVE_BTN ) ;
201
+
202
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) - 1 } ) ;
204
203
} ) ;
205
204
206
205
test ( 'cancel a grant remove' , async function ( assert ) {
207
206
await visit ( urls . grants ) ;
208
- await click ( `${ grantsForm } button` ) ;
209
- await click ( '.rose-form-actions button:not([type="submit"])' ) ;
210
- assert . strictEqual (
211
- findAll ( `${ grantsForm } [name="grant"]` ) . length ,
212
- grantsCount ,
213
- ) ;
207
+
208
+ await click ( selectors . FIELD_GRANT_REMOVE_BTN ) ;
209
+ await click ( commonSelectors . CANCEL_BTN ) ;
210
+
211
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
214
212
} ) ;
215
213
216
214
test ( 'shows error message on grant remove' , async function ( assert ) {
@@ -227,12 +225,14 @@ module('Acceptance | roles | grants', function (hooks) {
227
225
) ;
228
226
} ) ;
229
227
await visit ( urls . grants ) ;
230
- assert . strictEqual (
231
- findAll ( `${ grantsForm } [name="grant"]` ) . length ,
232
- grantsCount ,
233
- ) ;
234
- await click ( `${ grantsForm } button:not([type="submit"])` ) ;
235
- await click ( '.rose-form-actions [type="submit"]:not(:disabled)' ) ;
236
- assert . ok ( find ( commonSelectors . ALERT_TOAST_BODY ) ) ;
228
+
229
+ assert . dom ( selectors . FIELD_GRANT ) . exists ( { count : grantsCount ( ) } ) ;
230
+
231
+ await click ( selectors . FIELD_GRANT_REMOVE_BTN ) ;
232
+ await click ( selectors . SAVE_BTN ) ;
233
+
234
+ assert
235
+ . dom ( commonSelectors . ALERT_TOAST_BODY )
236
+ . hasText ( 'The request was invalid.' ) ;
237
237
} ) ;
238
238
} ) ;
0 commit comments