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