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 de92979  unified account ut and fix account bugs. (#862)
de92979 is described below

commit de92979b948ca68aadfe8ac8fde6f5e2802ac380
Author: panqian <[email protected]>
AuthorDate: Sat Feb 20 16:33:05 2021 +0800

    unified account ut and fix account bugs. (#862)
---
 datasource/account.go                  |  10 +-
 datasource/{etcd => }/account_test.go  |  39 +++++---
 datasource/etcd/account.go             |  27 +++---
 datasource/mongo/account.go            |  36 ++++---
 datasource/mongo/account_test.go       | 165 ---------------------------------
 etc/conf/app.yaml                      |   2 +-
 server/service/rbac/dao/account_dao.go |   4 +-
 7 files changed, 65 insertions(+), 218 deletions(-)

diff --git a/datasource/account.go b/datasource/account.go
index b602cc0..c5a2df7 100644
--- a/datasource/account.go
+++ b/datasource/account.go
@@ -32,9 +32,9 @@ var (
 // AccountManager contains the RBAC CRUD
 type AccountManager interface {
        CreateAccount(ctx context.Context, a *rbac.Account) error
-       AccountExist(ctx context.Context, key string) (bool, error)
-       GetAccount(ctx context.Context, key string) (*rbac.Account, error)
-       ListAccount(ctx context.Context, key string) ([]*rbac.Account, int64, 
error)
-       DeleteAccount(ctx context.Context, key string) (bool, error)
-       UpdateAccount(ctx context.Context, key string, account *rbac.Account) 
error
+       AccountExist(ctx context.Context, name string) (bool, error)
+       GetAccount(ctx context.Context, name string) (*rbac.Account, error)
+       ListAccount(ctx context.Context) ([]*rbac.Account, int64, error)
+       DeleteAccount(ctx context.Context, names []string) (bool, error)
+       UpdateAccount(ctx context.Context, name string, account *rbac.Account) 
error
 }
diff --git a/datasource/etcd/account_test.go b/datasource/account_test.go
similarity index 65%
rename from datasource/etcd/account_test.go
rename to datasource/account_test.go
index 7b6ddc9..f7bce08 100644
--- a/datasource/etcd/account_test.go
+++ b/datasource/account_test.go
@@ -17,13 +17,14 @@
  * under the License.
  */
 
-package etcd_test
+package datasource_test
 
 import (
        "context"
-       "github.com/go-chassis/cari/rbac"
        "testing"
 
+       "github.com/go-chassis/cari/rbac"
+
        "github.com/apache/servicecomb-service-center/datasource"
        "github.com/stretchr/testify/assert"
 )
@@ -43,37 +44,49 @@ var (
                Password:            "tnuocca-tset",
                Roles:               []string{"admin"},
                TokenExpirationTime: "2020-12-30",
-               CurrentPassword:     "tnuocca-tset1",
+               CurrentPassword:     "tnuocca-tset2",
        }
 )
 
 func TestAccount(t *testing.T) {
        t.Run("add and get account", func(t *testing.T) {
-               err := 
datasource.Instance().UpdateAccount(context.Background(), "test-account-key", 
&a1)
+               err := 
datasource.Instance().CreateAccount(context.Background(), &a1)
                assert.NoError(t, err)
-               r, err := 
datasource.Instance().GetAccount(context.Background(), "test-account-key")
+               r, err := 
datasource.Instance().GetAccount(context.Background(), a1.Name)
                assert.NoError(t, err)
                assert.Equal(t, a1, *r)
+               _, err = 
datasource.Instance().DeleteAccount(context.Background(), []string{a1.Name})
+               assert.NoError(t, err)
        })
        t.Run("account should exist", func(t *testing.T) {
-               exist, err := 
datasource.Instance().AccountExist(context.Background(), "test-account-key")
+               err := 
datasource.Instance().CreateAccount(context.Background(), &a1)
+               assert.NoError(t, err)
+               exist, err := 
datasource.Instance().AccountExist(context.Background(), a1.Name)
                assert.NoError(t, err)
                assert.True(t, exist)
+               _, err = 
datasource.Instance().DeleteAccount(context.Background(), []string{a1.Name})
+               assert.NoError(t, err)
        })
        t.Run("delete account", func(t *testing.T) {
-               err := 
datasource.Instance().UpdateAccount(context.Background(), 
"test-account-key222", &a1)
+               err := 
datasource.Instance().CreateAccount(context.Background(), &a2)
                assert.NoError(t, err)
-               _, err = 
datasource.Instance().DeleteAccount(context.Background(), "test-account-key222")
+               _, err = 
datasource.Instance().DeleteAccount(context.Background(), []string{a2.Name})
                assert.NoError(t, err)
        })
-       t.Run("add two accounts and list", func(t *testing.T) {
-               err := 
datasource.Instance().UpdateAccount(context.Background(), "key1", &a1)
+       t.Run("add and update accounts then list", func(t *testing.T) {
+               err := 
datasource.Instance().CreateAccount(context.Background(), &a1)
+               assert.NoError(t, err)
+               err = datasource.Instance().CreateAccount(context.Background(), 
&a2)
                assert.NoError(t, err)
-               err = datasource.Instance().UpdateAccount(context.Background(), 
"key2", &a2)
+               a2.Password = "new-password"
+               err = datasource.Instance().UpdateAccount(context.Background(), 
a2.Name, &a2)
                assert.NoError(t, err)
-               accs, n, err := 
datasource.Instance().ListAccount(context.Background(), "key")
+               _, n, err := 
datasource.Instance().ListAccount(context.Background())
                assert.NoError(t, err)
                assert.Equal(t, int64(2), n)
-               t.Log(accs)
+               _, err = 
datasource.Instance().DeleteAccount(context.Background(), []string{a1.Name})
+               assert.NoError(t, err)
+               _, err = 
datasource.Instance().DeleteAccount(context.Background(), []string{a2.Name})
+               assert.NoError(t, err)
        })
 }
diff --git a/datasource/etcd/account.go b/datasource/etcd/account.go
index 7b887b1..0b677c2 100644
--- a/datasource/etcd/account.go
+++ b/datasource/etcd/account.go
@@ -41,7 +41,7 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbac.Account) error
                        log.Errorf(err, "can not release account lock")
                }
        }()
-       key := path.GenerateRBACAccountKey(a.Name)
+       name := path.GenerateRBACAccountKey(a.Name)
        exist, err := datasource.Instance().AccountExist(ctx, a.Name)
        if err != nil {
                log.Errorf(err, "can not save account info")
@@ -61,7 +61,7 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbac.Account) error
                log.Errorf(err, "account info is invalid")
                return err
        }
