|
| 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 | + |
| 5 | +package webhook |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + "code.gitea.io/gitea/models" |
| 11 | + api "code.gitea.io/gitea/modules/structs" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestPrepareWebhooks(t *testing.T) { |
| 16 | + assert.NoError(t, models.PrepareTestDatabase()) |
| 17 | + |
| 18 | + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) |
| 19 | + hookTasks := []*models.HookTask{ |
| 20 | + {RepoID: repo.ID, HookID: 1, EventType: models.HookEventPush}, |
| 21 | + } |
| 22 | + for _, hookTask := range hookTasks { |
| 23 | + models.AssertNotExistsBean(t, hookTask) |
| 24 | + } |
| 25 | + assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{})) |
| 26 | + for _, hookTask := range hookTasks { |
| 27 | + models.AssertExistsAndLoadBean(t, hookTask) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestPrepareWebhooksBranchFilterMatch(t *testing.T) { |
| 32 | + assert.NoError(t, models.PrepareTestDatabase()) |
| 33 | + |
| 34 | + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) |
| 35 | + hookTasks := []*models.HookTask{ |
| 36 | + {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, |
| 37 | + } |
| 38 | + for _, hookTask := range hookTasks { |
| 39 | + models.AssertNotExistsBean(t, hookTask) |
| 40 | + } |
| 41 | + // this test also ensures that * doesn't handle / in any special way (like shell would) |
| 42 | + assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791"})) |
| 43 | + for _, hookTask := range hookTasks { |
| 44 | + models.AssertExistsAndLoadBean(t, hookTask) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) { |
| 49 | + assert.NoError(t, models.PrepareTestDatabase()) |
| 50 | + |
| 51 | + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 2}).(*models.Repository) |
| 52 | + hookTasks := []*models.HookTask{ |
| 53 | + {RepoID: repo.ID, HookID: 4, EventType: models.HookEventPush}, |
| 54 | + } |
| 55 | + for _, hookTask := range hookTasks { |
| 56 | + models.AssertNotExistsBean(t, hookTask) |
| 57 | + } |
| 58 | + assert.NoError(t, PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"})) |
| 59 | + |
| 60 | + for _, hookTask := range hookTasks { |
| 61 | + models.AssertNotExistsBean(t, hookTask) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// TODO TestHookTask_deliver |
| 66 | + |
| 67 | +// TODO TestDeliverHooks |
0 commit comments