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"
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-08 01:18:38 +03:00
pc := packagistConvertor {
PackageURL : "https://packagist.org/packages/example" ,
}
2022-01-23 16:46:30 +03:00
t . Run ( "Create" , func ( t * testing . T ) {
p := createTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Create ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Delete" , func ( t * testing . T ) {
p := deleteTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Delete ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Fork" , func ( t * testing . T ) {
p := forkTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Fork ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Push" , func ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Push ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
assert . Equal ( t , "https://packagist.org/packages/example" , pl . PackagistRepository . URL )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Issue" , func ( t * testing . T ) {
p := issueTestPayload ( )
p . Action = api . HookIssueOpened
2024-03-08 01:18:38 +03:00
pl , err := pc . Issue ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
p . Action = api . HookIssueClosed
2024-03-08 01:18:38 +03:00
pl , err = pc . Issue ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "IssueComment" , func ( t * testing . T ) {
p := issueCommentTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . IssueComment ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "PullRequest" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . PullRequest ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "PullRequestComment" , func ( t * testing . T ) {
p := pullRequestCommentTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . IssueComment ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Review" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
p . Action = api . HookIssueReviewed
2024-03-08 01:18:38 +03:00
pl , err := pc . Review ( p , webhook_module . HookEventPullRequestReviewApproved )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
t . Run ( "Repository" , func ( t * testing . T ) {
p := repositoryTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Repository ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
2023-11-17 14:17:33 +03:00
t . Run ( "Package" , func ( t * testing . T ) {
p := packageTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Package ( p )
2023-11-17 14:17:33 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2023-11-17 14:17:33 +03:00
} )
2022-09-04 22:54:23 +03:00
t . Run ( "Wiki" , func ( t * testing . T ) {
p := wikiTestPayload ( )
p . Action = api . HookWikiCreated
2024-03-08 01:18:38 +03:00
pl , err := pc . Wiki ( p )
2022-09-04 22:54:23 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-09-04 22:54:23 +03:00
p . Action = api . HookWikiEdited
2024-03-08 01:18:38 +03:00
pl , err = pc . Wiki ( p )
2022-09-04 22:54:23 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-09-04 22:54:23 +03:00
p . Action = api . HookWikiDeleted
2024-03-08 01:18:38 +03:00
pl , err = pc . Wiki ( p )
2022-09-04 22:54:23 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-09-04 22:54:23 +03:00
} )
2022-01-23 16:46:30 +03:00
t . Run ( "Release" , func ( t * testing . T ) {
p := pullReleaseTestPayload ( )
2024-03-08 01:18:38 +03:00
pl , err := pc . Release ( p )
2022-01-23 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
require . Equal ( t , pl , PackagistPayload { } )
2022-01-23 16:46:30 +03:00
} )
}
func TestPackagistJSONPayload ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-08 01:18:38 +03:00
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 16:46:30 +03:00
2024-03-08 01:18:38 +03:00
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 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
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 16:46:30 +03:00
require . NoError ( t , err )
2024-03-08 01:18:38 +03:00
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 16:46:30 +03:00
}