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.

243 lines
6.9 KiB

  1. // Copyright 2017 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 models
  5. import (
  6. "encoding/json"
  7. "testing"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestHookContentType_Name(t *testing.T) {
  12. assert.Equal(t, "json", ContentTypeJSON.Name())
  13. assert.Equal(t, "form", ContentTypeForm.Name())
  14. }
  15. func TestIsValidHookContentType(t *testing.T) {
  16. assert.True(t, IsValidHookContentType("json"))
  17. assert.True(t, IsValidHookContentType("form"))
  18. assert.False(t, IsValidHookContentType("invalid"))
  19. }
  20. func TestWebhook_History(t *testing.T) {
  21. assert.NoError(t, PrepareTestDatabase())
  22. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  23. tasks, err := webhook.History(0)
  24. assert.NoError(t, err)
  25. if assert.Len(t, tasks, 1) {
  26. assert.Equal(t, int64(1), tasks[0].ID)
  27. }
  28. webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  29. tasks, err = webhook.History(0)
  30. assert.NoError(t, err)
  31. assert.Len(t, tasks, 0)
  32. }
  33. func TestWebhook_UpdateEvent(t *testing.T) {
  34. assert.NoError(t, PrepareTestDatabase())
  35. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  36. hookEvent := &HookEvent{
  37. PushOnly: true,
  38. SendEverything: false,
  39. ChooseEvents: false,
  40. HookEvents: HookEvents{
  41. Create: false,
  42. Push: true,
  43. PullRequest: false,
  44. },
  45. }
  46. webhook.HookEvent = hookEvent
  47. assert.NoError(t, webhook.UpdateEvent())
  48. assert.NotEmpty(t, webhook.Events)
  49. actualHookEvent := &HookEvent{}
  50. assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
  51. assert.Equal(t, *hookEvent, *actualHookEvent)
  52. }
  53. func TestWebhook_EventsArray(t *testing.T) {
  54. assert.Equal(t, []string{"create", "delete", "fork", "push", "issues", "issue_comment", "pull_request", "repository", "release"},
  55. (&Webhook{
  56. HookEvent: &HookEvent{SendEverything: true},
  57. }).EventsArray(),
  58. )
  59. assert.Equal(t, []string{"push"},
  60. (&Webhook{
  61. HookEvent: &HookEvent{PushOnly: true},
  62. }).EventsArray(),
  63. )
  64. }
  65. func TestCreateWebhook(t *testing.T) {
  66. hook := &Webhook{
  67. RepoID: 3,
  68. URL: "www.example.com/unit_test",
  69. ContentType: ContentTypeJSON,
  70. Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
  71. }
  72. AssertNotExistsBean(t, hook)
  73. assert.NoError(t, CreateWebhook(hook))
  74. AssertExistsAndLoadBean(t, hook)
  75. }
  76. func TestGetWebhookByRepoID(t *testing.T) {
  77. assert.NoError(t, PrepareTestDatabase())
  78. hook, err := GetWebhookByRepoID(1, 1)
  79. assert.NoError(t, err)
  80. assert.Equal(t, int64(1), hook.ID)
  81. _, err = GetWebhookByRepoID(NonexistentID, NonexistentID)
  82. assert.Error(t, err)
  83. assert.True(t, IsErrWebhookNotExist(err))
  84. }
  85. func TestGetWebhookByOrgID(t *testing.T) {
  86. assert.NoError(t, PrepareTestDatabase())
  87. hook, err := GetWebhookByOrgID(3, 3)
  88. assert.NoError(t, err)
  89. assert.Equal(t, int64(3), hook.ID)
  90. _, err = GetWebhookByOrgID(NonexistentID, NonexistentID)
  91. assert.Error(t, err)
  92. assert.True(t, IsErrWebhookNotExist(err))
  93. }
  94. func TestGetActiveWebhooksByRepoID(t *testing.T) {
  95. assert.NoError(t, PrepareTestDatabase())
  96. hooks, err := GetActiveWebhooksByRepoID(1)
  97. assert.NoError(t, err)
  98. if assert.Len(t, hooks, 1) {
  99. assert.Equal(t, int64(1), hooks[0].ID)
  100. assert.True(t, hooks[0].IsActive)
  101. }
  102. }
  103. func TestGetWebhooksByRepoID(t *testing.T) {
  104. assert.NoError(t, PrepareTestDatabase())
  105. hooks, err := GetWebhooksByRepoID(1)
  106. assert.NoError(t, err)
  107. if assert.Len(t, hooks, 2) {
  108. assert.Equal(t, int64(1), hooks[0].ID)
  109. assert.Equal(t, int64(2), hooks[1].ID)
  110. }
  111. }
  112. func TestGetActiveWebhooksByOrgID(t *testing.T) {
  113. assert.NoError(t, PrepareTestDatabase())
  114. hooks, err := GetActiveWebhooksByOrgID(3)
  115. assert.NoError(t, err)
  116. if assert.Len(t, hooks, 1) {
  117. assert.Equal(t, int64(3), hooks[0].ID)
  118. assert.True(t, hooks[0].IsActive)
  119. }
  120. }
  121. func TestGetWebhooksByOrgID(t *testing.T) {
  122. assert.NoError(t, PrepareTestDatabase())
  123. hooks, err := GetWebhooksByOrgID(3)
  124. assert.NoError(t, err)
  125. if assert.Len(t, hooks, 1) {
  126. assert.Equal(t, int64(3), hooks[0].ID)
  127. assert.True(t, hooks[0].IsActive)
  128. }
  129. }
  130. func TestUpdateWebhook(t *testing.T) {
  131. assert.NoError(t, PrepareTestDatabase())
  132. hook := AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  133. hook.IsActive = true
  134. hook.ContentType = ContentTypeForm
  135. AssertNotExistsBean(t, hook)
  136. assert.NoError(t, UpdateWebhook(hook))
  137. AssertExistsAndLoadBean(t, hook)
  138. }
  139. func TestDeleteWebhookByRepoID(t *testing.T) {
  140. assert.NoError(t, PrepareTestDatabase())
  141. AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
  142. assert.NoError(t, DeleteWebhookByRepoID(1, 2))
  143. AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
  144. err := DeleteWebhookByRepoID(NonexistentID, NonexistentID)
  145. assert.Error(t, err)
  146. assert.True(t, IsErrWebhookNotExist(err))
  147. }
  148. func TestDeleteWebhookByOrgID(t *testing.T) {
  149. assert.NoError(t, PrepareTestDatabase())
  150. AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3})
  151. assert.NoError(t, DeleteWebhookByOrgID(3, 3))
  152. AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3})
  153. err := DeleteWebhookByOrgID(NonexistentID, NonexistentID)
  154. assert.Error(t, err)
  155. assert.True(t, IsErrWebhookNotExist(err))
  156. }
  157. func TestToHookTaskType(t *testing.T) {
  158. assert.Equal(t, GOGS, ToHookTaskType("gogs"))
  159. assert.Equal(t, SLACK, ToHookTaskType("slack"))
  160. assert.Equal(t, GITEA, ToHookTaskType("gitea"))
  161. assert.Equal(t, TELEGRAM, ToHookTaskType("telegram"))
  162. }
  163. func TestHookTaskType_Name(t *testing.T) {
  164. assert.Equal(t, "gogs", GOGS.Name())
  165. assert.Equal(t, "slack", SLACK.Name())
  166. assert.Equal(t, "gitea", GITEA.Name())
  167. assert.Equal(t, "telegram", TELEGRAM.Name())
  168. }
  169. func TestIsValidHookTaskType(t *testing.T) {
  170. assert.True(t, IsValidHookTaskType("gogs"))
  171. assert.True(t, IsValidHookTaskType("slack"))
  172. assert.True(t, IsValidHookTaskType("gitea"))
  173. assert.True(t, IsValidHookTaskType("telegram"))
  174. assert.False(t, IsValidHookTaskType("invalid"))
  175. }
  176. func TestHookTasks(t *testing.T) {
  177. assert.NoError(t, PrepareTestDatabase())
  178. hookTasks, err := HookTasks(1, 1)
  179. assert.NoError(t, err)
  180. if assert.Len(t, hookTasks, 1) {
  181. assert.Equal(t, int64(1), hookTasks[0].ID)
  182. }
  183. hookTasks, err = HookTasks(NonexistentID, 1)
  184. assert.NoError(t, err)
  185. assert.Len(t, hookTasks, 0)
  186. }
  187. func TestCreateHookTask(t *testing.T) {
  188. assert.NoError(t, PrepareTestDatabase())
  189. hookTask := &HookTask{
  190. RepoID: 3,
  191. HookID: 3,
  192. Type: GITEA,
  193. URL: "http://www.example.com/unit_test",
  194. Payloader: &api.PushPayload{},
  195. }
  196. AssertNotExistsBean(t, hookTask)
  197. assert.NoError(t, CreateHookTask(hookTask))
  198. AssertExistsAndLoadBean(t, hookTask)
  199. }
  200. func TestUpdateHookTask(t *testing.T) {
  201. assert.NoError(t, PrepareTestDatabase())
  202. hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask)
  203. hook.PayloadContent = "new payload content"
  204. hook.DeliveredString = "new delivered string"
  205. hook.IsDelivered = true
  206. AssertNotExistsBean(t, hook)
  207. assert.NoError(t, UpdateHookTask(hook))
  208. AssertExistsAndLoadBean(t, hook)
  209. }