2022-01-23 19:16:30 +05:30
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-23 19:16:30 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-08 03:48:38 +05:30
|
|
|
"context"
|
2022-01-23 19:16:30 +05:30
|
|
|
"testing"
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
2022-01-23 19:16:30 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-01-23 19:16:30 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPackagistPayload(t *testing.T) {
|
2024-03-08 03:48:38 +05:30
|
|
|
pc := packagistConvertor{
|
|
|
|
PackageURL: "https://packagist.org/packages/example",
|
|
|
|
}
|
2022-01-23 19:16:30 +05:30
|
|
|
t.Run("Create", func(t *testing.T) {
|
|
|
|
p := createTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Create(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
p := deleteTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Delete(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Fork", func(t *testing.T) {
|
|
|
|
p := forkTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Fork(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Push", func(t *testing.T) {
|
|
|
|
p := pushTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Push(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
assert.Equal(t, "https://packagist.org/packages/example", pl.PackagistRepository.URL)
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Issue", func(t *testing.T) {
|
|
|
|
p := issueTestPayload()
|
|
|
|
|
|
|
|
p.Action = api.HookIssueOpened
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Issue(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
|
|
|
|
p.Action = api.HookIssueClosed
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err = pc.Issue(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("IssueComment", func(t *testing.T) {
|
|
|
|
p := issueCommentTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.IssueComment(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("PullRequest", func(t *testing.T) {
|
|
|
|
p := pullRequestTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.PullRequest(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("PullRequestComment", func(t *testing.T) {
|
|
|
|
p := pullRequestCommentTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.IssueComment(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Review", func(t *testing.T) {
|
|
|
|
p := pullRequestTestPayload()
|
|
|
|
p.Action = api.HookIssueReviewed
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Repository", func(t *testing.T) {
|
|
|
|
p := repositoryTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Repository(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
|
2023-11-17 16:47:33 +05:30
|
|
|
t.Run("Package", func(t *testing.T) {
|
|
|
|
p := packageTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Package(p)
|
2023-11-17 16:47:33 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2023-11-17 16:47:33 +05:30
|
|
|
})
|
|
|
|
|
2022-09-05 01:24:23 +05:30
|
|
|
t.Run("Wiki", func(t *testing.T) {
|
|
|
|
p := wikiTestPayload()
|
|
|
|
|
|
|
|
p.Action = api.HookWikiCreated
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Wiki(p)
|
2022-09-05 01:24:23 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-09-05 01:24:23 +05:30
|
|
|
|
|
|
|
p.Action = api.HookWikiEdited
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err = pc.Wiki(p)
|
2022-09-05 01:24:23 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-09-05 01:24:23 +05:30
|
|
|
|
|
|
|
p.Action = api.HookWikiDeleted
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err = pc.Wiki(p)
|
2022-09-05 01:24:23 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-09-05 01:24:23 +05:30
|
|
|
})
|
|
|
|
|
2022-01-23 19:16:30 +05:30
|
|
|
t.Run("Release", func(t *testing.T) {
|
|
|
|
p := pullReleaseTestPayload()
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
pl, err := pc.Release(p)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
require.Equal(t, pl, PackagistPayload{})
|
2022-01-23 19:16:30 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackagistJSONPayload(t *testing.T) {
|
|
|
|
p := pushTestPayload()
|
2024-03-08 03:48:38 +05:30
|
|
|
data, err := p.JSONPayload()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
hook := &webhook_model.Webhook{
|
|
|
|
RepoID: 3,
|
|
|
|
IsActive: true,
|
|
|
|
Type: webhook_module.PACKAGIST,
|
|
|
|
URL: "https://packagist.org/api/update-package?username=THEUSERNAME&apiToken=TOPSECRETAPITOKEN",
|
|
|
|
Meta: `{"package_url":"https://packagist.org/packages/example"}`,
|
|
|
|
HTTPMethod: "POST",
|
|
|
|
}
|
|
|
|
task := &webhook_model.HookTask{
|
|
|
|
HookID: hook.ID,
|
|
|
|
EventType: webhook_module.HookEventPush,
|
|
|
|
PayloadContent: string(data),
|
|
|
|
PayloadVersion: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, reqBody, err := newPackagistRequest(context.Background(), hook, task)
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.NotNil(t, reqBody)
|
|
|
|
require.NoError(t, err)
|
2022-01-23 19:16:30 +05:30
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
assert.Equal(t, "POST", req.Method)
|
|
|
|
assert.Equal(t, "https://packagist.org/api/update-package?username=THEUSERNAME&apiToken=TOPSECRETAPITOKEN", req.URL.String())
|
|
|
|
assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
|
|
|
|
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
|
|
|
|
var body PackagistPayload
|
|
|
|
err = json.NewDecoder(req.Body).Decode(&body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "https://packagist.org/packages/example", body.PackagistRepository.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackagistEmptyPayload(t *testing.T) {
|
|
|
|
p := createTestPayload()
|
|
|
|
data, err := p.JSONPayload()
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-03-08 03:48:38 +05:30
|
|
|
hook := &webhook_model.Webhook{
|
|
|
|
RepoID: 3,
|
|
|
|
IsActive: true,
|
|
|
|
Type: webhook_module.PACKAGIST,
|
|
|
|
URL: "https://packagist.org/api/update-package?username=THEUSERNAME&apiToken=TOPSECRETAPITOKEN",
|
|
|
|
Meta: `{"package_url":"https://packagist.org/packages/example"}`,
|
|
|
|
HTTPMethod: "POST",
|
|
|
|
}
|
|
|
|
task := &webhook_model.HookTask{
|
|
|
|
HookID: hook.ID,
|
|
|
|
EventType: webhook_module.HookEventCreate,
|
|
|
|
PayloadContent: string(data),
|
|
|
|
PayloadVersion: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, reqBody, err := newPackagistRequest(context.Background(), hook, task)
|
|
|
|
require.NotNil(t, req)
|
|
|
|
require.NotNil(t, reqBody)
|
2022-01-23 19:16:30 +05:30
|
|
|
require.NoError(t, err)
|
2024-03-08 03:48:38 +05:30
|
|
|
|
|
|
|
assert.Equal(t, "POST", req.Method)
|
|
|
|
assert.Equal(t, "https://packagist.org/api/update-package?username=THEUSERNAME&apiToken=TOPSECRETAPITOKEN", req.URL.String())
|
|
|
|
assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
|
|
|
|
assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
|
|
|
|
var body PackagistPayload
|
|
|
|
err = json.NewDecoder(req.Body).Decode(&body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "", body.PackagistRepository.URL)
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|