@@ -14,6 +14,7 @@ import (
14
14
"github.com/golang-jwt/jwt/v4"
15
15
"github.com/google/go-github/v43/github"
16
16
"github.com/google/uuid"
17
+ "github.com/prometheus/client_golang/prometheus"
17
18
"github.com/stretchr/testify/require"
18
19
"golang.org/x/xerrors"
19
20
@@ -25,6 +26,7 @@ import (
25
26
"github.com/coder/coder/v2/coderd/database"
26
27
"github.com/coder/coder/v2/coderd/database/dbgen"
27
28
"github.com/coder/coder/v2/coderd/database/dbtestutil"
29
+ "github.com/coder/coder/v2/coderd/promoauth"
28
30
"github.com/coder/coder/v2/codersdk"
29
31
"github.com/coder/coder/v2/testutil"
30
32
)
@@ -591,6 +593,87 @@ func TestUserOAuth2Github(t *testing.T) {
591
593
592
594
require .Equal (t , http .StatusUnauthorized , resp .StatusCode )
593
595
})
596
+ t .Run ("ChangedEmail" , func (t * testing.T ) {
597
+ t .Parallel ()
598
+
599
+ fake := oidctest .NewFakeIDP (t ,
600
+ oidctest .WithServing (),
601
+ oidctest .WithCallbackPath ("/api/v2/users/oauth2/github/callback" ),
602
+ )
603
+ const ghID = int64 (7777 )
604
+ auditor := audit .NewMock ()
605
+ coderEmail := & github.UserEmail {
606
+ Email : github .String ("alice@coder.com" ),
607
+ Verified : github .Bool (true ),
608
+ Primary : github .Bool (true ),
609
+ }
610
+ gmailEmail := & github.UserEmail {
611
+ Email : github .String ("alice@gmail.com" ),
612
+ Verified : github .Bool (true ),
613
+ Primary : github .Bool (false ),
614
+ }
615
+ emails := []* github.UserEmail {
616
+ gmailEmail ,
617
+ coderEmail ,
618
+ }
619
+
620
+ client := coderdtest .New (t , & coderdtest.Options {
621
+ Auditor : auditor ,
622
+ GithubOAuth2Config : & coderd.GithubOAuth2Config {
623
+ AllowSignups : true ,
624
+ AllowEveryone : true ,
625
+ OAuth2Config : promoauth .NewFactory (prometheus .NewRegistry ()).NewGithub ("test-github" , fake .OIDCConfig (t , []string {})),
626
+ ListOrganizationMemberships : func (ctx context.Context , client * http.Client ) ([]* github.Membership , error ) {
627
+ return []* github.Membership {}, nil
628
+ },
629
+ TeamMembership : func (ctx context.Context , client * http.Client , org , team , username string ) (* github.Membership , error ) {
630
+ return nil , xerrors .New ("no teams" )
631
+ },
632
+ AuthenticatedUser : func (ctx context.Context , client * http.Client ) (* github.User , error ) {
633
+ return & github.User {
634
+ Login : github .String ("alice" ),
635
+ ID : github .Int64 (ghID ),
636
+ }, nil
637
+ },
638
+ ListEmails : func (ctx context.Context , client * http.Client ) ([]* github.UserEmail , error ) {
639
+ return emails , nil
640
+ },
641
+ },
642
+ })
643
+
644
+ ctx := testutil .Context (t , testutil .WaitMedium )
645
+ // This should register the user
646
+ client , _ = fake .Login (t , client , jwt.MapClaims {})
647
+ user , err := client .User (ctx , "me" )
648
+ require .NoError (t , err )
649
+ userID := user .ID
650
+ require .Equal (t , user .Email , * coderEmail .Email )
651
+
652
+ // Now the user is registered, let's change their primary email.
653
+ coderEmail .Primary = github .Bool (false )
654
+ gmailEmail .Primary = github .Bool (true )
655
+
656
+ client , _ = fake .Login (t , client , jwt.MapClaims {})
657
+ user , err = client .User (ctx , "me" )
658
+ require .NoError (t , err )
659
+ require .Equal (t , user .ID , userID )
660
+ require .Equal (t , user .Email , * gmailEmail .Email )
661
+
662
+ // Entirely change emails.
663
+ newEmail := "alice@newdomain.com"
664
+ emails = []* github.UserEmail {
665
+ {
666
+ Email : github .String ("alice@newdomain.com" ),
667
+ Primary : github .Bool (true ),
668
+ Verified : github .Bool (true ),
669
+ },
670
+ }
671
+ client , _ = fake .Login (t , client , jwt.MapClaims {})
672
+ user , err = client .User (ctx , "me" )
673
+ require .NoError (t , err )
674
+ require .Equal (t , user .ID , userID )
675
+ require .Equal (t , user .Email , newEmail )
676
+ })
594
677
}
595
678
596
679
// nolint:bodyclose
0 commit comments