github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/plugin/helpers_kv_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package plugin_test 5 6 import ( 7 "strconv" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 12 "github.com/mattermost/mattermost-server/v5/model" 13 "github.com/mattermost/mattermost-server/v5/plugin" 14 "github.com/mattermost/mattermost-server/v5/plugin/plugintest" 15 ) 16 17 func TestKVGetJSON(t *testing.T) { 18 t.Run("incompatible server version", func(t *testing.T) { 19 api := &plugintest.API{} 20 api.On("GetServerVersion").Return("5.1.0") 21 22 p := &plugin.HelpersImpl{API: api} 23 var dat map[string]interface{} 24 25 ok, err := p.KVGetJSON("test-key", dat) 26 27 api.AssertExpectations(t) 28 assert.False(t, ok) 29 assert.Error(t, err) 30 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.2.0, current version: 5.1.0", err.Error()) 31 }) 32 33 t.Run("KVGet error", func(t *testing.T) { 34 p := &plugin.HelpersImpl{} 35 36 api := &plugintest.API{} 37 api.On("GetServerVersion").Return("5.2.0") 38 api.On("KVGet", "test-key").Return(nil, &model.AppError{}) 39 p.API = api 40 41 var dat map[string]interface{} 42 43 ok, err := p.KVGetJSON("test-key", dat) 44 api.AssertExpectations(t) 45 assert.False(t, ok) 46 assert.Error(t, err) 47 assert.Nil(t, dat) 48 }) 49 50 t.Run("unknown key", func(t *testing.T) { 51 p := &plugin.HelpersImpl{} 52 53 api := &plugintest.API{} 54 api.On("GetServerVersion").Return("5.2.0") 55 api.On("KVGet", "test-key").Return(nil, nil) 56 p.API = api 57 58 var dat map[string]interface{} 59 60 ok, err := p.KVGetJSON("test-key", dat) 61 api.AssertExpectations(t) 62 assert.False(t, ok) 63 assert.NoError(t, err) 64 assert.Nil(t, dat) 65 }) 66 67 t.Run("malformed JSON", func(t *testing.T) { 68 p := &plugin.HelpersImpl{} 69 70 api := &plugintest.API{} 71 api.On("GetServerVersion").Return("5.2.0") 72 api.On("KVGet", "test-key").Return([]byte(`{{:}"val-a": 10}`), nil) 73 p.API = api 74 75 var dat map[string]interface{} 76 77 ok, err := p.KVGetJSON("test-key", &dat) 78 api.AssertExpectations(t) 79 assert.False(t, ok) 80 assert.Error(t, err) 81 assert.Nil(t, dat) 82 }) 83 84 t.Run("wellformed JSON", func(t *testing.T) { 85 p := &plugin.HelpersImpl{} 86 87 api := &plugintest.API{} 88 api.On("GetServerVersion").Return("5.2.0") 89 api.On("KVGet", "test-key").Return([]byte(`{"val-a": 10}`), nil) 90 p.API = api 91 92 var dat map[string]interface{} 93 94 ok, err := p.KVGetJSON("test-key", &dat) 95 assert.True(t, ok) 96 api.AssertExpectations(t) 97 assert.NoError(t, err) 98 assert.Equal(t, map[string]interface{}{ 99 "val-a": float64(10), 100 }, dat) 101 }) 102 } 103 104 func TestKVSetJSON(t *testing.T) { 105 t.Run("incompatible server version", func(t *testing.T) { 106 api := &plugintest.API{} 107 api.On("GetServerVersion").Return("5.1.0") 108 109 p := &plugin.HelpersImpl{API: api} 110 111 err := p.KVSetJSON("test-key", map[string]interface{}{ 112 "val-a": float64(10), 113 }) 114 115 api.AssertExpectations(t) 116 assert.Error(t, err) 117 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.2.0, current version: 5.1.0", err.Error()) 118 }) 119 120 t.Run("JSON marshal error", func(t *testing.T) { 121 api := &plugintest.API{} 122 api.AssertNotCalled(t, "KVSet") 123 api.On("GetServerVersion").Return("5.2.0") 124 125 p := &plugin.HelpersImpl{API: api} 126 127 err := p.KVSetJSON("test-key", func() {}) 128 api.AssertExpectations(t) 129 assert.Error(t, err) 130 }) 131 132 t.Run("KVSet error", func(t *testing.T) { 133 api := &plugintest.API{} 134 api.On("KVSet", "test-key", []byte(`{"val-a":10}`)).Return(&model.AppError{}) 135 api.On("GetServerVersion").Return("5.2.0") 136 137 p := &plugin.HelpersImpl{API: api} 138 139 err := p.KVSetJSON("test-key", map[string]interface{}{ 140 "val-a": float64(10), 141 }) 142 143 api.AssertExpectations(t) 144 assert.Error(t, err) 145 }) 146 147 t.Run("marshallable struct", func(t *testing.T) { 148 api := &plugintest.API{} 149 api.On("KVSet", "test-key", []byte(`{"val-a":10}`)).Return(nil) 150 api.On("GetServerVersion").Return("5.2.0") 151 152 p := &plugin.HelpersImpl{API: api} 153 154 err := p.KVSetJSON("test-key", map[string]interface{}{ 155 "val-a": float64(10), 156 }) 157 158 api.AssertExpectations(t) 159 assert.NoError(t, err) 160 }) 161 } 162 163 func TestKVCompareAndSetJSON(t *testing.T) { 164 t.Run("incompatible server version", func(t *testing.T) { 165 api := &plugintest.API{} 166 api.On("GetServerVersion").Return("5.10.0") 167 p := &plugin.HelpersImpl{API: api} 168 169 ok, err := p.KVCompareAndSetJSON("test-key", nil, map[string]interface{}{ 170 "val-b": 20, 171 }) 172 173 assert.Equal(t, false, ok) 174 assert.Error(t, err) 175 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.12.0, current version: 5.10.0", err.Error()) 176 }) 177 178 t.Run("old value JSON marshal error", func(t *testing.T) { 179 api := &plugintest.API{} 180 api.AssertNotCalled(t, "KVCompareAndSet") 181 api.On("GetServerVersion").Return("5.12.0") 182 p := &plugin.HelpersImpl{API: api} 183 184 ok, err := p.KVCompareAndSetJSON("test-key", func() {}, map[string]interface{}{}) 185 186 api.AssertExpectations(t) 187 assert.Equal(t, false, ok) 188 assert.Error(t, err) 189 }) 190 191 t.Run("new value JSON marshal error", func(t *testing.T) { 192 api := &plugintest.API{} 193 api.On("GetServerVersion").Return("5.12.0") 194 api.AssertNotCalled(t, "KVCompareAndSet") 195 196 p := &plugin.HelpersImpl{API: api} 197 198 ok, err := p.KVCompareAndSetJSON("test-key", map[string]interface{}{}, func() {}) 199 200 api.AssertExpectations(t) 201 assert.False(t, ok) 202 assert.Error(t, err) 203 }) 204 205 t.Run("KVCompareAndSet error", func(t *testing.T) { 206 api := &plugintest.API{} 207 api.On("GetServerVersion").Return("5.12.0") 208 api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(`{"val-b":20}`)).Return(false, &model.AppError{}) 209 p := &plugin.HelpersImpl{API: api} 210 211 ok, err := p.KVCompareAndSetJSON("test-key", map[string]interface{}{ 212 "val-a": 10, 213 }, map[string]interface{}{ 214 "val-b": 20, 215 }) 216 217 api.AssertExpectations(t) 218 assert.False(t, ok) 219 assert.Error(t, err) 220 }) 221 222 t.Run("old value nil", func(t *testing.T) { 223 api := &plugintest.API{} 224 api.On("GetServerVersion").Return("5.12.0") 225 api.On("KVCompareAndSet", "test-key", []byte(nil), []byte(`{"val-b":20}`)).Return(true, nil) 226 p := &plugin.HelpersImpl{API: api} 227 228 ok, err := p.KVCompareAndSetJSON("test-key", nil, map[string]interface{}{ 229 "val-b": 20, 230 }) 231 232 api.AssertExpectations(t) 233 assert.True(t, ok) 234 assert.NoError(t, err) 235 }) 236 237 t.Run("old value non-nil", func(t *testing.T) { 238 api := &plugintest.API{} 239 api.On("GetServerVersion").Return("5.12.0") 240 api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(`{"val-b":20}`)).Return(true, nil) 241 p := &plugin.HelpersImpl{API: api} 242 243 ok, err := p.KVCompareAndSetJSON("test-key", map[string]interface{}{ 244 "val-a": 10, 245 }, map[string]interface{}{ 246 "val-b": 20, 247 }) 248 249 api.AssertExpectations(t) 250 assert.True(t, ok) 251 assert.NoError(t, err) 252 }) 253 254 t.Run("new value nil", func(t *testing.T) { 255 api := &plugintest.API{} 256 api.On("GetServerVersion").Return("5.12.0") 257 api.On("KVCompareAndSet", "test-key", []byte(`{"val-a":10}`), []byte(nil)).Return(true, nil) 258 p := &plugin.HelpersImpl{API: api} 259 260 ok, err := p.KVCompareAndSetJSON("test-key", map[string]interface{}{ 261 "val-a": 10, 262 }, nil) 263 264 api.AssertExpectations(t) 265 assert.True(t, ok) 266 assert.NoError(t, err) 267 }) 268 } 269 270 func TestKVCompareAndDeleteJSON(t *testing.T) { 271 t.Run("incompatible server version", func(t *testing.T) { 272 api := &plugintest.API{} 273 api.On("GetServerVersion").Return("5.10.0") 274 p := &plugin.HelpersImpl{API: api} 275 276 ok, err := p.KVCompareAndDeleteJSON("test-key", map[string]interface{}{ 277 "val-a": 10, 278 }) 279 280 assert.Equal(t, false, ok) 281 assert.Error(t, err) 282 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.16.0, current version: 5.10.0", err.Error()) 283 }) 284 285 t.Run("old value JSON marshal error", func(t *testing.T) { 286 api := &plugintest.API{} 287 api.On("GetServerVersion").Return("5.16.0") 288 api.AssertNotCalled(t, "KVCompareAndDelete") 289 p := &plugin.HelpersImpl{API: api} 290 291 ok, err := p.KVCompareAndDeleteJSON("test-key", func() {}) 292 293 api.AssertExpectations(t) 294 assert.Equal(t, false, ok) 295 assert.Error(t, err) 296 }) 297 298 t.Run("KVCompareAndDelete error", func(t *testing.T) { 299 api := &plugintest.API{} 300 api.On("GetServerVersion").Return("5.16.0") 301 api.On("KVCompareAndDelete", "test-key", []byte(`{"val-a":10}`)).Return(false, &model.AppError{}) 302 p := &plugin.HelpersImpl{API: api} 303 304 ok, err := p.KVCompareAndDeleteJSON("test-key", map[string]interface{}{ 305 "val-a": 10, 306 }) 307 308 api.AssertExpectations(t) 309 assert.False(t, ok) 310 assert.Error(t, err) 311 }) 312 313 t.Run("old value nil", func(t *testing.T) { 314 api := &plugintest.API{} 315 api.On("GetServerVersion").Return("5.16.0") 316 api.On("KVCompareAndDelete", "test-key", []byte(nil)).Return(true, nil) 317 p := &plugin.HelpersImpl{API: api} 318 319 ok, err := p.KVCompareAndDeleteJSON("test-key", nil) 320 321 api.AssertExpectations(t) 322 assert.True(t, ok) 323 assert.NoError(t, err) 324 }) 325 326 t.Run("old value non-nil", func(t *testing.T) { 327 api := &plugintest.API{} 328 api.On("GetServerVersion").Return("5.16.0") 329 api.On("KVCompareAndDelete", "test-key", []byte(`{"val-a":10}`)).Return(true, nil) 330 p := &plugin.HelpersImpl{API: api} 331 332 ok, err := p.KVCompareAndDeleteJSON("test-key", map[string]interface{}{ 333 "val-a": 10, 334 }) 335 336 api.AssertExpectations(t) 337 assert.True(t, ok) 338 assert.NoError(t, err) 339 }) 340 } 341 342 func TestKVSetWithExpiryJSON(t *testing.T) { 343 t.Run("incompatible server version", func(t *testing.T) { 344 api := &plugintest.API{} 345 api.On("GetServerVersion").Return("5.4.0") 346 347 p := &plugin.HelpersImpl{API: api} 348 349 err := p.KVSetWithExpiryJSON("test-key", map[string]interface{}{ 350 "val-a": float64(10), 351 }, 100) 352 353 api.AssertExpectations(t) 354 assert.Error(t, err) 355 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.6.0, current version: 5.4.0", err.Error()) 356 }) 357 358 t.Run("JSON marshal error", func(t *testing.T) { 359 api := &plugintest.API{} 360 api.On("GetServerVersion").Return("5.6.0") 361 api.AssertNotCalled(t, "KVSetWithExpiry") 362 363 p := &plugin.HelpersImpl{API: api} 364 365 err := p.KVSetWithExpiryJSON("test-key", func() {}, 100) 366 367 api.AssertExpectations(t) 368 assert.Error(t, err) 369 }) 370 371 t.Run("KVSetWithExpiry error", func(t *testing.T) { 372 api := &plugintest.API{} 373 api.On("GetServerVersion").Return("5.6.0") 374 api.On("KVSetWithExpiry", "test-key", []byte(`{"val-a":10}`), int64(100)).Return(&model.AppError{}) 375 p := &plugin.HelpersImpl{API: api} 376 377 err := p.KVSetWithExpiryJSON("test-key", map[string]interface{}{ 378 "val-a": float64(10), 379 }, 100) 380 381 api.AssertExpectations(t) 382 assert.Error(t, err) 383 }) 384 385 t.Run("wellformed JSON", func(t *testing.T) { 386 api := &plugintest.API{} 387 api.On("GetServerVersion").Return("5.6.0") 388 api.On("KVSetWithExpiry", "test-key", []byte(`{"val-a":10}`), int64(100)).Return(nil) 389 390 p := &plugin.HelpersImpl{API: api} 391 392 err := p.KVSetWithExpiryJSON("test-key", map[string]interface{}{ 393 "val-a": float64(10), 394 }, 100) 395 396 api.AssertExpectations(t) 397 assert.NoError(t, err) 398 }) 399 } 400 401 func TestKVListWithOptions(t *testing.T) { 402 t.Run("incompatible server version", func(t *testing.T) { 403 api := &plugintest.API{} 404 api.On("GetServerVersion").Return("5.1.0") 405 406 p := &plugin.HelpersImpl{API: api} 407 408 keys, err := p.KVListWithOptions() 409 410 api.AssertExpectations(t) 411 assert.Nil(t, keys) 412 assert.Error(t, err) 413 assert.Equal(t, "incompatible server version for plugin, minimum required version: 5.6.0, current version: 5.1.0", err.Error()) 414 }) 415 416 t.Run("KVList error", func(t *testing.T) { 417 p := &plugin.HelpersImpl{} 418 419 api := &plugintest.API{} 420 api.On("GetServerVersion").Return("5.6.0") 421 api.On("KVList", 0, 100).Return([]string{}, &model.AppError{}) 422 p.API = api 423 424 keys, err := p.KVListWithOptions() 425 api.AssertExpectations(t) 426 assert.Empty(t, keys) 427 assert.Error(t, err) 428 }) 429 430 t.Run("No keys", func(t *testing.T) { 431 p := &plugin.HelpersImpl{} 432 433 api := &plugintest.API{} 434 api.On("GetServerVersion").Return("5.6.0") 435 api.On("KVList", 0, 100).Return(nil, nil) 436 p.API = api 437 438 keys, err := p.KVListWithOptions() 439 api.AssertExpectations(t) 440 assert.Empty(t, keys) 441 assert.Nil(t, err) 442 }) 443 444 t.Run("Basic Success, one page", func(t *testing.T) { 445 p := &plugin.HelpersImpl{} 446 447 api := &plugintest.API{} 448 api.On("GetServerVersion").Return("5.6.0") 449 api.On("KVList", 0, 100).Return([]string{"key1", "key2"}, nil) 450 p.API = api 451 452 keys, err := p.KVListWithOptions() 453 api.AssertExpectations(t) 454 assert.ElementsMatch(t, keys, []string{"key1", "key2"}) 455 assert.Nil(t, err) 456 }) 457 458 t.Run("Basic Success, two page", func(t *testing.T) { 459 p := &plugin.HelpersImpl{} 460 461 api := &plugintest.API{} 462 api.On("GetServerVersion").Return("5.6.0") 463 api.On("KVList", 0, 100).Return(getKeys(100), nil) 464 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 465 p.API = api 466 467 keys, err := p.KVListWithOptions() 468 api.AssertExpectations(t) 469 assert.ElementsMatch(t, keys, getKeys(101)) 470 assert.Nil(t, err) 471 }) 472 473 t.Run("error on second page", func(t *testing.T) { 474 p := &plugin.HelpersImpl{} 475 476 api := &plugintest.API{} 477 api.On("GetServerVersion").Return("5.6.0") 478 api.On("KVList", 0, 100).Return(getKeys(100), nil) 479 api.On("KVList", 1, 100).Return([]string{"key100"}, &model.AppError{}) 480 p.API = api 481 482 keys, err := p.KVListWithOptions() 483 api.AssertExpectations(t) 484 assert.Empty(t, keys) 485 assert.Error(t, err) 486 }) 487 488 t.Run("success, two page, filter prefix, one", func(t *testing.T) { 489 p := &plugin.HelpersImpl{} 490 491 api := &plugintest.API{} 492 api.On("GetServerVersion").Return("5.6.0") 493 api.On("KVList", 0, 100).Return(getKeys(100), nil) 494 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 495 p.API = api 496 497 keys, err := p.KVListWithOptions(plugin.WithPrefix("key99")) 498 api.AssertExpectations(t) 499 assert.ElementsMatch(t, keys, []string{"key99"}) 500 assert.Nil(t, err) 501 }) 502 503 t.Run("success, two page, filter prefix, all", func(t *testing.T) { 504 p := &plugin.HelpersImpl{} 505 506 api := &plugintest.API{} 507 api.On("GetServerVersion").Return("5.6.0") 508 api.On("KVList", 0, 100).Return(getKeys(100), nil) 509 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 510 p.API = api 511 512 keys, err := p.KVListWithOptions(plugin.WithPrefix("notkey")) 513 api.AssertExpectations(t) 514 assert.Empty(t, keys) 515 assert.Nil(t, err) 516 }) 517 518 t.Run("success, two page, filter prefix, none", func(t *testing.T) { 519 p := &plugin.HelpersImpl{} 520 521 api := &plugintest.API{} 522 api.On("GetServerVersion").Return("5.6.0") 523 api.On("KVList", 0, 100).Return(getKeys(100), nil) 524 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 525 p.API = api 526 527 keys, err := p.KVListWithOptions(plugin.WithPrefix("key")) 528 api.AssertExpectations(t) 529 assert.ElementsMatch(t, keys, getKeys(101)) 530 assert.Nil(t, err) 531 }) 532 533 t.Run("success, two page, checker func, one", func(t *testing.T) { 534 p := &plugin.HelpersImpl{} 535 536 api := &plugintest.API{} 537 api.On("GetServerVersion").Return("5.6.0") 538 api.On("KVList", 0, 100).Return(getKeys(100), nil) 539 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 540 p.API = api 541 542 check := func(key string) (bool, error) { 543 if key == "key1" { 544 return true, nil 545 } 546 return false, nil 547 } 548 549 keys, err := p.KVListWithOptions(plugin.WithChecker(check)) 550 api.AssertExpectations(t) 551 assert.ElementsMatch(t, keys, []string{"key1"}) 552 assert.Nil(t, err) 553 }) 554 555 t.Run("success, two page, checker func, all", func(t *testing.T) { 556 p := &plugin.HelpersImpl{} 557 558 api := &plugintest.API{} 559 api.On("GetServerVersion").Return("5.6.0") 560 api.On("KVList", 0, 100).Return(getKeys(100), nil) 561 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 562 p.API = api 563 564 check := func(key string) (bool, error) { 565 return false, nil 566 } 567 568 keys, err := p.KVListWithOptions(plugin.WithChecker(check)) 569 api.AssertExpectations(t) 570 assert.Empty(t, keys) 571 assert.Nil(t, err) 572 }) 573 574 t.Run("success, two page, checker func, none", func(t *testing.T) { 575 p := &plugin.HelpersImpl{} 576 577 api := &plugintest.API{} 578 api.On("GetServerVersion").Return("5.6.0") 579 api.On("KVList", 0, 100).Return(getKeys(100), nil) 580 api.On("KVList", 1, 100).Return([]string{"key100"}, nil) 581 p.API = api 582 583 check := func(key string) (bool, error) { 584 return true, nil 585 } 586 587 keys, err := p.KVListWithOptions(plugin.WithChecker(check)) 588 api.AssertExpectations(t) 589 assert.ElementsMatch(t, keys, getKeys(101)) 590 assert.Nil(t, err) 591 }) 592 593 t.Run("error, checker func", func(t *testing.T) { 594 p := &plugin.HelpersImpl{} 595 596 api := &plugintest.API{} 597 api.On("GetServerVersion").Return("5.6.0") 598 api.On("KVList", 0, 100).Return([]string{"key1"}, nil) 599 p.API = api 600 601 check := func(key string) (bool, error) { 602 return true, &model.AppError{} 603 } 604 605 keys, err := p.KVListWithOptions(plugin.WithChecker(check)) 606 api.AssertExpectations(t) 607 assert.Empty(t, keys) 608 assert.Error(t, err) 609 }) 610 611 t.Run("success, filter and checker func, partial on both", func(t *testing.T) { 612 p := &plugin.HelpersImpl{} 613 614 api := &plugintest.API{} 615 api.On("GetServerVersion").Return("5.6.0") 616 api.On("KVList", 0, 100).Return([]string{"key1", "key2", "notkey3", "key4", "key5"}, nil) 617 p.API = api 618 619 check := func(key string) (bool, error) { 620 if key == "key1" || key == "key5" { 621 return false, nil 622 } 623 return true, nil 624 } 625 626 keys, err := p.KVListWithOptions(plugin.WithPrefix("key"), plugin.WithChecker(check)) 627 api.AssertExpectations(t) 628 assert.ElementsMatch(t, keys, []string{"key2", "key4"}) 629 assert.Nil(t, err) 630 }) 631 } 632 633 func getKeys(count int) []string { 634 ret := make([]string, count) 635 for i := 0; i < count; i++ { 636 ret[i] = "key" + strconv.Itoa(i) 637 } 638 return ret 639 }