@@ -45,6 +45,8 @@ import (
45
45
"github.com/coder/coder/v2/cli/clitest"
46
46
"github.com/coder/coder/v2/cli/config"
47
47
"github.com/coder/coder/v2/coderd/coderdtest"
48
+ "github.com/coder/coder/v2/coderd/database"
49
+ "github.com/coder/coder/v2/coderd/database/dbgen"
48
50
"github.com/coder/coder/v2/coderd/database/dbtestutil"
49
51
"github.com/coder/coder/v2/coderd/database/migrations"
50
52
"github.com/coder/coder/v2/coderd/httpapi"
@@ -306,6 +308,144 @@ func TestServer(t *testing.T) {
306
308
require .Less (t , numLines , 20 )
307
309
})
308
310
311
+ t .Run ("OAuth2GitHubDefaultProvider" , func (t * testing.T ) {
312
+ type testCase struct {
313
+ name string
314
+ githubDefaultProviderEnabled string
315
+ githubClientID string
316
+ githubClientSecret string
317
+ expectGithubEnabled bool
318
+ expectGithubDefaultProviderConfigured bool
319
+ createUserPreStart bool
320
+ createUserPostRestart bool
321
+ }
322
+
323
+ runGitHubProviderTest := func (t * testing.T , tc testCase ) {
324
+ t .Parallel ()
325
+ if ! dbtestutil .WillUsePostgres () {
326
+ t .Skip ("test requires postgres" )
327
+ }
328
+
329
+ ctx , cancelFunc := context .WithCancel (testutil .Context (t , testutil .WaitLong ))
330
+ defer cancelFunc ()
331
+
332
+ dbURL , err := dbtestutil .Open (t )
333
+ require .NoError (t , err )
334
+ db , _ := dbtestutil .NewDB (t , dbtestutil .WithURL (dbURL ))
335
+
336
+ if tc .createUserPreStart {
337
+ _ = dbgen .User (t , db , database.User {})
338
+ }
339
+
340
+ args := []string {
341
+ "server" ,
342
+ "--postgres-url" , dbURL ,
343
+ "--http-address" , ":0" ,
344
+ }
345
+ if tc .githubClientID != "" {
346
+ args = append (args , fmt .Sprintf ("--oauth2-github-client-id=%s" , tc .githubClientID ))
347
+ }
348
+ if tc .githubClientSecret != "" {
349
+ args = append (args , fmt .Sprintf ("--oauth2-github-client-secret=%s" , tc .githubClientSecret ))
350
+ }
351
+ if tc .githubClientID != "" || tc .githubClientSecret != "" {
352
+ args = append (args , "--oauth2-github-allow-everyone" )
353
+ }
354
+ if tc .githubDefaultProviderEnabled != "" {
355
+ args = append (args , fmt .Sprintf ("--oauth2-github-default-provider-enable=%s" , tc .githubDefaultProviderEnabled ))
356
+ }
357
+
358
+ inv , cfg := clitest .New (t , args ... )
359
+ errChan := make (chan error , 1 )
360
+ go func () {
361
+ errChan <- inv .WithContext (ctx ).Run ()
362
+ }()
363
+ accessURLChan := make (chan * url.URL , 1 )
364
+ go func () {
365
+ accessURLChan <- waitAccessURL (t , cfg )
366
+ }()
367
+
368
+ var accessURL * url.URL
369
+ select {
370
+ case err := <- errChan :
371
+ require .NoError (t , err )
372
+ case accessURL = <- accessURLChan :
373
+ require .NotNil (t , accessURL )
374
+ }
375
+
376
+ client := codersdk .New (accessURL )
377
+
378
+ authMethods , err := client .AuthMethods (ctx )
379
+ require .NoError (t , err )
380
+ require .Equal (t , tc .expectGithubEnabled , authMethods .Github .Enabled )
381
+ require .Equal (t , tc .expectGithubDefaultProviderConfigured , authMethods .Github .DefaultProviderConfigured )
382
+
383
+ cancelFunc ()
384
+ select {
385
+ case err := <- errChan :
386
+ require .NoError (t , err )
387
+ case <- time .After (testutil .WaitLong ):
388
+ t .Fatal ("server did not exit" )
389
+ }
390
+
391
+ if tc .createUserPostRestart {
392
+ _ = dbgen .User (t , db , database.User {})
393
+ }
394
+
395
+ // Ensure that it stays at that setting after the server restarts.
396
+ inv , cfg = clitest .New (t , args ... )
397
+ clitest .Start (t , inv )
398
+ accessURL = waitAccessURL (t , cfg )
399
+ client = codersdk .New (accessURL )
400
+
401
+ ctx = testutil .Context (t , testutil .WaitLong )
402
+ authMethods , err = client .AuthMethods (ctx )
403
+ require .NoError (t , err )
404
+ require .Equal (t , tc .expectGithubEnabled , authMethods .Github .Enabled )
405
+ require .Equal (t , tc .expectGithubDefaultProviderConfigured , authMethods .Github .DefaultProviderConfigured )
406
+ }
407
+
408
+ for _ , tc := range []testCase {
409
+ {
410
+ name : "NewDeployment" ,
411
+ expectGithubEnabled : true ,
412
+ expectGithubDefaultProviderConfigured : true ,
413
+ createUserPreStart : false ,
414
+ createUserPostRestart : true ,
415
+ },
416
+ {
417
+ name : "ExistingDeployment" ,
418
+ expectGithubEnabled : false ,
419
+ expectGithubDefaultProviderConfigured : false ,
420
+ createUserPreStart : true ,
421
+ createUserPostRestart : false ,
422
+ },
423
+ {
424
+ name : "ManuallyDisabled" ,
425
+ githubDefaultProviderEnabled : "false" ,
426
+ expectGithubEnabled : false ,
427
+ expectGithubDefaultProviderConfigured : false ,
428
+ },
429
+ {
430
+ name : "ConfiguredClientID" ,
431
+ githubClientID : "123" ,
432
+ expectGithubEnabled : true ,
433
+ expectGithubDefaultProviderConfigured : false ,
434
+ },
435
+ {
436
+ name : "ConfiguredClientSecret" ,
437
+ githubClientSecret : "456" ,
438
+ expectGithubEnabled : true ,
439
+ expectGithubDefaultProviderConfigured : false ,
440
+ },
441
+ } {
442
+ tc := tc
443
+ t .Run (tc .name , func (t * testing.T ) {
444
+ runGitHubProviderTest (t , tc )
445
+ })
446
+ }
447
+ })
448
+
309
449
// Validate that a warning is printed that it may not be externally
310
450
// reachable.
311
451
t .Run ("LocalAccessURL" , func (t * testing.T ) {
0 commit comments