@@ -13,7 +13,7 @@ import (
13
13
"github.com/coder/coder/v2/testutil"
14
14
)
15
15
16
- func TestOAuthApps (t * testing.T ) {
16
+ func TestOAuth2ProviderApps (t * testing.T ) {
17
17
t .Parallel ()
18
18
19
19
t .Run ("Validation" , func (t * testing.T ) {
@@ -162,71 +162,62 @@ func TestOAuthApps(t *testing.T) {
162
162
t .Run ("DeleteNonExisting" , func (t * testing.T ) {
163
163
t .Parallel ()
164
164
165
- client , _ := coderdenttest .New (t , & coderdenttest.Options {LicenseOptions : & coderdenttest.LicenseOptions {
165
+ client , owner := coderdenttest .New (t , & coderdenttest.Options {LicenseOptions : & coderdenttest.LicenseOptions {
166
166
Features : license.Features {
167
167
codersdk .FeatureOAuth2Provider : 1 ,
168
168
},
169
169
}})
170
+ another , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
170
171
171
172
ctx := testutil .Context (t , testutil .WaitLong )
172
173
173
- //nolint:gocritic // OAauth2 app management requires owner permission.
174
- _ , err := client .OAuth2ProviderApp (ctx , uuid .New ())
174
+ _ , err := another .OAuth2ProviderApp (ctx , uuid .New ())
175
175
require .Error (t , err )
176
176
})
177
177
178
178
t .Run ("OK" , func (t * testing.T ) {
179
179
t .Parallel ()
180
180
181
- client , _ := coderdenttest .New (t , & coderdenttest.Options {LicenseOptions : & coderdenttest.LicenseOptions {
181
+ client , owner := coderdenttest .New (t , & coderdenttest.Options {LicenseOptions : & coderdenttest.LicenseOptions {
182
182
Features : license.Features {
183
183
codersdk .FeatureOAuth2Provider : 1 ,
184
184
},
185
185
}})
186
+ another , _ := coderdtest .CreateAnotherUser (t , client , owner .OrganizationID )
186
187
187
188
ctx := testutil .Context (t , testutil .WaitLong )
188
189
189
190
// No apps yet.
190
- //nolint:gocritic // OAauth2 app management requires owner permission.
191
- apps , err := client .OAuth2ProviderApps (ctx )
191
+ apps , err := another .OAuth2ProviderApps (ctx )
192
192
require .NoError (t , err )
193
193
require .Len (t , apps , 0 )
194
194
195
195
// Should be able to add apps.
196
- expected := []codersdk.OAuth2ProviderApp {}
197
- for i := 0 ; i < 5 ; i ++ {
198
- postReq := codersdk.PostOAuth2ProviderAppRequest {
199
- Name : "foo-" + strconv .Itoa (i ),
200
- CallbackURL : "http://" + strconv .Itoa (i ) + ".localhost:3000" ,
201
- }
202
- //nolint:gocritic // OAauth2 app management requires owner permission.
203
- app , err := client .PostOAuth2ProviderApp (ctx , postReq )
204
- require .NoError (t , err )
205
- require .Equal (t , postReq .Name , app .Name )
206
- require .Equal (t , postReq .CallbackURL , app .CallbackURL )
207
- expected = append (expected , app )
196
+ expected := generateApps (ctx , t , client , "get-apps" )
197
+ expectedOrder := []codersdk.OAuth2ProviderApp {
198
+ expected .Default , expected .NoPort , expected .Subdomain ,
199
+ expected .Extra [0 ], expected .Extra [1 ],
208
200
}
209
201
210
202
// Should get all the apps now.
211
- //nolint:gocritic // OAauth2 app management requires owner permission.
212
- apps , err = client .OAuth2ProviderApps (ctx )
203
+ apps , err = another .OAuth2ProviderApps (ctx )
213
204
require .NoError (t , err )
214
205
require .Len (t , apps , 5 )
215
- require .Equal (t , expected , apps )
206
+ require .Equal (t , expectedOrder , apps )
216
207
217
208
// Should be able to keep the same name when updating.
218
209
req := codersdk.PutOAuth2ProviderAppRequest {
219
- Name : expected [ 0 ] .Name ,
210
+ Name : expected . Default .Name ,
220
211
CallbackURL : "http://coder.com" ,
221
212
Icon : "test" ,
222
213
}
223
214
//nolint:gocritic // OAauth2 app management requires owner permission.
224
- newApp , err := client .PutOAuth2ProviderApp (ctx , expected [ 0 ] .ID , req )
215
+ newApp , err := client .PutOAuth2ProviderApp (ctx , expected . Default .ID , req )
225
216
require .NoError (t , err )
226
217
require .Equal (t , req .Name , newApp .Name )
227
218
require .Equal (t , req .CallbackURL , newApp .CallbackURL )
228
219
require .Equal (t , req .Icon , newApp .Icon )
229
- require .Equal (t , expected [ 0 ] .ID , newApp .ID )
220
+ require .Equal (t , expected . Default .ID , newApp .ID )
230
221
231
222
// Should be able to update name.
232
223
req = codersdk.PutOAuth2ProviderAppRequest {
@@ -235,34 +226,33 @@ func TestOAuthApps(t *testing.T) {
235
226
Icon : "test" ,
236
227
}
237
228
//nolint:gocritic // OAauth2 app management requires owner permission.
238
- newApp , err = client .PutOAuth2ProviderApp (ctx , expected [ 0 ] .ID , req )
229
+ newApp , err = client .PutOAuth2ProviderApp (ctx , expected . Default .ID , req )
239
230
require .NoError (t , err )
240
231
require .Equal (t , req .Name , newApp .Name )
241
232
require .Equal (t , req .CallbackURL , newApp .CallbackURL )
242
233
require .Equal (t , req .Icon , newApp .Icon )
243
- require .Equal (t , expected [ 0 ] .ID , newApp .ID )
234
+ require .Equal (t , expected . Default .ID , newApp .ID )
244
235
245
236
// Should be able to get a single app.
246
- //nolint:gocritic // OAauth2 app management requires owner permission.
247
- got , err := client .OAuth2ProviderApp (ctx , expected [0 ].ID )
237
+ got , err := another .OAuth2ProviderApp (ctx , expected .Default .ID )
248
238
require .NoError (t , err )
249
239
require .Equal (t , newApp , got )
250
240
251
241
// Should be able to delete an app.
252
242
//nolint:gocritic // OAauth2 app management requires owner permission.
253
- err = client .DeleteOAuth2ProviderApp (ctx , expected [ 0 ] .ID )
243
+ err = client .DeleteOAuth2ProviderApp (ctx , expected . Default .ID )
254
244
require .NoError (t , err )
255
245
256
246
// Should show the new count.
257
- //nolint:gocritic // OAauth2 app management requires owner permission.
258
- newApps , err := client .OAuth2ProviderApps (ctx )
247
+ newApps , err := another .OAuth2ProviderApps (ctx )
259
248
require .NoError (t , err )
260
249
require .Len (t , newApps , 4 )
261
- require .Equal (t , expected [1 :], newApps )
250
+
251
+ require .Equal (t , expectedOrder [1 :], newApps )
262
252
})
263
253
}
264
254
265
- func TestOAuthAppSecrets (t * testing.T ) {
255
+ func TestOAuth2ProviderAppSecrets (t * testing.T ) {
266
256
t .Parallel ()
267
257
268
258
client , _ := coderdenttest .New (t , & coderdenttest.Options {LicenseOptions : & coderdenttest.LicenseOptions {
@@ -274,27 +264,15 @@ func TestOAuthAppSecrets(t *testing.T) {
274
264
topCtx := testutil .Context (t , testutil .WaitLong )
275
265
276
266
// Make some apps.
277
- //nolint:gocritic // OAauth2 app management requires owner permission.
278
- app1 , err := client .PostOAuth2ProviderApp (topCtx , codersdk.PostOAuth2ProviderAppRequest {
279
- Name : "razzle-dazzle" ,
280
- CallbackURL : "http://localhost" ,
281
- })
282
- require .NoError (t , err )
283
-
284
- //nolint:gocritic // OAauth2 app management requires owner permission.
285
- app2 , err := client .PostOAuth2ProviderApp (topCtx , codersdk.PostOAuth2ProviderAppRequest {
286
- Name : "razzle-dazzle-the-sequel" ,
287
- CallbackURL : "http://localhost" ,
288
- })
289
- require .NoError (t , err )
267
+ apps := generateApps (topCtx , t , client , "app-secrets" )
290
268
291
269
t .Run ("DeleteNonExisting" , func (t * testing.T ) {
292
270
t .Parallel ()
293
271
ctx := testutil .Context (t , testutil .WaitLong )
294
272
295
273
// Should not be able to create secrets for a non-existent app.
296
274
//nolint:gocritic // OAauth2 app management requires owner permission.
297
- _ , err = client .OAuth2ProviderAppSecrets (ctx , uuid .New ())
275
+ _ , err : = client .OAuth2ProviderAppSecrets (ctx , uuid .New ())
298
276
require .Error (t , err )
299
277
300
278
// Should not be able to delete non-existing secrets when there is no app.
@@ -304,16 +282,16 @@ func TestOAuthAppSecrets(t *testing.T) {
304
282
305
283
// Should not be able to delete non-existing secrets when the app exists.
306
284
//nolint:gocritic // OAauth2 app management requires owner permission.
307
- err = client .DeleteOAuth2ProviderAppSecret (ctx , app1 .ID , uuid .New ())
285
+ err = client .DeleteOAuth2ProviderAppSecret (ctx , apps . Default .ID , uuid .New ())
308
286
require .Error (t , err )
309
287
310
288
// Should not be able to delete an existing secret with the wrong app ID.
311
289
//nolint:gocritic // OAauth2 app management requires owner permission.
312
- secret , err := client .PostOAuth2ProviderAppSecret (ctx , app2 .ID )
290
+ secret , err := client .PostOAuth2ProviderAppSecret (ctx , apps . NoPort .ID )
313
291
require .NoError (t , err )
314
292
315
293
//nolint:gocritic // OAauth2 app management requires owner permission.
316
- err = client .DeleteOAuth2ProviderAppSecret (ctx , app1 .ID , secret .ID )
294
+ err = client .DeleteOAuth2ProviderAppSecret (ctx , apps . Default .ID , secret .ID )
317
295
require .Error (t , err )
318
296
})
319
297
@@ -323,26 +301,26 @@ func TestOAuthAppSecrets(t *testing.T) {
323
301
324
302
// No secrets yet.
325
303
//nolint:gocritic // OAauth2 app management requires owner permission.
326
- secrets , err := client .OAuth2ProviderAppSecrets (ctx , app1 .ID )
304
+ secrets , err := client .OAuth2ProviderAppSecrets (ctx , apps . Default .ID )
327
305
require .NoError (t , err )
328
306
require .Len (t , secrets , 0 )
329
307
330
308
// Should be able to create secrets.
331
309
for i := 0 ; i < 5 ; i ++ {
332
310
//nolint:gocritic // OAauth2 app management requires owner permission.
333
- secret , err := client .PostOAuth2ProviderAppSecret (ctx , app1 .ID )
311
+ secret , err := client .PostOAuth2ProviderAppSecret (ctx , apps . Default .ID )
334
312
require .NoError (t , err )
335
313
require .NotEmpty (t , secret .ClientSecretFull )
336
314
require .True (t , len (secret .ClientSecretFull ) > 6 )
337
315
338
316
//nolint:gocritic // OAauth2 app management requires owner permission.
339
- _ , err = client .PostOAuth2ProviderAppSecret (ctx , app2 .ID )
317
+ _ , err = client .PostOAuth2ProviderAppSecret (ctx , apps . NoPort .ID )
340
318
require .NoError (t , err )
341
319
}
342
320
343
321
// Should get secrets now, but only for the one app.
344
322
//nolint:gocritic // OAauth2 app management requires owner permission.
345
- secrets , err = client .OAuth2ProviderAppSecrets (ctx , app1 .ID )
323
+ secrets , err = client .OAuth2ProviderAppSecrets (ctx , apps . Default .ID )
346
324
require .NoError (t , err )
347
325
require .Len (t , secrets , 5 )
348
326
for _ , secret := range secrets {
@@ -351,19 +329,53 @@ func TestOAuthAppSecrets(t *testing.T) {
351
329
352
330
// Should be able to delete a secret.
353
331
//nolint:gocritic // OAauth2 app management requires owner permission.
354
- err = client .DeleteOAuth2ProviderAppSecret (ctx , app1 .ID , secrets [0 ].ID )
332
+ err = client .DeleteOAuth2ProviderAppSecret (ctx , apps . Default .ID , secrets [0 ].ID )
355
333
require .NoError (t , err )
356
- secrets , err = client .OAuth2ProviderAppSecrets (ctx , app1 .ID )
334
+ secrets , err = client .OAuth2ProviderAppSecrets (ctx , apps . Default .ID )
357
335
require .NoError (t , err )
358
336
require .Len (t , secrets , 4 )
359
337
360
338
// No secrets once the app is deleted.
361
339
//nolint:gocritic // OAauth2 app management requires owner permission.
362
- err = client .DeleteOAuth2ProviderApp (ctx , app1 .ID )
340
+ err = client .DeleteOAuth2ProviderApp (ctx , apps . Default .ID )
363
341
require .NoError (t , err )
364
342
365
343
//nolint:gocritic // OAauth2 app management requires owner permission.
366
- _ , err = client .OAuth2ProviderAppSecrets (ctx , app1 .ID )
344
+ _ , err = client .OAuth2ProviderAppSecrets (ctx , apps . Default .ID )
367
345
require .Error (t , err )
368
346
})
369
347
}
348
+
349
+ type provisionedApps struct {
350
+ Default codersdk.OAuth2ProviderApp
351
+ NoPort codersdk.OAuth2ProviderApp
352
+ Subdomain codersdk.OAuth2ProviderApp
353
+ // For sorting purposes these are included. You will likely never touch them.
354
+ Extra []codersdk.OAuth2ProviderApp
355
+ }
356
+
357
+ func generateApps (ctx context.Context , t * testing.T , client * codersdk.Client , suffix string ) provisionedApps {
358
+ create := func (name , callback string ) codersdk.OAuth2ProviderApp {
359
+ name = fmt .Sprintf ("%s-%s" , name , suffix )
360
+ //nolint:gocritic // OAauth2 app management requires owner permission.
361
+ app , err := client .PostOAuth2ProviderApp (ctx , codersdk.PostOAuth2ProviderAppRequest {
362
+ Name : name ,
363
+ CallbackURL : callback ,
364
+ Icon : "" ,
365
+ })
366
+ require .NoError (t , err )
367
+ require .Equal (t , name , app .Name )
368
+ require .Equal (t , callback , app .CallbackURL )
369
+ return app
370
+ }
371
+
372
+ return provisionedApps {
373
+ Default : create ("razzle-dazzle-a" , "http://localhost1:8080/foo/bar" ),
374
+ NoPort : create ("razzle-dazzle-b" , "http://localhost2" ),
375
+ Subdomain : create ("razzle-dazzle-z" , "http://30.localhost:3000" ),
376
+ Extra : []codersdk.OAuth2ProviderApp {
377
+ create ("second-to-last" , "http://20.localhost:3000" ),
378
+ create ("woo-10" , "http://10.localhost:3000" ),
379
+ },
380
+ }
381
+ }
0 commit comments