2019-12-28 09:55:09 +01:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 19:20:29 +01:00
// SPDX-License-Identifier: MIT
2019-12-28 09:55:09 +01:00
package webhook
import (
2024-03-07 23:18:38 +01:00
"context"
2019-12-28 09:55:09 +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"
2019-12-28 09:55:09 +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"
2021-06-21 04:12:19 +02:00
2019-12-28 09:55:09 +01:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2021-06-21 04:12:19 +02:00
func TestSlackPayload ( t * testing . T ) {
2024-03-07 23:18:38 +01:00
sc := slackConvertor { }
2021-06-21 04:12:19 +02:00
t . Run ( "Create" , func ( t * testing . T ) {
p := createTestPayload ( )
2020-09-05 04:57:13 +02:00
2024-03-07 23:18:38 +01:00
pl , err := sc . Create ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2020-09-05 04:57:13 +02:00
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
2019-12-28 09:55:09 +01:00
2021-06-21 04:12:19 +02:00
t . Run ( "Delete" , func ( t * testing . T ) {
p := deleteTestPayload ( )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
pl , err := sc . Delete ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1" , pl . Text )
2021-06-21 04:12:19 +02:00
} )
2019-12-28 09:55:09 +01:00
2021-06-21 04:12:19 +02:00
t . Run ( "Fork" , func ( t * testing . T ) {
p := forkTestPayload ( )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
pl , err := sc . Fork ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
2019-12-28 09:55:09 +01:00
2021-06-21 04:12:19 +02:00
t . Run ( "Push" , func ( t * testing . T ) {
p := pushTestPayload ( )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
pl , err := sc . Push ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "Issue" , func ( t * testing . T ) {
p := issueTestPayload ( )
p . Action = api . HookIssueOpened
2024-03-07 23:18:38 +01:00
pl , err := sc . Issue ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 09:55:09 +01:00
2021-06-21 04:12:19 +02:00
p . Action = api . HookIssueClosed
2024-03-07 23:18:38 +01:00
pl , err = sc . Issue ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "IssueComment" , func ( t * testing . T ) {
p := issueCommentTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . IssueComment ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "PullRequest" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . PullRequest ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "PullRequestComment" , func ( t * testing . T ) {
p := pullRequestCommentTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . IssueComment ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "Review" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
p . Action = api . HookIssueReviewed
2024-03-07 23:18:38 +01:00
pl , err := sc . Review ( p , webhook_module . HookEventPullRequestReviewApproved )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
t . Run ( "Repository" , func ( t * testing . T ) {
p := repositoryTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . Repository ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
assert . Equal ( t , "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>" , pl . Text )
2021-06-21 04:12:19 +02:00
} )
2023-11-17 12:17:33 +01:00
t . Run ( "Package" , func ( t * testing . T ) {
p := packageTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . Package ( p )
2023-11-17 12:17:33 +01:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 12:17:33 +01:00
} )
2022-09-04 21:54:23 +02:00
t . Run ( "Wiki" , func ( t * testing . T ) {
p := wikiTestPayload ( )
p . Action = api . HookWikiCreated
2024-03-07 23:18:38 +01:00
pl , err := sc . Wiki ( p )
2022-09-04 21:54:23 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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-04 21:54:23 +02:00
p . Action = api . HookWikiEdited
2024-03-07 23:18:38 +01:00
pl , err = sc . Wiki ( p )
2022-09-04 21:54:23 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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-04 21:54:23 +02:00
p . Action = api . HookWikiDeleted
2024-03-07 23:18:38 +01:00
pl , err = sc . Wiki ( p )
2022-09-04 21:54:23 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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-04 21:54:23 +02:00
} )
2021-06-21 04:12:19 +02:00
t . Run ( "Release" , func ( t * testing . T ) {
p := pullReleaseTestPayload ( )
2024-03-07 23:18:38 +01:00
pl , err := sc . Release ( p )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 04:12:19 +02:00
} )
2019-12-28 09:55:09 +01:00
}
2021-06-21 04:12:19 +02:00
func TestSlackJSONPayload ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-07 23:18:38 +01:00
data , err := p . JSONPayload ( )
2020-07-19 11:53:40 +02:00
require . NoError ( t , err )
2019-12-28 09:55:09 +01:00
2024-03-07 23:18:38 +01:00
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 15:44:01 +01:00
req , reqBody , err := slackHandler { } . NewRequest ( context . Background ( ) , hook , task )
2024-03-07 23:18:38 +01:00
require . NotNil ( t , req )
require . NotNil ( t , reqBody )
2021-06-21 04:12:19 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 )
2024-07-30 21:42:06 +02:00
require . NoError ( t , err )
2024-03-07 23:18:38 +01:00
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 09:55:09 +01:00
}
2022-08-11 17:48:23 +02:00
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 15:44:01 +01:00
func TestSlackMetadata ( t * testing . T ) {
w := & webhook_model . Webhook {
Meta : ` { "channel": "foo", "username": "username", "color": "blue"} ` ,
}
slackHook := slackHandler { } . Metadata ( w )
2024-07-30 21:42:06 +02:00
assert . Equal ( t , SlackMeta {
2024-03-20 15:44:01 +01:00
Channel : "foo" ,
Username : "username" ,
Color : "blue" ,
2024-07-30 21:42:06 +02:00
} ,
* slackHook . ( * SlackMeta ) )
2024-03-20 15:44:01 +01:00
}
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 )
2024-07-30 21:42:06 +02:00
require . NoError ( t , err )
2024-03-20 15:44:01 +01:00
2024-07-30 21:42:06 +02:00
assert . Equal ( t , map [ string ] string {
2024-03-20 15:44:01 +01:00
"url" : "https://slack.example.com" ,
"content_type" : "json" ,
"channel" : "foo" ,
"color" : "blue" ,
"icon_url" : "" ,
"username" : "username" ,
2024-07-30 21:42:06 +02:00
} , h . Config )
assert . Equal ( t , "https://slack.example.com" , h . URL )
assert . Equal ( t , "json" , h . ContentType )
assert . Equal ( t , & SlackMeta {
2024-03-20 15:44:01 +01:00
Channel : "foo" ,
Username : "username" ,
IconURL : "" ,
Color : "blue" ,
2024-07-30 21:42:06 +02:00
} , h . Metadata )
2024-03-20 15:44:01 +01:00
}