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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2021 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. webhook_model "code.gitea.io/gitea/models/webhook"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestFeishuPayload(t *testing.T) {
  13. t.Run("Create", func(t *testing.T) {
  14. p := createTestPayload()
  15. d := new(FeishuPayload)
  16. pl, err := d.Create(p)
  17. require.NoError(t, err)
  18. require.NotNil(t, pl)
  19. require.IsType(t, &FeishuPayload{}, pl)
  20. assert.Equal(t, `[test/repo] branch test created`, pl.(*FeishuPayload).Content.Text)
  21. })
  22. t.Run("Delete", func(t *testing.T) {
  23. p := deleteTestPayload()
  24. d := new(FeishuPayload)
  25. pl, err := d.Delete(p)
  26. require.NoError(t, err)
  27. require.NotNil(t, pl)
  28. require.IsType(t, &FeishuPayload{}, pl)
  29. assert.Equal(t, `[test/repo] branch test deleted`, pl.(*FeishuPayload).Content.Text)
  30. })
  31. t.Run("Fork", func(t *testing.T) {
  32. p := forkTestPayload()
  33. d := new(FeishuPayload)
  34. pl, err := d.Fork(p)
  35. require.NoError(t, err)
  36. require.NotNil(t, pl)
  37. require.IsType(t, &FeishuPayload{}, pl)
  38. assert.Equal(t, `test/repo2 is forked to test/repo`, pl.(*FeishuPayload).Content.Text)
  39. })
  40. t.Run("Push", func(t *testing.T) {
  41. p := pushTestPayload()
  42. d := new(FeishuPayload)
  43. pl, err := d.Push(p)
  44. require.NoError(t, err)
  45. require.NotNil(t, pl)
  46. require.IsType(t, &FeishuPayload{}, pl)
  47. 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)
  48. })
  49. t.Run("Issue", func(t *testing.T) {
  50. p := issueTestPayload()
  51. d := new(FeishuPayload)
  52. p.Action = api.HookIssueOpened
  53. pl, err := d.Issue(p)
  54. require.NoError(t, err)
  55. require.NotNil(t, pl)
  56. require.IsType(t, &FeishuPayload{}, pl)
  57. assert.Equal(t, "#2 crash\r\n[test/repo] Issue opened: #2 crash by user1\r\n\r\nissue body", pl.(*FeishuPayload).Content.Text)
  58. p.Action = api.HookIssueClosed
  59. pl, err = d.Issue(p)
  60. require.NoError(t, err)
  61. require.NotNil(t, pl)
  62. require.IsType(t, &FeishuPayload{}, pl)
  63. assert.Equal(t, "#2 crash\r\n[test/repo] Issue closed: #2 crash by user1", pl.(*FeishuPayload).Content.Text)
  64. })
  65. t.Run("IssueComment", func(t *testing.T) {
  66. p := issueCommentTestPayload()
  67. d := new(FeishuPayload)
  68. pl, err := d.IssueComment(p)
  69. require.NoError(t, err)
  70. require.NotNil(t, pl)
  71. require.IsType(t, &FeishuPayload{}, pl)
  72. assert.Equal(t, "#2 crash\r\n[test/repo] New comment on issue #2 crash by user1\r\n\r\nmore info needed", pl.(*FeishuPayload).Content.Text)
  73. })
  74. t.Run("PullRequest", func(t *testing.T) {
  75. p := pullRequestTestPayload()
  76. d := new(FeishuPayload)
  77. pl, err := d.PullRequest(p)
  78. require.NoError(t, err)
  79. require.NotNil(t, pl)
  80. require.IsType(t, &FeishuPayload{}, pl)
  81. assert.Equal(t, "#12 Fix bug\r\n[test/repo] Pull request opened: #12 Fix bug by user1\r\n\r\nfixes bug #2", pl.(*FeishuPayload).Content.Text)
  82. })
  83. t.Run("PullRequestComment", func(t *testing.T) {
  84. p := pullRequestCommentTestPayload()
  85. d := new(FeishuPayload)
  86. pl, err := d.IssueComment(p)
  87. require.NoError(t, err)
  88. require.NotNil(t, pl)
  89. require.IsType(t, &FeishuPayload{}, pl)
  90. assert.Equal(t, "#12 Fix bug\r\n[test/repo] New comment on pull request #12 Fix bug by user1\r\n\r\nchanges requested", pl.(*FeishuPayload).Content.Text)
  91. })
  92. t.Run("Review", func(t *testing.T) {
  93. p := pullRequestTestPayload()
  94. p.Action = api.HookIssueReviewed
  95. d := new(FeishuPayload)
  96. pl, err := d.Review(p, webhook_model.HookEventPullRequestReviewApproved)
  97. require.NoError(t, err)
  98. require.NotNil(t, pl)
  99. require.IsType(t, &FeishuPayload{}, pl)
  100. assert.Equal(t, "[test/repo] Pull request review approved : #12 Fix bug\r\n\r\ngood job", pl.(*FeishuPayload).Content.Text)
  101. })
  102. t.Run("Repository", func(t *testing.T) {
  103. p := repositoryTestPayload()
  104. d := new(FeishuPayload)
  105. pl, err := d.Repository(p)
  106. require.NoError(t, err)
  107. require.NotNil(t, pl)
  108. require.IsType(t, &FeishuPayload{}, pl)
  109. assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
  110. })
  111. t.Run("Release", func(t *testing.T) {
  112. p := pullReleaseTestPayload()
  113. d := new(FeishuPayload)
  114. pl, err := d.Release(p)
  115. require.NoError(t, err)
  116. require.NotNil(t, pl)
  117. require.IsType(t, &FeishuPayload{}, pl)
  118. assert.Equal(t, "[test/repo] Release created: v1.0 by user1", pl.(*FeishuPayload).Content.Text)
  119. })
  120. }
  121. func TestFeishuJSONPayload(t *testing.T) {
  122. p := pushTestPayload()
  123. pl, err := new(FeishuPayload).Push(p)
  124. require.NoError(t, err)
  125. require.NotNil(t, pl)
  126. require.IsType(t, &FeishuPayload{}, pl)
  127. json, err := pl.JSONPayload()
  128. require.NoError(t, err)
  129. assert.NotEmpty(t, json)
  130. }