This is an automated email from the ASF dual-hosted git repository.
tianxiaoliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git
The following commit(s) were added to refs/heads/master by this push:
new 44b36e3e Change: allow creating account with ID (#1347)
44b36e3e is described below
commit 44b36e3e2c6a84ef35566c2899fc2a974b45f5bf
Author: little-cui <[email protected]>
AuthorDate: Tue Oct 25 18:48:11 2022 +0800
Change: allow creating account with ID (#1347)
---
server/service/rbac/account_service.go | 4 +++-
server/service/rbac/account_service_test.go | 22 ++++++++++++++++++----
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/server/service/rbac/account_service.go
b/server/service/rbac/account_service.go
index 264405ef..c0aad00f 100644
--- a/server/service/rbac/account_service.go
+++ b/server/service/rbac/account_service.go
@@ -79,7 +79,9 @@ func CreateAccount(ctx context.Context, a *rbacmodel.Account)
error {
}
a.Role = ""
a.CurrentPassword = ""
- a.ID = util.GenerateUUID()
+ if a.ID == "" {
+ a.ID = util.GenerateUUID()
+ }
a.CreateTime = strconv.FormatInt(time.Now().Unix(), 10)
a.UpdateTime = a.CreateTime
diff --git a/server/service/rbac/account_service_test.go
b/server/service/rbac/account_service_test.go
index 0ccc6354..6c472c93 100644
--- a/server/service/rbac/account_service_test.go
+++ b/server/service/rbac/account_service_test.go
@@ -52,19 +52,20 @@ func newAccount(name string) *rbac.Account {
}
func TestCreateAccount(t *testing.T) {
+ ctx := context.TODO()
t.Run("create account, should succeed", func(t *testing.T) {
a := newAccount("TestCreateAccount_create_account")
- err := rbacsvc.CreateAccount(context.TODO(), a)
+ err := rbacsvc.CreateAccount(ctx, a)
assert.Nil(t, err)
})
t.Run("create account twice, should return:
"+rbac.NewError(rbac.ErrAccountConflict, "").Error(), func(t *testing.T) {
name := "TestCreateAccount_create_account_twice"
a := newAccount(name)
- err := rbacsvc.CreateAccount(context.TODO(), a)
+ err := rbacsvc.CreateAccount(ctx, a)
assert.Nil(t, err)
a = newAccount(name)
- err = rbacsvc.CreateAccount(context.TODO(), a)
+ err = rbacsvc.CreateAccount(ctx, a)
assert.NotNil(t, err)
svcErr := err.(*errsvc.Error)
assert.Equal(t, rbac.ErrAccountConflict, svcErr.Code)
@@ -72,11 +73,24 @@ func TestCreateAccount(t *testing.T) {
t.Run("account has invalid role, should return:
"+rbac.NewError(rbac.ErrAccountHasInvalidRole, "").Error(), func(t *testing.T) {
a := newAccount("TestCreateAccount_account_has_invalid_role")
a.Roles = append(a.Roles, "invalid_role")
- err := rbacsvc.CreateAccount(context.TODO(), a)
+ err := rbacsvc.CreateAccount(ctx, a)
assert.NotNil(t, err)
svcErr := err.(*errsvc.Error)
assert.Equal(t, rbac.ErrAccountHasInvalidRole, svcErr.Code)
})
+ t.Run("account has id, should succeed", func(t *testing.T) {
+ accountName := "TestCreateAccount_account_has_id"
+ a := newAccount(accountName)
+ a.ID = "specifyID"
+ err := rbacsvc.CreateAccount(ctx, a)
+ assert.NoError(t, err)
+
+ defer rbacsvc.DeleteAccount(ctx, accountName)
+
+ account, err := rbacsvc.GetAccount(ctx, accountName)
+ assert.NoError(t, err)
+ assert.Equal(t, "specifyID", account.ID)
+ })
}
func TestDeleteAccount(t *testing.T) {