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