github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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 243 if err := (<-ss.Webhook().SaveOutgoing(&o1)).Err; err != nil { 244 t.Fatal("couldn't save item", err) 245 } 246 247 if err := (<-ss.Webhook().SaveOutgoing(&o1)).Err; err == nil { 248 t.Fatal("shouldn't be able to update from save") 249 } 250 } 251 252 func testWebhookStoreGetOutgoing(t *testing.T, ss store.Store) { 253 o1 := &model.OutgoingWebhook{} 254 o1.ChannelId = model.NewId() 255 o1.CreatorId = model.NewId() 256 o1.TeamId = model.NewId() 257 o1.CallbackURLs = []string{"http://nowhere.com/"} 258 259 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 260 261 if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil { 262 t.Fatal(r1.Err) 263 } else { 264 if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt { 265 t.Fatal("invalid returned webhook") 266 } 267 } 268 269 if err := (<-ss.Webhook().GetOutgoing("123")).Err; err == nil { 270 t.Fatal("Missing id should have failed") 271 } 272 } 273 274 func testWebhookStoreGetOutgoingList(t *testing.T, ss store.Store) { 275 o1 := &model.OutgoingWebhook{} 276 o1.ChannelId = model.NewId() 277 o1.CreatorId = model.NewId() 278 o1.TeamId = model.NewId() 279 o1.CallbackURLs = []string{"http://nowhere.com/"} 280 281 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 282 283 o2 := &model.OutgoingWebhook{} 284 o2.ChannelId = model.NewId() 285 o2.CreatorId = model.NewId() 286 o2.TeamId = model.NewId() 287 o2.CallbackURLs = []string{"http://nowhere.com/"} 288 289 o2 = (<-ss.Webhook().SaveOutgoing(o2)).Data.(*model.OutgoingWebhook) 290 291 if r1 := <-ss.Webhook().GetOutgoingList(0, 1000); r1.Err != nil { 292 t.Fatal(r1.Err) 293 } else { 294 hooks := r1.Data.([]*model.OutgoingWebhook) 295 found1 := false 296 found2 := false 297 298 for _, hook := range hooks { 299 if hook.CreateAt != o1.CreateAt { 300 found1 = true 301 } 302 303 if hook.CreateAt != o2.CreateAt { 304 found2 = true 305 } 306 } 307 308 if !found1 { 309 t.Fatal("missing hook1") 310 } 311 if !found2 { 312 t.Fatal("missing hook2") 313 } 314 } 315 316 if result := <-ss.Webhook().GetOutgoingList(0, 2); result.Err != nil { 317 t.Fatal(result.Err) 318 } else { 319 if len(result.Data.([]*model.OutgoingWebhook)) != 2 { 320 t.Fatal("wrong number of hooks returned") 321 } 322 } 323 } 324 325 func testWebhookStoreGetOutgoingByChannel(t *testing.T, ss store.Store) { 326 o1 := &model.OutgoingWebhook{} 327 o1.ChannelId = model.NewId() 328 o1.CreatorId = model.NewId() 329 o1.TeamId = model.NewId() 330 o1.CallbackURLs = []string{"http://nowhere.com/"} 331 332 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 333 334 if r1 := <-ss.Webhook().GetOutgoingByChannel(o1.ChannelId, 0, 100); r1.Err != nil { 335 t.Fatal(r1.Err) 336 } else { 337 if r1.Data.([]*model.OutgoingWebhook)[0].CreateAt != o1.CreateAt { 338 t.Fatal("invalid returned webhook") 339 } 340 } 341 342 if result := <-ss.Webhook().GetOutgoingByChannel("123", -1, -1); result.Err != nil { 343 t.Fatal(result.Err) 344 } else { 345 if len(result.Data.([]*model.OutgoingWebhook)) != 0 { 346 t.Fatal("no webhooks should have returned") 347 } 348 } 349 } 350 351 func testWebhookStoreGetOutgoingByTeam(t *testing.T, ss store.Store) { 352 o1 := &model.OutgoingWebhook{} 353 o1.ChannelId = model.NewId() 354 o1.CreatorId = model.NewId() 355 o1.TeamId = model.NewId() 356 o1.CallbackURLs = []string{"http://nowhere.com/"} 357 358 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 359 360 if r1 := <-ss.Webhook().GetOutgoingByTeam(o1.TeamId, 0, 100); r1.Err != nil { 361 t.Fatal(r1.Err) 362 } else { 363 if r1.Data.([]*model.OutgoingWebhook)[0].CreateAt != o1.CreateAt { 364 t.Fatal("invalid returned webhook") 365 } 366 } 367 368 if result := <-ss.Webhook().GetOutgoingByTeam("123", -1, -1); result.Err != nil { 369 t.Fatal(result.Err) 370 } else { 371 if len(result.Data.([]*model.OutgoingWebhook)) != 0 { 372 t.Fatal("no webhooks should have returned") 373 } 374 } 375 } 376 377 func testWebhookStoreDeleteOutgoing(t *testing.T, ss store.Store) { 378 o1 := &model.OutgoingWebhook{} 379 o1.ChannelId = model.NewId() 380 o1.CreatorId = model.NewId() 381 o1.TeamId = model.NewId() 382 o1.CallbackURLs = []string{"http://nowhere.com/"} 383 384 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 385 386 if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil { 387 t.Fatal(r1.Err) 388 } else { 389 if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt { 390 t.Fatal("invalid returned webhook") 391 } 392 } 393 394 if r2 := <-ss.Webhook().DeleteOutgoing(o1.Id, model.GetMillis()); r2.Err != nil { 395 t.Fatal(r2.Err) 396 } 397 398 if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil { 399 t.Log(r3.Data) 400 t.Fatal("Missing id should have failed") 401 } 402 } 403 404 func testWebhookStoreDeleteOutgoingByChannel(t *testing.T, ss store.Store) { 405 o1 := &model.OutgoingWebhook{} 406 o1.ChannelId = model.NewId() 407 o1.CreatorId = model.NewId() 408 o1.TeamId = model.NewId() 409 o1.CallbackURLs = []string{"http://nowhere.com/"} 410 411 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 412 413 if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil { 414 t.Fatal(r1.Err) 415 } else { 416 if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt { 417 t.Fatal("invalid returned webhook") 418 } 419 } 420 421 if r2 := <-ss.Webhook().PermanentDeleteOutgoingByChannel(o1.ChannelId); r2.Err != nil { 422 t.Fatal(r2.Err) 423 } 424 425 if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil { 426 t.Log(r3.Data) 427 t.Fatal("Missing id should have failed") 428 } 429 } 430 431 func testWebhookStoreDeleteOutgoingByUser(t *testing.T, ss store.Store) { 432 o1 := &model.OutgoingWebhook{} 433 o1.ChannelId = model.NewId() 434 o1.CreatorId = model.NewId() 435 o1.TeamId = model.NewId() 436 o1.CallbackURLs = []string{"http://nowhere.com/"} 437 438 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 439 440 if r1 := <-ss.Webhook().GetOutgoing(o1.Id); r1.Err != nil { 441 t.Fatal(r1.Err) 442 } else { 443 if r1.Data.(*model.OutgoingWebhook).CreateAt != o1.CreateAt { 444 t.Fatal("invalid returned webhook") 445 } 446 } 447 448 if r2 := <-ss.Webhook().PermanentDeleteOutgoingByUser(o1.CreatorId); r2.Err != nil { 449 t.Fatal(r2.Err) 450 } 451 452 if r3 := (<-ss.Webhook().GetOutgoing(o1.Id)); r3.Err == nil { 453 t.Log(r3.Data) 454 t.Fatal("Missing id should have failed") 455 } 456 } 457 458 func testWebhookStoreUpdateOutgoing(t *testing.T, ss store.Store) { 459 o1 := &model.OutgoingWebhook{} 460 o1.ChannelId = model.NewId() 461 o1.CreatorId = model.NewId() 462 o1.TeamId = model.NewId() 463 o1.CallbackURLs = []string{"http://nowhere.com/"} 464 465 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 466 467 o1.Token = model.NewId() 468 469 if r2 := <-ss.Webhook().UpdateOutgoing(o1); r2.Err != nil { 470 t.Fatal(r2.Err) 471 } 472 } 473 474 func testWebhookStoreCountIncoming(t *testing.T, ss store.Store) { 475 o1 := &model.IncomingWebhook{} 476 o1.ChannelId = model.NewId() 477 o1.UserId = model.NewId() 478 o1.TeamId = model.NewId() 479 480 o1 = (<-ss.Webhook().SaveIncoming(o1)).Data.(*model.IncomingWebhook) 481 482 if r := <-ss.Webhook().AnalyticsIncomingCount(""); r.Err != nil { 483 t.Fatal(r.Err) 484 } else { 485 if r.Data.(int64) == 0 { 486 t.Fatal("should have at least 1 incoming hook") 487 } 488 } 489 } 490 491 func testWebhookStoreCountOutgoing(t *testing.T, ss store.Store) { 492 o1 := &model.OutgoingWebhook{} 493 o1.ChannelId = model.NewId() 494 o1.CreatorId = model.NewId() 495 o1.TeamId = model.NewId() 496 o1.CallbackURLs = []string{"http://nowhere.com/"} 497 498 o1 = (<-ss.Webhook().SaveOutgoing(o1)).Data.(*model.OutgoingWebhook) 499 500 if r := <-ss.Webhook().AnalyticsOutgoingCount(""); r.Err != nil { 501 t.Fatal(r.Err) 502 } else { 503 if r.Data.(int64) == 0 { 504 t.Fatal("should have at least 1 outgoing hook") 505 } 506 } 507 }