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.

slack_test.go 6.9KB

Webhook for Wiki changes (#20219) Add support for triggering webhook notifications on wiki changes. This PR contains frontend and backend for webhook notifications on wiki actions (create a new page, rename a page, edit a page and delete a page). The frontend got a new checkbox under the Custom Event -> Repository Events section. There is only one checkbox for create/edit/rename/delete actions, because it makes no sense to separate it and others like releases or packages follow the same schema. ![image](https://user-images.githubusercontent.com/121972/177018803-26851196-831f-4fde-9a4c-9e639b0e0d6b.png) The actions itself are separated, so that different notifications will be executed (with the "action" field). All the webhook receivers implement the new interface method (Wiki) and the corresponding tests. When implementing this, I encounter a little bug on editing a wiki page. Creating and editing a wiki page is technically the same action and will be handled by the ```updateWikiPage``` function. But the function need to know if it is a new wiki page or just a change. This distinction is done by the ```action``` parameter, but this will not be sent by the frontend (on form submit). This PR will fix this by adding the ```action``` parameter with the values ```_new``` or ```_edit```, which will be used by the ```updateWikiPage``` function. I've done integration tests with matrix and gitea (http). ![image](https://user-images.githubusercontent.com/121972/177018795-eb5cdc01-9ba3-483e-a6b7-ed0e313a71fb.png) Fix #16457 Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "testing"
  6. api "code.gitea.io/gitea/modules/structs"
  7. webhook_module "code.gitea.io/gitea/modules/webhook"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestSlackPayload(t *testing.T) {
  12. t.Run("Create", func(t *testing.T) {
  13. p := createTestPayload()
  14. d := new(SlackPayload)
  15. pl, err := d.Create(p)
  16. require.NoError(t, err)
  17. require.NotNil(t, pl)
  18. require.IsType(t, &SlackPayload{}, pl)
  19. assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1", pl.(*SlackPayload).Text)
  20. })
  21. t.Run("Delete", func(t *testing.T) {
  22. p := deleteTestPayload()
  23. d := new(SlackPayload)
  24. pl, err := d.Delete(p)
  25. require.NoError(t, err)
  26. require.NotNil(t, pl)
  27. require.IsType(t, &SlackPayload{}, pl)
  28. assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1", pl.(*SlackPayload).Text)
  29. })
  30. t.Run("Fork", func(t *testing.T) {
  31. p := forkTestPayload()
  32. d := new(SlackPayload)
  33. pl, err := d.Fork(p)
  34. require.NoError(t, err)
  35. require.NotNil(t, pl)
  36. require.IsType(t, &SlackPayload{}, pl)
  37. assert.Equal(t, "<http://localhost:3000/test/repo2|test/repo2> is forked to <http://localhost:3000/test/repo|test/repo>", pl.(*SlackPayload).Text)
  38. })
  39. t.Run("Push", func(t *testing.T) {
  40. p := pushTestPayload()
  41. d := new(SlackPayload)
  42. pl, err := d.Push(p)
  43. require.NoError(t, err)
  44. require.NotNil(t, pl)
  45. require.IsType(t, &SlackPayload{}, pl)
  46. 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.(*SlackPayload).Text)
  47. })
  48. t.Run("Issue", func(t *testing.T) {
  49. p := issueTestPayload()
  50. d := new(SlackPayload)
  51. p.Action = api.HookIssueOpened
  52. pl, err := d.Issue(p)
  53. require.NoError(t, err)
  54. require.NotNil(t, pl)
  55. require.IsType(t, &SlackPayload{}, pl)
  56. 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.(*SlackPayload).Text)
  57. p.Action = api.HookIssueClosed
  58. pl, err = d.Issue(p)
  59. require.NoError(t, err)
  60. require.NotNil(t, pl)
  61. require.IsType(t, &SlackPayload{}, pl)
  62. 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.(*SlackPayload).Text)
  63. })
  64. t.Run("IssueComment", func(t *testing.T) {
  65. p := issueCommentTestPayload()
  66. d := new(SlackPayload)
  67. pl, err := d.IssueComment(p)
  68. require.NoError(t, err)
  69. require.NotNil(t, pl)
  70. require.IsType(t, &SlackPayload{}, pl)
  71. 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.(*SlackPayload).Text)
  72. })
  73. t.Run("PullRequest", func(t *testing.T) {
  74. p := pullRequestTestPayload()
  75. d := new(SlackPayload)
  76. pl, err := d.PullRequest(p)
  77. require.NoError(t, err)
  78. require.NotNil(t, pl)
  79. require.IsType(t, &SlackPayload{}, pl)
  80. 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.(*SlackPayload).Text)
  81. })
  82. t.Run("PullRequestComment", func(t *testing.T) {
  83. p := pullRequestCommentTestPayload()
  84. d := new(SlackPayload)
  85. pl, err := d.IssueComment(p)
  86. require.NoError(t, err)
  87. require.NotNil(t, pl)
  88. require.IsType(t, &SlackPayload{}, pl)
  89. 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.(*SlackPayload).Text)
  90. })
  91. t.Run("Review", func(t *testing.T) {
  92. p := pullRequestTestPayload()
  93. p.Action = api.HookIssueReviewed
  94. d := new(SlackPayload)
  95. pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved)
  96. require.NoError(t, err)
  97. require.NotNil(t, pl)
  98. require.IsType(t, &SlackPayload{}, pl)
  99. 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.(*SlackPayload).Text)
  100. })
  101. t.Run("Repository", func(t *testing.T) {
  102. p := repositoryTestPayload()
  103. d := new(SlackPayload)
  104. pl, err := d.Repository(p)
  105. require.NoError(t, err)
  106. require.NotNil(t, pl)
  107. require.IsType(t, &SlackPayload{}, pl)
  108. assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
  109. })
  110. t.Run("Wiki", func(t *testing.T) {
  111. p := wikiTestPayload()
  112. d := new(SlackPayload)
  113. p.Action = api.HookWikiCreated
  114. pl, err := d.Wiki(p)
  115. require.NoError(t, err)
  116. require.NotNil(t, pl)
  117. require.IsType(t, &SlackPayload{}, pl)
  118. 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.(*SlackPayload).Text)
  119. p.Action = api.HookWikiEdited
  120. pl, err = d.Wiki(p)
  121. require.NoError(t, err)
  122. require.NotNil(t, pl)
  123. require.IsType(t, &SlackPayload{}, pl)
  124. 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.(*SlackPayload).Text)
  125. p.Action = api.HookWikiDeleted
  126. pl, err = d.Wiki(p)
  127. require.NoError(t, err)
  128. require.NotNil(t, pl)
  129. require.IsType(t, &SlackPayload{}, pl)
  130. 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.(*SlackPayload).Text)
  131. })
  132. t.Run("Release", func(t *testing.T) {
  133. p := pullReleaseTestPayload()
  134. d := new(SlackPayload)
  135. pl, err := d.Release(p)
  136. require.NoError(t, err)
  137. require.NotNil(t, pl)
  138. require.IsType(t, &SlackPayload{}, pl)
  139. 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.(*SlackPayload).Text)
  140. })
  141. }
  142. func TestSlackJSONPayload(t *testing.T) {
  143. p := pushTestPayload()
  144. pl, err := new(SlackPayload).Push(p)
  145. require.NoError(t, err)
  146. require.NotNil(t, pl)
  147. require.IsType(t, &SlackPayload{}, pl)
  148. json, err := pl.JSONPayload()
  149. require.NoError(t, err)
  150. assert.NotEmpty(t, json)
  151. }
  152. func TestIsValidSlackChannel(t *testing.T) {
  153. tt := []struct {
  154. channelName string
  155. expected bool
  156. }{
  157. {"gitea", true},
  158. {"#gitea", true},
  159. {" ", false},
  160. {"#", false},
  161. {" #", false},
  162. {"gitea ", false},
  163. {" gitea", false},
  164. }
  165. for _, v := range tt {
  166. assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
  167. }
  168. }