Skip to content

Commit 4f0203d

Browse files
committed
chore: creaet unit test to exercise failed email change bug
Changing emails on github fails if another deleted user exists with the same link.
1 parent 2883815 commit 4f0203d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

coderd/userauth_test.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ func TestUserOAuth2Github(t *testing.T) {
603603

604604
require.Equal(t, http.StatusUnauthorized, resp.StatusCode)
605605
})
606+
607+
// The bug only is exercised when a deleted user with the same linked_id exists.
606608
t.Run("ChangedEmail", func(t *testing.T) {
607609
t.Parallel()
608610

@@ -627,7 +629,7 @@ func TestUserOAuth2Github(t *testing.T) {
627629
coderEmail,
628630
}
629631

630-
client := coderdtest.New(t, &coderdtest.Options{
632+
owner := coderdtest.New(t, &coderdtest.Options{
631633
Auditor: auditor,
632634
GithubOAuth2Config: &coderd.GithubOAuth2Config{
633635
AllowSignups: true,
@@ -650,9 +652,19 @@ func TestUserOAuth2Github(t *testing.T) {
650652
},
651653
},
652654
})
655+
coderdtest.CreateFirstUser(t, owner)
656+
657+
ctx := testutil.Context(t, testutil.WaitLong)
658+
// Create the user, then delete the user, then create again.
659+
// This causes the email change to fail.
660+
client := codersdk.New(owner.URL)
653661

654-
ctx := testutil.Context(t, testutil.WaitMedium)
655-
// This should register the user
662+
client, _ = fake.Login(t, client, jwt.MapClaims{})
663+
deleted, err := client.User(ctx, "me")
664+
err = owner.DeleteUser(ctx, deleted.ID)
665+
require.NoError(t, err)
666+
667+
// Create the user again.
656668
client, _ = fake.Login(t, client, jwt.MapClaims{})
657669
user, err := client.User(ctx, "me")
658670
require.NoError(t, err)
@@ -666,7 +678,8 @@ func TestUserOAuth2Github(t *testing.T) {
666678
client, _ = fake.Login(t, client, jwt.MapClaims{})
667679
user, err = client.User(ctx, "me")
668680
require.NoError(t, err)
669-
require.Equal(t, user.ID, userID)
681+
682+
require.Equal(t, user.ID, userID, "user_id is different, a new user was likely created")
670683
require.Equal(t, user.Email, *gmailEmail.Email)
671684

672685
// Entirely change emails.
@@ -681,7 +694,8 @@ func TestUserOAuth2Github(t *testing.T) {
681694
client, _ = fake.Login(t, client, jwt.MapClaims{})
682695
user, err = client.User(ctx, "me")
683696
require.NoError(t, err)
684-
require.Equal(t, user.ID, userID)
697+
698+
require.Equal(t, user.ID, userID, "user_id is different, a new user was likely created")
685699
require.Equal(t, user.Email, newEmail)
686700
})
687701
}

0 commit comments

Comments
 (0)