Skip to content

Commit

Permalink
Fixes 76
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Dec 19, 2023
1 parent a399391 commit 606f17d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
4 changes: 2 additions & 2 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type DB interface {
UpdateIncompleteUpload(tx db.Tx, id pacta.IncompleteUploadID, mutations ...db.UpdateIncompleteUploadFn) error
DeleteIncompleteUpload(tx db.Tx, id pacta.IncompleteUploadID) (pacta.BlobURI, error)

GetOrCreateOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error)
GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error)

PortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID) (*pacta.PortfolioGroup, error)
PortfolioGroupsByOwner(tx db.Tx, owner pacta.OwnerID) ([]*pacta.PortfolioGroup, error)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *Server) getUserOwnerID(ctx context.Context) (pacta.OwnerID, error) {
if err != nil {
return "", err
}
ownerID, err := s.DB.GetOrCreateOwnerForUser(s.DB.NoTxn(ctx), userID)
ownerID, err := s.DB.GetOwnerForUser(s.DB.NoTxn(ctx), userID)
if err != nil {
return "", oapierr.Internal("failed to find or create owner for user",
zap.String("user_id", string(userID)), zap.Error(err))
Expand Down
10 changes: 3 additions & 7 deletions db/sqldb/owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *DB) ownerByInitiative(tx db.Tx, id pacta.InitiativeID) (*pacta.Owner, e
return nil, db.NotFound(id, "ownerByInitiativeId")
}

func (d *DB) GetOrCreateOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error) {
func (d *DB) GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error) {
var ownerID pacta.OwnerID
err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error {
owner, err := d.ownerByUser(tx, uID)
Expand All @@ -79,13 +79,9 @@ func (d *DB) GetOrCreateOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID,
return nil
}
if !db.IsNotFound(err) {
return fmt.Errorf("querying owner by user: %w", err)
return fmt.Errorf("user owner not found: %w", err)
}
ownerID, err = d.createOwner(tx, &pacta.Owner{User: &pacta.User{ID: uID}})
if err != nil {
return fmt.Errorf("creating owner: %w", err)
}
return nil
return fmt.Errorf("looking up owner: %w", err)
})
if err != nil {
return "", fmt.Errorf("getting or creating owner for initiative: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions db/sqldb/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func TestGetOrCreateOwners(t *testing.T) {
u := userForTesting(t, tdb)
i := initiativeForTesting(t, tdb)

uo, err := tdb.GetOrCreateOwnerForUser(tx, u.ID)
uo, err := tdb.GetOwnerForUser(tx, u.ID)
if err != nil {
t.Fatalf("creating owner for user: %v", err)
t.Fatalf("getting owner for user: %v", err)
}
uo2, err := tdb.GetOrCreateOwnerForUser(tx, u.ID)
uo2, err := tdb.GetOwnerForUser(tx, u.ID)
if err != nil {
t.Fatalf("creating owner for user: %v", err)
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestReadOwners(t *testing.T) {
func ownerUserForTesting(t *testing.T, tdb *DB, u *pacta.User) *pacta.Owner {
t.Helper()
tx := tdb.NoTxn(context.Background())
oid, err := tdb.GetOrCreateOwnerForUser(tx, u.ID)
oid, err := tdb.GetOwnerForUser(tx, u.ID)
if err != nil {
t.Fatalf("creating owner for user: %v", err)
}
Expand Down
4 changes: 4 additions & 0 deletions db/sqldb/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func (d *DB) GetOrCreateUserByAuthn(tx db.Tx, authnMechanism pacta.AuthnMechanis
if err != nil {
return fmt.Errorf("creating user: %w", err)
}
_, err = d.createOwner(tx, &pacta.Owner{User: &pacta.User{ID: uID}})
if err != nil {
return fmt.Errorf("creating owner: %w", err)
}
u, err = d.User(tx, uID)
if err != nil {
return fmt.Errorf("reading back created user: %w", err)
Expand Down
15 changes: 6 additions & 9 deletions db/sqldb/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,18 +324,15 @@ func userForTesting(t *testing.T, tdb *DB) *pacta.User {

func userForTestingWithKey(t *testing.T, tdb *DB, key string) *pacta.User {
t.Helper()
u := &pacta.User{
CanonicalEmail: fmt.Sprintf("canoncal-email-%[email protected]", key),
EnteredEmail: fmt.Sprintf("entered-email-%[email protected]", key),
AuthnMechanism: pacta.AuthnMechanism_EmailAndPass,
AuthnID: fmt.Sprintf("authn-id-%s", key),
}
canonicalEmail := fmt.Sprintf("canoncal-email-%[email protected]", key)
enteredEmail := fmt.Sprintf("entered-email-%[email protected]", key)
authnMechanism := pacta.AuthnMechanism_EmailAndPass
authnID := fmt.Sprintf("authn-id-%s", key)
ctx := context.Background()
tx := tdb.NoTxn(ctx)
uid, err := tdb.createUser(tx, u)
user, err := tdb.GetOrCreateUserByAuthn(tx, authnMechanism, authnID, enteredEmail, canonicalEmail)
if err != nil {
t.Fatalf("creating user: %v", err)
}
u.ID = uid
return u
return user
}

0 comments on commit 606f17d

Please sign in to comment.