@@ -45,6 +45,8 @@ import (
4545"github.com/coder/coder/v2/cli/clitest"
4646"github.com/coder/coder/v2/cli/config"
4747"github.com/coder/coder/v2/coderd/coderdtest"
48+ "github.com/coder/coder/v2/coderd/database"
49+ "github.com/coder/coder/v2/coderd/database/dbgen"
4850"github.com/coder/coder/v2/coderd/database/dbtestutil"
4951"github.com/coder/coder/v2/coderd/database/migrations"
5052"github.com/coder/coder/v2/coderd/httpapi"
@@ -306,6 +308,103 @@ func TestServer(t *testing.T) {
306308require .Less (t ,numLines ,20 )
307309})
308310
311+ t .Run ("OAuth2GitHubDefaultProvider" ,func (t * testing.T ) {
312+ type testCase struct {
313+ githubEnabled bool
314+ createUserPreStart bool
315+ createUserPostRestart bool
316+ }
317+
318+ runGitHubProviderTest := func (t * testing.T ,tc testCase ) {
319+ t .Parallel ()
320+ if ! dbtestutil .WillUsePostgres () {
321+ t .Skip ("test requires postgres" )
322+ }
323+
324+ ctx ,cancelFunc := context .WithCancel (testutil .Context (t ,testutil .WaitLong ))
325+ defer cancelFunc ()
326+
327+ dbURL ,err := dbtestutil .Open (t )
328+ require .NoError (t ,err )
329+ db ,_ := dbtestutil .NewDB (t ,dbtestutil .WithURL (dbURL ))
330+
331+ if tc .createUserPreStart {
332+ _ = dbgen .User (t ,db , database.User {})
333+ }
334+
335+ inv ,cfg := clitest .New (t ,
336+ "server" ,
337+ "--postgres-url" ,dbURL ,
338+ "--http-address" ,":0" ,
339+ )
340+ errChan := make (chan error ,1 )
341+ go func () {
342+ errChan <- inv .WithContext (ctx ).Run ()
343+ }()
344+ accessURLChan := make (chan * url.URL ,1 )
345+ go func () {
346+ accessURLChan <- waitAccessURL (t ,cfg )
347+ }()
348+
349+ var accessURL * url.URL
350+ select {
351+ case err := <- errChan :
352+ require .NoError (t ,err )
353+ case accessURL = <- accessURLChan :
354+ require .NotNil (t ,accessURL )
355+ }
356+
357+ client := codersdk .New (accessURL )
358+
359+ authMethods ,err := client .AuthMethods (ctx )
360+ require .NoError (t ,err )
361+ require .Equal (t ,tc .githubEnabled ,authMethods .Github .Enabled )
362+
363+ cancelFunc ()
364+ select {
365+ case err := <- errChan :
366+ require .NoError (t ,err )
367+ case <- time .After (testutil .WaitLong ):
368+ t .Fatal ("server did not exit" )
369+ }
370+
371+ if tc .createUserPostRestart {
372+ _ = dbgen .User (t ,db , database.User {})
373+ }
374+
375+ // Ensure that it stays at that setting after the server restarts.
376+ inv ,cfg = clitest .New (t ,
377+ "server" ,
378+ "--postgres-url" ,dbURL ,
379+ "--http-address" ,":0" ,
380+ )
381+ clitest .Start (t ,inv )
382+ accessURL = waitAccessURL (t ,cfg )
383+ client = codersdk .New (accessURL )
384+
385+ ctx = testutil .Context (t ,testutil .WaitLong )
386+ authMethods ,err = client .AuthMethods (ctx )
387+ require .NoError (t ,err )
388+ require .Equal (t ,tc .githubEnabled ,authMethods .Github .Enabled )
389+ }
390+
391+ t .Run ("NewDeployment" ,func (t * testing.T ) {
392+ runGitHubProviderTest (t ,testCase {
393+ githubEnabled :true ,
394+ createUserPreStart :false ,
395+ createUserPostRestart :true ,
396+ })
397+ })
398+
399+ t .Run ("ExistingDeployment" ,func (t * testing.T ) {
400+ runGitHubProviderTest (t ,testCase {
401+ githubEnabled :false ,
402+ createUserPreStart :true ,
403+ createUserPostRestart :false ,
404+ })
405+ })
406+ })
407+
309408// Validate that a warning is printed that it may not be externally
310409// reachable.
311410t .Run ("LocalAccessURL" ,func (t * testing.T ) {