Skip to content

Commit 75005c8

Browse files
committed
Add test for webhook status
1 parent 1c2278f commit 75005c8

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/integration/repo_webhook_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration
55

66
import (
7+
"context"
78
"fmt"
89
"io"
910
"net/http"
@@ -15,6 +16,7 @@ import (
1516
auth_model "code.gitea.io/gitea/models/auth"
1617
"code.gitea.io/gitea/models/repo"
1718
"code.gitea.io/gitea/models/unittest"
19+
"code.gitea.io/gitea/modules/gitrepo"
1820
"code.gitea.io/gitea/modules/json"
1921
api "code.gitea.io/gitea/modules/structs"
2022
webhook_module "code.gitea.io/gitea/modules/webhook"
@@ -466,3 +468,50 @@ func Test_WebhookPackage(t *testing.T) {
466468
assert.EqualValues(t, "v1.24.0", payloads[0].Package.Version)
467469
})
468470
}
471+
472+
func Test_WebhookStatus(t *testing.T) {
473+
var payloads []api.CommitStatusPayload
474+
var triggeredEvent string
475+
provider := newMockWebhookProvider(func(content string) {
476+
var payload api.CommitStatusPayload
477+
err := json.Unmarshal([]byte(content), &payload)
478+
assert.NoError(t, err)
479+
payloads = append(payloads, payload)
480+
triggeredEvent = "status"
481+
})
482+
defer provider.Close()
483+
484+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
485+
// 1. create a new webhook with special webhook for repo1
486+
session := loginUser(t, "user2")
487+
488+
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "status")
489+
490+
repo1 := unittest.AssertExistsAndLoadBean(t, &repo.Repository{ID: 1})
491+
492+
gitRepo1, err := gitrepo.OpenRepository(context.Background(), repo1)
493+
assert.NoError(t, err)
494+
commitID, err := gitRepo1.GetBranchCommitID(repo1.DefaultBranch)
495+
assert.NoError(t, err)
496+
497+
// 2. trigger the webhook
498+
testCtx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeAll)
499+
500+
// update a status for a commit via API
501+
doAPICreateCommitStatus(testCtx, commitID, api.CreateStatusOption{
502+
State: api.CommitStatusSuccess,
503+
TargetURL: "http://test.ci/",
504+
Description: "",
505+
Context: "testci",
506+
})(t)
507+
508+
// 3. validate the webhook is triggered
509+
assert.EqualValues(t, "status", triggeredEvent)
510+
assert.Len(t, payloads, 1)
511+
assert.EqualValues(t, commitID, payloads[0].Commit.ID)
512+
assert.EqualValues(t, "repo1", payloads[0].Repo.Name)
513+
assert.EqualValues(t, "user2/repo1", payloads[0].Repo.FullName)
514+
assert.EqualValues(t, "testci", payloads[0].Context)
515+
assert.EqualValues(t, commitID, payloads[0].SHA)
516+
})
517+
}

0 commit comments

Comments
 (0)