github.com/mad-app/mattermost-server@v5.11.1+incompatible/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  	"testing"
     8  	"time"
     9  
    10  	"net/http"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/store"
    14  )
    15  
    16  func TestWebhookStore(t *testing.T, ss store.Store) {
    17  	t.Run("SaveIncoming", func(t *testing.T) { testWebhookStoreSaveIncoming(t, ss) })
    18  	t.Run("UpdateIncoming", func(t *testing.T) { testWebhookStoreUpdateIncoming(t, ss) })
    19  	t.Run("GetIncoming", func(t *testing.T) { testWebhookStoreGetIncoming(t, ss) })
    20  	t.Run("GetIncomingList", func(t *testing.T) { testWebhookStoreGetIncomingList(t, ss) })
    21  	t.Run("GetIncomingByTeam", func(t *testing.T) { testWebhookStoreGetIncomingByTeam(t, ss) })
    22  	t.Run("DeleteIncoming", func(t *testing.T) { testWebhookStoreDeleteIncoming(t, ss) })
    23  	t.Run("DeleteIncomingByChannel", func(t *testing.T) { testWebhookStoreDeleteIncomingByChannel(t, ss) })
    24  	t.Run("DeleteIncomingByUser", func(t *testing.T) { testWebhookStoreDeleteIncomingByUser(t, ss) })
    25  	t.Run("SaveOutgoing", func(t *testing.T) { testWebhookStoreSaveOutgoing(t, ss) })
    26  	t.Run("GetOutgoing", func(t *testing.T) { testWebhookStoreGetOutgoing(t, ss) })
    27  	t.Run("GetOutgoingList", func(t *testing.T) { testWebhookStoreGetOutgoingList(t, ss) })
    28  	t.Run("GetOutgoingByChannel", func(t *testing.T) { testWebhookStoreGetOutgoingByChannel(t, ss) })
    29  	t.Run("GetOutgoingByTeam", func(t *testing.T) { testWebhookStoreGetOutgoingByTeam(t, ss) })
    30  	t.Run("DeleteOutgoing", func(t *testing.T) { testWebhookStoreDeleteOutgoing(t, ss) })
    31  	t.Run("DeleteOutgoingByChannel", func(t *testing.T) { testWebhookStoreDeleteOutgoingByChannel(t, ss) })
    32  	t.Run("DeleteOutgoingByUser", func(t *testing.T) { testWebhookStoreDeleteOutgoingByUser(t, ss) })
    33  	t.Run("UpdateOutgoing", func(t *testing.T) { testWebhookStoreUpdateOutgoing(t, ss) })
    34  	t.Run("CountIncoming", func(t *testing.T) { testWebhookStoreCountIncoming(t, ss) })
    35  	t.Run("CountOutgoing", func(t *testing.T) { testWebhookStoreCountOutgoing(t, ss) })
    36  }
    37  
    38  func testWebhookStoreSaveIncoming(t *testing.T, ss store.Store) {
    39  	o1 := buildIncomingWebhook()
    40  
    41  	if err := (<-ss.Webhook().SaveIncoming(o1)).Err; err != nil {
    42  		t.Fatal("couldn't save item", err)
    43  	}
    44  
    45  	if err := (<-ss.Webhook().SaveIncoming(o1)).Err; err == nil {
    46  		t.Fatal("shouldn't be able to update from save")
    47  	}
    48  }
    49  
    50  func testWebhookStoreUpdateIncoming(t *testing.T, ss store.Store) {
    51  	o1 := buildIncomingWebhook()
    52  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
    53  	previousUpdatedAt := o1.UpdateAt
    54  
    55  	o1.DisplayName = "TestHook"
    56  	time.Sleep(10 * time.Millisecond)
    57  
    58  	if result := (<-ss.Webhook().UpdateIncoming(o1)); result.Err != nil {
    59  		t.Fatal("updation of incoming hook failed", result.Err)
    60  	} else {
    61  		if result.Data.(*model.IncomingWebhook).UpdateAt == previousUpdatedAt {
    62  			t.Fatal("should have updated the UpdatedAt of the hook")
    63  		}
    64  
    65  		if result.Data.(*model.IncomingWebhook).DisplayName != "TestHook" {
    66  			t.Fatal("display name is not updated")
    67  		}
    68  	}
    69  }
    70  
    71  func testWebhookStoreGetIncoming(t *testing.T, ss store.Store) {
    72  	o1 := buildIncomingWebhook()
    73  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
    74  
    75  	if r1 := <-ss.Webhook().GetIncoming(o1.Id, false); r1.Err != nil {
    76  		t.Fatal(r1.Err)
    77  	} else {
    78  		if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt {
    79  			t.Fatal("invalid returned webhook")
    80  		}
    81  	}
    82  
    83  	if r1 := <-ss.Webhook().GetIncoming(o1.Id, true); r1.Err != nil {
    84  		t.Fatal(r1.Err)
    85  	} else {
    86  		if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt {
    87  			t.Fatal("invalid returned webhook")
    88  		}
    89  	}
    90  
    91  	if err := (<-ss.Webhook().GetIncoming("123", false)).Err; err == nil {
    92  		t.Fatal("Missing id should have failed")
    93  	}
    94  
    95  	if err := (<-ss.Webhook().GetIncoming("123", true)).Err; err == nil {
    96  		t.Fatal("Missing id should have failed")
    97  	}
    98  
    99  	if err := (<-ss.Webhook().GetIncoming("123", true)).Err; err.StatusCode != http.StatusNotFound {
   100  		t.Fatal("Should have set the status as not found for missing id")
   101  	}
   102  }
   103  
   104  func testWebhookStoreGetIncomingList(t *testing.T, ss store.Store) {
   105  	o1 := &model.IncomingWebhook{}
   106  	o1.ChannelId = model.NewId()
   107  	o1.UserId = model.NewId()
   108  	o1.TeamId = model.NewId()
   109  
   110  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   111  
   112  	if r1 := <-ss.Webhook().GetIncomingList(0, 1000); r1.Err != nil {
   113  		t.Fatal(r1.Err)
   114  	} else {
   115  		found := false
   116  		hooks := r1.Data.([]*model.IncomingWebhook)
   117  		for _, hook := range hooks {
   118  			if hook.Id == o1.Id {
   119  				found = true
   120  			}
   121  		}
   122  		if !found {
   123  			t.Fatal("missing webhook")
   124  		}
   125  	}
   126  
   127  	if result := <-ss.Webhook().GetIncomingList(0, 1); result.Err != nil {
   128  		t.Fatal(result.Err)
   129  	} else {
   130  		if len(result.Data.([]*model.IncomingWebhook)) != 1 {
   131  			t.Fatal("only 1 should be returned")
   132  		}
   133  	}
   134  }
   135  
   136  func testWebhookStoreGetIncomingByTeam(t *testing.T, ss store.Store) {
   137  	o1 := buildIncomingWebhook()
   138  
   139  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   140  
   141  	if r1 := <-ss.Webhook().GetIncomingByTeam(o1.TeamId, 0, 100); r1.Err != nil {
   142  		t.Fatal(r1.Err)
   143  	} else {
   144  		if r1.Data.([]*model.IncomingWebhook)[0].CreateAt != o1.CreateAt {
   145  			t.Fatal("invalid returned webhook")
   146  		}
   147  	}
   148  
   149  	if result := <-ss.Webhook().GetIncomingByTeam("123", 0, 100); result.Err != nil {
   150  		t.Fatal(result.Err)
   151  	} else {
   152  		if len(result.Data.([]*model.IncomingWebhook)) != 0 {
   153  			t.Fatal("no webhooks should have returned")
   154  		}
   155  	}
   156  }
   157  
   158  func testWebhookStoreDeleteIncoming(t *testing.T, ss store.Store) {
   159  	o1 := buildIncomingWebhook()
   160  
   161  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   162  
   163  	if r1 := <-ss.Webhook().GetIncoming(o1.Id, true); r1.Err != nil {
   164  		t.Fatal(r1.Err)
   165  	} else {
   166  		if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt {
   167  			t.Fatal("invalid returned webhook")
   168  		}
   169  	}
   170  
   171  	if r2 := <-ss.Webhook().DeleteIncoming(o1.Id, model.GetMillis()); r2.Err != nil {
   172  		t.Fatal(r2.Err)
   173  	}
   174  
   175  	if r3 := (<-ss.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil {
   176  		t.Log(r3.Data)
   177  		t.Fatal("Missing id should have failed")
   178  	}
   179  }
   180  
   181  func testWebhookStoreDeleteIncomingByChannel(t *testing.T, ss store.Store) {
   182  	o1 := buildIncomingWebhook()
   183  
   184  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   185  
   186  	if r1 := <-ss.Webhook().GetIncoming(o1.Id, true); r1.Err != nil {
   187  		t.Fatal(r1.Err)
   188  	} else {
   189  		if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt {
   190  			t.Fatal("invalid returned webhook")
   191  		}
   192  	}
   193  
   194  	if r2 := <-ss.Webhook().PermanentDeleteIncomingByChannel(o1.ChannelId); r2.Err != nil {
   195  		t.Fatal(r2.Err)
   196  	}
   197  
   198  	if r3 := (<-ss.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil {
   199  		t.Log(r3.Data)
   200  		t.Fatal("Missing id should have failed")
   201  	}
   202  }
   203  
   204  func testWebhookStoreDeleteIncomingByUser(t *testing.T, ss store.Store) {
   205  	o1 := buildIncomingWebhook()
   206  
   207  	o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   208  
   209  	if r1 := <-ss.Webhook().GetIncoming(o1.Id, true); r1.Err != nil {
   210  		t.Fatal(r1.Err)
   211  	} else {
   212  		if r1.Data.(*model.IncomingWebhook).CreateAt != o1.CreateAt {
   213  			t.Fatal("invalid returned webhook")
   214  		}
   215  	}
   216  
   217  	if r2 := <-ss.Webhook().PermanentDeleteIncomingByUser(o1.UserId); r2.Err != nil {
   218  		t.Fatal(r2.Err)
   219  	}
   220  
   221  	if r3 := (<-ss.Webhook().GetIncoming(o1.Id, true)); r3.Err == nil {
   222  		t.Log(r3.Data)
   223  		t.Fatal("Missing id should have failed")
   224  	}
   225  }
   226  
   227  func buildIncomingWebhook() *model.IncomingWebhook {
   228  	o1 := &model.IncomingWebhook{}
   229  	o1.ChannelId = model.NewId()
   230  	o1.UserId = model.NewId()
   231  	o1.TeamId = model.NewId()
   232  
   233  	return o1
   234  }
   235  
   236  func testWebhookStoreSaveOutgoing(t *testing.T, ss store.Store) {
   237  	o1 := model.OutgoingWebhook{}
   238  	o1.ChannelId = model.NewId()
   239  	o1.CreatorId = model.NewId()
   240  	o1.TeamId = model.NewId()
   241  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   242  	o1.Username = "test-user-name"
   243  	o1.IconURL = "http://nowhere.com/icon"
   244  
   245  	if err := (<-ss.Webhook().SaveOutgoing(&o1)).Err; err != nil {
   246  		t.Fatal("couldn't save item", err)
   247  	}
   248  
   249  	if err := (<-ss.Webhook().SaveOutgoing(&o1)).Err; err == nil {
   250  		t.Fatal("shouldn't be able to update from save")
   251  	}
   252  }
   253  
   254  func testWebhookStoreGetOutgoing(t *testing.T, ss store.Store) {
   255  	o1 := &model.OutgoingWebhook{}
   256  	o1.ChannelId = model.NewId()
   257  	o1.CreatorId = model.NewId()
   258  	o1.TeamId = model.NewId()
   259  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   260  	o1.Username = "test-user-name"
   261  	o1.IconURL = "http://nowhere.com/icon"
   262  
   263  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   264  
   265  	if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil {
   266  		t.Fatal(r1.Err)
   267  	} else {
   268  		if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt {
   269  			t.Fatal("invalid returned webhook")
   270  		}
   271  	}
   272  
   273  	if err := (<-ss.Webhook().GetOutgoing("123")).Err; err == nil {
   274  		t.Fatal("Missing id should have failed")
   275  	}
   276  }
   277  
   278  func testWebhookStoreGetOutgoingList(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  
   285  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   286  
   287  	o2 := &model.OutgoingWebhook{}
   288  	o2.ChannelId = model.NewId()
   289  	o2.CreatorId = model.NewId()
   290  	o2.TeamId = model.NewId()
   291  	o2.CallbackURLs = []string{"http://nowhere.com/"}
   292  
   293  	o2 = (<-ss.Webhook().SaveOutgoing(o2)).Data.(*model.OutgoingWebhook)
   294  
   295  	if r1 := <-ss.Webhook().GetOutgoingList(0, 1000); r1.Err != nil {
   296  		t.Fatal(r1.Err)
   297  	} else {
   298  		hooks := r1.Data.([]*model.OutgoingWebhook)
   299  		found1 := false
   300  		found2 := false
   301  
   302  		for _, hook := range hooks {
   303  			if hook.CreateAt != o1.CreateAt {
   304  				found1 = true
   305  			}
   306  
   307  			if hook.CreateAt != o2.CreateAt {
   308  				found2 = true
   309  			}
   310  		}
   311  
   312  		if !found1 {
   313  			t.Fatal("missing hook1")
   314  		}
   315  		if !found2 {
   316  			t.Fatal("missing hook2")
   317  		}
   318  	}
   319  
   320  	if result := <-ss.Webhook().GetOutgoingList(0, 2); result.Err != nil {
   321  		t.Fatal(result.Err)
   322  	} else {
   323  		if len(result.Data.([]*model.OutgoingWebhook)) != 2 {
   324  			t.Fatal("wrong number of hooks returned")
   325  		}
   326  	}
   327  }
   328  
   329  func testWebhookStoreGetOutgoingByChannel(t *testing.T, ss store.Store) {
   330  	o1 := &model.OutgoingWebhook{}
   331  	o1.ChannelId = model.NewId()
   332  	o1.CreatorId = model.NewId()
   333  	o1.TeamId = model.NewId()
   334  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   335  
   336  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   337  
   338  	if r1 := <-ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100); r1.Err != nil {
   339  		t.Fatal(r1.Err)
   340  	} else {
   341  		if r1.Data.([]*model.OutgoingWebhook)[0].CreateAt != o1.CreateAt {
   342  			t.Fatal("invalid returned webhook")
   343  		}
   344  	}
   345  
   346  	if result := <-ss.Webhook().GetOutgoingByChannel("123", -1, -1); result.Err != nil {
   347  		t.Fatal(result.Err)
   348  	} else {
   349  		if len(result.Data.([]*model.OutgoingWebhook)) != 0 {
   350  			t.Fatal("no webhooks should have returned")
   351  		}
   352  	}
   353  }
   354  
   355  func testWebhookStoreGetOutgoingByTeam(t *testing.T, ss store.Store) {
   356  	o1 := &model.OutgoingWebhook{}
   357  	o1.ChannelId = model.NewId()
   358  	o1.CreatorId = model.NewId()
   359  	o1.TeamId = model.NewId()
   360  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   361  
   362  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   363  
   364  	if r1 := <-ss.Webhook().GetOutgoingByTeam(o1.TeamId, 0, 100); r1.Err != nil {
   365  		t.Fatal(r1.Err)
   366  	} else {
   367  		if r1.Data.([]*model.OutgoingWebhook)[0].CreateAt != o1.CreateAt {
   368  			t.Fatal("invalid returned webhook")
   369  		}
   370  	}
   371  
   372  	if result := <-ss.Webhook().GetOutgoingByTeam("123", -1, -1); result.Err != nil {
   373  		t.Fatal(result.Err)
   374  	} else {
   375  		if len(result.Data.([]*model.OutgoingWebhook)) != 0 {
   376  			t.Fatal("no webhooks should have returned")
   377  		}
   378  	}
   379  }
   380  
   381  func testWebhookStoreDeleteOutgoing(t *testing.T, ss store.Store) {
   382  	o1 := &model.OutgoingWebhook{}
   383  	o1.ChannelId = model.NewId()
   384  	o1.CreatorId = model.NewId()
   385  	o1.TeamId = model.NewId()
   386  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   387  
   388  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   389  
   390  	if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil {
   391  		t.Fatal(r1.Err)
   392  	} else {
   393  		if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt {
   394  			t.Fatal("invalid returned webhook")
   395  		}
   396  	}
   397  
   398  	if r2 := <-ss.Webhook().DeleteOutgoing(o1.Id, model.GetMillis()); r2.Err != nil {
   399  		t.Fatal(r2.Err)
   400  	}
   401  
   402  	if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil {
   403  		t.Log(r3.Data)
   404  		t.Fatal("Missing id should have failed")
   405  	}
   406  }
   407  
   408  func testWebhookStoreDeleteOutgoingByChannel(t *testing.T, ss store.Store) {
   409  	o1 := &model.OutgoingWebhook{}
   410  	o1.ChannelId = model.NewId()
   411  	o1.CreatorId = model.NewId()
   412  	o1.TeamId = model.NewId()
   413  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   414  
   415  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   416  
   417  	if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil {
   418  		t.Fatal(r1.Err)
   419  	} else {
   420  		if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt {
   421  			t.Fatal("invalid returned webhook")
   422  		}
   423  	}
   424  
   425  	if r2 := <-ss.Webhook().PermanentDeleteOutgoingByChannel(o1.ChannelId); r2.Err != nil {
   426  		t.Fatal(r2.Err)
   427  	}
   428  
   429  	if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil {
   430  		t.Log(r3.Data)
   431  		t.Fatal("Missing id should have failed")
   432  	}
   433  }
   434  
   435  func testWebhookStoreDeleteOutgoingByUser(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)).Data.(*model.OutgoingWebhook)
   443  
   444  	if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil {
   445  		t.Fatal(r1.Err)
   446  	} else {
   447  		if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt {
   448  			t.Fatal("invalid returned webhook")
   449  		}
   450  	}
   451  
   452  	if r2 := <-ss.Webhook().PermanentDeleteOutgoingByUser(o1.CreatorId); r2.Err != nil {
   453  		t.Fatal(r2.Err)
   454  	}
   455  
   456  	if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil {
   457  		t.Log(r3.Data)
   458  		t.Fatal("Missing id should have failed")
   459  	}
   460  }
   461  
   462  func testWebhookStoreUpdateOutgoing(t *testing.T, ss store.Store) {
   463  	o1 := &model.OutgoingWebhook{}
   464  	o1.ChannelId = model.NewId()
   465  	o1.CreatorId = model.NewId()
   466  	o1.TeamId = model.NewId()
   467  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   468  	o1.Username = "test-user-name"
   469  	o1.IconURL = "http://nowhere.com/icon"
   470  
   471  	o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   472  
   473  	o1.Token = model.NewId()
   474  	o1.Username = "another-test-user-name"
   475  
   476  	if r2 := <-ss.Webhook().UpdateOutgoing(o1); r2.Err != nil {
   477  		t.Fatal(r2.Err)
   478  	}
   479  }
   480  
   481  func testWebhookStoreCountIncoming(t *testing.T, ss store.Store) {
   482  	o1 := &model.IncomingWebhook{}
   483  	o1.ChannelId = model.NewId()
   484  	o1.UserId = model.NewId()
   485  	o1.TeamId = model.NewId()
   486  
   487  	_ = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook)
   488  
   489  	if r := <-ss.Webhook().AnalyticsIncomingCount(""); r.Err != nil {
   490  		t.Fatal(r.Err)
   491  	} else {
   492  		if r.Data.(int64) == 0 {
   493  			t.Fatal("should have at least 1 incoming hook")
   494  		}
   495  	}
   496  }
   497  
   498  func testWebhookStoreCountOutgoing(t *testing.T, ss store.Store) {
   499  	o1 := &model.OutgoingWebhook{}
   500  	o1.ChannelId = model.NewId()
   501  	o1.CreatorId = model.NewId()
   502  	o1.TeamId = model.NewId()
   503  	o1.CallbackURLs = []string{"http://nowhere.com/"}
   504  
   505  	_ = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook)
   506  
   507  	if r := <-ss.Webhook().AnalyticsOutgoingCount(""); r.Err != nil {
   508  		t.Fatal(r.Err)
   509  	} else {
   510  		if r.Data.(int64) == 0 {
   511  			t.Fatal("should have at least 1 outgoing hook")
   512  		}
   513  	}
   514  }