github.com/vnforks/kid@v5.11.1+incompatible/app/bot_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/model"
    15  )
    16  
    17  func TestCreateBot(t *testing.T) {
    18  	t.Run("invalid bot", func(t *testing.T) {
    19  		t.Run("relative to user", func(t *testing.T) {
    20  			th := Setup(t).InitBasic()
    21  			defer th.TearDown()
    22  
    23  			_, err := th.App.CreateBot(&model.Bot{
    24  				Username:    "invalid username",
    25  				Description: "a bot",
    26  				OwnerId:     th.BasicUser.Id,
    27  			})
    28  			require.NotNil(t, err)
    29  			require.Equal(t, "model.user.is_valid.username.app_error", err.Id)
    30  		})
    31  
    32  		t.Run("relative to bot", func(t *testing.T) {
    33  			th := Setup(t).InitBasic()
    34  			defer th.TearDown()
    35  
    36  			_, err := th.App.CreateBot(&model.Bot{
    37  				Username:    "username",
    38  				Description: strings.Repeat("x", 1025),
    39  				OwnerId:     th.BasicUser.Id,
    40  			})
    41  			require.NotNil(t, err)
    42  			require.Equal(t, "model.bot.is_valid.description.app_error", err.Id)
    43  		})
    44  	})
    45  
    46  	t.Run("create bot", func(t *testing.T) {
    47  		th := Setup(t).InitBasic()
    48  		defer th.TearDown()
    49  
    50  		bot, err := th.App.CreateBot(&model.Bot{
    51  			Username:    "username",
    52  			Description: "a bot",
    53  			OwnerId:     th.BasicUser.Id,
    54  		})
    55  		require.Nil(t, err)
    56  		defer th.App.PermanentDeleteBot(bot.UserId)
    57  		assert.Equal(t, "username", bot.Username)
    58  		assert.Equal(t, "a bot", bot.Description)
    59  		assert.Equal(t, th.BasicUser.Id, bot.OwnerId)
    60  	})
    61  
    62  	t.Run("create bot, username already used by a non-bot user", func(t *testing.T) {
    63  		th := Setup(t).InitBasic()
    64  		defer th.TearDown()
    65  
    66  		_, err := th.App.CreateBot(&model.Bot{
    67  			Username:    th.BasicUser.Username,
    68  			Description: "a bot",
    69  			OwnerId:     th.BasicUser.Id,
    70  		})
    71  		require.NotNil(t, err)
    72  		require.Equal(t, "store.sql_user.save.username_exists.app_error", err.Id)
    73  	})
    74  }
    75  
    76  func TestPatchBot(t *testing.T) {
    77  	t.Run("invalid patch for user", func(t *testing.T) {
    78  		th := Setup(t).InitBasic()
    79  		defer th.TearDown()
    80  
    81  		bot, err := th.App.CreateBot(&model.Bot{
    82  			Username:    "username",
    83  			Description: "a bot",
    84  			OwnerId:     th.BasicUser.Id,
    85  		})
    86  		require.Nil(t, err)
    87  		defer th.App.PermanentDeleteBot(bot.UserId)
    88  
    89  		botPatch := &model.BotPatch{
    90  			Username:    sToP("invalid username"),
    91  			DisplayName: sToP("an updated bot"),
    92  			Description: sToP("updated bot"),
    93  		}
    94  
    95  		_, err = th.App.PatchBot(bot.UserId, botPatch)
    96  		require.NotNil(t, err)
    97  		require.Equal(t, "model.user.is_valid.username.app_error", err.Id)
    98  	})
    99  
   100  	t.Run("invalid patch for bot", func(t *testing.T) {
   101  		th := Setup(t).InitBasic()
   102  		defer th.TearDown()
   103  
   104  		bot, err := th.App.CreateBot(&model.Bot{
   105  			Username:    "username",
   106  			Description: "a bot",
   107  			OwnerId:     th.BasicUser.Id,
   108  		})
   109  		require.Nil(t, err)
   110  		defer th.App.PermanentDeleteBot(bot.UserId)
   111  
   112  		botPatch := &model.BotPatch{
   113  			Username:    sToP("username"),
   114  			DisplayName: sToP("display name"),
   115  			Description: sToP(strings.Repeat("x", 1025)),
   116  		}
   117  
   118  		_, err = th.App.PatchBot(bot.UserId, botPatch)
   119  		require.NotNil(t, err)
   120  		require.Equal(t, "model.bot.is_valid.description.app_error", err.Id)
   121  	})
   122  
   123  	t.Run("patch bot", func(t *testing.T) {
   124  		th := Setup(t).InitBasic()
   125  		defer th.TearDown()
   126  
   127  		bot := &model.Bot{
   128  			Username:    "username",
   129  			DisplayName: "bot",
   130  			Description: "a bot",
   131  			OwnerId:     th.BasicUser.Id,
   132  		}
   133  
   134  		createdBot, err := th.App.CreateBot(bot)
   135  		require.Nil(t, err)
   136  		defer th.App.PermanentDeleteBot(createdBot.UserId)
   137  
   138  		botPatch := &model.BotPatch{
   139  			Username:    sToP("username2"),
   140  			DisplayName: sToP("updated bot"),
   141  			Description: sToP("an updated bot"),
   142  		}
   143  
   144  		patchedBot, err := th.App.PatchBot(createdBot.UserId, botPatch)
   145  		require.Nil(t, err)
   146  
   147  		createdBot.Username = "username2"
   148  		createdBot.DisplayName = "updated bot"
   149  		createdBot.Description = "an updated bot"
   150  		createdBot.UpdateAt = patchedBot.UpdateAt
   151  		require.Equal(t, createdBot, patchedBot)
   152  	})
   153  
   154  	t.Run("patch bot, username already used by a non-bot user", func(t *testing.T) {
   155  		th := Setup(t).InitBasic()
   156  		defer th.TearDown()
   157  
   158  		bot, err := th.App.CreateBot(&model.Bot{
   159  			Username:    "username",
   160  			DisplayName: "bot",
   161  			Description: "a bot",
   162  			OwnerId:     th.BasicUser.Id,
   163  		})
   164  		require.Nil(t, err)
   165  		defer th.App.PermanentDeleteBot(bot.UserId)
   166  
   167  		botPatch := &model.BotPatch{
   168  			Username: sToP(th.BasicUser2.Username),
   169  		}
   170  
   171  		_, err = th.App.PatchBot(bot.UserId, botPatch)
   172  		require.NotNil(t, err)
   173  		require.Equal(t, "store.sql_user.update.username_taken.app_error", err.Id)
   174  	})
   175  }
   176  
   177  func TestGetBot(t *testing.T) {
   178  	th := Setup(t).InitBasic()
   179  	defer th.TearDown()
   180  
   181  	bot1, err := th.App.CreateBot(&model.Bot{
   182  		Username:    "username",
   183  		Description: "a bot",
   184  		OwnerId:     th.BasicUser.Id,
   185  	})
   186  	require.Nil(t, err)
   187  	defer th.App.PermanentDeleteBot(bot1.UserId)
   188  
   189  	bot2, err := th.App.CreateBot(&model.Bot{
   190  		Username:    "username2",
   191  		Description: "a second bot",
   192  		OwnerId:     th.BasicUser.Id,
   193  	})
   194  	require.Nil(t, err)
   195  	defer th.App.PermanentDeleteBot(bot2.UserId)
   196  
   197  	deletedBot, err := th.App.CreateBot(&model.Bot{
   198  		Username:    "username3",
   199  		Description: "a deleted bot",
   200  		OwnerId:     th.BasicUser.Id,
   201  	})
   202  	require.Nil(t, err)
   203  	deletedBot, err = th.App.UpdateBotActive(deletedBot.UserId, false)
   204  	require.Nil(t, err)
   205  	defer th.App.PermanentDeleteBot(deletedBot.UserId)
   206  
   207  	t.Run("get unknown bot", func(t *testing.T) {
   208  		_, err := th.App.GetBot(model.NewId(), false)
   209  		require.NotNil(t, err)
   210  		require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id)
   211  	})
   212  
   213  	t.Run("get bot1", func(t *testing.T) {
   214  		bot, err := th.App.GetBot(bot1.UserId, false)
   215  		require.Nil(t, err)
   216  		assert.Equal(t, bot1, bot)
   217  	})
   218  
   219  	t.Run("get bot2", func(t *testing.T) {
   220  		bot, err := th.App.GetBot(bot2.UserId, false)
   221  		require.Nil(t, err)
   222  		assert.Equal(t, bot2, bot)
   223  	})
   224  
   225  	t.Run("get deleted bot", func(t *testing.T) {
   226  		_, err := th.App.GetBot(deletedBot.UserId, false)
   227  		require.NotNil(t, err)
   228  		require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id)
   229  	})
   230  
   231  	t.Run("get deleted bot, include deleted", func(t *testing.T) {
   232  		bot, err := th.App.GetBot(deletedBot.UserId, true)
   233  		require.Nil(t, err)
   234  		assert.Equal(t, deletedBot, bot)
   235  	})
   236  }
   237  
   238  func TestGetBots(t *testing.T) {
   239  	th := Setup(t).InitBasic()
   240  	defer th.TearDown()
   241  
   242  	OwnerId1 := model.NewId()
   243  	OwnerId2 := model.NewId()
   244  
   245  	bot1, err := th.App.CreateBot(&model.Bot{
   246  		Username:    "username",
   247  		Description: "a bot",
   248  		OwnerId:     OwnerId1,
   249  	})
   250  	require.Nil(t, err)
   251  	defer th.App.PermanentDeleteBot(bot1.UserId)
   252  
   253  	deletedBot1, err := th.App.CreateBot(&model.Bot{
   254  		Username:    "username4",
   255  		Description: "a deleted bot",
   256  		OwnerId:     OwnerId1,
   257  	})
   258  	require.Nil(t, err)
   259  	deletedBot1, err = th.App.UpdateBotActive(deletedBot1.UserId, false)
   260  	require.Nil(t, err)
   261  	defer th.App.PermanentDeleteBot(deletedBot1.UserId)
   262  
   263  	bot2, err := th.App.CreateBot(&model.Bot{
   264  		Username:    "username2",
   265  		Description: "a second bot",
   266  		OwnerId:     OwnerId1,
   267  	})
   268  	require.Nil(t, err)
   269  	defer th.App.PermanentDeleteBot(bot2.UserId)
   270  
   271  	bot3, err := th.App.CreateBot(&model.Bot{
   272  		Username:    "username3",
   273  		Description: "a third bot",
   274  		OwnerId:     OwnerId1,
   275  	})
   276  	require.Nil(t, err)
   277  	defer th.App.PermanentDeleteBot(bot3.UserId)
   278  
   279  	bot4, err := th.App.CreateBot(&model.Bot{
   280  		Username:    "username5",
   281  		Description: "a fourth bot",
   282  		OwnerId:     OwnerId2,
   283  	})
   284  	require.Nil(t, err)
   285  	defer th.App.PermanentDeleteBot(bot4.UserId)
   286  
   287  	deletedBot2, err := th.App.CreateBot(&model.Bot{
   288  		Username:    "username6",
   289  		Description: "a deleted bot",
   290  		OwnerId:     OwnerId2,
   291  	})
   292  	require.Nil(t, err)
   293  	deletedBot2, err = th.App.UpdateBotActive(deletedBot2.UserId, false)
   294  	require.Nil(t, err)
   295  	defer th.App.PermanentDeleteBot(deletedBot2.UserId)
   296  
   297  	t.Run("get bots, page=0, perPage=10", func(t *testing.T) {
   298  		bots, err := th.App.GetBots(&model.BotGetOptions{
   299  			Page:           0,
   300  			PerPage:        10,
   301  			OwnerId:        "",
   302  			IncludeDeleted: false,
   303  		})
   304  		require.Nil(t, err)
   305  		assert.Equal(t, model.BotList{bot1, bot2, bot3, bot4}, bots)
   306  	})
   307  
   308  	t.Run("get bots, page=0, perPage=1", func(t *testing.T) {
   309  		bots, err := th.App.GetBots(&model.BotGetOptions{
   310  			Page:           0,
   311  			PerPage:        1,
   312  			OwnerId:        "",
   313  			IncludeDeleted: false,
   314  		})
   315  		require.Nil(t, err)
   316  		assert.Equal(t, model.BotList{bot1}, bots)
   317  	})
   318  
   319  	t.Run("get bots, page=1, perPage=2", func(t *testing.T) {
   320  		bots, err := th.App.GetBots(&model.BotGetOptions{
   321  			Page:           1,
   322  			PerPage:        2,
   323  			OwnerId:        "",
   324  			IncludeDeleted: false,
   325  		})
   326  		require.Nil(t, err)
   327  		assert.Equal(t, model.BotList{bot3, bot4}, bots)
   328  	})
   329  
   330  	t.Run("get bots, page=2, perPage=2", func(t *testing.T) {
   331  		bots, err := th.App.GetBots(&model.BotGetOptions{
   332  			Page:           2,
   333  			PerPage:        2,
   334  			OwnerId:        "",
   335  			IncludeDeleted: false,
   336  		})
   337  		require.Nil(t, err)
   338  		assert.Equal(t, model.BotList{}, bots)
   339  	})
   340  
   341  	t.Run("get bots, page=0, perPage=10, include deleted", func(t *testing.T) {
   342  		bots, err := th.App.GetBots(&model.BotGetOptions{
   343  			Page:           0,
   344  			PerPage:        10,
   345  			OwnerId:        "",
   346  			IncludeDeleted: true,
   347  		})
   348  		require.Nil(t, err)
   349  		assert.Equal(t, model.BotList{bot1, deletedBot1, bot2, bot3, bot4, deletedBot2}, bots)
   350  	})
   351  
   352  	t.Run("get bots, page=0, perPage=1, include deleted", func(t *testing.T) {
   353  		bots, err := th.App.GetBots(&model.BotGetOptions{
   354  			Page:           0,
   355  			PerPage:        1,
   356  			OwnerId:        "",
   357  			IncludeDeleted: true,
   358  		})
   359  		require.Nil(t, err)
   360  		assert.Equal(t, model.BotList{bot1}, bots)
   361  	})
   362  
   363  	t.Run("get bots, page=1, perPage=2, include deleted", func(t *testing.T) {
   364  		bots, err := th.App.GetBots(&model.BotGetOptions{
   365  			Page:           1,
   366  			PerPage:        2,
   367  			OwnerId:        "",
   368  			IncludeDeleted: true,
   369  		})
   370  		require.Nil(t, err)
   371  		assert.Equal(t, model.BotList{bot2, bot3}, bots)
   372  	})
   373  
   374  	t.Run("get bots, page=2, perPage=2, include deleted", func(t *testing.T) {
   375  		bots, err := th.App.GetBots(&model.BotGetOptions{
   376  			Page:           2,
   377  			PerPage:        2,
   378  			OwnerId:        "",
   379  			IncludeDeleted: true,
   380  		})
   381  		require.Nil(t, err)
   382  		assert.Equal(t, model.BotList{bot4, deletedBot2}, bots)
   383  	})
   384  
   385  	t.Run("get offset=0, limit=10, creator id 1", func(t *testing.T) {
   386  		bots, err := th.App.GetBots(&model.BotGetOptions{
   387  			Page:           0,
   388  			PerPage:        10,
   389  			OwnerId:        OwnerId1,
   390  			IncludeDeleted: false,
   391  		})
   392  		require.Nil(t, err)
   393  		require.Equal(t, model.BotList{bot1, bot2, bot3}, bots)
   394  	})
   395  
   396  	t.Run("get offset=0, limit=10, creator id 2", func(t *testing.T) {
   397  		bots, err := th.App.GetBots(&model.BotGetOptions{
   398  			Page:           0,
   399  			PerPage:        10,
   400  			OwnerId:        OwnerId2,
   401  			IncludeDeleted: false,
   402  		})
   403  		require.Nil(t, err)
   404  		require.Equal(t, model.BotList{bot4}, bots)
   405  	})
   406  
   407  	t.Run("get offset=0, limit=10, include deleted, creator id 1", func(t *testing.T) {
   408  		bots, err := th.App.GetBots(&model.BotGetOptions{
   409  			Page:           0,
   410  			PerPage:        10,
   411  			OwnerId:        OwnerId1,
   412  			IncludeDeleted: true,
   413  		})
   414  		require.Nil(t, err)
   415  		require.Equal(t, model.BotList{bot1, deletedBot1, bot2, bot3}, bots)
   416  	})
   417  
   418  	t.Run("get offset=0, limit=10, include deleted, creator id 2", func(t *testing.T) {
   419  		bots, err := th.App.GetBots(&model.BotGetOptions{
   420  			Page:           0,
   421  			PerPage:        10,
   422  			OwnerId:        OwnerId2,
   423  			IncludeDeleted: true,
   424  		})
   425  		require.Nil(t, err)
   426  		require.Equal(t, model.BotList{bot4, deletedBot2}, bots)
   427  	})
   428  }
   429  
   430  func TestUpdateBotActive(t *testing.T) {
   431  	t.Run("unknown bot", func(t *testing.T) {
   432  		th := Setup(t).InitBasic()
   433  		defer th.TearDown()
   434  
   435  		_, err := th.App.UpdateBotActive(model.NewId(), false)
   436  		require.NotNil(t, err)
   437  		require.Equal(t, "store.sql_user.missing_account.const", err.Id)
   438  	})
   439  
   440  	t.Run("disable/enable bot", func(t *testing.T) {
   441  		th := Setup(t).InitBasic()
   442  		defer th.TearDown()
   443  
   444  		bot, err := th.App.CreateBot(&model.Bot{
   445  			Username:    "username",
   446  			Description: "a bot",
   447  			OwnerId:     th.BasicUser.Id,
   448  		})
   449  		require.Nil(t, err)
   450  		defer th.App.PermanentDeleteBot(bot.UserId)
   451  
   452  		disabledBot, err := th.App.UpdateBotActive(bot.UserId, false)
   453  		require.Nil(t, err)
   454  		require.NotEqual(t, 0, disabledBot.DeleteAt)
   455  
   456  		// Disabling should be idempotent
   457  		disabledBotAgain, err := th.App.UpdateBotActive(bot.UserId, false)
   458  		require.Nil(t, err)
   459  		require.Equal(t, disabledBot.DeleteAt, disabledBotAgain.DeleteAt)
   460  
   461  		reenabledBot, err := th.App.UpdateBotActive(bot.UserId, true)
   462  		require.Nil(t, err)
   463  		require.EqualValues(t, 0, reenabledBot.DeleteAt)
   464  
   465  		// Re-enabling should be idempotent
   466  		reenabledBotAgain, err := th.App.UpdateBotActive(bot.UserId, true)
   467  		require.Nil(t, err)
   468  		require.Equal(t, reenabledBot.DeleteAt, reenabledBotAgain.DeleteAt)
   469  	})
   470  }
   471  
   472  func TestPermanentDeleteBot(t *testing.T) {
   473  	th := Setup(t).InitBasic()
   474  	defer th.TearDown()
   475  
   476  	bot, err := th.App.CreateBot(&model.Bot{
   477  		Username:    "username",
   478  		Description: "a bot",
   479  		OwnerId:     th.BasicUser.Id,
   480  	})
   481  	require.Nil(t, err)
   482  
   483  	require.Nil(t, th.App.PermanentDeleteBot(bot.UserId))
   484  
   485  	_, err = th.App.GetBot(bot.UserId, false)
   486  	require.NotNil(t, err)
   487  	require.Equal(t, "store.sql_bot.get.missing.app_error", err.Id)
   488  }
   489  
   490  func TestDisableUserBots(t *testing.T) {
   491  	th := Setup(t).InitBasic()
   492  	defer th.TearDown()
   493  
   494  	ownerId1 := model.NewId()
   495  	ownerId2 := model.NewId()
   496  
   497  	bots := []*model.Bot{}
   498  	defer func() {
   499  		for _, bot := range bots {
   500  			th.App.PermanentDeleteBot(bot.UserId)
   501  		}
   502  	}()
   503  
   504  	for i := 0; i < 46; i++ {
   505  		bot, err := th.App.CreateBot(&model.Bot{
   506  			Username:    fmt.Sprintf("username%v", i),
   507  			Description: "a bot",
   508  			OwnerId:     ownerId1,
   509  		})
   510  		require.Nil(t, err)
   511  		bots = append(bots, bot)
   512  	}
   513  	require.Len(t, bots, 46)
   514  
   515  	u2bot1, err := th.App.CreateBot(&model.Bot{
   516  		Username:    "username_nodisable",
   517  		Description: "a bot",
   518  		OwnerId:     ownerId2,
   519  	})
   520  	require.Nil(t, err)
   521  	defer th.App.PermanentDeleteBot(u2bot1.UserId)
   522  
   523  	err = th.App.disableUserBots(ownerId1)
   524  	require.Nil(t, err)
   525  
   526  	// Check all bots and corrensponding users are disabled for creator 1
   527  	for _, bot := range bots {
   528  		retbot, err2 := th.App.GetBot(bot.UserId, true)
   529  		require.Nil(t, err2)
   530  		require.NotZero(t, retbot.DeleteAt, bot.Username)
   531  	}
   532  
   533  	// Check bots and corresponding user not disabled for creator 2
   534  	bot, err := th.App.GetBot(u2bot1.UserId, true)
   535  	require.Nil(t, err)
   536  	require.Zero(t, bot.DeleteAt)
   537  
   538  	user, err := th.App.GetUser(u2bot1.UserId)
   539  	require.Nil(t, err)
   540  	require.Zero(t, user.DeleteAt)
   541  
   542  	// Bad id doesn't do anything or break horribly
   543  	err = th.App.disableUserBots(model.NewId())
   544  	require.Nil(t, err)
   545  }
   546  
   547  func sToP(s string) *string {
   548  	return &s
   549  }