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.

feishu_test.go 6.3KB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2021 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 TestFeishuPayload(t *testing.T) {
  12. t.Run("Create", func(t *testing.T) {
  13. p := createTestPayload()
  14. d := new(FeishuPayload)
  15. pl, err := d.Create(p)
  16. require.NoError(t, err)
  17. require.NotNil(t, pl)
  18. require.IsType(t, &FeishuPayload{}, pl)
  19. assert.Equal(t, `[test/repo] branch test created`, pl.(*FeishuPayload).Content.Text)
  20. })
  21. t.Run("Delete", func(t *testing.T) {
  22. p := deleteTestPayload()
  23. d := new(FeishuPayload)
  24. pl, err := d.Delete(p)
  25. require.NoError(t, err)
  26. require.NotNil(t, pl)
  27. require.IsType(t, &FeishuPayload{}, pl)
  28. assert.Equal(t, `[test/repo] branch test deleted`, pl.(*FeishuPayload).Content.Text)
  29. })
  30. t.Run("Fork", func(t *testing.T) {
  31. p := forkTestPayload()
  32. d := new(FeishuPayload)
  33. pl, err := d.Fork(p)
  34. require.NoError(t, err)
  35. require.NotNil(t, pl)
  36. require.IsType(t, &FeishuPayload{}, pl)
  37. assert.Equal(t, `test/repo2 is forked to test/repo`, pl.(*FeishuPayload).Content.Text)
  38. })
  39. t.Run("Push", func(t *testing.T) {
  40. p := pushTestPayload()
  41. d := new(FeishuPayload)
  42. pl, err := d.Push(p)
  43. require.NoError(t, err)
  44. require.NotNil(t, pl)
  45. require.IsType(t, &FeishuPayload{}, pl)
  46. assert.Equal(t, "[test/repo:test] \r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1\r\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) commit message - user1", pl.(*FeishuPayload).Content.Text)
  47. })
  48. t.Run("Issue", func(t *testing.T) {
  49. p := issueTestPayload()
  50. d := new(FeishuPayload)
  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, &FeishuPayload{}, pl)
  56. assert.Equal(t, "[Issue-test/repo #2]: opened\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body", pl.(*FeishuPayload).Content.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, &FeishuPayload{}, pl)
  62. assert.Equal(t, "[Issue-test/repo #2]: closed\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body", pl.(*FeishuPayload).Content.Text)
  63. })
  64. t.Run("IssueComment", func(t *testing.T) {
  65. p := issueCommentTestPayload()
  66. d := new(FeishuPayload)
  67. pl, err := d.IssueComment(p)
  68. require.NoError(t, err)
  69. require.NotNil(t, pl)
  70. require.IsType(t, &FeishuPayload{}, pl)
  71. assert.Equal(t, "[Comment-test/repo #2]: created\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\n\nmore info needed", pl.(*FeishuPayload).Content.Text)
  72. })
  73. t.Run("PullRequest", func(t *testing.T) {
  74. p := pullRequestTestPayload()
  75. d := new(FeishuPayload)
  76. pl, err := d.PullRequest(p)
  77. require.NoError(t, err)
  78. require.NotNil(t, pl)
  79. require.IsType(t, &FeishuPayload{}, pl)
  80. assert.Equal(t, "[PullRequest-test/repo #12]: opened\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\nAssignees: user1\n\nfixes bug #2", pl.(*FeishuPayload).Content.Text)
  81. })
  82. t.Run("PullRequestComment", func(t *testing.T) {
  83. p := pullRequestCommentTestPayload()
  84. d := new(FeishuPayload)
  85. pl, err := d.IssueComment(p)
  86. require.NoError(t, err)
  87. require.NotNil(t, pl)
  88. require.IsType(t, &FeishuPayload{}, pl)
  89. assert.Equal(t, "[Comment-test/repo #12]: created\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\n\nchanges requested", pl.(*FeishuPayload).Content.Text)
  90. })
  91. t.Run("Review", func(t *testing.T) {
  92. p := pullRequestTestPayload()
  93. p.Action = api.HookIssueReviewed
  94. d := new(FeishuPayload)
  95. pl, err := d.Review(p, webhook_module.HookEventPullRequestReviewApproved)
  96. require.NoError(t, err)
  97. require.NotNil(t, pl)
  98. require.IsType(t, &FeishuPayload{}, pl)
  99. assert.Equal(t, "[test/repo] Pull request review approved : #12 Fix bug\r\n\r\ngood job", pl.(*FeishuPayload).Content.Text)
  100. })
  101. t.Run("Repository", func(t *testing.T) {
  102. p := repositoryTestPayload()
  103. d := new(FeishuPayload)
  104. pl, err := d.Repository(p)
  105. require.NoError(t, err)
  106. require.NotNil(t, pl)
  107. require.IsType(t, &FeishuPayload{}, pl)
  108. assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
  109. })
  110. t.Run("Package", func(t *testing.T) {
  111. p := packageTestPayload()
  112. d := new(FeishuPayload)
  113. pl, err := d.Package(p)
  114. require.NoError(t, err)
  115. require.NotNil(t, pl)
  116. require.IsType(t, &FeishuPayload{}, pl)
  117. assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*FeishuPayload).Content.Text)
  118. })
  119. t.Run("Wiki", func(t *testing.T) {
  120. p := wikiTestPayload()
  121. d := new(FeishuPayload)
  122. p.Action = api.HookWikiCreated
  123. pl, err := d.Wiki(p)
  124. require.NoError(t, err)
  125. require.NotNil(t, pl)
  126. require.IsType(t, &FeishuPayload{}, pl)
  127. assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text)
  128. p.Action = api.HookWikiEdited
  129. pl, err = d.Wiki(p)
  130. require.NoError(t, err)
  131. require.NotNil(t, pl)
  132. require.IsType(t, &FeishuPayload{}, pl)
  133. assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text)
  134. p.Action = api.HookWikiDeleted
  135. pl, err = d.Wiki(p)
  136. require.NoError(t, err)
  137. require.NotNil(t, pl)
  138. require.IsType(t, &FeishuPayload{}, pl)
  139. assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*FeishuPayload).Content.Text)
  140. })
  141. t.Run("Release", func(t *testing.T) {
  142. p := pullReleaseTestPayload()
  143. d := new(FeishuPayload)
  144. pl, err := d.Release(p)
  145. require.NoError(t, err)
  146. require.NotNil(t, pl)
  147. require.IsType(t, &FeishuPayload{}, pl)
  148. assert.Equal(t, "[test/repo] Release created: v1.0 by user1", pl.(*FeishuPayload).Content.Text)
  149. })
  150. }
  151. func TestFeishuJSONPayload(t *testing.T) {
  152. p := pushTestPayload()
  153. pl, err := new(FeishuPayload).Push(p)
  154. require.NoError(t, err)
  155. require.NotNil(t, pl)
  156. require.IsType(t, &FeishuPayload{}, pl)
  157. json, err := pl.JSONPayload()
  158. require.NoError(t, err)
  159. assert.NotEmpty(t, json)
  160. }