This is an automated email from the ASF dual-hosted git repository.
lahirujayathilake pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
The following commit(s) were added to refs/heads/master by this push:
new da70bd9e7 Report referenced user as in use (#689)
da70bd9e7 is described below
commit da70bd9e7d418cdd1ed31007d90a78e934abe9f2
Author: Yasith Jayawardana <[email protected]>
AuthorDate: Thu Nov 5 16:04:31 2026 -0500
Report referenced user as in use (#689)
---
pkg/service/user.go | 5 +++++
pkg/service/user_merge_integration_test.go | 11 +++++++++++
2 files changed, 16 insertions(+)
diff --git a/pkg/service/user.go b/pkg/service/user.go
index c5ed04125..d131ef064 100644
--- a/pkg/service/user.go
+++ b/pkg/service/user.go
@@ -20,10 +20,12 @@ package service
import (
"context"
"database/sql"
+ "errors"
"fmt"
"github.com/apache/airavata-custos/pkg/events"
"github.com/apache/airavata-custos/pkg/models"
+ "github.com/jackc/pgx/v5/pgconn"
)
// CreateUser persists a new user. If user.ID is empty a new UUID is generated.
@@ -243,6 +245,9 @@ func (s *Service) DeleteUser(ctx context.Context, id
string) error {
}
return s.eventBus.Publish(ctx, tx, events.UserDeleteEvent,
existing)
}); err != nil {
+ if pgErr := (*pgconn.PgError)(nil); errors.As(err, &pgErr) &&
pgErr.Code == "23503" {
+ return fmt.Errorf("%w: user %q is referenced by records
that must stay", ErrInUse, id)
+ }
return fmt.Errorf("delete user: %w", err)
}
s.InvalidateAllIdentities()
diff --git a/pkg/service/user_merge_integration_test.go
b/pkg/service/user_merge_integration_test.go
index 2aaf8fd90..33bd4a30f 100644
--- a/pkg/service/user_merge_integration_test.go
+++ b/pkg/service/user_merge_integration_test.go
@@ -22,6 +22,7 @@ package service
import (
"context"
"encoding/json"
+ "errors"
"fmt"
"strings"
"testing"
@@ -194,3 +195,13 @@ func TestDeleteUser_RemovesTheirAllocationMemberships(t
*testing.T) {
t.Fatalf("%d memberships left", len(left))
}
}
+
+func TestDeleteUser_RefusesAUserStillReferenced(t *testing.T) {
+ database := setupTestDB(t)
+ svc := newTestService(database)
+ pi := seedUser(t, database, "[email protected]")
+ newClusterAllocation(t, svc, pi)
+ if err := svc.DeleteUser(ctx(), pi); !errors.Is(err, ErrInUse) {
+ t.Fatalf("delete a project PI: %v, want ErrInUse", err)
+ }
+}