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"
|
2024-03-13 13:56:56 +05:30
|
|
|
"fmt"
|
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-13 13:56:56 +05:30
|
|
|
payloads := []api.Payloader{
|
|
|
|
createTestPayload(),
|
|
|
|
deleteTestPayload(),
|
|
|
|
forkTestPayload(),
|
|
|
|
pushTestPayload(),
|
|
|
|
issueTestPayload(),
|
|
|
|
issueCommentTestPayload(),
|
|
|
|
pullRequestCommentTestPayload(),
|
|
|
|
pullRequestTestPayload(),
|
|
|
|
repositoryTestPayload(),
|
|
|
|
packageTestPayload(),
|
|
|
|
wikiTestPayload(),
|
|
|
|
pullReleaseTestPayload(),
|
2024-03-08 03:48:38 +05:30
|
|
|
}
|
2022-01-23 19:16:30 +05:30
|
|
|
|
2024-03-13 13:56:56 +05:30
|
|
|
for _, payloader := range payloads {
|
|
|
|
t.Run(fmt.Sprintf("%T", payloader), func(t *testing.T) {
|
|
|
|
data, err := payloader.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,
|
|
|
|
}
|
|
|
|
|
2024-03-20 20:14:01 +05:30
|
|
|
req, reqBody, err := packagistHandler{}.NewRequest(context.Background(), hook, task)
|
2024-03-13 13:56:56 +05:30
|
|
|
require.NotNil(t, req)
|
|
|
|
require.NotNil(t, reqBody)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
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, "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)
|
|
|
|
})
|
2024-03-08 03:48:38 +05:30
|
|
|
}
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|