A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

341 lines
10 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
3 years ago
  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. "context"
  7. "testing"
  8. "time"
  9. api "code.gitea.io/gitea/modules/structs"
  10. jsoniter "github.com/json-iterator/go"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestHookContentType_Name(t *testing.T) {
  14. assert.Equal(t, "json", ContentTypeJSON.Name())
  15. assert.Equal(t, "form", ContentTypeForm.Name())
  16. }
  17. func TestIsValidHookContentType(t *testing.T) {
  18. assert.True(t, IsValidHookContentType("json"))
  19. assert.True(t, IsValidHookContentType("form"))
  20. assert.False(t, IsValidHookContentType("invalid"))
  21. }
  22. func TestWebhook_History(t *testing.T) {
  23. assert.NoError(t, PrepareTestDatabase())
  24. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  25. tasks, err := webhook.History(0)
  26. assert.NoError(t, err)
  27. if assert.Len(t, tasks, 1) {
  28. assert.Equal(t, int64(1), tasks[0].ID)
  29. }
  30. webhook = AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  31. tasks, err = webhook.History(0)
  32. assert.NoError(t, err)
  33. assert.Len(t, tasks, 0)
  34. }
  35. func TestWebhook_UpdateEvent(t *testing.T) {
  36. assert.NoError(t, PrepareTestDatabase())
  37. webhook := AssertExistsAndLoadBean(t, &Webhook{ID: 1}).(*Webhook)
  38. hookEvent := &HookEvent{
  39. PushOnly: true,
  40. SendEverything: false,
  41. ChooseEvents: false,
  42. HookEvents: HookEvents{
  43. Create: false,
  44. Push: true,
  45. PullRequest: false,
  46. },
  47. }
  48. webhook.HookEvent = hookEvent
  49. assert.NoError(t, webhook.UpdateEvent())
  50. assert.NotEmpty(t, webhook.Events)
  51. actualHookEvent := &HookEvent{}
  52. json := jsoniter.ConfigCompatibleWithStandardLibrary
  53. assert.NoError(t, json.Unmarshal([]byte(webhook.Events), actualHookEvent))
  54. assert.Equal(t, *hookEvent, *actualHookEvent)
  55. }
  56. func TestWebhook_EventsArray(t *testing.T) {
  57. assert.Equal(t, []string{
  58. "create", "delete", "fork", "push",
  59. "issues", "issue_assign", "issue_label", "issue_milestone", "issue_comment",
  60. "pull_request", "pull_request_assign", "pull_request_label", "pull_request_milestone",
  61. "pull_request_comment", "pull_request_review_approved", "pull_request_review_rejected",
  62. "pull_request_review_comment", "pull_request_sync", "repository", "release",
  63. },
  64. (&Webhook{
  65. HookEvent: &HookEvent{SendEverything: true},
  66. }).EventsArray(),
  67. )
  68. assert.Equal(t, []string{"push"},
  69. (&Webhook{
  70. HookEvent: &HookEvent{PushOnly: true},
  71. }).EventsArray(),
  72. )
  73. }
  74. func TestCreateWebhook(t *testing.T) {
  75. hook := &Webhook{
  76. RepoID: 3,
  77. URL: "www.example.com/unit_test",
  78. ContentType: ContentTypeJSON,
  79. Events: `{"push_only":false,"send_everything":false,"choose_events":false,"events":{"create":false,"push":true,"pull_request":true}}`,
  80. }
  81. AssertNotExistsBean(t, hook)
  82. assert.NoError(t, CreateWebhook(hook))
  83. AssertExistsAndLoadBean(t, hook)
  84. }
  85. func TestGetWebhookByRepoID(t *testing.T) {
  86. assert.NoError(t, PrepareTestDatabase())
  87. hook, err := GetWebhookByRepoID(1, 1)
  88. assert.NoError(t, err)
  89. assert.Equal(t, int64(1), hook.ID)
  90. _, err = GetWebhookByRepoID(NonexistentID, NonexistentID)
  91. assert.Error(t, err)
  92. assert.True(t, IsErrWebhookNotExist(err))
  93. }
  94. func TestGetWebhookByOrgID(t *testing.T) {
  95. assert.NoError(t, PrepareTestDatabase())
  96. hook, err := GetWebhookByOrgID(3, 3)
  97. assert.NoError(t, err)
  98. assert.Equal(t, int64(3), hook.ID)
  99. _, err = GetWebhookByOrgID(NonexistentID, NonexistentID)
  100. assert.Error(t, err)
  101. assert.True(t, IsErrWebhookNotExist(err))
  102. }
  103. func TestGetActiveWebhooksByRepoID(t *testing.T) {
  104. assert.NoError(t, PrepareTestDatabase())
  105. hooks, err := GetActiveWebhooksByRepoID(1)
  106. assert.NoError(t, err)
  107. if assert.Len(t, hooks, 1) {
  108. assert.Equal(t, int64(1), hooks[0].ID)
  109. assert.True(t, hooks[0].IsActive)
  110. }
  111. }
  112. func TestGetWebhooksByRepoID(t *testing.T) {
  113. assert.NoError(t, PrepareTestDatabase())
  114. hooks, err := GetWebhooksByRepoID(1, ListOptions{})
  115. assert.NoError(t, err)
  116. if 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. }
  121. func TestGetActiveWebhooksByOrgID(t *testing.T) {
  122. assert.NoError(t, PrepareTestDatabase())
  123. hooks, err := GetActiveWebhooksByOrgID(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 TestGetWebhooksByOrgID(t *testing.T) {
  131. assert.NoError(t, PrepareTestDatabase())
  132. hooks, err := GetWebhooksByOrgID(3, ListOptions{})
  133. assert.NoError(t, err)
  134. if assert.Len(t, hooks, 1) {
  135. assert.Equal(t, int64(3), hooks[0].ID)
  136. assert.True(t, hooks[0].IsActive)
  137. }
  138. }
  139. func TestUpdateWebhook(t *testing.T) {
  140. assert.NoError(t, PrepareTestDatabase())
  141. hook := AssertExistsAndLoadBean(t, &Webhook{ID: 2}).(*Webhook)
  142. hook.IsActive = true
  143. hook.ContentType = ContentTypeForm
  144. AssertNotExistsBean(t, hook)
  145. assert.NoError(t, UpdateWebhook(hook))
  146. AssertExistsAndLoadBean(t, hook)
  147. }
  148. func TestDeleteWebhookByRepoID(t *testing.T) {
  149. assert.NoError(t, PrepareTestDatabase())
  150. AssertExistsAndLoadBean(t, &Webhook{ID: 2, RepoID: 1})
  151. assert.NoError(t, DeleteWebhookByRepoID(1, 2))
  152. AssertNotExistsBean(t, &Webhook{ID: 2, RepoID: 1})
  153. err := DeleteWebhookByRepoID(NonexistentID, NonexistentID)
  154. assert.Error(t, err)
  155. assert.True(t, IsErrWebhookNotExist(err))
  156. }
  157. func TestDeleteWebhookByOrgID(t *testing.T) {
  158. assert.NoError(t, PrepareTestDatabase())
  159. AssertExistsAndLoadBean(t, &Webhook{ID: 3, OrgID: 3})
  160. assert.NoError(t, DeleteWebhookByOrgID(3, 3))
  161. AssertNotExistsBean(t, &Webhook{ID: 3, OrgID: 3})
  162. err := DeleteWebhookByOrgID(NonexistentID, NonexistentID)
  163. assert.Error(t, err)
  164. assert.True(t, IsErrWebhookNotExist(err))
  165. }
  166. func TestHookTasks(t *testing.T) {
  167. assert.NoError(t, PrepareTestDatabase())
  168. hookTasks, err := HookTasks(1, 1)
  169. assert.NoError(t, err)
  170. if assert.Len(t, hookTasks, 1) {
  171. assert.Equal(t, int64(1), hookTasks[0].ID)
  172. }
  173. hookTasks, err = HookTasks(NonexistentID, 1)
  174. assert.NoError(t, err)
  175. assert.Len(t, hookTasks, 0)
  176. }
  177. func TestCreateHookTask(t *testing.T) {
  178. assert.NoError(t, PrepareTestDatabase())
  179. hookTask := &HookTask{
  180. RepoID: 3,
  181. HookID: 3,
  182. Typ: GITEA,
  183. URL: "http://www.example.com/unit_test",
  184. Payloader: &api.PushPayload{},
  185. }
  186. AssertNotExistsBean(t, hookTask)
  187. assert.NoError(t, CreateHookTask(hookTask))
  188. AssertExistsAndLoadBean(t, hookTask)
  189. }
  190. func TestUpdateHookTask(t *testing.T) {
  191. assert.NoError(t, PrepareTestDatabase())
  192. hook := AssertExistsAndLoadBean(t, &HookTask{ID: 1}).(*HookTask)
  193. hook.PayloadContent = "new payload content"
  194. hook.DeliveredString = "new delivered string"
  195. hook.IsDelivered = true
  196. AssertNotExistsBean(t, hook)
  197. assert.NoError(t, UpdateHookTask(hook))
  198. AssertExistsAndLoadBean(t, hook)
  199. }
  200. func TestCleanupHookTaskTable_PerWebhook_DeletesDelivered(t *testing.T) {
  201. assert.NoError(t, PrepareTestDatabase())
  202. hookTask := &HookTask{
  203. RepoID: 3,
  204. HookID: 3,
  205. Typ: GITEA,
  206. URL: "http://www.example.com/unit_test",
  207. Payloader: &api.PushPayload{},
  208. IsDelivered: true,
  209. Delivered: time.Now().UnixNano(),
  210. }
  211. AssertNotExistsBean(t, hookTask)
  212. assert.NoError(t, CreateHookTask(hookTask))
  213. AssertExistsAndLoadBean(t, hookTask)
  214. assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
  215. AssertNotExistsBean(t, hookTask)
  216. }
  217. func TestCleanupHookTaskTable_PerWebhook_LeavesUndelivered(t *testing.T) {
  218. assert.NoError(t, PrepareTestDatabase())
  219. hookTask := &HookTask{
  220. RepoID: 2,
  221. HookID: 4,
  222. Typ: GITEA,
  223. URL: "http://www.example.com/unit_test",
  224. Payloader: &api.PushPayload{},
  225. IsDelivered: false,
  226. }
  227. AssertNotExistsBean(t, hookTask)
  228. assert.NoError(t, CreateHookTask(hookTask))
  229. AssertExistsAndLoadBean(t, hookTask)
  230. assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 0))
  231. AssertExistsAndLoadBean(t, hookTask)
  232. }
  233. func TestCleanupHookTaskTable_PerWebhook_LeavesMostRecentTask(t *testing.T) {
  234. assert.NoError(t, PrepareTestDatabase())
  235. hookTask := &HookTask{
  236. RepoID: 2,
  237. HookID: 4,
  238. Typ: GITEA,
  239. URL: "http://www.example.com/unit_test",
  240. Payloader: &api.PushPayload{},
  241. IsDelivered: true,
  242. Delivered: time.Now().UnixNano(),
  243. }
  244. AssertNotExistsBean(t, hookTask)
  245. assert.NoError(t, CreateHookTask(hookTask))
  246. AssertExistsAndLoadBean(t, hookTask)
  247. assert.NoError(t, CleanupHookTaskTable(context.Background(), PerWebhook, 168*time.Hour, 1))
  248. AssertExistsAndLoadBean(t, hookTask)
  249. }
  250. func TestCleanupHookTaskTable_OlderThan_DeletesDelivered(t *testing.T) {
  251. assert.NoError(t, PrepareTestDatabase())
  252. hookTask := &HookTask{
  253. RepoID: 3,
  254. HookID: 3,
  255. Typ: GITEA,
  256. URL: "http://www.example.com/unit_test",
  257. Payloader: &api.PushPayload{},
  258. IsDelivered: true,
  259. Delivered: time.Now().AddDate(0, 0, -8).UnixNano(),
  260. }
  261. AssertNotExistsBean(t, hookTask)
  262. assert.NoError(t, CreateHookTask(hookTask))
  263. AssertExistsAndLoadBean(t, hookTask)
  264. assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
  265. AssertNotExistsBean(t, hookTask)
  266. }
  267. func TestCleanupHookTaskTable_OlderThan_LeavesUndelivered(t *testing.T) {
  268. assert.NoError(t, PrepareTestDatabase())
  269. hookTask := &HookTask{
  270. RepoID: 2,
  271. HookID: 4,
  272. Typ: GITEA,
  273. URL: "http://www.example.com/unit_test",
  274. Payloader: &api.PushPayload{},
  275. IsDelivered: false,
  276. }
  277. AssertNotExistsBean(t, hookTask)
  278. assert.NoError(t, CreateHookTask(hookTask))
  279. AssertExistsAndLoadBean(t, hookTask)
  280. assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
  281. AssertExistsAndLoadBean(t, hookTask)
  282. }
  283. func TestCleanupHookTaskTable_OlderThan_LeavesTaskEarlierThanAgeToDelete(t *testing.T) {
  284. assert.NoError(t, PrepareTestDatabase())
  285. hookTask := &HookTask{
  286. RepoID: 2,
  287. HookID: 4,
  288. Typ: GITEA,
  289. URL: "http://www.example.com/unit_test",
  290. Payloader: &api.PushPayload{},
  291. IsDelivered: true,
  292. Delivered: time.Now().AddDate(0, 0, -6).UnixNano(),
  293. }
  294. AssertNotExistsBean(t, hookTask)
  295. assert.NoError(t, CreateHookTask(hookTask))
  296. AssertExistsAndLoadBean(t, hookTask)
  297. assert.NoError(t, CleanupHookTaskTable(context.Background(), OlderThan, 168*time.Hour, 0))
  298. AssertExistsAndLoadBean(t, hookTask)
  299. }