github.com/polarismesh/polaris@v1.17.8/store/boltdb/user_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 "sort" 23 "strings" 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 29 "github.com/polarismesh/polaris/common/model" 30 ) 31 32 func createTestUsers(num int) []*model.User { 33 ret := make([]*model.User, 0, num) 34 35 for i := 0; i < num; i++ { 36 ret = append(ret, &model.User{ 37 ID: fmt.Sprintf("user_%d", i), 38 Name: fmt.Sprintf("user_%d", i), 39 Password: fmt.Sprintf("user_%d", i), 40 Owner: "polaris", 41 Source: "Polaris", 42 Type: model.SubAccountUserRole, 43 Token: "polaris", 44 TokenEnable: true, 45 Valid: true, 46 Comment: "", 47 CreateTime: time.Now(), 48 ModifyTime: time.Now(), 49 }) 50 } 51 52 return ret 53 } 54 55 func Test_userStore_AddUser(t *testing.T) { 56 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 57 us := &userStore{handler: handler} 58 59 users := createTestUsers(1) 60 61 if err := us.AddUser(users[0]); err != nil { 62 t.Fatal(err) 63 } 64 65 ret, err := us.GetUser(users[0].ID) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 tn := time.Now() 71 72 users[0].CreateTime = tn 73 users[0].ModifyTime = tn 74 ret.CreateTime = tn 75 ret.ModifyTime = tn 76 77 if !assert.Equal(t, users[0], ret) { 78 t.FailNow() 79 } 80 }) 81 } 82 83 func Test_userStore_UpdateUser(t *testing.T) { 84 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 85 us := &userStore{handler: handler} 86 87 users := createTestUsers(1) 88 89 if err := us.AddUser(users[0]); err != nil { 90 t.Fatal(err) 91 } 92 93 users[0].Comment = "user update test" 94 95 if err := us.UpdateUser(users[0]); err != nil { 96 t.Fatal(err) 97 } 98 99 ret, err := us.GetUser(users[0].ID) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 tn := time.Now() 105 106 users[0].CreateTime = tn 107 users[0].ModifyTime = tn 108 ret.CreateTime = tn 109 ret.ModifyTime = tn 110 111 if !assert.Equal(t, users[0], ret) { 112 t.FailNow() 113 } 114 }) 115 } 116 117 func Test_userStore_DeleteUser(t *testing.T) { 118 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 119 us := &userStore{handler: handler} 120 121 users := createTestUsers(1) 122 123 if err := us.AddUser(users[0]); err != nil { 124 t.Fatal(err) 125 } 126 127 ret, err := us.GetUser(users[0].ID) 128 if err != nil { 129 t.Fatal(err) 130 } 131 132 if !assert.NotNil(t, ret) { 133 t.FailNow() 134 } 135 136 if err = us.DeleteUser(users[0]); err != nil { 137 t.Fatal(err) 138 } 139 140 ret, err = us.GetUser(users[0].ID) 141 if err != nil { 142 t.Fatal(err) 143 } 144 145 if !assert.Nil(t, ret) { 146 t.FailNow() 147 } 148 }) 149 } 150 151 func Test_userStore_GetUserByName(t *testing.T) { 152 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 153 us := &userStore{handler: handler} 154 155 users := createTestUsers(1) 156 157 if err := us.AddUser(users[0]); err != nil { 158 t.Fatal(err) 159 } 160 161 ret, err := us.GetUserByName(users[0].Name, users[0].Owner) 162 if err != nil { 163 t.Fatal(err) 164 } 165 166 tn := time.Now() 167 168 users[0].CreateTime = tn 169 users[0].ModifyTime = tn 170 ret.CreateTime = tn 171 ret.ModifyTime = tn 172 173 if !assert.Equal(t, users[0], ret) { 174 t.FailNow() 175 } 176 }) 177 } 178 179 func Test_userStore_GetUserByIds(t *testing.T) { 180 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 181 us := &userStore{handler: handler} 182 183 users := createTestUsers(5) 184 ids := make([]string, 0, len(users)) 185 186 for i := range users { 187 if err := us.AddUser(users[i]); err != nil { 188 t.Fatal(err) 189 } 190 ids = append(ids, users[i].ID) 191 } 192 193 ret, err := us.GetUserByIds(ids) 194 if err != nil { 195 t.Fatal(err) 196 } 197 198 if len(ret) != len(users) { 199 t.Fatal("len(ret) != len(users)") 200 } 201 202 tn := time.Now() 203 204 sort.Slice(users, func(i, j int) bool { 205 206 users[i].CreateTime = tn 207 users[i].ModifyTime = tn 208 users[j].CreateTime = tn 209 users[j].ModifyTime = tn 210 211 return strings.Compare(users[i].ID, users[j].ID) < 0 212 }) 213 214 sort.Slice(ret, func(i, j int) bool { 215 216 ret[i].CreateTime = tn 217 ret[i].ModifyTime = tn 218 ret[j].CreateTime = tn 219 ret[j].ModifyTime = tn 220 221 return strings.Compare(ret[i].ID, ret[j].ID) < 0 222 }) 223 224 if !assert.ElementsMatch(t, users, ret) { 225 t.FailNow() 226 } 227 228 }) 229 } 230 231 func Test_userStore_GetSubCount(t *testing.T) { 232 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 233 us := &userStore{handler: handler} 234 235 users := createTestUsers(5) 236 237 for i := range users { 238 if err := us.AddUser(users[i]); err != nil { 239 t.Fatal(err) 240 } 241 } 242 243 total, err := us.GetSubCount(&model.User{ 244 ID: "polaris", 245 }) 246 247 if err != nil { 248 t.Fatal(err) 249 } 250 251 if !assert.Equal(t, int(len(users)), int(total)) { 252 t.FailNow() 253 } 254 255 }) 256 } 257 258 func Test_userStore_GetUsers(t *testing.T) { 259 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 260 us := &userStore{handler: handler} 261 262 users := createTestUsers(10) 263 264 for i := range users { 265 if err := us.AddUser(users[i]); err != nil { 266 t.Fatal(err) 267 } 268 } 269 270 total, ret, err := us.GetUsers(map[string]string{ 271 "name": "ser*", 272 }, 0, 2) 273 274 if err != nil { 275 t.Fatal(err) 276 } 277 278 if !assert.Equal(t, 2, len(ret)) { 279 t.Fatal("len(ret) != limit(2)") 280 } 281 282 if !assert.Equal(t, 10, int(total)) { 283 t.Fatal("total != 10") 284 } 285 286 total, ret, err = us.GetUsers(map[string]string{ 287 "name": "pser*", 288 }, 0, 2) 289 290 if err != nil { 291 t.Fatal(err) 292 } 293 294 if !assert.Equal(t, 0, len(ret)) { 295 t.Fatal("len(ret) need to zero") 296 } 297 298 if !assert.Equal(t, 0, int(total)) { 299 t.Fatal("total != 0") 300 } 301 302 admins := createTestUsers(1) 303 admins[0].ID = "admin" 304 admins[0].Name = "admin" 305 admins[0].Type = model.AdminUserRole 306 307 if err := us.AddUser(admins[0]); err != nil { 308 t.Fatal(err) 309 } 310 311 total, ret, err = us.GetUsers(map[string]string{ 312 "hide_admin": "true", 313 }, 0, 1000) 314 315 if !assert.Equal(t, 10, len(ret)) { 316 t.Fatal("len(ret) not equal 10") 317 } 318 319 if !assert.Equal(t, 10, int(total)) { 320 t.Fatal("total != 10") 321 } 322 323 total, ret, err = us.GetUsers(map[string]string{ 324 "hide_admin": "false", 325 }, 0, 1000) 326 327 if !assert.Equal(t, 11, len(ret)) { 328 t.Fatal("len(ret) not equal 11") 329 } 330 331 if !assert.Equal(t, 11, int(total)) { 332 t.Fatal("total != 11") 333 } 334 }) 335 } 336 337 func Test_userStore_GetUsersByGroup(t *testing.T) { 338 CreateTableDBHandlerAndRun(t, "test_user", func(t *testing.T, handler BoltHandler) { 339 us := &userStore{handler: handler} 340 gs := &groupStore{handler: handler} 341 342 groups := createTestUserGroup(1) 343 for i := range groups { 344 if err := gs.AddGroup(groups[i]); err != nil { 345 t.Fatal(err) 346 } 347 } 348 349 users := createTestUsers(10) 350 for i := range users { 351 if err := us.AddUser(users[i]); err != nil { 352 t.Fatal(err) 353 } 354 } 355 356 total, ret, err := us.GetUsers(map[string]string{ 357 "group_id": groups[0].ID, 358 }, 0, 100) 359 360 if err != nil { 361 t.Fatal(err) 362 } 363 364 if !assert.Equal(t, 1, len(ret)) { 365 t.Fatal("len(ret) need to zero") 366 } 367 368 if !assert.Equal(t, 1, int(total)) { 369 t.Fatal("total != 0") 370 } 371 }) 372 }