本站源代码
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.

310 lines
8.8KB

  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_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", "delete", "fork", "push", "issues", "issue_comment", "pull_request", "repository", "release"},
  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. assert.Equal(t, TELEGRAM, ToHookTaskType("telegram"))
  173. }
  174. func TestHookTaskType_Name(t *testing.T) {
  175. assert.Equal(t, "gogs", GOGS.Name())
  176. assert.Equal(t, "slack", SLACK.Name())
  177. assert.Equal(t, "gitea", GITEA.Name())
  178. assert.Equal(t, "telegram", TELEGRAM.Name())
  179. }
  180. func TestIsValidHookTaskType(t *testing.T) {
  181. assert.True(t, IsValidHookTaskType("gogs"))
  182. assert.True(t, IsValidHookTaskType("slack"))
  183. assert.True(t, IsValidHookTaskType("gitea"))
  184. assert.True(t, IsValidHookTaskType("telegram"))
  185. assert.False(t, IsValidHookTaskType("invalid"))
  186. }
  187. func TestHookTasks(t *testing.T) {
  188. assert.NoError(t, PrepareTestDatabase())
  189. hookTasks, err := HookTasks(1, 1)
  190. assert.NoError(t, err)
  191. if assert.Len(t, hookTasks, 1) {
  192. assert.Equal(t, int64(1), hookTasks[0].ID)
  193. }
  194. hookTasks, err = HookTasks(NonexistentID, 1)
  195. assert.NoError(t, err)
  196. assert.Len(t, hookTasks, 0)
  197. }
  198. func TestCreateHookTask(t *testing.T) {
  199. assert.NoError(t, PrepareTestDatabase())
  200. hookTask := &HookTask{
  201. RepoID: 3,
  202. HookID: 3,
  203. Type: GITEA,
  204. URL: "http://www.example.com/unit_test",
  205. Payloader: &api.PushPayload{},
  206. }
  207. AssertNotExistsBean(t, hookTask)
  208. assert.NoError(t, CreateHookTask(hookTask))
  209. AssertExistsAndLoadBean(t, hookTask)
  210. }
  211. func TestUpdateHookTask(t *testing.T) {
  212. assert.NoError(t, PrepareTestDatabase())
  213. hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask)
  214. hook.PayloadContent = "new payload content"
  215. hook.DeliveredString = "new delivered string"
  216. hook.IsDelivered = true
  217. AssertNotExistsBean(t, hook)
  218. assert.NoError(t, UpdateHookTask(hook))
  219. AssertExistsAndLoadBean(t, hook)
  220. }
  221. func TestPrepareWebhooks(t *testing.T) {
  222. assert.NoError(t, PrepareTestDatabase())
  223. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  224. hookTasks := []*HookTask{
  225. {RepoID: repo.ID, HookID: 1, EventType: HookEventPush},
  226. }
  227. for _, hookTask := range hookTasks {
  228. AssertNotExistsBean(t, hookTask)
  229. }
  230. assert.NoError(t, PrepareWebhooks(repo, HookEventPush, &api.PushPayload{}))
  231. for _, hookTask := range hookTasks {
  232. AssertExistsAndLoadBean(t, hookTask)
  233. }
  234. }
  235. func TestPrepareWebhooksBranchFilterMatch(t *testing.T) {
  236. assert.NoError(t, PrepareTestDatabase())
  237. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  238. hookTasks := []*HookTask{
  239. {RepoID: repo.ID, HookID: 4, EventType: HookEventPush},
  240. }
  241. for _, hookTask := range hookTasks {
  242. AssertNotExistsBean(t, hookTask)
  243. }
  244. // this test also ensures that * doesn't handle / in any special way (like shell would)
  245. assert.NoError(t, PrepareWebhooks(repo, HookEventPush, &api.PushPayload{Ref: "refs/heads/feature/7791"}))
  246. for _, hookTask := range hookTasks {
  247. AssertExistsAndLoadBean(t, hookTask)
  248. }
  249. }
  250. func TestPrepareWebhooksBranchFilterNoMatch(t *testing.T) {
  251. assert.NoError(t, PrepareTestDatabase())
  252. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  253. hookTasks := []*HookTask{
  254. {RepoID: repo.ID, HookID: 4, EventType: HookEventPush},
  255. }
  256. for _, hookTask := range hookTasks {
  257. AssertNotExistsBean(t, hookTask)
  258. }
  259. assert.NoError(t, PrepareWebhooks(repo, HookEventPush, &api.PushPayload{Ref: "refs/heads/fix_weird_bug"}))
  260. for _, hookTask := range hookTasks {
  261. AssertNotExistsBean(t, hookTask)
  262. }
  263. }
  264. // TODO TestHookTask_deliver
  265. // TODO TestDeliverHooks
上海开阖软件有限公司 沪ICP备12045867号-1