1
1
import { expect , test } from '@playwright/test' ;
2
2
3
3
import { appConfigs } from '../../presets' ;
4
- import type { FakeUser } from '../../testUtils' ;
4
+ import type { FakeOrganization , FakeUser } from '../../testUtils' ;
5
5
import { createTestUtils , testAgainstRunningApps } from '../../testUtils' ;
6
6
7
7
testAgainstRunningApps ( { withEnv : [ appConfigs . envs . withAPIKeys ] } ) ( 'api keys @generic' , ( { app } ) => {
8
8
test . describe . configure ( { mode : 'serial' } ) ;
9
9
10
- let fakeUser : FakeUser ;
10
+ let fakeAdmin : FakeUser ;
11
+ let fakeOrganization : FakeOrganization ;
11
12
12
13
test . beforeAll ( async ( ) => {
13
14
const u = createTestUtils ( { app } ) ;
14
- fakeUser = u . services . users . createFakeUser ( ) ;
15
- await u . services . users . createBapiUser ( fakeUser ) ;
15
+ fakeAdmin = u . services . users . createFakeUser ( ) ;
16
+ const admin = await u . services . users . createBapiUser ( fakeAdmin ) ;
17
+ fakeOrganization = await u . services . users . createFakeOrganization ( admin . id ) ;
16
18
} ) ;
17
19
18
20
test . afterAll ( async ( ) => {
19
- await fakeUser . deleteIfExists ( ) ;
21
+ await fakeOrganization . delete ( ) ;
22
+ await fakeAdmin . deleteIfExists ( ) ;
20
23
await app . teardown ( ) ;
21
24
} ) ;
22
25
23
26
test ( 'can create api keys' , async ( { page, context } ) => {
24
27
const u = createTestUtils ( { app, page, context } ) ;
25
28
await u . po . signIn . goTo ( ) ;
26
29
await u . po . signIn . waitForMounted ( ) ;
27
- await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeUser . email , password : fakeUser . password } ) ;
30
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeAdmin . email , password : fakeAdmin . password } ) ;
28
31
await u . po . expect . toBeSignedIn ( ) ;
29
32
30
33
await u . po . page . goToRelative ( '/api-keys' ) ;
@@ -33,7 +36,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
33
36
// Create API key 1
34
37
await u . po . apiKeys . clickAddButton ( ) ;
35
38
await u . po . apiKeys . waitForFormOpened ( ) ;
36
- await u . po . apiKeys . typeName ( `${ fakeUser . firstName } -api-key-1` ) ;
39
+ await u . po . apiKeys . typeName ( `${ fakeAdmin . firstName } -api-key-1` ) ;
37
40
await u . po . apiKeys . selectExpiration ( '1d' ) ;
38
41
await u . po . apiKeys . clickSaveButton ( ) ;
39
42
@@ -42,7 +45,7 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
42
45
// Create API key 2
43
46
await u . po . apiKeys . clickAddButton ( ) ;
44
47
await u . po . apiKeys . waitForFormOpened ( ) ;
45
- await u . po . apiKeys . typeName ( `${ fakeUser . firstName } -api-key-2` ) ;
48
+ await u . po . apiKeys . typeName ( `${ fakeAdmin . firstName } -api-key-2` ) ;
46
49
await u . po . apiKeys . selectExpiration ( '7d' ) ;
47
50
await u . po . apiKeys . clickSaveButton ( ) ;
48
51
@@ -54,13 +57,13 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
54
57
const u = createTestUtils ( { app, page, context } ) ;
55
58
await u . po . signIn . goTo ( ) ;
56
59
await u . po . signIn . waitForMounted ( ) ;
57
- await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeUser . email , password : fakeUser . password } ) ;
60
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeAdmin . email , password : fakeAdmin . password } ) ;
58
61
await u . po . expect . toBeSignedIn ( ) ;
59
62
60
63
await u . po . page . goToRelative ( '/api-keys' ) ;
61
64
await u . po . apiKeys . waitForMounted ( ) ;
62
65
63
- const apiKeyName = `${ fakeUser . firstName } -${ Date . now ( ) } ` ;
66
+ const apiKeyName = `${ fakeAdmin . firstName } -${ Date . now ( ) } ` ;
64
67
65
68
// Create API key
66
69
await u . po . apiKeys . clickAddButton ( ) ;
@@ -95,13 +98,13 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
95
98
const u = createTestUtils ( { app, page, context } ) ;
96
99
await u . po . signIn . goTo ( ) ;
97
100
await u . po . signIn . waitForMounted ( ) ;
98
- await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeUser . email , password : fakeUser . password } ) ;
101
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeAdmin . email , password : fakeAdmin . password } ) ;
99
102
await u . po . expect . toBeSignedIn ( ) ;
100
103
101
104
await u . po . page . goToRelative ( '/api-keys' ) ;
102
105
await u . po . apiKeys . waitForMounted ( ) ;
103
106
104
- const apiKeyName = `${ fakeUser . firstName } -${ Date . now ( ) } ` ;
107
+ const apiKeyName = `${ fakeAdmin . firstName } -${ Date . now ( ) } ` ;
105
108
106
109
// Create API key
107
110
await u . po . apiKeys . clickAddButton ( ) ;
@@ -133,13 +136,13 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
133
136
const u = createTestUtils ( { app, page, context } ) ;
134
137
await u . po . signIn . goTo ( ) ;
135
138
await u . po . signIn . waitForMounted ( ) ;
136
- await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeUser . email , password : fakeUser . password } ) ;
139
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeAdmin . email , password : fakeAdmin . password } ) ;
137
140
await u . po . expect . toBeSignedIn ( ) ;
138
141
139
142
await u . po . page . goToRelative ( '/api-keys' ) ;
140
143
await u . po . apiKeys . waitForMounted ( ) ;
141
144
142
- const apiKeyName = `${ fakeUser . firstName } -${ Date . now ( ) } ` ;
145
+ const apiKeyName = `${ fakeAdmin . firstName } -${ Date . now ( ) } ` ;
143
146
144
147
// Create API key
145
148
await u . po . apiKeys . clickAddButton ( ) ;
@@ -169,4 +172,82 @@ testAgainstRunningApps({ withEnv: [appConfigs.envs.withAPIKeys] })('api keys @ge
169
172
await row . locator ( '.cl-apiKeysRevealButton' ) . click ( ) ;
170
173
await expect ( row . locator ( 'input' ) ) . toHaveAttribute ( 'type' , 'password' ) ;
171
174
} ) ;
175
+
176
+ test ( 'component does not render for orgs when user does not have permissions' , async ( { page, context } ) => {
177
+ const u = createTestUtils ( { app, page, context } ) ;
178
+
179
+ const fakeMember = u . services . users . createFakeUser ( ) ;
180
+ const member = await u . services . users . createBapiUser ( fakeMember ) ;
181
+
182
+ await u . services . clerk . organizations . createOrganizationMembership ( {
183
+ organizationId : fakeOrganization . organization . id ,
184
+ role : 'org:member' ,
185
+ userId : member . id ,
186
+ } ) ;
187
+
188
+ await u . po . signIn . goTo ( ) ;
189
+ await u . po . signIn . waitForMounted ( ) ;
190
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeMember . email , password : fakeMember . password } ) ;
191
+ await u . po . expect . toBeSignedIn ( ) ;
192
+
193
+ let apiKeysRequestWasMade = false ;
194
+ u . page . on ( 'request' , request => {
195
+ if ( request . url ( ) . includes ( '/api_keys' ) ) {
196
+ apiKeysRequestWasMade = true ;
197
+ }
198
+ } ) ;
199
+
200
+ // Check that standalone component is not rendered
201
+ await u . po . page . goToRelative ( '/api-keys' ) ;
202
+ await expect ( u . page . locator ( '.cl-apiKeys-root' ) ) . toBeHidden ( { timeout : 1000 } ) ;
203
+
204
+ // Check that page is not rendered in OrganizationProfile
205
+ await u . po . page . goToRelative ( '/organization-profile#/organization-api-keys' ) ;
206
+ await expect ( u . page . locator ( '.cl-apiKeys-root' ) ) . toBeHidden ( { timeout : 1000 } ) ;
207
+
208
+ expect ( apiKeysRequestWasMade ) . toBe ( false ) ;
209
+
210
+ await fakeMember . deleteIfExists ( ) ;
211
+ } ) ;
212
+
213
+ test ( 'user with read permission can view API keys but not manage them' , async ( { page, context } ) => {
214
+ const u = createTestUtils ( { app, page, context } ) ;
215
+
216
+ const fakeViewer = u . services . users . createFakeUser ( ) ;
217
+ const viewer = await u . services . users . createBapiUser ( fakeViewer ) ;
218
+
219
+ await u . services . clerk . organizations . createOrganizationMembership ( {
220
+ organizationId : fakeOrganization . organization . id ,
221
+ role : 'org:viewer' ,
222
+ userId : viewer . id ,
223
+ } ) ;
224
+
225
+ await u . po . signIn . goTo ( ) ;
226
+ await u . po . signIn . waitForMounted ( ) ;
227
+ await u . po . signIn . signInWithEmailAndInstantPassword ( { email : fakeViewer . email , password : fakeViewer . password } ) ;
228
+ await u . po . expect . toBeSignedIn ( ) ;
229
+
230
+ let apiKeysRequestWasMade = false ;
231
+ u . page . on ( 'request' , request => {
232
+ if ( request . url ( ) . includes ( '/api_keys' ) ) {
233
+ apiKeysRequestWasMade = true ;
234
+ }
235
+ } ) ;
236
+
237
+ // Check that standalone component is rendered and user can read API keys
238
+ await u . po . page . goToRelative ( '/api-keys' ) ;
239
+ await u . po . apiKeys . waitForMounted ( ) ;
240
+ await expect ( u . page . getByRole ( 'button' , { name : / A d d n e w k e y / i } ) ) . toBeHidden ( ) ;
241
+ await expect ( u . page . getByRole ( 'columnheader' , { name : / A c t i o n s / i } ) ) . toBeHidden ( ) ;
242
+
243
+ // Check that page is rendered in OrganizationProfile and user can read API keys
244
+ await u . po . page . goToRelative ( '/organization-profile#/organization-api-keys' ) ;
245
+ await expect ( u . page . locator ( '.cl-apiKeys' ) ) . toBeVisible ( ) ;
246
+ await expect ( u . page . getByRole ( 'button' , { name : / A d d n e w k e y / i } ) ) . toBeHidden ( ) ;
247
+ await expect ( u . page . getByRole ( 'columnheader' , { name : / A c t i o n s / i } ) ) . toBeHidden ( ) ;
248
+
249
+ expect ( apiKeysRequestWasMade ) . toBe ( true ) ;
250
+
251
+ await fakeViewer . deleteIfExists ( ) ;
252
+ } ) ;
172
253
} ) ;
0 commit comments