You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
8.4 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package webhook
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestMatrixIssuesPayloadOpened(t *testing.T) {
  13. p := issueTestPayload()
  14. m := new(MatrixPayloadUnsafe)
  15. p.Action = api.HookIssueOpened
  16. pl, err := m.Issue(p)
  17. require.NoError(t, err)
  18. require.NotNil(t, pl)
  19. 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)
  20. 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)
  21. p.Action = api.HookIssueClosed
  22. pl, err = m.Issue(p)
  23. require.NoError(t, err)
  24. require.NotNil(t, pl)
  25. 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)
  26. 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)
  27. }
  28. func TestMatrixIssueCommentPayload(t *testing.T) {
  29. p := issueCommentTestPayload()
  30. m := new(MatrixPayloadUnsafe)
  31. pl, err := m.IssueComment(p)
  32. require.NoError(t, err)
  33. require.NotNil(t, pl)
  34. 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)
  35. 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)
  36. }
  37. func TestMatrixPullRequestCommentPayload(t *testing.T) {
  38. p := pullRequestCommentTestPayload()
  39. m := new(MatrixPayloadUnsafe)
  40. pl, err := m.IssueComment(p)
  41. require.NoError(t, err)
  42. require.NotNil(t, pl)
  43. 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)
  44. 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)
  45. }
  46. func TestMatrixReleasePayload(t *testing.T) {
  47. p := pullReleaseTestPayload()
  48. m := new(MatrixPayloadUnsafe)
  49. pl, err := m.Release(p)
  50. require.NoError(t, err)
  51. require.NotNil(t, pl)
  52. 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)
  53. 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)
  54. }
  55. func TestMatrixPullRequestPayload(t *testing.T) {
  56. p := pullRequestTestPayload()
  57. m := new(MatrixPayloadUnsafe)
  58. pl, err := m.PullRequest(p)
  59. require.NoError(t, err)
  60. require.NotNil(t, pl)
  61. 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)
  62. 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)
  63. }
  64. func TestMatrixHookRequest(t *testing.T) {
  65. h := &models.HookTask{
  66. PayloadContent: `{
  67. "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",
  68. "msgtype": "m.notice",
  69. "format": "org.matrix.custom.html",
  70. "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",
  71. "io.gitea.commits": [
  72. {
  73. "id": "5175ef26201c58b035a3404b3fe02b4e8d436eee",
  74. "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",
  75. "url": "http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee",
  76. "author": {
  77. "name": "user1",
  78. "email": "user@mail.com",
  79. "username": ""
  80. },
  81. "committer": {
  82. "name": "user1",
  83. "email": "user@mail.com",
  84. "username": ""
  85. },
  86. "verification": null,
  87. "timestamp": "0001-01-01T00:00:00Z",
  88. "added": null,
  89. "removed": null,
  90. "modified": null
  91. }
  92. ],
  93. "access_token": "dummy_access_token"
  94. }`,
  95. }
  96. wantPayloadContent := `{
  97. "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",
  98. "msgtype": "m.notice",
  99. "format": "org.matrix.custom.html",
  100. "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",
  101. "io.gitea.commits": [
  102. {
  103. "id": "5175ef26201c58b035a3404b3fe02b4e8d436eee",
  104. "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",
  105. "url": "http://localhost:3000/user1/test/commit/5175ef26201c58b035a3404b3fe02b4e8d436eee",
  106. "author": {
  107. "name": "user1",
  108. "email": "user@mail.com",
  109. "username": ""
  110. },
  111. "committer": {
  112. "name": "user1",
  113. "email": "user@mail.com",
  114. "username": ""
  115. },
  116. "verification": null,
  117. "timestamp": "0001-01-01T00:00:00Z",
  118. "added": null,
  119. "removed": null,
  120. "modified": null
  121. }
  122. ]
  123. }`
  124. req, err := getMatrixHookRequest(h)
  125. require.NoError(t, err)
  126. require.NotNil(t, req)
  127. assert.Equal(t, "Bearer dummy_access_token", req.Header.Get("Authorization"))
  128. assert.Equal(t, wantPayloadContent, h.PayloadContent)
  129. }
  130. func Test_getTxnID(t *testing.T) {
  131. type args struct {
  132. payload []byte
  133. }
  134. tests := []struct {
  135. name string
  136. args args
  137. want string
  138. wantErr bool
  139. }{
  140. {
  141. name: "dummy payload",
  142. args: args{payload: []byte("Hello World")},
  143. want: "0a4d55a8d778e5022fab701977c5d840bbc486d0",
  144. wantErr: false,
  145. },
  146. }
  147. for _, tt := range tests {
  148. t.Run(tt.name, func(t *testing.T) {
  149. got, err := getMatrixTxnID(tt.args.payload)
  150. if (err != nil) != tt.wantErr {
  151. t.Errorf("getMatrixTxnID() error = %v, wantErr %v", err, tt.wantErr)
  152. return
  153. }
  154. assert.Equal(t, tt.want, got)
  155. })
  156. }
  157. }