2020-03-28 22:09:55 +09:00
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package webhook
import (
"testing"
"code.gitea.io/gitea/models"
api "code.gitea.io/gitea/modules/structs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatrixIssuesPayloadOpened ( t * testing . T ) {
p := issueTestPayload ( )
2020-09-05 11:57:13 +09:00
m := new ( MatrixPayloadUnsafe )
2020-03-28 22:09:55 +09:00
p . Action = api . HookIssueOpened
2020-09-05 11:57:13 +09:00
pl , err := m . Issue ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09: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 . ( * MatrixPayloadUnsafe ) . 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 . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
p . Action = api . HookIssueClosed
2020-09-05 11:57:13 +09:00
pl , err = m . Issue ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09: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 . ( * MatrixPayloadUnsafe ) . 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 . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
}
func TestMatrixIssueCommentPayload ( t * testing . T ) {
p := issueCommentTestPayload ( )
2020-09-05 11:57:13 +09:00
m := new ( MatrixPayloadUnsafe )
2020-03-28 22:09:55 +09:00
2020-09-05 11:57:13 +09:00
pl , err := m . IssueComment ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09: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 . ( * MatrixPayloadUnsafe ) . 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 . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
}
func TestMatrixPullRequestCommentPayload ( t * testing . T ) {
p := pullRequestCommentTestPayload ( )
2020-09-05 11:57:13 +09:00
m := new ( MatrixPayloadUnsafe )
2020-03-28 22:09:55 +09:00
2020-09-05 11:57:13 +09:00
pl , err := m . IssueComment ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#2 Fix bug](http://localhost:3000/test/repo/pulls/2) by [user1](https://try.gitea.io/user1)" , pl . ( * MatrixPayloadUnsafe ) . 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/2\">#2 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>" , pl . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
}
func TestMatrixReleasePayload ( t * testing . T ) {
p := pullReleaseTestPayload ( )
2020-09-05 11:57:13 +09:00
m := new ( MatrixPayloadUnsafe )
2020-03-28 22:09:55 +09:00
2020-09-05 11:57:13 +09:00
pl , err := m . Release ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/src/v1.0) by [user1](https://try.gitea.io/user1)" , pl . ( * MatrixPayloadUnsafe ) . Body )
assert . Equal ( t , "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Release created: <a href=\"http://localhost:3000/test/repo/src/v1.0\">v1.0</a> by <a href=\"https://try.gitea.io/user1\">user1</a>" , pl . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
}
func TestMatrixPullRequestPayload ( t * testing . T ) {
p := pullRequestTestPayload ( )
2020-09-05 11:57:13 +09:00
m := new ( MatrixPayloadUnsafe )
2020-03-28 22:09:55 +09:00
2020-09-05 11:57:13 +09:00
pl , err := m . PullRequest ( p )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , pl )
2020-09-05 11:57:13 +09:00
assert . Equal ( t , "[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#2 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)" , pl . ( * MatrixPayloadUnsafe ) . 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\">#2 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>" , pl . ( * MatrixPayloadUnsafe ) . FormattedBody )
2020-03-28 22:09:55 +09:00
}
func TestMatrixHookRequest ( t * testing . T ) {
h := & models . HookTask {
PayloadContent : ` {
"body" : "[[user1/test](http://localhost:3000/user1/test)] user1 pushed 1 commit to [master](http://localhost:3000/user1/test/src/branch/master):\n[5175ef2](http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee): Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n - user1" ,
"msgtype" : "m.notice" ,
"format" : "org.matrix.custom.html" ,
"formatted_body" : "[\u003ca href=\"http://localhost:3000/user1/test\"\u003euser1/test\u003c/a\u003e] user1 pushed 1 commit to \u003ca href=\"http://localhost:3000/user1/test/src/branch/master\"\u003emaster\u003c/a\u003e:\u003cbr\u003e\u003ca href=\"http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee\"\u003e5175ef2\u003c/a\u003e: Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n - user1" ,
"io.gitea.commits" : [
{
"id" : "5175ef26201c58b035a3404b3fe02b4e8d436eee" ,
"message" : "Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n" ,
"url" : "http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee" ,
"author" : {
"name" : "user1" ,
"email" : "user@mail.com" ,
"username" : ""
} ,
"committer" : {
"name" : "user1" ,
"email" : "user@mail.com" ,
"username" : ""
} ,
"verification" : null ,
"timestamp" : "0001-01-01T00:00:00Z" ,
"added" : null ,
"removed" : null ,
"modified" : null
}
] ,
"access_token" : "dummy_access_token"
} ` ,
}
wantPayloadContent := ` {
"body" : "[[user1/test](http://localhost:3000/user1/test)] user1 pushed 1 commit to [master](http://localhost:3000/user1/test/src/branch/master):\n[5175ef2](http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee): Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n - user1" ,
"msgtype" : "m.notice" ,
"format" : "org.matrix.custom.html" ,
"formatted_body" : "[\u003ca href=\"http://localhost:3000/user1/test\"\u003euser1/test\u003c/a\u003e] user1 pushed 1 commit to \u003ca href=\"http://localhost:3000/user1/test/src/branch/master\"\u003emaster\u003c/a\u003e:\u003cbr\u003e\u003ca href=\"http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee\"\u003e5175ef2\u003c/a\u003e: Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n - user1" ,
"io.gitea.commits" : [
{
"id" : "5175ef26201c58b035a3404b3fe02b4e8d436eee" ,
"message" : "Merge pull request 'Change readme.md' (#2) from add-matrix-webhook into master\n\nReviewed-on: http://localhost:3000/user1/test/pulls/2\n" ,
"url" : "http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee" ,
"author" : {
"name" : "user1" ,
"email" : "user@mail.com" ,
"username" : ""
} ,
"committer" : {
"name" : "user1" ,
"email" : "user@mail.com" ,
"username" : ""
} ,
"verification" : null ,
"timestamp" : "0001-01-01T00:00:00Z" ,
"added" : null ,
"removed" : null ,
"modified" : null
}
]
} `
req , err := getMatrixHookRequest ( h )
2020-07-19 18:53:40 +09:00
require . NoError ( t , err )
2020-03-28 22:09:55 +09:00
require . NotNil ( t , req )
assert . Equal ( t , "Bearer dummy_access_token" , req . Header . Get ( "Authorization" ) )
assert . Equal ( t , wantPayloadContent , h . PayloadContent )
}
2020-07-31 07:04:19 +09: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 )
} )
}
}