github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/model/bot_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package model 5 6 import ( 7 "bytes" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestBotTrace(t *testing.T) { 16 bot := &Bot{ 17 UserId: NewId(), 18 Username: "username", 19 DisplayName: "display name", 20 Description: "description", 21 OwnerId: NewId(), 22 LastIconUpdate: 1, 23 CreateAt: 2, 24 UpdateAt: 3, 25 DeleteAt: 4, 26 } 27 28 require.Equal(t, map[string]interface{}{"user_id": bot.UserId}, bot.Trace()) 29 } 30 31 func TestBotClone(t *testing.T) { 32 bot := &Bot{ 33 UserId: NewId(), 34 Username: "username", 35 DisplayName: "display name", 36 Description: "description", 37 OwnerId: NewId(), 38 LastIconUpdate: 1, 39 CreateAt: 2, 40 UpdateAt: 3, 41 DeleteAt: 4, 42 } 43 44 clone := bot.Clone() 45 46 require.Equal(t, bot, bot.Clone()) 47 require.False(t, bot == clone) 48 } 49 50 func TestBotIsValid(t *testing.T) { 51 testCases := []struct { 52 Description string 53 Bot *Bot 54 ExpectedIsValid bool 55 }{ 56 { 57 "nil bot", 58 &Bot{}, 59 false, 60 }, 61 { 62 "bot with missing user id", 63 &Bot{ 64 UserId: "", 65 Username: "username", 66 DisplayName: "display name", 67 Description: "description", 68 OwnerId: NewId(), 69 LastIconUpdate: 1, 70 CreateAt: 2, 71 UpdateAt: 3, 72 DeleteAt: 4, 73 }, 74 false, 75 }, 76 { 77 "bot with invalid user id", 78 &Bot{ 79 UserId: "invalid", 80 Username: "username", 81 DisplayName: "display name", 82 Description: "description", 83 OwnerId: NewId(), 84 LastIconUpdate: 1, 85 CreateAt: 2, 86 UpdateAt: 3, 87 DeleteAt: 4, 88 }, 89 false, 90 }, 91 { 92 "bot with missing username", 93 &Bot{ 94 UserId: NewId(), 95 Username: "", 96 DisplayName: "display name", 97 Description: "description", 98 OwnerId: NewId(), 99 LastIconUpdate: 1, 100 CreateAt: 2, 101 UpdateAt: 3, 102 DeleteAt: 4, 103 }, 104 false, 105 }, 106 { 107 "bot with invalid username", 108 &Bot{ 109 UserId: NewId(), 110 Username: "a@", 111 DisplayName: "display name", 112 Description: "description", 113 OwnerId: NewId(), 114 LastIconUpdate: 1, 115 CreateAt: 2, 116 UpdateAt: 3, 117 DeleteAt: 4, 118 }, 119 false, 120 }, 121 { 122 "bot with long description", 123 &Bot{ 124 UserId: "", 125 Username: "username", 126 DisplayName: "display name", 127 Description: strings.Repeat("x", 1025), 128 OwnerId: NewId(), 129 LastIconUpdate: 1, 130 CreateAt: 2, 131 UpdateAt: 3, 132 DeleteAt: 4, 133 }, 134 false, 135 }, 136 { 137 "bot with missing creator id", 138 &Bot{ 139 UserId: NewId(), 140 Username: "username", 141 DisplayName: "display name", 142 Description: "description", 143 OwnerId: "", 144 LastIconUpdate: 1, 145 CreateAt: 2, 146 UpdateAt: 3, 147 DeleteAt: 4, 148 }, 149 false, 150 }, 151 { 152 "bot without create at timestamp", 153 &Bot{ 154 UserId: NewId(), 155 Username: "username", 156 DisplayName: "display name", 157 Description: "description", 158 OwnerId: NewId(), 159 LastIconUpdate: 1, 160 CreateAt: 0, 161 UpdateAt: 3, 162 DeleteAt: 4, 163 }, 164 false, 165 }, 166 { 167 "bot without update at timestamp", 168 &Bot{ 169 UserId: NewId(), 170 Username: "username", 171 DisplayName: "display name", 172 Description: "description", 173 OwnerId: NewId(), 174 LastIconUpdate: 1, 175 CreateAt: 2, 176 UpdateAt: 0, 177 DeleteAt: 4, 178 }, 179 false, 180 }, 181 { 182 "bot", 183 &Bot{ 184 UserId: NewId(), 185 Username: "username", 186 DisplayName: "display name", 187 Description: "description", 188 OwnerId: NewId(), 189 LastIconUpdate: 1, 190 CreateAt: 2, 191 UpdateAt: 3, 192 DeleteAt: 0, 193 }, 194 true, 195 }, 196 { 197 "bot without description", 198 &Bot{ 199 UserId: NewId(), 200 Username: "username", 201 DisplayName: "display name", 202 Description: "", 203 OwnerId: NewId(), 204 LastIconUpdate: 1, 205 CreateAt: 2, 206 UpdateAt: 3, 207 DeleteAt: 0, 208 }, 209 true, 210 }, 211 { 212 "deleted bot", 213 &Bot{ 214 UserId: NewId(), 215 Username: "username", 216 DisplayName: "display name", 217 Description: "a description", 218 OwnerId: NewId(), 219 LastIconUpdate: 1, 220 CreateAt: 2, 221 UpdateAt: 3, 222 DeleteAt: 4, 223 }, 224 true, 225 }, 226 } 227 228 for _, testCase := range testCases { 229 t.Run(testCase.Description, func(t *testing.T) { 230 if testCase.ExpectedIsValid { 231 require.Nil(t, testCase.Bot.IsValid()) 232 } else { 233 require.NotNil(t, testCase.Bot.IsValid()) 234 } 235 }) 236 } 237 } 238 239 func TestBotPreSave(t *testing.T) { 240 bot := &Bot{ 241 UserId: NewId(), 242 Username: "username", 243 DisplayName: "display name", 244 Description: "description", 245 OwnerId: NewId(), 246 LastIconUpdate: 0, 247 DeleteAt: 0, 248 } 249 250 originalBot := &*bot 251 252 bot.PreSave() 253 assert.NotEqual(t, 0, bot.CreateAt) 254 assert.NotEqual(t, 0, bot.UpdateAt) 255 256 originalBot.CreateAt = bot.CreateAt 257 originalBot.UpdateAt = bot.UpdateAt 258 assert.Equal(t, originalBot, bot) 259 } 260 261 func TestBotPreUpdate(t *testing.T) { 262 bot := &Bot{ 263 UserId: NewId(), 264 Username: "username", 265 DisplayName: "display name", 266 Description: "description", 267 OwnerId: NewId(), 268 LastIconUpdate: 1, 269 CreateAt: 2, 270 DeleteAt: 0, 271 } 272 273 originalBot := &*bot 274 275 bot.PreSave() 276 assert.NotEqual(t, 0, bot.UpdateAt) 277 278 originalBot.UpdateAt = bot.UpdateAt 279 assert.Equal(t, originalBot, bot) 280 } 281 282 func TestBotEtag(t *testing.T) { 283 t.Run("same etags", func(t *testing.T) { 284 bot1 := &Bot{ 285 UserId: NewId(), 286 Username: "username", 287 DisplayName: "display name", 288 Description: "description", 289 OwnerId: NewId(), 290 LastIconUpdate: 1, 291 CreateAt: 2, 292 UpdateAt: 3, 293 DeleteAt: 4, 294 } 295 bot2 := bot1 296 297 assert.Equal(t, bot1.Etag(), bot2.Etag()) 298 }) 299 t.Run("different etags", func(t *testing.T) { 300 t.Run("different user id", func(t *testing.T) { 301 bot1 := &Bot{ 302 UserId: NewId(), 303 Username: "username", 304 DisplayName: "display name", 305 Description: "description", 306 OwnerId: NewId(), 307 LastIconUpdate: 1, 308 CreateAt: 2, 309 UpdateAt: 3, 310 DeleteAt: 4, 311 } 312 bot2 := &Bot{ 313 UserId: NewId(), 314 Username: "username", 315 DisplayName: "display name", 316 Description: "description", 317 OwnerId: bot1.OwnerId, 318 LastIconUpdate: 1, 319 CreateAt: 2, 320 UpdateAt: 3, 321 DeleteAt: 4, 322 } 323 324 assert.NotEqual(t, bot1.Etag(), bot2.Etag()) 325 }) 326 t.Run("different update at", func(t *testing.T) { 327 bot1 := &Bot{ 328 UserId: NewId(), 329 Username: "username", 330 DisplayName: "display name", 331 Description: "description", 332 OwnerId: NewId(), 333 LastIconUpdate: 1, 334 CreateAt: 2, 335 UpdateAt: 3, 336 DeleteAt: 4, 337 } 338 bot2 := &Bot{ 339 UserId: bot1.UserId, 340 Username: "username", 341 DisplayName: "display name", 342 Description: "description", 343 OwnerId: bot1.OwnerId, 344 LastIconUpdate: 1, 345 CreateAt: 2, 346 UpdateAt: 10, 347 DeleteAt: 4, 348 } 349 350 assert.NotEqual(t, bot1.Etag(), bot2.Etag()) 351 }) 352 }) 353 } 354 355 func TestBotToAndFromJson(t *testing.T) { 356 bot1 := &Bot{ 357 UserId: NewId(), 358 Username: "username", 359 DisplayName: "display name", 360 Description: "description", 361 OwnerId: NewId(), 362 LastIconUpdate: 1, 363 CreateAt: 2, 364 UpdateAt: 3, 365 DeleteAt: 4, 366 } 367 368 bot2 := &Bot{ 369 UserId: NewId(), 370 Username: "username", 371 DisplayName: "display name", 372 Description: "description 2", 373 OwnerId: NewId(), 374 LastIconUpdate: 5, 375 CreateAt: 6, 376 UpdateAt: 7, 377 DeleteAt: 8, 378 } 379 380 assert.Equal(t, bot1, BotFromJson(bytes.NewReader(bot1.ToJson()))) 381 assert.Equal(t, bot2, BotFromJson(bytes.NewReader(bot2.ToJson()))) 382 } 383 384 func sToP(s string) *string { 385 return &s 386 } 387 388 func TestBotPatch(t *testing.T) { 389 userId1 := NewId() 390 creatorId1 := NewId() 391 392 testCases := []struct { 393 Description string 394 Bot *Bot 395 BotPatch *BotPatch 396 ExpectedBot *Bot 397 }{ 398 { 399 "no update", 400 &Bot{ 401 UserId: userId1, 402 Username: "username", 403 DisplayName: "display name", 404 Description: "description", 405 OwnerId: creatorId1, 406 LastIconUpdate: 1, 407 CreateAt: 2, 408 UpdateAt: 3, 409 DeleteAt: 4, 410 }, 411 &BotPatch{}, 412 &Bot{ 413 UserId: userId1, 414 Username: "username", 415 DisplayName: "display name", 416 Description: "description", 417 OwnerId: creatorId1, 418 LastIconUpdate: 1, 419 CreateAt: 2, 420 UpdateAt: 3, 421 DeleteAt: 4, 422 }, 423 }, 424 { 425 "partial update", 426 &Bot{ 427 UserId: userId1, 428 Username: "username", 429 DisplayName: "display name", 430 Description: "description", 431 OwnerId: creatorId1, 432 LastIconUpdate: 1, 433 CreateAt: 2, 434 UpdateAt: 3, 435 DeleteAt: 4, 436 }, 437 &BotPatch{ 438 Username: sToP("new_username"), 439 DisplayName: nil, 440 Description: sToP("new description"), 441 }, 442 &Bot{ 443 UserId: userId1, 444 Username: "new_username", 445 DisplayName: "display name", 446 Description: "new description", 447 OwnerId: creatorId1, 448 LastIconUpdate: 1, 449 CreateAt: 2, 450 UpdateAt: 3, 451 DeleteAt: 4, 452 }, 453 }, 454 { 455 "full update", 456 &Bot{ 457 UserId: userId1, 458 Username: "username", 459 DisplayName: "display name", 460 Description: "description", 461 OwnerId: creatorId1, 462 LastIconUpdate: 1, 463 CreateAt: 2, 464 UpdateAt: 3, 465 DeleteAt: 4, 466 }, 467 &BotPatch{ 468 Username: sToP("new_username"), 469 DisplayName: sToP("new display name"), 470 Description: sToP("new description"), 471 }, 472 &Bot{ 473 UserId: userId1, 474 Username: "new_username", 475 DisplayName: "new display name", 476 Description: "new description", 477 OwnerId: creatorId1, 478 LastIconUpdate: 1, 479 CreateAt: 2, 480 UpdateAt: 3, 481 DeleteAt: 4, 482 }, 483 }, 484 } 485 486 for _, testCase := range testCases { 487 t.Run(testCase.Description, func(t *testing.T) { 488 testCase.Bot.Patch(testCase.BotPatch) 489 assert.Equal(t, testCase.ExpectedBot, testCase.Bot) 490 }) 491 } 492 } 493 494 func TestBotPatchToAndFromJson(t *testing.T) { 495 botPatch1 := &BotPatch{ 496 Username: sToP("username"), 497 DisplayName: sToP("display name"), 498 Description: sToP("description"), 499 } 500 501 botPatch2 := &BotPatch{ 502 Username: sToP("username"), 503 DisplayName: sToP("display name"), 504 Description: sToP("description 2"), 505 } 506 507 assert.Equal(t, botPatch1, BotPatchFromJson(bytes.NewReader(botPatch1.ToJson()))) 508 assert.Equal(t, botPatch2, BotPatchFromJson(bytes.NewReader(botPatch2.ToJson()))) 509 } 510 511 func TestUserFromBot(t *testing.T) { 512 bot1 := &Bot{ 513 UserId: NewId(), 514 Username: "username", 515 DisplayName: "display name", 516 Description: "description", 517 OwnerId: NewId(), 518 LastIconUpdate: 1, 519 CreateAt: 2, 520 UpdateAt: 3, 521 DeleteAt: 4, 522 } 523 524 bot2 := &Bot{ 525 UserId: NewId(), 526 Username: "username2", 527 DisplayName: "display name 2", 528 Description: "description 2", 529 OwnerId: NewId(), 530 LastIconUpdate: 5, 531 CreateAt: 6, 532 UpdateAt: 7, 533 DeleteAt: 8, 534 } 535 536 assert.Equal(t, &User{ 537 Id: bot1.UserId, 538 Username: "username", 539 Email: "username@localhost", 540 FirstName: "display name", 541 Roles: "system_user", 542 }, UserFromBot(bot1)) 543 assert.Equal(t, &User{ 544 Id: bot2.UserId, 545 Username: "username2", 546 Email: "username2@localhost", 547 FirstName: "display name 2", 548 Roles: "system_user", 549 }, UserFromBot(bot2)) 550 } 551 552 func TestBotFromUser(t *testing.T) { 553 user := &User{ 554 Id: NewId(), 555 Username: "username", 556 CreateAt: 1, 557 UpdateAt: 2, 558 DeleteAt: 3, 559 } 560 561 assert.Equal(t, &Bot{ 562 OwnerId: user.Id, 563 UserId: user.Id, 564 Username: "username", 565 DisplayName: "username", 566 }, BotFromUser(user)) 567 } 568 569 func TestBotListToAndFromJson(t *testing.T) { 570 testCases := []struct { 571 Description string 572 BotList BotList 573 }{ 574 { 575 "empty list", 576 BotList{}, 577 }, 578 { 579 "single item", 580 BotList{ 581 &Bot{ 582 UserId: NewId(), 583 Username: "username", 584 DisplayName: "display name", 585 Description: "description", 586 OwnerId: NewId(), 587 LastIconUpdate: 1, 588 CreateAt: 2, 589 UpdateAt: 3, 590 DeleteAt: 4, 591 }, 592 }, 593 }, 594 { 595 "multiple items", 596 BotList{ 597 &Bot{ 598 UserId: NewId(), 599 Username: "username", 600 DisplayName: "display name", 601 Description: "description", 602 OwnerId: NewId(), 603 LastIconUpdate: 1, 604 CreateAt: 2, 605 UpdateAt: 3, 606 DeleteAt: 4, 607 }, 608 609 &Bot{ 610 UserId: NewId(), 611 Username: "username", 612 DisplayName: "display name", 613 Description: "description 2", 614 OwnerId: NewId(), 615 LastIconUpdate: 5, 616 CreateAt: 6, 617 UpdateAt: 7, 618 DeleteAt: 8, 619 }, 620 }, 621 }, 622 } 623 624 for _, testCase := range testCases { 625 t.Run(testCase.Description, func(t *testing.T) { 626 assert.Equal(t, testCase.BotList, BotListFromJson(bytes.NewReader(testCase.BotList.ToJson()))) 627 }) 628 } 629 } 630 631 func TestBotListEtag(t *testing.T) { 632 bot1 := &Bot{ 633 UserId: NewId(), 634 Username: "username", 635 DisplayName: "display name", 636 Description: "description", 637 OwnerId: NewId(), 638 LastIconUpdate: 1, 639 CreateAt: 2, 640 UpdateAt: 3, 641 DeleteAt: 4, 642 } 643 644 bot1Updated := &Bot{ 645 UserId: NewId(), 646 Username: "username", 647 DisplayName: "display name", 648 Description: "description", 649 OwnerId: NewId(), 650 LastIconUpdate: 1, 651 CreateAt: 2, 652 UpdateAt: 10, 653 DeleteAt: 4, 654 } 655 656 bot2 := &Bot{ 657 UserId: NewId(), 658 Username: "username", 659 DisplayName: "display name", 660 Description: "description", 661 OwnerId: NewId(), 662 LastIconUpdate: 5, 663 CreateAt: 6, 664 UpdateAt: 7, 665 DeleteAt: 8, 666 } 667 668 testCases := []struct { 669 Description string 670 BotListA BotList 671 BotListB BotList 672 ExpectedEqual bool 673 }{ 674 { 675 "empty lists", 676 BotList{}, 677 BotList{}, 678 true, 679 }, 680 { 681 "single item, same list", 682 BotList{bot1}, 683 BotList{bot1}, 684 true, 685 }, 686 { 687 "single item, different update at", 688 BotList{bot1}, 689 BotList{bot1Updated}, 690 false, 691 }, 692 { 693 "single item vs. multiple items", 694 BotList{bot1}, 695 BotList{bot1, bot2}, 696 false, 697 }, 698 { 699 "multiple items, different update at", 700 BotList{bot1, bot2}, 701 BotList{bot1Updated, bot2}, 702 false, 703 }, 704 { 705 "multiple items, same list", 706 BotList{bot1, bot2}, 707 BotList{bot1, bot2}, 708 true, 709 }, 710 } 711 712 for _, testCase := range testCases { 713 t.Run(testCase.Description, func(t *testing.T) { 714 if testCase.ExpectedEqual { 715 assert.Equal(t, testCase.BotListA.Etag(), testCase.BotListB.Etag()) 716 } else { 717 assert.NotEqual(t, testCase.BotListA.Etag(), testCase.BotListB.Etag()) 718 } 719 }) 720 } 721 } 722 723 func TestIsBotChannel(t *testing.T) { 724 for _, test := range []struct { 725 Name string 726 Channel *Channel 727 Expected bool 728 }{ 729 { 730 Name: "not a direct channel", 731 Channel: &Channel{Type: CHANNEL_OPEN}, 732 Expected: false, 733 }, 734 { 735 Name: "a direct channel with another user", 736 Channel: &Channel{ 737 Name: "user1__user2", 738 Type: CHANNEL_DIRECT, 739 }, 740 Expected: false, 741 }, 742 { 743 Name: "a direct channel with the name containing the bot's ID first", 744 Channel: &Channel{ 745 Name: "botUserID__user2", 746 Type: CHANNEL_DIRECT, 747 }, 748 Expected: true, 749 }, 750 { 751 Name: "a direct channel with the name containing the bot's ID second", 752 Channel: &Channel{ 753 Name: "user1__botUserID", 754 Type: CHANNEL_DIRECT, 755 }, 756 Expected: true, 757 }, 758 } { 759 t.Run(test.Name, func(t *testing.T) { 760 assert.Equal(t, test.Expected, IsBotDMChannel(test.Channel, "botUserID")) 761 }) 762 } 763 }