@@ -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,26 +264,14 @@ func TestOAuthAppSecrets(t *testing.T) {
274
264
ctx := 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 (ctx , 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 (ctx , codersdk.PostOAuth2ProviderAppRequest {
286
- Name :"razzle-dazzle-the-sequel" ,
287
- CallbackURL :"http://localhost" ,
288
- })
289
- require .NoError (t ,err )
267
+ apps := generateApps (ctx ,t ,client ,"app-secrets" )
290
268
291
269
t .Run ("DeleteNonExisting" ,func (t * testing.T ) {
292
270
t .Parallel ()
293
271
294
272
// Should not be able to create secrets for a non-existent app.
295
273
//nolint:gocritic // OAauth2 app management requires owner permission.
296
- _ ,err = client .OAuth2ProviderAppSecrets (ctx ,uuid .New ())
274
+ _ ,err : =client .OAuth2ProviderAppSecrets (ctx ,uuid .New ())
297
275
require .Error (t ,err )
298
276
299
277
// Should not be able to delete non-existing secrets when there is no app.
@@ -303,16 +281,16 @@ func TestOAuthAppSecrets(t *testing.T) {
303
281
304
282
// Should not be able to delete non-existing secrets when the app exists.
305
283
//nolint:gocritic // OAauth2 app management requires owner permission.
306
- err = client .DeleteOAuth2ProviderAppSecret (ctx ,app1 .ID ,uuid .New ())
284
+ err = client .DeleteOAuth2ProviderAppSecret (ctx ,apps . Default .ID ,uuid .New ())
307
285
require .Error (t ,err )
308
286
309
287
// Should not be able to delete an existing secret with the wrong app ID.
310
288
//nolint:gocritic // OAauth2 app management requires owner permission.
311
- secret ,err := client .PostOAuth2ProviderAppSecret (ctx ,app2 .ID )
289
+ secret ,err := client .PostOAuth2ProviderAppSecret (ctx ,apps . NoPort .ID )
312
290
require .NoError (t ,err )
313
291
314
292
//nolint:gocritic // OAauth2 app management requires owner permission.
315
- err = client .DeleteOAuth2ProviderAppSecret (ctx ,app1 .ID ,secret .ID )
293
+ err = client .DeleteOAuth2ProviderAppSecret (ctx ,apps . Default .ID ,secret .ID )
316
294
require .Error (t ,err )
317
295
})
318
296
@@ -321,26 +299,26 @@ func TestOAuthAppSecrets(t *testing.T) {
321
299
322
300
// No secrets yet.
323
301
//nolint:gocritic // OAauth2 app management requires owner permission.
324
- secrets ,err := client .OAuth2ProviderAppSecrets (ctx ,app1 .ID )
302
+ secrets ,err := client .OAuth2ProviderAppSecrets (ctx ,apps . Default .ID )
325
303
require .NoError (t ,err )
326
304
require .Len (t ,secrets ,0 )
327
305
328
306
// Should be able to create secrets.
329
307
for i := 0 ;i < 5 ;i ++ {
330
308
//nolint:gocritic // OAauth2 app management requires owner permission.
331
- secret ,err := client .PostOAuth2ProviderAppSecret (ctx ,app1 .ID )
309
+ secret ,err := client .PostOAuth2ProviderAppSecret (ctx ,apps . Default .ID )
332
310
require .NoError (t ,err )
333
311
require .NotEmpty (t ,secret .ClientSecretFull )
334
312
require .True (t ,len (secret .ClientSecretFull )> 6 )
335
313
336
314
//nolint:gocritic // OAauth2 app management requires owner permission.
337
- _ ,err = client .PostOAuth2ProviderAppSecret (ctx ,app2 .ID )
315
+ _ ,err = client .PostOAuth2ProviderAppSecret (ctx ,apps . NoPort .ID )
338
316
require .NoError (t ,err )
339
317
}
340
318
341
319
// Should get secrets now, but only for the one app.
342
320
//nolint:gocritic // OAauth2 app management requires owner permission.
343
- secrets ,err = client .OAuth2ProviderAppSecrets (ctx ,app1 .ID )
321
+ secrets ,err = client .OAuth2ProviderAppSecrets (ctx ,apps . Default .ID )
344
322
require .NoError (t ,err )
345
323
require .Len (t ,secrets ,5 )
346
324
for _ ,secret := range secrets {
@@ -349,19 +327,53 @@ func TestOAuthAppSecrets(t *testing.T) {
349
327
350
328
// Should be able to delete a secret.
351
329
//nolint:gocritic // OAauth2 app management requires owner permission.
352
- err = client .DeleteOAuth2ProviderAppSecret (ctx ,app1 .ID ,secrets [0 ].ID )
330
+ err = client .DeleteOAuth2ProviderAppSecret (ctx ,apps . Default .ID ,secrets [0 ].ID )
353
331
require .NoError (t ,err )
354
- secrets ,err = client .OAuth2ProviderAppSecrets (ctx ,app1 .ID )
332
+ secrets ,err = client .OAuth2ProviderAppSecrets (ctx ,apps . Default .ID )
355
333
require .NoError (t ,err )
356
334
require .Len (t ,secrets ,4 )
357
335
358
336
// No secrets once the app is deleted.
359
337
//nolint:gocritic // OAauth2 app management requires owner permission.
360
- err = client .DeleteOAuth2ProviderApp (ctx ,app1 .ID )
338
+ err = client .DeleteOAuth2ProviderApp (ctx ,apps . Default .ID )
361
339
require .NoError (t ,err )
362
340
363
341
//nolint:gocritic // OAauth2 app management requires owner permission.
364
- _ ,err = client .OAuth2ProviderAppSecrets (ctx ,app1 .ID )
342
+ _ ,err = client .OAuth2ProviderAppSecrets (ctx ,apps . Default .ID )
365
343
require .Error (t ,err )
366
344
})
367
345
}
346
+
347
+ type provisionedApps struct {
348
+ Default codersdk.OAuth2ProviderApp
349
+ NoPort codersdk.OAuth2ProviderApp
350
+ Subdomain codersdk.OAuth2ProviderApp
351
+ // For sorting purposes these are included. You will likely never touch them.
352
+ Extra []codersdk.OAuth2ProviderApp
353
+ }
354
+
355
+ func generateApps (ctx context.Context ,t * testing.T ,client * codersdk.Client ,suffix string )provisionedApps {
356
+ create := func (name ,callback string ) codersdk.OAuth2ProviderApp {
357
+ name = fmt .Sprintf ("%s-%s" ,name ,suffix )
358
+ //nolint:gocritic // OAauth2 app management requires owner permission.
359
+ app ,err := client .PostOAuth2ProviderApp (ctx , codersdk.PostOAuth2ProviderAppRequest {
360
+ Name :name ,
361
+ CallbackURL :callback ,
362
+ Icon :"" ,
363
+ })
364
+ require .NoError (t ,err )
365
+ require .Equal (t ,name ,app .Name )
366
+ require .Equal (t ,callback ,app .CallbackURL )
367
+ return app
368
+ }
369
+
370
+ return provisionedApps {
371
+ Default :create ("razzle-dazzle-a" ,"http://localhost1:8080/foo/bar" ),
372
+ NoPort :create ("razzle-dazzle-b" ,"http://localhost2" ),
373
+ Subdomain :create ("razzle-dazzle-z" ,"http://30.localhost:3000" ),
374
+ Extra : []codersdk.OAuth2ProviderApp {
375
+ create ("second-to-last" ,"http://20.localhost:3000" ),
376
+ create ("woo-10" ,"http://10.localhost:3000" ),
377
+ },
378
+ }
379
+ }