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.

156 lines
7.6 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. sl := &MatrixMeta{}
  15. p.Action = api.HookIssueOpened
  16. pl, err := getMatrixIssuesPayload(p, sl)
  17. require.Nil(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.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.FormattedBody)
  21. p.Action = api.HookIssueClosed
  22. pl, err = getMatrixIssuesPayload(p, sl)
  23. require.Nil(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.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.FormattedBody)
  27. }
  28. func TestMatrixIssueCommentPayload(t *testing.T) {
  29. p := issueCommentTestPayload()
  30. sl := &MatrixMeta{}
  31. pl, err := getMatrixIssueCommentPayload(p, sl)
  32. require.Nil(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.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.FormattedBody)
  36. }
  37. func TestMatrixPullRequestCommentPayload(t *testing.T) {
  38. p := pullRequestCommentTestPayload()
  39. sl := &MatrixMeta{}
  40. pl, err := getMatrixIssueCommentPayload(p, sl)
  41. require.Nil(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.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.FormattedBody)
  45. }
  46. func TestMatrixReleasePayload(t *testing.T) {
  47. p := pullReleaseTestPayload()
  48. sl := &MatrixMeta{}
  49. pl, err := getMatrixReleasePayload(p, sl)
  50. require.Nil(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.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.FormattedBody)
  54. }
  55. func TestMatrixPullRequestPayload(t *testing.T) {
  56. p := pullRequestTestPayload()
  57. sl := &MatrixMeta{}
  58. pl, err := getMatrixPullRequestPayload(p, sl)
  59. require.Nil(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.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.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.Nil(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. }