2020-03-28 13:09:55 +00:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2020-03-28 13:09:55 +00:00
package webhook
import (
2024-03-07 22:18:38 +00:00
"context"
2020-03-28 13:09:55 +00:00
"testing"
2024-03-07 22:18:38 +00:00
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/json"
2020-03-28 13:09:55 +00:00
api "code.gitea.io/gitea/modules/structs"
2023-01-01 15:23:15 +00:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2020-03-28 13:09:55 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2021-06-21 02:12:19 +00:00
func TestMatrixPayload ( t * testing . T ) {
2024-03-07 22:18:38 +00:00
mc := matrixConvertor {
MsgType : "m.text" ,
}
2021-06-21 02:12:19 +00:00
t . Run ( "Create" , func ( t * testing . T ) {
p := createTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Create ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1 ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Delete" , func ( t * testing . T ) {
p := deleteTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Delete ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1 ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Fork" , func ( t * testing . T ) {
p := forkTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Fork ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)" , pl . Body )
assert . Equal ( t , ` <a href="http://localhost:3000/test/repo2">test/repo2</a> is forked to <a href="http://localhost:3000/test/repo">test/repo</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Push" , func ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Push ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] user1 pushed 2 commits to <a href="http://localhost:3000/test/repo/src/branch/test">test</a>:<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1<br><a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778">2020558</a>: commit message - user1 ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Issue" , func ( t * testing . T ) {
p := issueTestPayload ( )
p . Action = api . HookIssueOpened
2024-03-07 22:18:38 +00:00
pl , err := mc . Issue ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
p . Action = api . HookIssueClosed
2024-03-07 22:18:38 +00:00
pl , err = mc . Issue ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "IssueComment" , func ( t * testing . T ) {
p := issueCommentTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . IssueComment ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "PullRequest" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . PullRequest ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "PullRequestComment" , func ( t * testing . T ) {
p := pullRequestCommentTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . IssueComment ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Review" , func ( t * testing . T ) {
p := pullRequestTestPayload ( )
p . Action = api . HookIssueReviewed
2024-03-07 22:18:38 +00:00
pl , err := mc . Review ( p , webhook_module . HookEventPullRequestReviewApproved )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Pull request review approved: <a href="http://localhost:3000/test/repo/pulls/12">#12 Fix bug</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
t . Run ( "Repository" , func ( t * testing . T ) {
p := repositoryTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Repository ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , ` [[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1) ` , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
2023-11-17 11:17:33 +00:00
t . Run ( "Package" , func ( t * testing . T ) {
p := packageTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Package ( p )
2023-11-17 11:17:33 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , ` [[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1) ` , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2023-11-17 11:17:33 +00:00
} )
2022-09-04 19:54:23 +00:00
t . Run ( "Wiki" , func ( t * testing . T ) {
p := wikiTestPayload ( )
p . Action = api . HookWikiCreated
2024-03-07 22:18:38 +00:00
pl , err := mc . Wiki ( p )
2022-09-04 19:54:23 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2022-09-04 19:54:23 +00:00
p . Action = api . HookWikiEdited
2024-03-07 22:18:38 +00:00
pl , err = mc . Wiki ( p )
2022-09-04 19:54:23 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2022-09-04 19:54:23 +00:00
p . Action = api . HookWikiDeleted
2024-03-07 22:18:38 +00:00
pl , err = mc . Wiki ( p )
2022-09-04 19:54:23 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2022-09-04 19:54:23 +00:00
} )
2021-06-21 02:12:19 +00:00
t . Run ( "Release" , func ( t * testing . T ) {
p := pullReleaseTestPayload ( )
2024-03-07 22:18:38 +00:00
pl , err := mc . Release ( p )
2021-06-21 02:12:19 +00:00
require . NoError ( t , err )
require . NotNil ( t , pl )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by [user1](https://try.gitea.io/user1)" , pl . Body )
assert . Equal ( t , ` [<a href="http://localhost:3000/test/repo">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0">v1.0</a> by <a href="https://try.gitea.io/user1">user1</a> ` , pl . FormattedBody )
2021-06-21 02:12:19 +00:00
} )
2020-03-28 13:09:55 +00:00
}
2021-06-21 02:12:19 +00:00
func TestMatrixJSONPayload ( t * testing . T ) {
p := pushTestPayload ( )
2024-03-07 22:18:38 +00:00
data , err := p . JSONPayload ( )
2020-07-19 09:53:40 +00:00
require . NoError ( t , err )
2020-03-28 13:09:55 +00:00
2024-03-07 22:18:38 +00:00
hook := & webhook_model . Webhook {
RepoID : 3 ,
IsActive : true ,
Type : webhook_module . MATRIX ,
2024-04-12 07:53:12 +00:00
URL : "https://matrix.example.com/_matrix/client/v3/rooms/ROOM_ID/send/m.room.message" ,
2024-03-07 22:18:38 +00:00
Meta : ` { "message_type":0} ` , // text
}
task := & webhook_model . HookTask {
HookID : hook . ID ,
EventType : webhook_module . HookEventPush ,
PayloadContent : string ( data ) ,
PayloadVersion : 2 ,
}
2024-03-20 14:44:01 +00:00
req , reqBody , err := matrixHandler { } . NewRequest ( context . Background ( ) , hook , task )
2024-03-07 22:18:38 +00:00
require . NotNil ( t , req )
require . NotNil ( t , reqBody )
2020-07-19 09:53:40 +00:00
require . NoError ( t , err )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "PUT" , req . Method )
2024-04-12 07:53:12 +00:00
assert . Equal ( t , "/_matrix/client/v3/rooms/ROOM_ID/send/m.room.message/6db5dc1e282529a8c162c7fe93dd2667494eeb51" , req . URL . Path )
2024-03-07 22:18:38 +00:00
assert . Equal ( t , "application/json" , req . Header . Get ( "Content-Type" ) )
var body MatrixPayload
err = json . NewDecoder ( req . Body ) . Decode ( & body )
assert . NoError ( t , err )
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] user1 pushed 2 commits to [test](http://localhost:3000/test/repo/src/branch/test):\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778): commit message - user1" , body . Body )
2020-03-28 13:09:55 +00:00
}
2020-07-30 22:04:19 +00:00
func Test_getTxnID ( t * testing . T ) {
type args struct {
payload [ ] byte
}
tests := [ ] struct {
name string
args args
want string
wantErr bool
} {
{
name : "dummy payload" ,
args : args { payload : [ ] byte ( "Hello World" ) } ,
want : "0a4d55a8d778e5022fab701977c5d840bbc486d0" ,
wantErr : false ,
} ,
}
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
got , err := getMatrixTxnID ( tt . args . payload )
if ( err != nil ) != tt . wantErr {
t . Errorf ( "getMatrixTxnID() error = %v, wantErr %v" , err , tt . wantErr )
return
}
assert . Equal ( t , tt . want , got )
} )
}
}