mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-01 13:44:06 +01:00
Add tests for webhook release events
Co-authored-by: oliverpool <git@olivier.pfad.fr>
(cherry picked from commit 8506dbe2e5
)
This commit is contained in:
parent
ee500dacd0
commit
145cac0865
1 changed files with 116 additions and 0 deletions
|
@ -9,10 +9,16 @@ import (
|
|||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/gitrepo"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
"code.gitea.io/gitea/services/release"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
@ -70,3 +76,113 @@ func TestWebhookPayloadRef(t *testing.T) {
|
|||
assert.Empty(t, expected)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWebhookReleaseEvents(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
||||
w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{
|
||||
ID: 1,
|
||||
RepoID: repo.ID,
|
||||
})
|
||||
w.HookEvent = &webhook_module.HookEvent{
|
||||
SendEverything: true,
|
||||
}
|
||||
assert.NoError(t, w.UpdateEvent())
|
||||
assert.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w))
|
||||
|
||||
hookTasks := retrieveHookTasks(t, w.ID, true)
|
||||
|
||||
gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo)
|
||||
assert.NoError(t, err)
|
||||
defer gitRepo.Close()
|
||||
|
||||
t.Run("CreateRelease", func(t *testing.T) {
|
||||
assert.NoError(t, release.CreateRelease(gitRepo, &repo_model.Release{
|
||||
RepoID: repo.ID,
|
||||
Repo: repo,
|
||||
PublisherID: user.ID,
|
||||
Publisher: user,
|
||||
TagName: "v1.1.1",
|
||||
Target: "master",
|
||||
Title: "v1.1.1 is released",
|
||||
Note: "v1.1.1 is released",
|
||||
IsDraft: false,
|
||||
IsPrerelease: false,
|
||||
IsTag: false,
|
||||
}, nil, ""))
|
||||
|
||||
// check the newly created hooktasks
|
||||
hookTasksLenBefore := len(hookTasks)
|
||||
hookTasks = retrieveHookTasks(t, w.ID, false)
|
||||
|
||||
checkHookTasks(t, map[webhook_module.HookEventType]string{
|
||||
webhook_module.HookEventRelease: "published",
|
||||
webhook_module.HookEventCreate: "", // a tag was created as well
|
||||
webhook_module.HookEventPush: "", // the tag creation also means a push event
|
||||
}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
|
||||
|
||||
t.Run("UpdateRelease", func(t *testing.T) {
|
||||
rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{RepoID: repo.ID, TagName: "v1.1.1"})
|
||||
assert.NoError(t, release.UpdateRelease(db.DefaultContext, user, gitRepo, rel, nil, nil, nil, false))
|
||||
|
||||
// check the newly created hooktasks
|
||||
hookTasksLenBefore := len(hookTasks)
|
||||
hookTasks = retrieveHookTasks(t, w.ID, false)
|
||||
|
||||
checkHookTasks(t, map[webhook_module.HookEventType]string{
|
||||
webhook_module.HookEventRelease: "updated",
|
||||
}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("CreateNewTag", func(t *testing.T) {
|
||||
assert.NoError(t, release.CreateNewTag(db.DefaultContext,
|
||||
user,
|
||||
repo,
|
||||
"master",
|
||||
"v1.1.2",
|
||||
"v1.1.2 is tagged",
|
||||
))
|
||||
|
||||
// check the newly created hooktasks
|
||||
hookTasksLenBefore := len(hookTasks)
|
||||
hookTasks = retrieveHookTasks(t, w.ID, false)
|
||||
|
||||
checkHookTasks(t, map[webhook_module.HookEventType]string{
|
||||
webhook_module.HookEventCreate: "", // tag was created as well
|
||||
webhook_module.HookEventPush: "", // the tag creation also means a push event
|
||||
}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
|
||||
|
||||
t.Run("UpdateRelease", func(t *testing.T) {
|
||||
rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{RepoID: repo.ID, TagName: "v1.1.2"})
|
||||
assert.NoError(t, release.UpdateRelease(db.DefaultContext, user, gitRepo, rel, nil, nil, nil, true))
|
||||
|
||||
// check the newly created hooktasks
|
||||
hookTasksLenBefore := len(hookTasks)
|
||||
hookTasks = retrieveHookTasks(t, w.ID, false)
|
||||
|
||||
checkHookTasks(t, map[webhook_module.HookEventType]string{
|
||||
webhook_module.HookEventRelease: "published",
|
||||
}, hookTasks[:len(hookTasks)-hookTasksLenBefore])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func checkHookTasks(t *testing.T, expectedActions map[webhook_module.HookEventType]string, hookTasks []*webhook_model.HookTask) {
|
||||
t.Helper()
|
||||
for _, hookTask := range hookTasks {
|
||||
expectedAction, ok := expectedActions[hookTask.EventType]
|
||||
if !ok {
|
||||
t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType)
|
||||
}
|
||||
var payload struct {
|
||||
Action string `json:"action"`
|
||||
}
|
||||
assert.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload))
|
||||
assert.Equal(t, expectedAction, payload.Action, "unexpected action for %q event", hookTask.EventType)
|
||||
delete(expectedActions, hookTask.EventType)
|
||||
}
|
||||
assert.Empty(t, expectedActions)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue