@@ -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 )
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 {
@@ -365,3 +355,35 @@ func TestOAuthAppSecrets(t *testing.T) {
365
355
require .Error (t ,err )
366
356
})
367
357
}
358
+
359
+ type provisionedApps struct {
360
+ Default codersdk.OAuth2ProviderApp
361
+ NoPort codersdk.OAuth2ProviderApp
362
+ Subdomain codersdk.OAuth2ProviderApp
363
+ // For sorting purposes these are included. You will likely never touch them.
364
+ Extra []codersdk.OAuth2ProviderApp
365
+ }
366
+ func generateApps (ctx context.Context ,t * testing.T ,client * codersdk.Client )provisionedApps {
367
+ create := func (name ,callback string ) codersdk.OAuth2ProviderApp {
368
+ //nolint:gocritic // OAauth2 app management requires owner permission.
369
+ app ,err := client .PostOAuth2ProviderApp (ctx , codersdk.PostOAuth2ProviderAppRequest {
370
+ Name :name ,
371
+ CallbackURL :callback ,
372
+ Icon :"" ,
373
+ })
374
+ require .NoError (t ,err )
375
+ require .Equal (t ,name ,app .Name )
376
+ require .Equal (t ,callback ,app .CallbackURL )
377
+ return app
378
+ }
379
+
380
+ return provisionedApps {
381
+ Default :create ("razzle-dazzle" ,"http://localhost1:8080/foo/bar" ),
382
+ NoPort :create ("razzle-dazzle-the-sequel" ,"http://localhost2" ),
383
+ Subdomain :create ("razzle-dazzle-the-z-prequel" ,"http://30.localhost:3000" ),
384
+ Extra : []codersdk.OAuth2ProviderApp {
385
+ create ("the-not-really-twenty" ,"http://20.localhost:3000" ),
386
+ create ("woo-10" ,"http://10.localhost:3000" ),
387
+ },
388
+ }
389
+ }