github.com/polarismesh/polaris@v1.17.8/store/boltdb/group_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package boltdb
    19  
    20  import (
    21  	"fmt"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/polarismesh/polaris/common/model"
    28  )
    29  
    30  func buildUserIds(users []*model.User) map[string]struct{} {
    31  	ret := make(map[string]struct{}, len(users))
    32  
    33  	for i := range users {
    34  		user := users[i]
    35  		ret[user.ID] = struct{}{}
    36  	}
    37  
    38  	return ret
    39  }
    40  
    41  func createTestUserGroup(num int) []*model.UserGroupDetail {
    42  	ret := make([]*model.UserGroupDetail, 0, num)
    43  
    44  	users := createTestUsers(num)
    45  
    46  	for i := 0; i < num; i++ {
    47  		ret = append(ret, &model.UserGroupDetail{
    48  			UserGroup: &model.UserGroup{
    49  				ID:          fmt.Sprintf("test_group_%d", i),
    50  				Name:        fmt.Sprintf("test_group_%d", i),
    51  				Owner:       "polaris",
    52  				Token:       "polaris",
    53  				TokenEnable: true,
    54  				Valid:       true,
    55  				Comment:     "polaris",
    56  				CreateTime:  time.Now(),
    57  				ModifyTime:  time.Now(),
    58  			},
    59  			UserIds: buildUserIds(users),
    60  		})
    61  	}
    62  
    63  	return ret
    64  }
    65  
    66  func Test_groupStore_AddGroup(t *testing.T) {
    67  	CreateTableDBHandlerAndRun(t, "test_group", func(t *testing.T, handler BoltHandler) {
    68  		gs := &groupStore{handler: handler}
    69  
    70  		groups := createTestUserGroup(1)
    71  
    72  		if err := gs.AddGroup(groups[0]); err != nil {
    73  			t.Fatal(err)
    74  		}
    75  
    76  		ret, err := gs.GetGroup(groups[0].ID)
    77  		if err != nil {
    78  			t.Fatal(err)
    79  		}
    80  
    81  		tn := time.Now()
    82  		groups[0].CreateTime = tn
    83  		groups[0].ModifyTime = tn
    84  		ret.CreateTime = tn
    85  		ret.ModifyTime = tn
    86  
    87  		assert.Equal(t, groups[0], ret)
    88  	})
    89  }
    90  
    91  func Test_groupStore_UpdateGroup(t *testing.T) {
    92  	CreateTableDBHandlerAndRun(t, "test_group", func(t *testing.T, handler BoltHandler) {
    93  		gs := &groupStore{handler: handler}
    94  
    95  		groups := createTestUserGroup(1)
    96  
    97  		if err := gs.AddGroup(groups[0]); err != nil {
    98  			t.Fatal(err)
    99  		}
   100  
   101  		groups[0].Comment = time.Now().String()
   102  
   103  		if err := gs.UpdateGroup(&model.ModifyUserGroup{
   104  			ID:          groups[0].ID,
   105  			Owner:       groups[0].Owner,
   106  			Token:       groups[0].Token,
   107  			TokenEnable: groups[0].TokenEnable,
   108  			Comment:     groups[0].Comment,
   109  		}); err != nil {
   110  			t.Fatal(err)
   111  		}
   112  
   113  		ret, err := gs.GetGroup(groups[0].ID)
   114  		if err != nil {
   115  			t.Fatal(err)
   116  		}
   117  
   118  		tn := time.Now()
   119  		groups[0].CreateTime = tn
   120  		groups[0].ModifyTime = tn
   121  		ret.CreateTime = tn
   122  		ret.ModifyTime = tn
   123  
   124  		assert.Equal(t, groups[0], ret)
   125  	})
   126  }
   127  
   128  func Test_groupStore_DeleteGroup(t *testing.T) {
   129  	CreateTableDBHandlerAndRun(t, "test_group", func(t *testing.T, handler BoltHandler) {
   130  		gs := &groupStore{handler: handler}
   131  
   132  		groups := createTestUserGroup(1)
   133  
   134  		if err := gs.AddGroup(groups[0]); err != nil {
   135  			t.Fatal(err)
   136  		}
   137  
   138  		groups[0].Comment = time.Now().String()
   139  
   140  		if err := gs.DeleteGroup(groups[0]); err != nil {
   141  			t.Fatal(err)
   142  		}
   143  
   144  		ret, err := gs.GetGroup(groups[0].ID)
   145  		if err != nil {
   146  			t.Fatal(err)
   147  		}
   148  
   149  		assert.Nil(t, ret)
   150  	})
   151  }
   152  
   153  func Test_groupStore_GetGroupByName(t *testing.T) {
   154  	CreateTableDBHandlerAndRun(t, "test_group", func(t *testing.T, handler BoltHandler) {
   155  		gs := &groupStore{handler: handler}
   156  
   157  		groups := createTestUserGroup(1)
   158  
   159  		if err := gs.AddGroup(groups[0]); err != nil {
   160  			t.Fatal(err)
   161  		}
   162  
   163  		ret, err := gs.GetGroupByName(groups[0].Name, groups[0].Owner)
   164  		if err != nil {
   165  			t.Fatal(err)
   166  		}
   167  
   168  		tn := time.Now()
   169  		groups[0].CreateTime = tn
   170  		groups[0].ModifyTime = tn
   171  		ret.CreateTime = tn
   172  		ret.ModifyTime = tn
   173  
   174  		assert.Equal(t, groups[0].UserGroup, ret)
   175  	})
   176  }
   177  
   178  func Test_groupStore_GetGroups(t *testing.T) {
   179  	CreateTableDBHandlerAndRun(t, "test_group", func(t *testing.T, handler BoltHandler) {
   180  		gs := &groupStore{handler: handler}
   181  
   182  		groups := createTestUserGroup(10)
   183  
   184  		for i := range groups {
   185  			if err := gs.AddGroup(groups[i]); err != nil {
   186  				t.Fatal(err)
   187  			}
   188  		}
   189  
   190  		total, ret, err := gs.GetGroups(map[string]string{
   191  			"name": "gr*",
   192  		}, 0, 2)
   193  		if err != nil {
   194  			t.Fatal(err)
   195  		}
   196  
   197  		if !assert.Equal(t, 2, len(ret)) {
   198  			t.Fatal("len(ret) need equal 2")
   199  		}
   200  
   201  		if !assert.Equal(t, len(groups), int(total)) {
   202  			t.Fatal("total != len(groups)")
   203  		}
   204  
   205  		total, ret, err = gs.GetGroups(map[string]string{
   206  			"name": "gr*",
   207  		}, 100, 2)
   208  		if err != nil {
   209  			t.Fatal(err)
   210  		}
   211  
   212  		if !assert.Equal(t, 0, len(ret)) {
   213  			t.Fatal("len(ret) need zero")
   214  		}
   215  
   216  		if !assert.Equal(t, len(groups), int(total)) {
   217  			t.Fatal("total != len(groups)")
   218  		}
   219  
   220  		total, ret, err = gs.GetGroups(map[string]string{
   221  			"name": "gr*",
   222  		}, 0, 100)
   223  		if err != nil {
   224  			t.Fatal(err)
   225  		}
   226  
   227  		if !assert.Equal(t, len(groups), len(ret)) {
   228  			t.Fatal("len(ret) need zero")
   229  		}
   230  
   231  		if !assert.Equal(t, len(groups), int(total)) {
   232  			t.Fatal("total != len(groups)")
   233  		}
   234  
   235  		total, ret, err = gs.GetGroups(map[string]string{
   236  			"user_id": "user_1",
   237  		}, 0, 100)
   238  		if err != nil {
   239  			t.Fatal(err)
   240  		}
   241  
   242  		if !assert.Equal(t, len(groups), len(ret)) {
   243  			t.Fatal("len(ret) need zero")
   244  		}
   245  
   246  		if !assert.Equal(t, len(groups), int(total)) {
   247  			t.Fatal("total != len(groups)")
   248  		}
   249  
   250  		total, ret, err = gs.GetGroups(map[string]string{
   251  			"owner": "polaris",
   252  		}, 0, 100)
   253  		if err != nil {
   254  			t.Fatal(err)
   255  		}
   256  
   257  		if !assert.Equal(t, len(groups), len(ret)) {
   258  			t.Fatal("len(ret) need zero")
   259  		}
   260  
   261  		if !assert.Equal(t, len(groups), int(total)) {
   262  			t.Fatal("total != len(groups)")
   263  		}
   264  	})
   265  }