gitea源码

telegram_test.go 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "testing"
  6. webhook_model "code.gitea.io/gitea/models/webhook"
  7. "code.gitea.io/gitea/modules/json"
  8. api "code.gitea.io/gitea/modules/structs"
  9. webhook_module "code.gitea.io/gitea/modules/webhook"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. )
  13. func TestTelegramPayload(t *testing.T) {
  14. tc := telegramConvertor{}
  15. t.Run("Correct webhook params", func(t *testing.T) {
  16. p := createTelegramPayloadHTML(`<a href=".">testMsg</a> <bad>`)
  17. assert.Equal(t, TelegramPayload{
  18. Message: `<a href="." rel="nofollow">testMsg</a>`,
  19. ParseMode: "HTML",
  20. DisableWebPreview: true,
  21. }, p)
  22. })
  23. t.Run("Create", func(t *testing.T) {
  24. p := createTestPayload()
  25. pl, err := tc.Create(p)
  26. require.NoError(t, err)
  27. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> created`, pl.Message)
  28. })
  29. t.Run("Delete", func(t *testing.T) {
  30. p := deleteTestPayload()
  31. pl, err := tc.Delete(p)
  32. require.NoError(t, err)
  33. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> deleted`, pl.Message)
  34. })
  35. t.Run("Fork", func(t *testing.T) {
  36. p := forkTestPayload()
  37. pl, err := tc.Fork(p)
  38. require.NoError(t, err)
  39. assert.Equal(t, `test/repo2 is forked to <a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>`, pl.Message)
  40. })
  41. t.Run("Push", func(t *testing.T) {
  42. p := pushTestPayload()
  43. pl, err := tc.Push(p)
  44. require.NoError(t, err)
  45. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
  46. [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
  47. [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, pl.Message)
  48. })
  49. t.Run("Issue", func(t *testing.T) {
  50. p := issueTestPayload()
  51. p.Action = api.HookIssueOpened
  52. pl, err := tc.Issue(p)
  53. require.NoError(t, err)
  54. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
  55. issue body`, pl.Message)
  56. p.Action = api.HookIssueClosed
  57. pl, err = tc.Issue(p)
  58. require.NoError(t, err)
  59. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  60. })
  61. t.Run("IssueComment", func(t *testing.T) {
  62. p := issueCommentTestPayload()
  63. pl, err := tc.IssueComment(p)
  64. require.NoError(t, err)
  65. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
  66. more info needed`, pl.Message)
  67. })
  68. t.Run("PullRequest", func(t *testing.T) {
  69. p := pullRequestTestPayload()
  70. pl, err := tc.PullRequest(p)
  71. require.NoError(t, err)
  72. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
  73. fixes bug #2`, pl.Message)
  74. })
  75. t.Run("PullRequestComment", func(t *testing.T) {
  76. p := pullRequestCommentTestPayload()
  77. pl, err := tc.IssueComment(p)
  78. require.NoError(t, err)
  79. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
  80. changes requested`, pl.Message)
  81. })
  82. t.Run("Review", func(t *testing.T) {
  83. p := pullRequestTestPayload()
  84. p.Action = api.HookIssueReviewed
  85. pl, err := tc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
  86. require.NoError(t, err)
  87. assert.Equal(t, `[test/repo] Pull request review approved: #12 Fix bug
  88. good job`, pl.Message)
  89. })
  90. t.Run("Repository", func(t *testing.T) {
  91. p := repositoryTestPayload()
  92. pl, err := tc.Repository(p)
  93. require.NoError(t, err)
  94. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Repository created`, pl.Message)
  95. })
  96. t.Run("Package", func(t *testing.T) {
  97. p := packageTestPayload()
  98. pl, err := tc.Package(p)
  99. require.NoError(t, err)
  100. assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest" rel="nofollow">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  101. })
  102. t.Run("Wiki", func(t *testing.T) {
  103. p := wikiTestPayload()
  104. p.Action = api.HookWikiCreated
  105. pl, err := tc.Wiki(p)
  106. require.NoError(t, err)
  107. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  108. p.Action = api.HookWikiEdited
  109. pl, err = tc.Wiki(p)
  110. require.NoError(t, err)
  111. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; edited (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  112. p.Action = api.HookWikiDeleted
  113. pl, err = tc.Wiki(p)
  114. require.NoError(t, err)
  115. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; deleted by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  116. })
  117. t.Run("Release", func(t *testing.T) {
  118. p := pullReleaseTestPayload()
  119. pl, err := tc.Release(p)
  120. require.NoError(t, err)
  121. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0" rel="nofollow">v1.0</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
  122. })
  123. }
  124. func TestTelegramJSONPayload(t *testing.T) {
  125. p := pushTestPayload()
  126. data, err := p.JSONPayload()
  127. require.NoError(t, err)
  128. hook := &webhook_model.Webhook{
  129. RepoID: 3,
  130. IsActive: true,
  131. Type: webhook_module.TELEGRAM,
  132. URL: "https://telegram.example.com/",
  133. Meta: ``,
  134. HTTPMethod: "POST",
  135. }
  136. task := &webhook_model.HookTask{
  137. HookID: hook.ID,
  138. EventType: webhook_module.HookEventPush,
  139. PayloadContent: string(data),
  140. PayloadVersion: 2,
  141. }
  142. req, reqBody, err := newTelegramRequest(t.Context(), hook, task)
  143. require.NotNil(t, req)
  144. require.NotNil(t, reqBody)
  145. require.NoError(t, err)
  146. assert.Equal(t, "POST", req.Method)
  147. assert.Equal(t, "https://telegram.example.com/", req.URL.String())
  148. assert.Equal(t, "sha256=", req.Header.Get("X-Hub-Signature-256"))
  149. assert.Equal(t, "application/json", req.Header.Get("Content-Type"))
  150. var body TelegramPayload
  151. err = json.NewDecoder(req.Body).Decode(&body)
  152. assert.NoError(t, err)
  153. assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
  154. [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
  155. [<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, body.Message)
  156. }