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