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.

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