2019-12-28 14:25:09 +05:30
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 23:50:29 +05:30
// SPDX-License-Identifier: MIT
2019-12-28 14:25:09 +05:30
package webhook
import (
2024-03-08 03:48:38 +05:30
"context"
2019-12-28 14:25:09 +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"
2019-12-28 14:25:09 +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"
2021-06-21 07:42:19 +05:30
2019-12-28 14:25:09 +05:30
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2021-06-21 07:42:19 +05:30
func TestSlackPayload ( t * testing . T ) {
2024-03-08 03:48:38 +05:30
sc := slackConvertor { }
2021-06-21 07:42:19 +05:30
t . Run ( "Create" , func ( t * testing . T ) {
p := createTestPayload ( )
2020-09-05 08:27:13 +05:30
2024-03-08 03:48:38 +05:30
pl , err := sc . Create ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2020-09-05 08:27:13 +05:30
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
2019-12-28 14:25:09 +05:30
2021-06-21 07:42:19 +05:30
t . Run ( "Delete" , func ( t * testing . T ) {
p := deleteTestPayload ( )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
pl , err := sc . Delete ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
2019-12-28 14:25:09 +05:30
2021-06-21 07:42:19 +05:30
t . Run ( "Fork" , func ( t * testing . T ) {
p := forkTestPayload ( )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
pl , err := sc . Fork ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "<http://localhost:3000/test/repo2|test/repo2> is forked to <http://localhost:3000/test/repo|test/repo>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
2019-12-28 14:25:09 +05:30
2021-06-21 07:42:19 +05:30
t . Run ( "Push" , func ( t * testing . T ) {
p := pushTestPayload ( )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
pl , err := sc . Push ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "Issue" , func ( t * testing . T ) {
p := issueTestPayload ( )
p . Action = api . HookIssueOpened
2024-03-08 03:48:38 +05:30
pl , err := sc . Issue ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>" , pl . Text )
2019-12-28 14:25:09 +05:30
2021-06-21 07:42:19 +05:30
p . Action = api . HookIssueClosed
2024-03-08 03:48:38 +05:30
pl , err = sc . Issue ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "IssueComment" , func ( t * testing . T ) {
p := issueCommentTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . IssueComment ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] New comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "PullRequest" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . PullRequest ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "PullRequestComment" , func ( t * testing . T ) {
p := pullRequestCommentTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . IssueComment ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] New comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "Review" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
p . Action = api . HookIssueReviewed
2024-03-08 03:48:38 +05:30
pl , err := sc . Review ( p , webhook_module . HookEventPullRequestReviewApproved )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
t . Run ( "Repository" , func ( t * testing . T ) {
p := repositoryTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . Repository ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
2023-11-17 16:47:33 +05:30
t . Run ( "Package" , func ( t * testing . T ) {
p := packageTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . Package ( p )
2023-11-17 16:47:33 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>" , pl . Text )
2023-11-17 16:47:33 +05:30
} )
2022-09-05 01:24:23 +05:30
t . Run ( "Wiki" , func ( t * testing . T ) {
p := wikiTestPayload ( )
p . Action = api . HookWikiCreated
2024-03-08 03:48:38 +05:30
pl , err := sc . Wiki ( p )
2022-09-05 01:24:23 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by <https://try.gitea.io/user1|user1>" , pl . Text )
2022-09-05 01:24:23 +05:30
p . Action = api . HookWikiEdited
2024-03-08 03:48:38 +05:30
pl , err = sc . Wiki ( p )
2022-09-05 01:24:23 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by <https://try.gitea.io/user1|user1>" , pl . Text )
2022-09-05 01:24:23 +05:30
p . Action = api . HookWikiDeleted
2024-03-08 03:48:38 +05:30
pl , err = sc . Wiki ( p )
2022-09-05 01:24:23 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by <https://try.gitea.io/user1|user1>" , pl . Text )
2022-09-05 01:24:23 +05:30
} )
2021-06-21 07:42:19 +05:30
t . Run ( "Release" , func ( t * testing . T ) {
p := pullReleaseTestPayload ( )
2024-03-08 03:48:38 +05:30
pl , err := sc . Release ( p )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 07:42:19 +05:30
} )
2019-12-28 14:25:09 +05:30
}
2021-06-21 07:42:19 +05:30
func TestSlackJSONPayload ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-08 03:48:38 +05:30
data , err := p . JSONPayload ( )
2020-07-19 15:23:40 +05:30
require . NoError ( t , err )
2019-12-28 14:25:09 +05:30
2024-03-08 03:48:38 +05:30
hook := & webhook_model . Webhook {
RepoID : 3 ,
IsActive : true ,
Type : webhook_module . SLACK ,
URL : "https://slack.example.com/" ,
Meta : ` { } ` ,
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 := slackHandler { } . NewRequest ( context . Background ( ) , hook , task )
2024-03-08 03:48:38 +05:30
require . NotNil ( t , req )
require . NotNil ( t , reqBody )
2021-06-21 07:42:19 +05:30
require . NoError ( t , err )
2024-03-08 03:48:38 +05:30
assert . Equal ( t , "POST" , req . Method )
assert . Equal ( t , "https://slack.example.com/" , 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 SlackPayload
err = json . NewDecoder ( req . Body ) . Decode ( & body )
assert . NoError ( t , err )
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1" , body . Text )
2019-12-28 14:25:09 +05:30
}
2022-08-11 21:18:23 +05:30
func TestIsValidSlackChannel ( t * testing . T ) {
tt := [ ] struct {
channelName string
expected bool
} {
{ "gitea" , true } ,
{ "#gitea" , true } ,
{ " " , false } ,
{ "#" , false } ,
{ " #" , false } ,
{ "gitea " , false } ,
{ " gitea" , false } ,
}
for _ , v := range tt {
assert . Equal ( t , v . expected , IsValidSlackChannel ( v . channelName ) )
}
}
2024-03-20 20:14:01 +05:30
func TestSlackMetadata ( t * testing . T ) {
w := & webhook_model . Webhook {
Meta : ` { "channel": "foo", "username": "username", "color": "blue"} ` ,
}
slackHook := slackHandler { } . Metadata ( w )
assert . Equal ( t , * slackHook . ( * SlackMeta ) , SlackMeta {
Channel : "foo" ,
Username : "username" ,
Color : "blue" ,
} )
}
func TestSlackToHook ( t * testing . T ) {
w := & webhook_model . Webhook {
Type : webhook_module . SLACK ,
ContentType : webhook_model . ContentTypeJSON ,
URL : "https://slack.example.com" ,
Meta : ` { "channel": "foo", "username": "username", "color": "blue"} ` ,
HookEvent : & webhook_module . HookEvent {
PushOnly : true ,
SendEverything : false ,
ChooseEvents : false ,
HookEvents : webhook_module . HookEvents {
Create : false ,
Push : true ,
PullRequest : false ,
} ,
} ,
}
h , err := ToHook ( "repoLink" , w )
assert . NoError ( t , err )
assert . Equal ( t , h . Config , map [ string ] string {
"url" : "https://slack.example.com" ,
"content_type" : "json" ,
"channel" : "foo" ,
"color" : "blue" ,
"icon_url" : "" ,
"username" : "username" ,
} )
assert . Equal ( t , h . URL , "https://slack.example.com" )
assert . Equal ( t , h . ContentType , "json" )
assert . Equal ( t , h . Metadata , & SlackMeta {
Channel : "foo" ,
Username : "username" ,
IconURL : "" ,
Color : "blue" ,
} )
}