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.

272 lines
7.4 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/sdk/gitea"
  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_GetSlackHook(t *testing.T) {
  21. w := &Webhook{
  22. Meta: `{"channel": "foo", "username": "username", "color": "blue"}`,
  23. }
  24. slackHook := w.GetSlackHook()
  25. assert.Equal(t, *slackHook, SlackMeta{
  26. Channel: "foo",
  27. Username: "username",
  28. Color: "blue",
  29. })
  30. }
  31. func TestWebhook_History(t *testing.T) {
  32. assert.NoError(t, PrepareTestDatabase())
  33. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  34. tasks, err := webhook.History(0)
  35. assert.NoError(t, err)
  36. if assert.Len(t, tasks, 1) {
  37. assert.Equal(t, int64(1), tasks[0].ID)
  38. }
  39. webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  40. tasks, err = webhook.History(0)
  41. assert.NoError(t, err)
  42. assert.Len(t, tasks, 0)
  43. }
  44. func TestWebhook_UpdateEvent(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  47. hookEvent := &HookEvent{
  48. PushOnly: true,
  49. SendEverything: false,
  50. ChooseEvents: false,
  51. HookEvents: HookEvents{
  52. Create: false,
  53. Push: true,
  54. PullRequest: false,
  55. },
  56. }
  57. webhook.HookEvent = hookEvent
  58. assert.NoError(t, webhook.UpdateEvent())
  59. assert.NotEmpty(t, webhook.Events)
  60. actualHookEvent := &HookEvent{}
  61. assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
  62. assert.Equal(t, *hookEvent, *actualHookEvent)
  63. }
  64. func TestWebhook_EventsArray(t *testing.T) {
  65. assert.Equal(t, []string{"create", "push", "pull_request"},
  66. (&Webhook{
  67. HookEvent: &HookEvent{SendEverything: true},
  68. }).EventsArray(),
  69. )
  70. assert.Equal(t, []string{"push"},
  71. (&Webhook{
  72. HookEvent: &HookEvent{PushOnly: true},
  73. }).EventsArray(),
  74. )
  75. }
  76. func TestCreateWebhook(t *testing.T) {
  77. hook := &Webhook{
  78. RepoID: 3,
  79. URL: "www.example.com/unit_test",
  80. ContentType: ContentTypeJSON,
  81. Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
  82. }
  83. AssertNotExistsBean(t, hook)
  84. assert.NoError(t, CreateWebhook(hook))
  85. AssertExistsAndLoadBean(t, hook)
  86. }
  87. func TestGetWebhookByRepoID(t *testing.T) {
  88. assert.NoError(t, PrepareTestDatabase())
  89. hook, err := GetWebhookByRepoID(1, 1)
  90. assert.NoError(t, err)
  91. assert.Equal(t, int64(1), hook.ID)
  92. _, err = GetWebhookByRepoID(NonexistentID, NonexistentID)
  93. assert.Error(t, err)
  94. assert.True(t, IsErrWebhookNotExist(err))
  95. }
  96. func TestGetWebhookByOrgID(t *testing.T) {
  97. assert.NoError(t, PrepareTestDatabase())
  98. hook, err := GetWebhookByOrgID(3, 3)
  99. assert.NoError(t, err)
  100. assert.Equal(t, int64(3), hook.ID)
  101. _, err = GetWebhookByOrgID(NonexistentID, NonexistentID)
  102. assert.Error(t, err)
  103. assert.True(t, IsErrWebhookNotExist(err))
  104. }
  105. func TestGetActiveWebhooksByRepoID(t *testing.T) {
  106. assert.NoError(t, PrepareTestDatabase())
  107. hooks, err := GetActiveWebhooksByRepoID(1)
  108. assert.NoError(t, err)
  109. if assert.Len(t, hooks, 1) {
  110. assert.Equal(t, int64(1), hooks[0].ID)
  111. assert.True(t, hooks[0].IsActive)
  112. }
  113. }
  114. func TestGetWebhooksByRepoID(t *testing.T) {
  115. assert.NoError(t, PrepareTestDatabase())
  116. hooks, err := GetWebhooksByRepoID(1)
  117. assert.NoError(t, err)
  118. if assert.Len(t, hooks, 2) {
  119. assert.Equal(t, int64(1), hooks[0].ID)
  120. assert.Equal(t, int64(2), hooks[1].ID)
  121. }
  122. }
  123. func TestGetActiveWebhooksByOrgID(t *testing.T) {
  124. assert.NoError(t, PrepareTestDatabase())
  125. hooks, err := GetActiveWebhooksByOrgID(3)
  126. assert.NoError(t, err)
  127. if assert.Len(t, hooks, 1) {
  128. assert.Equal(t, int64(3), hooks[0].ID)
  129. assert.True(t, hooks[0].IsActive)
  130. }
  131. }
  132. func TestGetWebhooksByOrgID(t *testing.T) {
  133. assert.NoError(t, PrepareTestDatabase())
  134. hooks, err := GetWebhooksByOrgID(3)
  135. assert.NoError(t, err)
  136. if assert.Len(t, hooks, 1) {
  137. assert.Equal(t, int64(3), hooks[0].ID)
  138. assert.True(t, hooks[0].IsActive)
  139. }
  140. }
  141. func TestUpdateWebhook(t *testing.T) {
  142. assert.NoError(t, PrepareTestDatabase())
  143. hook := AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  144. hook.IsActive = true
  145. hook.ContentType = ContentTypeForm
  146. AssertNotExistsBean(t, hook)
  147. assert.NoError(t, UpdateWebhook(hook))
  148. AssertExistsAndLoadBean(t, hook)
  149. }
  150. func TestDeleteWebhookByRepoID(t *testing.T) {
  151. assert.NoError(t, PrepareTestDatabase())
  152. AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
  153. assert.NoError(t, DeleteWebhookByRepoID(1, 2))
  154. AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
  155. err := DeleteWebhookByRepoID(NonexistentID, NonexistentID)
  156. assert.Error(t, err)
  157. assert.True(t, IsErrWebhookNotExist(err))
  158. }
  159. func TestDeleteWebhookByOrgID(t *testing.T) {
  160. assert.NoError(t, PrepareTestDatabase())
  161. AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3})
  162. assert.NoError(t, DeleteWebhookByOrgID(3, 3))
  163. AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3})
  164. err := DeleteWebhookByOrgID(NonexistentID, NonexistentID)
  165. assert.Error(t, err)
  166. assert.True(t, IsErrWebhookNotExist(err))
  167. }
  168. func TestToHookTaskType(t *testing.T) {
  169. assert.Equal(t, GOGS, ToHookTaskType("gogs"))
  170. assert.Equal(t, SLACK, ToHookTaskType("slack"))
  171. assert.Equal(t, GITEA, ToHookTaskType("gitea"))
  172. }
  173. func TestHookTaskType_Name(t *testing.T) {
  174. assert.Equal(t, "gogs", GOGS.Name())
  175. assert.Equal(t, "slack", SLACK.Name())
  176. assert.Equal(t, "gitea", GITEA.Name())
  177. }
  178. func TestIsValidHookTaskType(t *testing.T) {
  179. assert.True(t, IsValidHookTaskType("gogs"))
  180. assert.True(t, IsValidHookTaskType("slack"))
  181. assert.True(t, IsValidHookTaskType("gitea"))
  182. assert.False(t, IsValidHookTaskType("invalid"))
  183. }
  184. func TestHookTasks(t *testing.T) {
  185. assert.NoError(t, PrepareTestDatabase())
  186. hookTasks, err := HookTasks(1, 1)
  187. assert.NoError(t, err)
  188. if assert.Len(t, hookTasks, 1) {
  189. assert.Equal(t, int64(1), hookTasks[0].ID)
  190. }
  191. hookTasks, err = HookTasks(NonexistentID, 1)
  192. assert.NoError(t, err)
  193. assert.Len(t, hookTasks, 0)
  194. }
  195. func TestCreateHookTask(t *testing.T) {
  196. assert.NoError(t, PrepareTestDatabase())
  197. hookTask := &HookTask{
  198. RepoID: 3,
  199. HookID: 3,
  200. Type: GITEA,
  201. URL: "http://www.example.com/unit_test",
  202. Payloader: &api.PushPayload{},
  203. }
  204. AssertNotExistsBean(t, hookTask)
  205. assert.NoError(t, CreateHookTask(hookTask))
  206. AssertExistsAndLoadBean(t, hookTask)
  207. }
  208. func TestUpdateHookTask(t *testing.T) {
  209. assert.NoError(t, PrepareTestDatabase())
  210. hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask)
  211. hook.PayloadContent = "new payload content"
  212. hook.DeliveredString = "new delivered string"
  213. hook.IsDelivered = true
  214. AssertNotExistsBean(t, hook)
  215. assert.NoError(t, UpdateHookTask(hook))
  216. AssertExistsAndLoadBean(t, hook)
  217. }
  218. func TestPrepareWebhooks(t *testing.T) {
  219. assert.NoError(t, PrepareTestDatabase())
  220. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  221. hookTasks := []*HookTask{
  222. {RepoID: repo.ID, HookID: 1, EventType: HookEventPush},
  223. }
  224. for _, hookTask := range hookTasks {
  225. AssertNotExistsBean(t, hookTask)
  226. }
  227. assert.NoError(t, PrepareWebhooks(repo, HookEventPush, &api.PushPayload{}))
  228. for _, hookTask := range hookTasks {
  229. AssertExistsAndLoadBean(t, hookTask)
  230. }
  231. }
  232. // TODO TestHookTask_deliver
  233. // TODO TestDeliverHooks