You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.4 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package webhook
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestWebhook_GetSlackHook(t *testing.T) {
  12. w := &models.Webhook{
  13. Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
  14. }
  15. slackHook := GetSlackHook(w)
  16. assert.Equal(t, *slackHook, SlackMeta{
  17. Channel: "foo",
  18. Username: "username",
  19. Color: "blue",
  20. })
  21. }
  22. func TestPrepareWebhooks(t *testing.T) {
  23. assert.NoError(t, models.PrepareTestDatabase())
  24. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  25. hookTasks := []*models.HookTask{
  26. {RepoID: repo.ID, HookID: 1, EventType: models.HookEventPush},
  27. }
  28. for _, hookTask := range hookTasks {
  29. models.AssertNotExistsBean(t, hookTask)
  30. }
  31. assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{}))
  32. for _, hookTask := range hookTasks {
  33. models.AssertExistsAndLoadBean(t, hookTask)
  34. }
  35. }
  36. func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
  37. assert.NoError(t, models.PrepareTestDatabase())
  38. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  39. hookTasks := []*models.HookTask{
  40. {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush},
  41. }
  42. for _, hookTask := range hookTasks {
  43. models.AssertNotExistsBean(t, hookTask)
  44. }
  45. // this test also ensures that * doesn't handle / in any special way (like shell would)
  46. assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791"}))
  47. for _, hookTask := range hookTasks {
  48. models.AssertExistsAndLoadBean(t, hookTask)
  49. }
  50. }
  51. func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
  52. assert.NoError(t, models.PrepareTestDatabase())
  53. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository)
  54. hookTasks := []*models.HookTask{
  55. {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush},
  56. }
  57. for _, hookTask := range hookTasks {
  58. models.AssertNotExistsBean(t, hookTask)
  59. }
  60. assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
  61. for _, hookTask := range hookTasks {
  62. models.AssertNotExistsBean(t, hookTask)
  63. }
  64. }
  65. // TODO TestHookTask_deliver
  66. // TODO TestDeliverHooks