-       err = client.PutBytes(ctx, key, value)
+       err = client.PutBytes(ctx, name, value)
        if err != nil {
                log.Errorf(err, "can not save account info")
                return err
@@ -70,9 +70,9 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbac.Account) error
        return nil
 }
 
-func (ds *DataSource) AccountExist(ctx context.Context, key string) (bool, 
error) {
+func (ds *DataSource) AccountExist(ctx context.Context, name string) (bool, 
error) {
        resp, err := client.Instance().Do(ctx, client.GET,
-               client.WithStrKey(path.GenerateRBACAccountKey(key)))
+               client.WithStrKey(path.GenerateRBACAccountKey(name)))
        if err != nil {
                return false, err
        }
@@ -81,9 +81,9 @@ func (ds *DataSource) AccountExist(ctx context.Context, key 
string) (bool, error
        }
        return true, nil
 }
-func (ds *DataSource) GetAccount(ctx context.Context, key string) 
(*rbac.Account, error) {
+func (ds *DataSource) GetAccount(ctx context.Context, name string) 
(*rbac.Account, error) {
        resp, err := client.Instance().Do(ctx, client.GET,
-               client.WithStrKey(path.GenerateRBACAccountKey(key)))
+               client.WithStrKey(path.GenerateRBACAccountKey(name)))
        if err != nil {
                return nil, err
        }
@@ -98,9 +98,9 @@ func (ds *DataSource) GetAccount(ctx context.Context, key 
string) (*rbac.Account
        }
        return account, nil
 }
-func (ds *DataSource) ListAccount(ctx context.Context, key string) 
([]*rbac.Account, int64, error) {
+func (ds *DataSource) ListAccount(ctx context.Context) ([]*rbac.Account, 
int64, error) {
        resp, err := client.Instance().Do(ctx, client.GET,
-               client.WithStrKey(path.GenerateRBACAccountKey(key)), 
client.WithPrefix())
+               client.WithStrKey(path.GenerateRBACAccountKey("")), 
client.WithPrefix())
        if err != nil {
                return nil, 0, err
        }
@@ -117,22 +117,25 @@ func (ds *DataSource) ListAccount(ctx context.Context, 
key string) ([]*rbac.Acco
        }
        return accounts, resp.Count, nil
 }
