github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/webhook_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/store"
    15  )
    16  
    17  func TestWebhookStore(t *testing.T, ss store.Store) {
    18  	t.Run("SaveIncoming", func(t *testing.T) { testWebhookStoreSaveIncoming(t, ss) })
    19  	t.Run("UpdateIncoming", func(t *testing.T) { testWebhookStoreUpdateIncoming(t, ss) })
    20  	t.Run("GetIncoming", func(t *testing.T) { testWebhookStoreGetIncoming(t, ss) })
    21  	t.Run("GetIncomingList", func(t *testing.T) { testWebhookStoreGetIncomingList(t, ss) })
    22  	t.Run("GetIncomingListByUser", func(t *testing.T) { testWebhookStoreGetIncomingListByUser(t, ss) })
    23  	t.Run("GetIncomingByTeam", func(t *testing.T) { testWebhookStoreGetIncomingByTeam(t, ss) })
    24  	t.Run("GetIncomingByTeamByUser", func(t *testing.T) { TestWebhookStoreGetIncomingByTeamByUser(t, ss) })
    25  	t.Run("GetIncomingByTeamByChannel", func(t *testing.T) { testWebhookStoreGetIncomingByChannel(t, ss) })
    26  	t.Run("DeleteIncoming", func(t *testing.T) { testWebhookStoreDeleteIncoming(t, ss) })
    27  	t.Run("DeleteIncomingByChannel", func(t *testing.T) { testWebhookStoreDeleteIncomingByChannel(t, ss) })
    28  	t.Run("DeleteIncomingByUser", func(t *testing.T) { testWebhookStoreDeleteIncomingByUser(t, ss) })
    29  	t.Run("SaveOutgoing", func(t *testing.T) { testWebhookStoreSaveOutgoing(t, ss) })
    30  	t.Run("GetOutgoing", func(t *testing.T) { testWebhookStoreGetOutgoing(t, ss) })
    31  	t.Run("GetOutgoingList", func(t *testing.T) { testWebhookStoreGetOutgoingList(t, ss) })
    32  	t.Run("GetOutgoingListByUser", func(t *testing.T) { testWebhookStoreGetOutgoingListByUser(t, ss) })
    33  	t.Run("GetOutgoingByChannel", func(t *testing.T) { testWebhookStoreGetOutgoingByChannel(t, ss) })
    34  	t.Run("GetOutgoingByChannelByUser", func(t *testing.T) { testWebhookStoreGetOutgoingByChannelByUser(t, ss) })
    35  	t.Run("GetOutgoingByTeam", func(t *testing.T) { testWebhookStoreGetOutgoingByTeam(t, ss) })
    36  	t.Run("GetOutgoingByTeamByUser", func(t *testing.T) { testWebhookStoreGetOutgoingByTeamByUser(t, ss) })
    37  	t.Run("DeleteOutgoing", func(t *testing.T) { testWebhookStoreDeleteOutgoing(t, ss) })
    38  	t.Run("DeleteOutgoingByChannel", func(t *testing.T) { testWebhookStoreDeleteOutgoingByChannel(t, ss) })
    39  	t.Run("DeleteOutgoingByUser", func(t *testing.T) { testWebhookStoreDeleteOutgoingByUser(t, ss) })
    40  	t.Run("UpdateOutgoing", func(t *testing.T) { testWebhookStoreUpdateOutgoing(t, ss) })
    41  	t.Run("CountIncoming", func(t *testing.T) { testWebhookStoreCountIncoming(t, ss) })
    42  	t.Run("CountOutgoing", func(t *testing.T) { testWebhookStoreCountOutgoing(t, ss) })
    43  }
    44  
    45  func testWebhookStoreSaveIncoming(t *testing.T, ss store.Store) {
    46  	o1 := buildIncomingWebhook()
    47  
    48  	_, err := ss.Webhook().SaveIncoming(o1)
    49  	require.NoError(t, err, "couldn't save item")
    50  
    51  	_, err = ss.Webhook().SaveIncoming(o1)
    52  	require.Error(t, err, "shouldn't be able to update from save")
    53  }
    54  
    55  func testWebhookStoreUpdateIncoming(t *testing.T, ss store.Store) {
    56  
    57  	var err error
    58  
    59  	o1 := buildIncomingWebhook()
    60  	o1, err = ss.Webhook().SaveIncoming(o1)
    61  	require.NoError(t, err, "unable to save webhook")
    62  
    63  	previousUpdatedAt := o1.UpdateAt
    64  
    65  	o1.DisplayName = "TestHook"
    66  	time.Sleep(10 * time.Millisecond)
    67  
    68  	webhook, err := ss.Webhook().UpdateIncoming(o1)
    69  	require.NoError(t, err)
    70  
    71  	require.NotEqual(t, webhook.UpdateAt, previousUpdatedAt, "should have updated the UpdatedAt of the hook")
    72  
    73  	require.Equal(t, "TestHook", webhook.DisplayName, "display name is not updated")
    74  }
    75  
    76  func testWebhookStoreGetIncoming(t *testing.T, ss store.Store) {
    77  	var err error
    78  
    79  	o1 := buildIncomingWebhook()
    80  	o1, err = ss.Webhook().SaveIncoming(o1)
    81  	require.NoError(t, err, "unable to save webhook")
    82  
    83  	webhook, err := ss.Webhook().GetIncoming(o1.Id, false)
    84  	require.NoError(t, err)
    85  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
    86  
    87  	webhook, err = ss.Webhook().GetIncoming(o1.Id, true)
    88  	require.NoError(t, err)
    89  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
    90  
    91  	_, err = ss.Webhook().GetIncoming("123", false)
    92  	require.Error(t, err, "Missing id should have failed")
    93  
    94  	_, err = ss.Webhook().GetIncoming("123", true)
    95  	require.Error(t, err, "Missing id should have failed")
    96  
    97  	_, err = ss.Webhook().GetIncoming("123", true)
    98  	require.Error(t, err)
    99  	var nfErr *store.ErrNotFound
   100  	require.True(t, errors.As(err, &nfErr), "Should have set the status as not found for missing id")
   101  }
   102  
   103  func testWebhookStoreGetIncomingList(t *testing.T, ss store.Store) {
   104  	o1 := &model.IncomingWebhook{}
   105  	o1.ChannelId = model.NewId()
   106  	o1.UserId = model.NewId()
   107  	o1.TeamId = model.NewId()
   108  
   109  	var err error
   110  	o1, err = ss.Webhook().SaveIncoming(o1)
   111  	require.NoError(t, err, "unable to save webhook")
   112  
   113  	hooks, err := ss.Webhook().GetIncomingList(0, 1000)
   114  	require.NoError(t, err)
   115  
   116  	found := false
   117  	for _, hook := range hooks {
   118  		if hook.Id == o1.Id {
   119  			found = true
   120  		}
   121  	}
   122  	require.True(t, found, "missing webhook")
   123  
   124  	hooks, err = ss.Webhook().GetIncomingList(0, 1)
   125  	require.NoError(t, err)
   126  	require.Len(t, hooks, 1, "only 1 should be returned")
   127  }
   128  
   129  func testWebhookStoreGetIncomingListByUser(t *testing.T, ss store.Store) {
   130  	o1 := &model.IncomingWebhook{}
   131  	o1.ChannelId = model.NewId()
   132  	o1.UserId = model.NewId()
   133  	o1.TeamId = model.NewId()
   134  
   135  	o1, err := ss.Webhook().SaveIncoming(o1)
   136  	require.NoError(t, err)
   137  
   138  	t.Run("GetIncomingListByUser, known user filtered", func(t *testing.T) {
   139  		hooks, err := ss.Webhook().GetIncomingListByUser(o1.UserId, 0, 100)
   140  		require.NoError(t, err)
   141  		require.Equal(t, 1, len(hooks))
   142  		require.Equal(t, o1.CreateAt, hooks[0].CreateAt)
   143  	})
   144  
   145  	t.Run("GetIncomingListByUser, unknown user filtered", func(t *testing.T) {
   146  		hooks, err := ss.Webhook().GetIncomingListByUser("123465", 0, 100)
   147  		require.NoError(t, err)
   148  		require.Equal(t, 0, len(hooks))
   149  	})
   150  }
   151  
   152  func testWebhookStoreGetIncomingByTeam(t *testing.T, ss store.Store) {
   153  	var err error
   154  
   155  	o1 := buildIncomingWebhook()
   156  	o1, err = ss.Webhook().SaveIncoming(o1)
   157  	require.NoError(t, err)
   158  
   159  	hooks, err := ss.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100)
   160  	require.NoError(t, err)
   161  	require.Equal(t, hooks[0].CreateAt, o1.CreateAt, "invalid returned webhook")
   162  
   163  	hooks, err = ss.Webhook().GetIncomingByTeam("123", 0, 100)
   164  	require.NoError(t, err)
   165  	require.Empty(t, hooks, "no webhooks should have returned")
   166  }
   167  
   168  func TestWebhookStoreGetIncomingByTeamByUser(t *testing.T, ss store.Store) {
   169  	var err error
   170  
   171  	o1 := buildIncomingWebhook()
   172  	o1, err = ss.Webhook().SaveIncoming(o1)
   173  	require.NoError(t, err)
   174  
   175  	o2 := buildIncomingWebhook()
   176  	o2.TeamId = o1.TeamId //Set both to the same team
   177  	o2, err = ss.Webhook().SaveIncoming(o2)
   178  	require.NoError(t, err)
   179  
   180  	t.Run("GetIncomingByTeamByUser, no user filter", func(t *testing.T) {
   181  		hooks, err := ss.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100)
   182  		require.NoError(t, err)
   183  		require.Equal(t, len(hooks), 2)
   184  	})
   185  
   186  	t.Run("GetIncomingByTeamByUser, known user filtered", func(t *testing.T) {
   187  		hooks, err := ss.Webhook().GetIncomingByTeamByUser(o1.TeamId, o1.UserId, 0, 100)
   188  		require.NoError(t, err)
   189  		require.Equal(t, len(hooks), 1)
   190  		require.Equal(t, hooks[0].CreateAt, o1.CreateAt)
   191  	})
   192  
   193  	t.Run("GetIncomingByTeamByUser, unknown user filtered", func(t *testing.T) {
   194  		hooks, err := ss.Webhook().GetIncomingByTeamByUser(o2.TeamId, "123465", 0, 100)
   195  		require.NoError(t, err)
   196  		require.Equal(t, len(hooks), 0)
   197  	})
   198  }
   199  
   200  func testWebhookStoreGetIncomingByChannel(t *testing.T, ss store.Store) {
   201  	o1 := buildIncomingWebhook()
   202  
   203  	o1, err := ss.Webhook().SaveIncoming(o1)
   204  	require.NoError(t, err, "unable to save webhook")
   205  
   206  	webhooks, err := ss.Webhook().GetIncomingByChannel(o1.ChannelId)
   207  	require.NoError(t, err)
   208  	require.Equal(t, webhooks[0].CreateAt, o1.CreateAt, "invalid returned webhook")
   209  
   210  	webhooks, err = ss.Webhook().GetIncomingByChannel("123")
   211  	require.NoError(t, err)
   212  	require.Empty(t, webhooks, "no webhooks should have returned")
   213  }
   214  
   215  func testWebhookStoreDeleteIncoming(t *testing.T, ss store.Store) {
   216  	var err error
   217  
   218  	o1 := buildIncomingWebhook()
   219  	o1, err = ss.Webhook().SaveIncoming(o1)
   220  	require.NoError(t, err, "unable to save webhook")
   221  
   222  	webhook, err := ss.Webhook().GetIncoming(o1.Id, true)
   223  	require.NoError(t, err)
   224  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   225  
   226  	err = ss.Webhook().DeleteIncoming(o1.Id, model.GetMillis())
   227  	require.NoError(t, err)
   228  
   229  	webhook, err = ss.Webhook().GetIncoming(o1.Id, true)
   230  	require.Error(t, err)
   231  }
   232  
   233  func testWebhookStoreDeleteIncomingByChannel(t *testing.T, ss store.Store) {
   234  	var err error
   235  
   236  	o1 := buildIncomingWebhook()
   237  	o1, err = ss.Webhook().SaveIncoming(o1)
   238  	require.NoError(t, err, "unable to save webhook")
   239  
   240  	webhook, err := ss.Webhook().GetIncoming(o1.Id, true)
   241  	require.NoError(t, err)
   242  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   243  
   244  	err = ss.Webhook().PermanentDeleteIncomingByChannel(o1.ChannelId)
   245  	require.NoError(t, err)
   246  
   247  	_, err = ss.Webhook().GetIncoming(o1.Id, true)
   248  	require.Error(t, err, "Missing id should have failed")
   249  }
   250  
   251  func testWebhookStoreDeleteIncomingByUser(t *testing.T, ss store.Store) {
   252  	var err error
   253  
   254  	o1 := buildIncomingWebhook()
   255  	o1, err = ss.Webhook().SaveIncoming(o1)
   256  	require.NoError(t, err, "unable to save webhook")
   257  
   258  	webhook, err := ss.Webhook().GetIncoming(o1.Id, true)
   259  	require.NoError(t, err)
   260  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   261  
   262  	err = ss.Webhook().PermanentDeleteIncomingByUser(o1.UserId)
   263  	require.NoError(t, err)
   264  
   265  	_, err = ss.Webhook().GetIncoming(o1.Id, true)
   266  	require.Error(t, err, "Missing id should have failed")
   267  }
   268  
   269  func buildIncomingWebhook() *model.IncomingWebhook {
   270  	o1 := &model.IncomingWebhook{}
   271  	o1.ChannelId = model.NewId()
   272  	o1.UserId = model.NewId()
   273  	o1.TeamId = model.NewId()
   274  
   275  	return o1
   276  }
   277  
   278  func testWebhookStoreSaveOutgoing(t *testing.T, ss store.Store) {
   279  	o1 := model.OutgoingWebhook{}
   280  	o1.ChannelId = model.NewId()
   281  	o1.CreatorId = model.NewId()
   282  	o1.TeamId = model.NewId()
   283  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   284  	o1.Username = "test-user-name"
   285  	o1.IconURL = "http://nowhere.com/icon"
   286  
   287  	_, err := ss.Webhook().SaveOutgoing(&o1)
   288  	require.NoError(t, err, "couldn't save item")
   289  
   290  	_, err = ss.Webhook().SaveOutgoing(&o1)
   291  	require.Error(t, err, "shouldn't be able to update from save")
   292  }
   293  
   294  func testWebhookStoreGetOutgoing(t *testing.T, ss store.Store) {
   295  	o1 := &model.OutgoingWebhook{}
   296  	o1.ChannelId = model.NewId()
   297  	o1.CreatorId = model.NewId()
   298  	o1.TeamId = model.NewId()
   299  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   300  	o1.Username = "test-user-name"
   301  	o1.IconURL = "http://nowhere.com/icon"
   302  
   303  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   304  
   305  	webhook, err := ss.Webhook().GetOutgoing(o1.Id)
   306  	require.NoError(t, err)
   307  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   308  
   309  	_, err = ss.Webhook().GetOutgoing("123")
   310  	require.Error(t, err, "Missing id should have failed")
   311  }
   312  
   313  func testWebhookStoreGetOutgoingListByUser(t *testing.T, ss store.Store) {
   314  	o1 := &model.OutgoingWebhook{}
   315  	o1.ChannelId = model.NewId()
   316  	o1.CreatorId = model.NewId()
   317  	o1.TeamId = model.NewId()
   318  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   319  
   320  	o1, err := ss.Webhook().SaveOutgoing(o1)
   321  	require.NoError(t, err)
   322  
   323  	t.Run("GetOutgoingListByUser, known user filtered", func(t *testing.T) {
   324  		hooks, err := ss.Webhook().GetOutgoingListByUser(o1.CreatorId, 0, 100)
   325  		require.NoError(t, err)
   326  		require.Equal(t, 1, len(hooks))
   327  		require.Equal(t, o1.CreateAt, hooks[0].CreateAt)
   328  	})
   329  
   330  	t.Run("GetOutgoingListByUser, unknown user filtered", func(t *testing.T) {
   331  		hooks, err := ss.Webhook().GetOutgoingListByUser("123465", 0, 100)
   332  		require.NoError(t, err)
   333  		require.Equal(t, 0, len(hooks))
   334  	})
   335  }
   336  
   337  func testWebhookStoreGetOutgoingList(t *testing.T, ss store.Store) {
   338  	o1 := &model.OutgoingWebhook{}
   339  	o1.ChannelId = model.NewId()
   340  	o1.CreatorId = model.NewId()
   341  	o1.TeamId = model.NewId()
   342  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   343  
   344  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   345  
   346  	o2 := &model.OutgoingWebhook{}
   347  	o2.ChannelId = model.NewId()
   348  	o2.CreatorId = model.NewId()
   349  	o2.TeamId = model.NewId()
   350  	o2.CallbackURLs = []string{"http://nowhere.com/"}
   351  
   352  	o2, _ = ss.Webhook().SaveOutgoing(o2)
   353  
   354  	r1, err := ss.Webhook().GetOutgoingList(0, 1000)
   355  	require.NoError(t, err)
   356  	hooks := r1
   357  	found1 := false
   358  	found2 := false
   359  
   360  	for _, hook := range hooks {
   361  		if hook.CreateAt != o1.CreateAt {
   362  			found1 = true
   363  		}
   364  
   365  		if hook.CreateAt != o2.CreateAt {
   366  			found2 = true
   367  		}
   368  	}
   369  
   370  	require.True(t, found1, "missing hook1")
   371  	require.True(t, found2, "missing hook2")
   372  
   373  	result, err := ss.Webhook().GetOutgoingList(0, 2)
   374  	require.NoError(t, err)
   375  	require.Len(t, result, 2, "wrong number of hooks returned")
   376  }
   377  
   378  func testWebhookStoreGetOutgoingByChannel(t *testing.T, ss store.Store) {
   379  	o1 := &model.OutgoingWebhook{}
   380  	o1.ChannelId = model.NewId()
   381  	o1.CreatorId = model.NewId()
   382  	o1.TeamId = model.NewId()
   383  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   384  
   385  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   386  
   387  	r1, err := ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100)
   388  	require.NoError(t, err)
   389  	require.Equal(t, r1[0].CreateAt, o1.CreateAt, "invalid returned webhook")
   390  
   391  	result, err := ss.Webhook().GetOutgoingByChannel("123", -1, -1)
   392  	require.NoError(t, err)
   393  	require.Empty(t, result, "no webhooks should have returned")
   394  }
   395  
   396  func testWebhookStoreGetOutgoingByChannelByUser(t *testing.T, ss store.Store) {
   397  	o1 := &model.OutgoingWebhook{}
   398  	o1.ChannelId = model.NewId()
   399  	o1.CreatorId = model.NewId()
   400  	o1.TeamId = model.NewId()
   401  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   402  
   403  	o1, err := ss.Webhook().SaveOutgoing(o1)
   404  	require.NoError(t, err)
   405  
   406  	o2 := &model.OutgoingWebhook{}
   407  	o2.ChannelId = o1.ChannelId
   408  	o2.CreatorId = model.NewId()
   409  	o2.TeamId = model.NewId()
   410  	o2.CallbackURLs = []string{"http://nowhere.com/"}
   411  
   412  	o2, err = ss.Webhook().SaveOutgoing(o2)
   413  	require.NoError(t, err)
   414  
   415  	t.Run("GetOutgoingByChannelByUser, no user filter", func(t *testing.T) {
   416  		hooks, err := ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100)
   417  		require.NoError(t, err)
   418  		require.Equal(t, len(hooks), 2)
   419  	})
   420  
   421  	t.Run("GetOutgoingByChannelByUser, known user filtered", func(t *testing.T) {
   422  		hooks, err := ss.Webhook().GetOutgoingByChannelByUser(o1.ChannelId, o1.CreatorId, 0, 100)
   423  		require.NoError(t, err)
   424  		require.Equal(t, 1, len(hooks))
   425  		require.Equal(t, o1.CreateAt, hooks[0].CreateAt)
   426  	})
   427  
   428  	t.Run("GetOutgoingByChannelByUser, unknown user filtered", func(t *testing.T) {
   429  		hooks, err := ss.Webhook().GetOutgoingByChannelByUser(o1.ChannelId, "123465", 0, 100)
   430  		require.NoError(t, err)
   431  		require.Equal(t, 0, len(hooks))
   432  	})
   433  }
   434  
   435  func testWebhookStoreGetOutgoingByTeam(t *testing.T, ss store.Store) {
   436  	o1 := &model.OutgoingWebhook{}
   437  	o1.ChannelId = model.NewId()
   438  	o1.CreatorId = model.NewId()
   439  	o1.TeamId = model.NewId()
   440  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   441  
   442  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   443  
   444  	r1, err := ss.Webhook().GetOutgoingByTeam(o1.TeamId, 0, 100)
   445  	require.NoError(t, err)
   446  	require.Equal(t, r1[0].CreateAt, o1.CreateAt, "invalid returned webhook")
   447  
   448  	result, err := ss.Webhook().GetOutgoingByTeam("123", -1, -1)
   449  	require.NoError(t, err)
   450  	require.Empty(t, result, "no webhooks should have returned")
   451  }
   452  
   453  func testWebhookStoreGetOutgoingByTeamByUser(t *testing.T, ss store.Store) {
   454  	var err error
   455  
   456  	o1 := &model.OutgoingWebhook{}
   457  	o1.ChannelId = model.NewId()
   458  	o1.CreatorId = model.NewId()
   459  	o1.TeamId = model.NewId()
   460  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   461  
   462  	o1, err = ss.Webhook().SaveOutgoing(o1)
   463  	require.NoError(t, err)
   464  
   465  	o2 := &model.OutgoingWebhook{}
   466  	o2.ChannelId = model.NewId()
   467  	o2.CreatorId = model.NewId()
   468  	o2.TeamId = o1.TeamId
   469  	o2.CallbackURLs = []string{"http://nowhere.com/"}
   470  
   471  	o2, err = ss.Webhook().SaveOutgoing(o2)
   472  	require.NoError(t, err)
   473  
   474  	t.Run("GetOutgoingByTeamByUser, no user filter", func(t *testing.T) {
   475  		hooks, err := ss.Webhook().GetOutgoingByTeam(o1.TeamId, 0, 100)
   476  		require.NoError(t, err)
   477  		require.Equal(t, len(hooks), 2)
   478  	})
   479  
   480  	t.Run("GetOutgoingByTeamByUser, known user filtered", func(t *testing.T) {
   481  		hooks, err := ss.Webhook().GetOutgoingByTeamByUser(o1.TeamId, o1.CreatorId, 0, 100)
   482  		require.NoError(t, err)
   483  		require.Equal(t, len(hooks), 1)
   484  		require.Equal(t, hooks[0].CreateAt, o1.CreateAt)
   485  	})
   486  
   487  	t.Run("GetOutgoingByTeamByUser, unknown user filtered", func(t *testing.T) {
   488  		hooks, err := ss.Webhook().GetOutgoingByTeamByUser(o2.TeamId, "123465", 0, 100)
   489  		require.NoError(t, err)
   490  		require.Equal(t, len(hooks), 0)
   491  	})
   492  }
   493  
   494  func testWebhookStoreDeleteOutgoing(t *testing.T, ss store.Store) {
   495  	o1 := &model.OutgoingWebhook{}
   496  	o1.ChannelId = model.NewId()
   497  	o1.CreatorId = model.NewId()
   498  	o1.TeamId = model.NewId()
   499  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   500  
   501  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   502  
   503  	webhook, err := ss.Webhook().GetOutgoing(o1.Id)
   504  	require.NoError(t, err)
   505  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   506  
   507  	err = ss.Webhook().DeleteOutgoing(o1.Id, model.GetMillis())
   508  	require.NoError(t, err)
   509  
   510  	_, err = ss.Webhook().GetOutgoing(o1.Id)
   511  	require.Error(t, err, "Missing id should have failed")
   512  }
   513  
   514  func testWebhookStoreDeleteOutgoingByChannel(t *testing.T, ss store.Store) {
   515  	o1 := &model.OutgoingWebhook{}
   516  	o1.ChannelId = model.NewId()
   517  	o1.CreatorId = model.NewId()
   518  	o1.TeamId = model.NewId()
   519  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   520  
   521  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   522  
   523  	webhook, err := ss.Webhook().GetOutgoing(o1.Id)
   524  	require.NoError(t, err)
   525  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   526  
   527  	err = ss.Webhook().PermanentDeleteOutgoingByChannel(o1.ChannelId)
   528  	require.NoError(t, err)
   529  
   530  	_, err = ss.Webhook().GetOutgoing(o1.Id)
   531  	require.Error(t, err, "Missing id should have failed")
   532  }
   533  
   534  func testWebhookStoreDeleteOutgoingByUser(t *testing.T, ss store.Store) {
   535  	o1 := &model.OutgoingWebhook{}
   536  	o1.ChannelId = model.NewId()
   537  	o1.CreatorId = model.NewId()
   538  	o1.TeamId = model.NewId()
   539  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   540  
   541  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   542  
   543  	webhook, err := ss.Webhook().GetOutgoing(o1.Id)
   544  	require.NoError(t, err)
   545  	require.Equal(t, webhook.CreateAt, o1.CreateAt, "invalid returned webhook")
   546  
   547  	err = ss.Webhook().PermanentDeleteOutgoingByUser(o1.CreatorId)
   548  	require.NoError(t, err)
   549  
   550  	_, err = ss.Webhook().GetOutgoing(o1.Id)
   551  	require.Error(t, err, "Missing id should have failed")
   552  }
   553  
   554  func testWebhookStoreUpdateOutgoing(t *testing.T, ss store.Store) {
   555  	o1 := &model.OutgoingWebhook{}
   556  	o1.ChannelId = model.NewId()
   557  	o1.CreatorId = model.NewId()
   558  	o1.TeamId = model.NewId()
   559  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   560  	o1.Username = "test-user-name"
   561  	o1.IconURL = "http://nowhere.com/icon"
   562  
   563  	o1, _ = ss.Webhook().SaveOutgoing(o1)
   564  
   565  	o1.Token = model.NewId()
   566  	o1.Username = "another-test-user-name"
   567  
   568  	_, err := ss.Webhook().UpdateOutgoing(o1)
   569  	require.NoError(t, err)
   570  }
   571  
   572  func testWebhookStoreCountIncoming(t *testing.T, ss store.Store) {
   573  	o1 := &model.IncomingWebhook{}
   574  	o1.ChannelId = model.NewId()
   575  	o1.UserId = model.NewId()
   576  	o1.TeamId = model.NewId()
   577  
   578  	_, _ = ss.Webhook().SaveIncoming(o1)
   579  
   580  	c, err := ss.Webhook().AnalyticsIncomingCount("")
   581  	require.NoError(t, err)
   582  
   583  	require.NotEqual(t, 0, c, "should have at least 1 incoming hook")
   584  }
   585  
   586  func testWebhookStoreCountOutgoing(t *testing.T, ss store.Store) {
   587  	o1 := &model.OutgoingWebhook{}
   588  	o1.ChannelId = model.NewId()
   589  	o1.CreatorId = model.NewId()
   590  	o1.TeamId = model.NewId()
   591  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   592  
   593  	_, err := ss.Webhook().SaveOutgoing(o1)
   594  	require.NoError(t, err)
   595  
   596  	r, err := ss.Webhook().AnalyticsOutgoingCount("")
   597  	require.NoError(t, err)
   598  	require.NotEqual(t, 0, r, "should have at least 1 outgoing hook")
   599  }