Add Delete Password Accounts to repo. (#220)
This commit is contained in:
1 parent
76abd4f3b4
commit
e943220468
2 files changed
+83
No files matched your search
@@ -99,6 +99,38 @@ func (r *Repository) ListAccounts(ctx context.Context, withAuthMethodId string,
|
||||
return accts, nil
|
||||
}
|
||||
|
||||
// DeleteAccount deletes the account for the provided id from the repository returning a count of the
|
||||
// number of records deleted. All options are ignored.
|
||||
func (r *Repository) DeleteAccount(ctx context.Context, withPublicId string, opt ...Option) (int, error) {
|
||||
if withPublicId == "" {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password account: missing public id: %w", db.ErrInvalidParameter)
|
||||
}
|
||||
ac := allocAccount()
|
||||
ac.PublicId = withPublicId
|
||||
|
||||
var rowsDeleted int
|
||||
_, err := r.writer.DoTx(
|
||||
ctx,
|
||||
db.StdRetryCnt,
|
||||
db.ExpBackoff{},
|
||||
func(_ db.Reader, w db.Writer) (err error) {
|
||||
metadata := ac.oplog(oplog.OpType_OP_TYPE_DELETE)
|
||||
dAc := ac.clone()
|
||||
rowsDeleted, err = w.Delete(ctx, dAc, db.WithOplog(r.wrapper, metadata))
|
||||
if err == nil && rowsDeleted > 1 {
|
||||
return db.ErrMultipleRecords
|
||||
}
|
||||
return err
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return db.NoRowsAffected, fmt.Errorf("delete: password account: %s: %w", withPublicId, err)
|
||||
}
|
||||
|
||||
return rowsDeleted, nil
|
||||
}
|
||||
|
||||
var reInvalidUserName = regexp.MustCompile("[^a-z0-9.]")
|
||||
|
||||
func validUserName(u string) bool {
|
||||
|
||||
@@ -337,6 +337,57 @@ func TestRepository_LookupAccount(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_DeleteAccount(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
|
||||
authMethod := testAuthMethods(t, conn, 1)[0]
|
||||
account := testAccounts(t, conn, authMethod.GetScopeId(), authMethod.GetPublicId(), 1)[0]
|
||||
|
||||
newAcctId, err := newAccountId()
|
||||
require.NoError(t, err)
|
||||
var tests = []struct {
|
||||
name string
|
||||
in string
|
||||
want int
|
||||
wantIsErr error
|
||||
}{
|
||||
{
|
||||
name: "With no public id",
|
||||
wantIsErr: db.ErrInvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "With non existing account id",
|
||||
in: newAcctId,
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "With existing account id",
|
||||
in: account.GetPublicId(),
|
||||
want: 1,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
repo, err := NewRepository(rw, rw, wrapper)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
got, err := repo.DeleteAccount(context.Background(), tt.in)
|
||||
if tt.wantIsErr != nil {
|
||||
assert.Truef(errors.Is(err, tt.wantIsErr), "want err: %q got: %q", tt.wantIsErr, err)
|
||||
assert.Zero(got)
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.EqualValues(tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListAccounts(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
|
||||
Reference in new issue
Block a user