-func (ds *DataSource) DeleteAccount(ctx context.Context, key string) (bool, 
error) {
+func (ds *DataSource) DeleteAccount(ctx context.Context, names []string) 
(bool, error) {
+       if len(names) == 0 {
+               return false, nil
+       }
        resp, err := client.Instance().Do(ctx, client.DEL,
-               client.WithStrKey(path.GenerateRBACAccountKey(key)))
+               client.WithStrKey(path.GenerateRBACAccountKey(names[0])))
        if err != nil {
                return false, err
        }
        return resp.Succeeded, nil
 }
-func (ds *DataSource) UpdateAccount(ctx context.Context, key string, account 
*rbac.Account) error {
+func (ds *DataSource) UpdateAccount(ctx context.Context, name string, account 
*rbac.Account) error {
        value, err := json.Marshal(account)
        if err != nil {
                log.Errorf(err, "account info is invalid")
                return err
        }
        _, err = client.Instance().Do(ctx, client.PUT,
-               client.WithStrKey(path.GenerateRBACAccountKey(key)),
+               client.WithStrKey(path.GenerateRBACAccountKey(name)),
                client.WithValue(value))
        return err
 }
diff --git a/datasource/mongo/account.go b/datasource/mongo/account.go
index 1347f69..12f8e87 100644
--- a/datasource/mongo/account.go
+++ b/datasource/mongo/account.go
@@ -20,11 +20,11 @@ package mongo
 import (
        "context"
        "errors"
-       "github.com/apache/servicecomb-service-center/pkg/privacy"
 
        "github.com/apache/servicecomb-service-center/datasource"
        "github.com/apache/servicecomb-service-center/datasource/mongo/client"
        "github.com/apache/servicecomb-service-center/pkg/log"
+       "github.com/apache/servicecomb-service-center/pkg/privacy"
        "github.com/apache/servicecomb-service-center/pkg/util"
        "github.com/go-chassis/cari/rbac"
        "go.mongodb.org/mongo-driver/bson"
@@ -56,9 +56,9 @@ func (ds *DataSource) CreateAccount(ctx context.Context, a 
*rbac.Account) error
        return nil
 }
 
-func (ds *DataSource) AccountExist(ctx context.Context, key string) (bool, 
error) {
+func (ds *DataSource) AccountExist(ctx context.Context, name string) (bool, 
error) {
        filter := bson.M{
-               ColumnAccountName: key,
+               ColumnAccountName: name,
        }
        count, err := client.GetMongoClient().Count(ctx, CollectionAccount, 
filter)
        if err != nil {
@@ -70,9 +70,9 @@ func (ds *DataSource) AccountExist(ctx context.Context, key 
string) (bool, error
        return true, nil
 }
 
-func (ds *DataSource) GetAccount(ctx context.Context, key string) 
(*rbac.Account, error) {
+func (ds *DataSource) GetAccount(ctx context.Context, name string) 
(*rbac.Account, error) {
        filter := bson.M{
-               ColumnAccountName: key,
+               ColumnAccountName: name,
        }
        result, err := client.GetMongoClient().FindOne(ctx, CollectionAccount, 
filter)
        if err != nil {
@@ -91,14 +91,8 @@ func (ds *DataSource) GetAccount(ctx context.Context, key 
string) (*rbac.Account
        return &account, nil
 }
 
-func (ds *DataSource) ListAccount(ctx context.Context, key string) 
([]*rbac.Account, int64, error) {
-       filter := bson.M{
-               ColumnAccountName: bson.M{"$regex": key},
-       }
-       if len(key) == 0 {
-               filter = bson.M{}
-       }
-       cursor, err := client.GetMongoClient().Find(ctx, CollectionAccount, 
filter)
+func (ds *DataSource) ListAccount(ctx context.Context) ([]*rbac.Account, 
int64, error) {
+       cursor, err := client.GetMongoClient().Find(ctx, CollectionAccount, 
bson.M{})
        if err != nil {
                return nil, 0, err
        }
@@ -117,9 +111,12 @@ func (ds *DataSource) ListAccount(ctx context.Context, key 
string) ([]*rbac.Acco
        return accounts, int64(len(accounts)), nil
 }
 
-func (ds *DataSource) DeleteAccount(ctx context.Context, key string) (bool, 
error) {
+func (ds *DataSource) DeleteAccount(ctx context.Context, names []string) 
(bool, error) {
+       if len(names) == 0 {
+               return false, nil
+       }
        filter := bson.M{
-               ColumnAccountName: key,
+               ColumnAccountName: names[0],
        }
        result, err := client.GetMongoClient().Delete(ctx, CollectionAccount, 
filter)
        if err != nil {
@@ -131,14 +128,13 @@ func (ds *DataSource) DeleteAccount(ctx context.Context, 
key string) (bool, erro
        return true, nil
 }
 
-func (ds *DataSource) UpdateAccount(ctx context.Context, key string, account 
*rbac.Account) error {
+func (ds *DataSource) UpdateAccount(ctx context.Context, name string, account 
*rbac.Account) error {
        filter := bson.M{
-               ColumnAccountName: key,
+               ColumnAccountName: name,
        }
        update := bson.M{
                "$set": bson.M{
                        ColumnID:                  account.ID,
-                       ColumnAccountName:         account.Name,
                        ColumnPassword:            account.Password,
                        ColumnRoles:               account.Roles,
                        ColumnTokenExpirationTime: account.TokenExpirationTime,
@@ -146,11 +142,11 @@ func (ds *DataSource) UpdateAccount(ctx context.Context, 
key string, account *rb
                        ColumnStatus:              account.Status,
                },
        }
-       result, err := client.GetMongoClient().Update(ctx, CollectionAccount, 
filter, update)
+       res, err := client.GetMongoClient().Update(ctx, CollectionAccount, 
filter, update)
        if err != nil {
                return err
        }
-       if result.ModifiedCount == 0 {
+       if res.ModifiedCount == 0 {
                return ErrUpdateNodata
        }
        return nil
diff --git a/datasource/mongo/account_test.go b/datasource/mongo/account_test.go
deleted file mode 100644
index 538cfd1..0000000
--- a/datasource/mongo/account_test.go
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package mongo_test
-
-import (
-       "context"
-       "github.com/go-chassis/cari/rbac"
-       "testing"
-
-       "github.com/apache/servicecomb-service-center/datasource"
-       "github.com/stretchr/testify/assert"
-)
-
-func TestCreateAccount(t *testing.T) {
-       account := rbac.Account{
-               ID:                  "11111-22222-33333",
-               Name:                "test-account1",
-               Password:            "tnuocca-tset",
-               Roles:               []string{"admin"},
-               TokenExpirationTime: "2020-12-30",
-               CurrentPassword:     "tnuocca-tset1",
-       }
-       t.Run("create account: should be able to create account", func(t 
*testing.T) {
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               err := 
datasource.Instance().CreateAccount(context.Background(), &account)
-               assert.Nil(t, err)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-       })
-
-       t.Run("create account: should not be able to create two same account", 
func(t *testing.T) {
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               err := 
datasource.Instance().CreateAccount(context.Background(), &account)
-               assert.Nil(t, err)
-               err = datasource.Instance().CreateAccount(context.Background(), 
&account)
-               assert.NotNil(t, err)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-       })
-}
-
-func TestGetAccount(t *testing.T) {
-       account := rbac.Account{
-               ID:                  "11111-22222-33333",
-               Name:                "test-account1",
-               Password:            "tnuocca-tset",
-               Roles:               []string{"admin"},
-               TokenExpirationTime: "2020-12-30",
-               CurrentPassword:     "tnuocca-tset1",
-       }
-       t.Run("get account: if the account exists, it should be able to get the 
account", func(t *testing.T) {
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               err := 
datasource.Instance().CreateAccount(context.Background(), &account)
-               assert.Nil(t, err)
-               result, err := 
datasource.Instance().GetAccount(context.Background(), account.Name)
-               assert.Nil(t, err)
-               assert.Equal(t, &account, result)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-       })
-
-       t.Run("get account: if the account not exists, it should not be able to 
get the account", func(t *testing.T) {
-               _, err := 
datasource.Instance().GetAccount(context.Background(), account.Name)
-               assert.NotNil(t, err)
-       })
-}
-
-func TestListAccount(t *testing.T) {
-       account1 := rbac.Account{
-               ID:                  "11111-22222-33333",
-               Name:                "test-account1",
-               Password:            "tnuocca-tset",
-               Roles:               []string{"admin"},
-               TokenExpirationTime: "2020-12-30",
-               CurrentPassword:     "tnuocca-tset1",
-       }
-       account2 := rbac.Account{
-               ID:                  "11111-22222-33333",
-               Name:                "test-account2",
-               Password:            "tnuocca-tset",
-               Roles:               []string{"admin"},
-               TokenExpirationTime: "2020-12-30",
-               CurrentPassword:     "tnuocca-tset2",
-       }
-       t.Run("list account: if there are multiple accounts exist, it should be 
able to query multiple accounts", func(t *testing.T) {
-               _ = datasource.Instance().CreateAccount(context.Background(), 
&account1)
-               _ = datasource.Instance().CreateAccount(context.Background(), 
&account2)
-               _, count, err := 
datasource.Instance().ListAccount(context.Background(), "test-account")
-               assert.Equal(t, int64(2), count)
-               assert.Nil(t, err)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account1.Name)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account2.Name)
-       })
-}
-
-func TestDeleteAccount(t *testing.T) {
-       t.Run("delete account, if the account does not exist,it should not be 
deleted", func(t *testing.T) {
-               flag, _ := 
datasource.Instance().DeleteAccount(context.Background(), "not_exist")
-               assert.Equal(t, false, flag)
-       })
-
-       t.Run("delete account, if the account exists,it should be deleted", 
func(t *testing.T) {
-               account := rbac.Account{
-                       ID:                  "11111-22222-33333",
-                       Name:                "test-account1",
-                       Password:            "tnuocca-tset",
-                       Roles:               []string{"admin"},
-                       TokenExpirationTime: "2020-12-30",
-                       CurrentPassword:     "tnuocca-tset1",
-               }
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               err := 
datasource.Instance().CreateAccount(context.Background(), &account)
-               assert.Nil(t, err)
-               flag, err := 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               assert.Equal(t, true, flag)
-               assert.Nil(t, err)
-       })
-}
-
-func TestUpdateAccount(t *testing.T) {
-       t.Run("update account: if the account does not exist,the update should 
fail", func(t *testing.T) {
-               account := rbac.Account{
-                       ID:                  "11111-22222-33333",
-                       Name:                "test-account1",
-                       Password:            "tnuocca-tset",
-                       Roles:               []string{"admin"},
-                       TokenExpirationTime: "2020-12-30",
-                       CurrentPassword:     "tnuocca-tset1",
-               }
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-               err := 
datasource.Instance().UpdateAccount(context.Background(), account.Name, 
&account)
-               assert.NotNil(t, err)
-       })
-
-       t.Run("update account: if the account exists,it should be updated 
successfully", func(t *testing.T) {
-               account := rbac.Account{
-                       ID:                  "11111-22222-33333",
-                       Name:                "test-account1",
-                       Password:            "tnuocca-tset",
-                       Roles:               []string{"admin"},
-                       TokenExpirationTime: "2020-12-30",
-                       CurrentPassword:     "tnuocca-tset1",
-               }
-               _ = datasource.Instance().CreateAccount(context.Background(), 
&account)
-               account.ID = "11111-22222-33333-44444"
-               err := 
datasource.Instance().UpdateAccount(context.Background(), account.Name, 
&account)
-               assert.Nil(t, err)
-               result, err := 
datasource.Instance().GetAccount(context.Background(), account.Name)
-               assert.Nil(t, err)
-               assert.Equal(t, account.ID, result.ID)
-               _, _ = 
datasource.Instance().DeleteAccount(context.Background(), account.Name)
-       })
-}
diff --git a/etc/conf/app.yaml b/etc/conf/app.yaml
index 1bd7719..9a21d6e 100644
--- a/etc/conf/app.yaml
+++ b/etc/conf/app.yaml
@@ -58,7 +58,7 @@ plugin:
   dir:
 
 registry:
-  # buildin, etcd, embeded_etcd, mongodb
+  # buildin, etcd, embeded_etcd, mongo
   kind: etcd
   compact:
     indexDelta:
diff --git a/server/service/rbac/dao/account_dao.go 
b/server/service/rbac/dao/account_dao.go
index 930b14e..9ff46a2 100644
--- a/server/service/rbac/dao/account_dao.go
+++ b/server/service/rbac/dao/account_dao.go
@@ -36,13 +36,13 @@ func GetAccount(ctx context.Context, name string) 
(*rbacmodel.Account, error) {
        return datasource.Instance().GetAccount(ctx, name)
 }
 func ListAccount(ctx context.Context) ([]*rbacmodel.Account, int64, error) {
-       return datasource.Instance().ListAccount(ctx, "")
+       return datasource.Instance().ListAccount(ctx)
 }
 func AccountExist(ctx context.Context, name string) (bool, error) {
        return datasource.Instance().AccountExist(ctx, name)
 }
 func DeleteAccount(ctx context.Context, name string) (bool, error) {
-       return datasource.Instance().DeleteAccount(ctx, name)
+       return datasource.Instance().DeleteAccount(ctx, []string{name})
 }
 
 //CreateAccount save 2 kv

Reply via email to