2022-01-23 14:46:30 +01:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-23 14:46:30 +01:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-07 23:18:38 +01:00
|
|
|
"context"
|
2024-03-13 09:26:56 +01:00
|
|
|
"fmt"
|
2022-01-23 14:46:30 +01:00
|
|
|
"testing"
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
2022-01-23 14:46:30 +01:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 16:23:15 +01:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-01-23 14:46:30 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPackagistPayload(t *testing.T) {
|
2024-03-13 09:26:56 +01:00
|
|
|
payloads := []api.Payloader{
|
|
|
|
createTestPayload(),
|
|
|
|
deleteTestPayload(),
|
|
|
|
forkTestPayload(),
|
|
|
|
pushTestPayload(),
|
|
|
|
issueTestPayload(),
|
|
|
|
issueCommentTestPayload(),
|
|
|
|
pullRequestCommentTestPayload(),
|
|
|
|
pullRequestTestPayload(),
|
|
|
|
repositoryTestPayload(),
|
|
|
|
packageTestPayload(),
|
|
|
|
wikiTestPayload(),
|
|
|
|
pullReleaseTestPayload(),
|
2024-03-07 23:18:38 +01:00
|
|
|
}
|
2022-01-23 14:46:30 +01:00
|
|
|
|
2024-03-13 09:26:56 +01:00
|
|
|
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 15:44:01 +01:00
|
|
|
req, reqBody, err := packagistHandler{}.NewRequest(context.Background(), hook, task)
|
2024-03-13 09:26:56 +01:00
|
|
|
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)
|
2024-07-30 21:41:27 +02:00
|
|
|
require.NoError(t, err)
|
2024-03-13 09:26:56 +01:00
|
|
|
assert.Equal(t, "https://packagist.org/packages/example", body.PackagistRepository.URL)
|
|
|
|
})
|
2024-03-07 23:18:38 +01:00
|
|
|
}
|
2022-01-23 14:46:30 +01:00
|
|
|
}
|