refactor user account repo function names (#476)

This commit is contained in:
Jim authored and GitHub committed 2020-09-28 15:39:10 -04:00
1 parent f1873d623d
commit fde96924af
5 files changed
+92 -92

No files matched your search

+1 -1
View File
@@ -117,7 +117,7 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) (*password.AuthMet
if u, err = iamRepo.CreateUser(cancelCtx, u, opts...); err != nil {
return nil, nil, fmt.Errorf("error creating initial admin user: %w", err)
}
if _, err = iamRepo.AssociateAccounts(cancelCtx, u.GetPublicId(), u.GetVersion(), []string{acct.GetPublicId()}); err != nil {
if _, err = iamRepo.AddUserAccounts(cancelCtx, u.GetPublicId(), u.GetVersion(), []string{acct.GetPublicId()}); err != nil {
return nil, nil, fmt.Errorf("error associating initial admin user with account: %w", err)
}
// Create a role tying them together
+14 -14
View File
@@ -139,7 +139,7 @@ func (r *Repository) UpdateUser(ctx context.Context, user *User, version uint32,
// intentionally not setting the defaultLimit, so we'll get all
// the account ids without a limit
}
currentAccountIds, err = txRepo.ListAssociatedAccountIds(ctx, user.PublicId)
currentAccountIds, err = txRepo.ListUserAccounts(ctx, user.PublicId)
if err != nil {
return fmt.Errorf("unable to retrieve current account ids after update: %w", err)
}
@@ -170,7 +170,7 @@ func (r *Repository) LookupUser(ctx context.Context, userId string, opt ...Optio
}
return nil, nil, fmt.Errorf("lookup user: failed %w for %s", err, userId)
}
currentAccountIds, err := r.ListAssociatedAccountIds(ctx, userId)
currentAccountIds, err := r.ListUserAccounts(ctx, userId)
if err != nil {
return nil, nil, fmt.Errorf("lookup user: unable to retrieve current account ids: %w", err)
}
@@ -331,9 +331,9 @@ func (r *Repository) getUserWithAccount(ctx context.Context, withAccountId strin
return &u, nil
}
// ListAssociatedAccountIds returns the account ids for the userId and supports the
// ListUserAccounts returns the account ids for the userId and supports the
// WithLimit option. Returns nil, nil when no associated accounts are found.
func (r *Repository) ListAssociatedAccountIds(ctx context.Context, userId string, opt ...Option) ([]string, error) {
func (r *Repository) ListUserAccounts(ctx context.Context, userId string, opt ...Option) ([]string, error) {
if userId == "" {
return nil, fmt.Errorf("list auth account ids: missing user id: %w", db.ErrInvalidParameter)
}
@@ -351,11 +351,11 @@ func (r *Repository) ListAssociatedAccountIds(ctx context.Context, userId string
return ids, nil
}
// AssociateAccounts will associate a user with existing accounts and
// AddUserAccounts will associate a user with existing accounts and
// return a list of all associated account ids for the user. The accounts must
// not already be associated with different users. No options are currently
// supported.
func (r *Repository) AssociateAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
func (r *Repository) AddUserAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
if userId == "" {
return nil, fmt.Errorf("associate accounts: missing user public id %w", db.ErrInvalidParameter)
}
@@ -419,7 +419,7 @@ func (r *Repository) AssociateAccounts(ctx context.Context, userId string, userV
// intentionally not setting the defaultLimit, so we'll get all
// the account ids without a limit
}
currentAccountIds, err = txRepo.ListAssociatedAccountIds(ctx, user.PublicId)
currentAccountIds, err = txRepo.ListUserAccounts(ctx, user.PublicId)
if err != nil {
return fmt.Errorf("associate accounts: unable to retrieve current account ids after adds: %w", err)
}
@@ -432,11 +432,11 @@ func (r *Repository) AssociateAccounts(ctx context.Context, userId string, userV
return currentAccountIds, nil
}
// DisassociateAccounts will disassociate a user from existing accounts and
// DeleteUserAccounts will disassociate a user from existing accounts and
// return a list of all associated account ids for the user. The accounts must
// not be associated with different users. No options are currently
// supported.
func (r *Repository) DisassociateAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
func (r *Repository) DeleteUserAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
if userId == "" {
return nil, fmt.Errorf("disassociate accounts: missing user public id %w", db.ErrInvalidParameter)
}
@@ -499,7 +499,7 @@ func (r *Repository) DisassociateAccounts(ctx context.Context, userId string, us
// intentionally not setting the defaultLimit, so we'll get all
// the account ids without a limit
}
currentAccountIds, err = txRepo.ListAssociatedAccountIds(ctx, user.PublicId)
currentAccountIds, err = txRepo.ListUserAccounts(ctx, user.PublicId)
if err != nil {
return fmt.Errorf("disassociate accounts: unable to retrieve current account ids after adds: %w", err)
}
@@ -512,11 +512,11 @@ func (r *Repository) DisassociateAccounts(ctx context.Context, userId string, us
return currentAccountIds, nil
}
// DisassociatedAccounts will associate a user with existing accounts and
// SetUserAccounts will associate a user with existing accounts and
// return a list of all associated account ids for the user. The accounts must
// not already be associated with different users. No options are currently
// supported.
func (r *Repository) SetAssociatedAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
func (r *Repository) SetUserAccounts(ctx context.Context, userId string, userVersion uint32, accountIds []string, opt ...Option) ([]string, error) {
if userId == "" {
return nil, fmt.Errorf("set associated accounts: missing user public id %w", db.ErrInvalidParameter)
}
@@ -555,7 +555,7 @@ func (r *Repository) SetAssociatedAccounts(ctx context.Context, userId string, u
// intentionally not setting the defaultLimit, so we'll get all
// the account ids without a limit
}
currentAccountIds, err = txRepo.ListAssociatedAccountIds(ctx, userId)
currentAccountIds, err = txRepo.ListUserAccounts(ctx, userId)
if err != nil {
return fmt.Errorf("set associated accounts: unable to retrieve current account ids after set: %w", err)
}
@@ -606,7 +606,7 @@ func (r *Repository) SetAssociatedAccounts(ctx context.Context, userId string, u
// intentionally not setting the defaultLimit, so we'll get all
// the account ids without a limit
}
currentAccountIds, err = txRepo.ListAssociatedAccountIds(ctx, user.PublicId)
currentAccountIds, err = txRepo.ListUserAccounts(ctx, user.PublicId)
if err != nil {
return fmt.Errorf("set associated accounts: unable to retrieve current account ids after set: %w", err)
}
+6 -6
View File
@@ -1096,7 +1096,7 @@ func TestRepository_AssociateAccounts(t *testing.T) {
version = *tt.args.userVersionOverride
}
got, err := repo.AssociateAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
got, err := repo.AddUserAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
if tt.wantErr {
require.Error(err)
if tt.wantErrIs != nil {
@@ -1115,7 +1115,7 @@ func TestRepository_AssociateAccounts(t *testing.T) {
sort.Strings(got)
assert.Equal(accountIds, got)
foundIds, err := repo.ListAssociatedAccountIds(context.Background(), tt.args.userId)
foundIds, err := repo.ListUserAccounts(context.Background(), tt.args.userId)
require.NoError(err)
sort.Strings(foundIds)
assert.Equal(accountIds, foundIds)
@@ -1226,7 +1226,7 @@ func TestRepository_DisassociateAccounts(t *testing.T) {
version = *tt.args.userVersionOverride
}
got, err := repo.DisassociateAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
got, err := repo.DeleteUserAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
if tt.wantErr {
require.Error(err)
if tt.wantErrIs != nil {
@@ -1241,7 +1241,7 @@ func TestRepository_DisassociateAccounts(t *testing.T) {
err = db.TestVerifyOplog(t, rw, id, db.WithOperation(oplog.OpType_OP_TYPE_UPDATE), db.WithCreateNotBefore(10*time.Second))
assert.NoError(err)
}
foundIds, err := repo.ListAssociatedAccountIds(context.Background(), tt.args.userId)
foundIds, err := repo.ListUserAccounts(context.Background(), tt.args.userId)
require.NoError(err)
for _, id := range accountIds {
assert.True(!strutil.StrListContains(foundIds, id))
@@ -1407,7 +1407,7 @@ func TestRepository_SetAssociatedAccounts(t *testing.T) {
version = *tt.args.userVersionOverride
}
got, err := repo.SetAssociatedAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
got, err := repo.SetUserAccounts(context.Background(), tt.args.userId, version, accountIds, tt.args.opt...)
if tt.wantErr {
require.Error(err)
if tt.wantErrIs != nil {
@@ -1428,7 +1428,7 @@ func TestRepository_SetAssociatedAccounts(t *testing.T) {
sort.Strings(got)
assert.Equal(accountIds, got)
foundIds, err := repo.ListAssociatedAccountIds(context.Background(), tt.args.userId)
foundIds, err := repo.ListUserAccounts(context.Background(), tt.args.userId)
require.NoError(err)
sort.Strings(foundIds)
assert.Equal(accountIds, foundIds)
@@ -295,7 +295,7 @@ func (s Service) addInRepo(ctx context.Context, userId string, accountIds []stri
if err != nil {
return nil, err
}
_, err = repo.AssociateAccounts(ctx, userId, version, accountIds)
_, err = repo.AddUserAccounts(ctx, userId, version, accountIds)
if err != nil {
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to add accounts to user: %v.", err)
}
@@ -314,7 +314,7 @@ func (s Service) setInRepo(ctx context.Context, userId string, accountIds []stri
if err != nil {
return nil, err
}
_, err = repo.SetAssociatedAccounts(ctx, userId, version, accountIds)
_, err = repo.SetUserAccounts(ctx, userId, version, accountIds)
if err != nil {
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to set accounts for the user: %v.", err)
}
@@ -333,7 +333,7 @@ func (s Service) removeInRepo(ctx context.Context, userId string, accountIds []s
if err != nil {
return nil, err
}
_, err = repo.DisassociateAccounts(ctx, userId, version, accountIds)
_, err = repo.DeleteUserAccounts(ctx, userId, version, accountIds)
if err != nil {
return nil, handlers.ApiErrorWithCodeAndMessage(codes.Internal, "Unable to remove accounts from user: %v.", err)
}
@@ -393,7 +393,7 @@ func toProto(in *iam.User, accts []string) *pb.User {
CreatedTime: in.GetCreateTime().GetTimestamp(),
UpdatedTime: in.GetUpdateTime().GetTimestamp(),
Version: in.GetVersion(),
AccountIds: accts,
AccountIds: accts,
}
if in.GetDescription() != "" {
out.Description = &wrapperspb.StringValue{Value: in.GetDescription()}
@@ -403,7 +403,7 @@ func toProto(in *iam.User, accts []string) *pb.User {
}
for _, a := range accts {
out.Accounts = append(out.Accounts, &pb.Account{
Id: a,
Id: a,
// TODO: Update this when an account can be associated with a user from a different scope.
ScopeId: in.GetScopeId(),
})
@@ -621,33 +621,33 @@ func TestAddAccount(t *testing.T) {
accts := password.TestAccounts(t, conn, amId, 3)
addCases := []struct {
name string
setup func(*iam.User)
addAccounts []string
resultAccounts []string
wantErr bool
name string
setup func(*iam.User)
addAccounts []string
resultAccounts []string
wantErr bool
}{
{
name: "Add account on empty user",
setup: func(u *iam.User) {},
addAccounts: []string{accts[1].GetPublicId()},
name: "Add account on empty user",
setup: func(u *iam.User) {},
addAccounts: []string{accts[1].GetPublicId()},
resultAccounts: []string{accts[1].GetPublicId()},
},
{
name: "Add account on populated user",
setup: func(u *iam.User) {
_, err := iamRepo.SetAssociatedAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
_, err := iamRepo.SetUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
},
addAccounts: []string{accts[1].GetPublicId()},
addAccounts: []string{accts[1].GetPublicId()},
resultAccounts: []string{accts[0].GetPublicId(), accts[1].GetPublicId()},
},
{
name: "Add empty on populated user",
setup: func(u *iam.User) {
iamRepo.SetAssociatedAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
iamRepo.SetUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId(), accts[1].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -723,22 +723,22 @@ func TestSetAccount(t *testing.T) {
accts := password.TestAccounts(t, conn, amId, 3)
setCases := []struct {
name string
setup func(*iam.User)
setAccounts []string
name string
setup func(*iam.User)
setAccounts []string
resultAccounts []string
wantErr bool
wantErr bool
}{
{
name: "Set account on empty user",
setup: func(u *iam.User) {},
name: "Set account on empty user",
setup: func(u *iam.User) {},
setAccounts: []string{accts[1].GetPublicId()},
resultAccounts: []string{accts[1].GetPublicId()},
},
{
name: "Set account on populated user",
setup: func(u *iam.User) {
iamRepo.AssociateAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
iamRepo.AddUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -749,7 +749,7 @@ func TestSetAccount(t *testing.T) {
{
name: "Set empty on populated user",
setup: func(u *iam.User) {
iamRepo.AssociateAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
iamRepo.AddUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId(), accts[1].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -760,27 +760,27 @@ func TestSetAccount(t *testing.T) {
}
for _, tc := range setCases {
t.Run(tc.name, func(t *testing.T) {
usr := iam.TestUser(t, iamRepo, o.GetPublicId())
defer func() {
iamRepo.DeleteUser(context.Background(), usr.GetPublicId())
}()
t.Run(tc.name, func(t *testing.T) {
usr := iam.TestUser(t, iamRepo, o.GetPublicId())
defer func() {
iamRepo.DeleteUser(context.Background(), usr.GetPublicId())
}()
tc.setup(usr)
req := &pbs.SetUserAccountsRequest{
Id: usr.GetPublicId(),
Version: usr.GetVersion(),
AccountIds: tc.setAccounts,
}
tc.setup(usr)
req := &pbs.SetUserAccountsRequest{
Id: usr.GetPublicId(),
Version: usr.GetVersion(),
AccountIds: tc.setAccounts,
}
got, err := s.SetUserAccounts(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
if tc.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err, "Got error: %v", err)
assert.ElementsMatch(t, got.GetItem().GetAccountIds(), tc.resultAccounts)
})
got, err := s.SetUserAccounts(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
if tc.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err, "Got error: %v", err)
assert.ElementsMatch(t, got.GetItem().GetAccountIds(), tc.resultAccounts)
})
}
usr := iam.TestUser(t, iamRepo, o.GetPublicId())
@@ -826,22 +826,22 @@ func TestRemoveAccount(t *testing.T) {
accts := password.TestAccounts(t, conn, amId, 3)
addCases := []struct {
name string
setup func(*iam.User)
name string
setup func(*iam.User)
removeAccounts []string
resultAccounts []string
wantErr bool
wantErr bool
}{
{
name: "Remove account on empty user",
setup: func(*iam.User) {},
name: "Remove account on empty user",
setup: func(*iam.User) {},
removeAccounts: []string{accts[1].GetPublicId()},
wantErr: true,
wantErr: true,
},
{
name: "Remove 1 of 2 accounts from user",
setup: func(u *iam.User) {
_, err := iamRepo.SetAssociatedAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
_, err := iamRepo.SetUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId(), accts[1].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -852,7 +852,7 @@ func TestRemoveAccount(t *testing.T) {
{
name: "Remove all accounts from user",
setup: func(u *iam.User) {
_, err := iamRepo.SetAssociatedAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
_, err := iamRepo.SetUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId(), accts[1].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -863,7 +863,7 @@ func TestRemoveAccount(t *testing.T) {
{
name: "Remove empty on populated user",
setup: func(u *iam.User) {
_, err := iamRepo.SetAssociatedAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
_, err := iamRepo.SetUserAccounts(context.Background(), u.GetPublicId(), u.GetVersion(),
[]string{accts[0].GetPublicId()})
require.NoError(t, err)
u.Version = u.Version + 1
@@ -873,27 +873,27 @@ func TestRemoveAccount(t *testing.T) {
}
for _, tc := range addCases {
t.Run(tc.name, func(t *testing.T) {
usr := iam.TestUser(t, iamRepo, o.GetPublicId())
defer func() {
iamRepo.DeleteUser(context.Background(), usr.GetPublicId())
}()
tc.setup(usr)
req := &pbs.RemoveUserAccountsRequest{
Id: usr.GetPublicId(),
Version: usr.GetVersion(),
AccountIds: tc.removeAccounts,
}
t.Run(tc.name, func(t *testing.T) {
usr := iam.TestUser(t, iamRepo, o.GetPublicId())
defer func() {
iamRepo.DeleteUser(context.Background(), usr.GetPublicId())
}()
tc.setup(usr)
req := &pbs.RemoveUserAccountsRequest{
Id: usr.GetPublicId(),
Version: usr.GetVersion(),
AccountIds: tc.removeAccounts,
}
got, err := s.RemoveUserAccounts(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
if tc.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err, "Got error: %v", err)
got, err := s.RemoveUserAccounts(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), req)
if tc.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err, "Got error: %v", err)
assert.ElementsMatch(t, got.GetItem().GetAccountIds(), tc.resultAccounts)
})
assert.ElementsMatch(t, got.GetItem().GetAccountIds(), tc.resultAccounts)
})
}
usr := iam.TestUser(t, iamRepo, o.GetPublicId())