mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: add postgres triggers to remove deleted users from user_links (#12117)
* chore: add database test fixture to insert non-unique linked_ids * chore: create unit test to exercise failed email change bug * fix: add postgres triggers to keep user_links clear of deleted users * Add migrations to prevent deleted users with links * Force soft delete of users, do not allow un-delete
This commit is contained in:
+24
-2
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
@@ -24,6 +25,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/coderdtest"
|
||||
"github.com/coder/coder/v2/coderd/coderdtest/oidctest"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/promoauth"
|
||||
@@ -632,7 +634,7 @@ func TestUserOAuth2Github(t *testing.T) {
|
||||
coderEmail,
|
||||
}
|
||||
|
||||
owner := coderdtest.New(t, &coderdtest.Options{
|
||||
owner, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
|
||||
Auditor: auditor,
|
||||
GithubOAuth2Config: &coderd.GithubOAuth2Config{
|
||||
AllowSignups: true,
|
||||
@@ -655,9 +657,12 @@ func TestUserOAuth2Github(t *testing.T) {
|
||||
},
|
||||
},
|
||||
})
|
||||
coderdtest.CreateFirstUser(t, owner)
|
||||
first := coderdtest.CreateFirstUser(t, owner)
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
ownerUser, err := owner.User(context.Background(), "me")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create the user, then delete the user, then create again.
|
||||
// This causes the email change to fail.
|
||||
client := codersdk.New(owner.URL)
|
||||
@@ -668,6 +673,23 @@ func TestUserOAuth2Github(t *testing.T) {
|
||||
|
||||
err = owner.DeleteUser(ctx, deleted.ID)
|
||||
require.NoError(t, err)
|
||||
// Check no user links for the user
|
||||
links, err := db.GetUserLinksByUserID(dbauthz.As(ctx, coderdtest.AuthzUserSubject(ownerUser, first.OrganizationID)), deleted.ID)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, links)
|
||||
|
||||
// Make sure a user_link cannot be created with a deleted user.
|
||||
// nolint:gocritic // Unit test
|
||||
_, err = db.InsertUserLink(dbauthz.AsSystemRestricted(ctx), database.InsertUserLinkParams{
|
||||
UserID: deleted.ID,
|
||||
LoginType: "github",
|
||||
LinkedID: "100",
|
||||
OAuthAccessToken: "random",
|
||||
OAuthRefreshToken: "random",
|
||||
OAuthExpiry: time.Now(),
|
||||
DebugContext: []byte(`{}`),
|
||||
})
|
||||
require.ErrorContains(t, err, "Cannot create user_link for deleted user")
|
||||
|
||||
// Create the user again.
|
||||
client, _ = fake.Login(t, client, jwt.MapClaims{})
|
||||
|
||||
Reference in New Issue
Block a user