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