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.

general_test.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "testing"
  6. api "code.gitea.io/gitea/modules/structs"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func createTestPayload() *api.CreatePayload {
  10. return &api.CreatePayload{
  11. Sha: "2020558fe2e34debb818a514715839cabd25e777",
  12. Ref: "refs/heads/test",
  13. RefType: "branch",
  14. Repo: &api.Repository{
  15. HTMLURL: "http://localhost:3000/test/repo",
  16. Name: "repo",
  17. FullName: "test/repo",
  18. },
  19. Sender: &api.User{
  20. UserName: "user1",
  21. AvatarURL: "http://localhost:3000/user1/avatar",
  22. },
  23. }
  24. }
  25. func deleteTestPayload() *api.DeletePayload {
  26. return &api.DeletePayload{
  27. Ref: "refs/heads/test",
  28. RefType: "branch",
  29. Repo: &api.Repository{
  30. HTMLURL: "http://localhost:3000/test/repo",
  31. Name: "repo",
  32. FullName: "test/repo",
  33. },
  34. Sender: &api.User{
  35. UserName: "user1",
  36. AvatarURL: "http://localhost:3000/user1/avatar",
  37. },
  38. }
  39. }
  40. func forkTestPayload() *api.ForkPayload {
  41. return &api.ForkPayload{
  42. Forkee: &api.Repository{
  43. HTMLURL: "http://localhost:3000/test/repo2",
  44. Name: "repo2",
  45. FullName: "test/repo2",
  46. },
  47. Repo: &api.Repository{
  48. HTMLURL: "http://localhost:3000/test/repo",
  49. Name: "repo",
  50. FullName: "test/repo",
  51. },
  52. Sender: &api.User{
  53. UserName: "user1",
  54. AvatarURL: "http://localhost:3000/user1/avatar",
  55. },
  56. }
  57. }
  58. func pushTestPayload() *api.PushPayload {
  59. commit := &api.PayloadCommit{
  60. ID: "2020558fe2e34debb818a514715839cabd25e778",
  61. Message: "commit message",
  62. URL: "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
  63. Author: &api.PayloadUser{
  64. Name: "user1",
  65. Email: "user1@localhost",
  66. UserName: "user1",
  67. },
  68. Committer: &api.PayloadUser{
  69. Name: "user1",
  70. Email: "user1@localhost",
  71. UserName: "user1",
  72. },
  73. }
  74. return &api.PushPayload{
  75. Ref: "refs/heads/test",
  76. Before: "2020558fe2e34debb818a514715839cabd25e777",
  77. After: "2020558fe2e34debb818a514715839cabd25e778",
  78. CompareURL: "",
  79. HeadCommit: commit,
  80. Commits: []*api.PayloadCommit{commit, commit},
  81. TotalCommits: 2,
  82. Repo: &api.Repository{
  83. HTMLURL: "http://localhost:3000/test/repo",
  84. Name: "repo",
  85. FullName: "test/repo",
  86. },
  87. Pusher: &api.User{
  88. UserName: "user1",
  89. AvatarURL: "http://localhost:3000/user1/avatar",
  90. },
  91. Sender: &api.User{
  92. UserName: "user1",
  93. AvatarURL: "http://localhost:3000/user1/avatar",
  94. },
  95. }
  96. }
  97. func issueTestPayload() *api.IssuePayload {
  98. return &api.IssuePayload{
  99. Index: 2,
  100. Sender: &api.User{
  101. UserName: "user1",
  102. AvatarURL: "http://localhost:3000/user1/avatar",
  103. },
  104. Repository: &api.Repository{
  105. HTMLURL: "http://localhost:3000/test/repo",
  106. Name: "repo",
  107. FullName: "test/repo",
  108. },
  109. Issue: &api.Issue{
  110. ID: 2,
  111. Index: 2,
  112. URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
  113. HTMLURL: "http://localhost:3000/test/repo/issues/2",
  114. Title: "crash",
  115. Body: "issue body",
  116. Assignees: []*api.User{
  117. {
  118. UserName: "user1",
  119. AvatarURL: "http://localhost:3000/user1/avatar",
  120. },
  121. },
  122. Milestone: &api.Milestone{
  123. ID: 1,
  124. Title: "Milestone Title",
  125. Description: "Milestone Description",
  126. },
  127. },
  128. }
  129. }
  130. func issueCommentTestPayload() *api.IssueCommentPayload {
  131. return &api.IssueCommentPayload{
  132. Action: api.HookIssueCommentCreated,
  133. Sender: &api.User{
  134. UserName: "user1",
  135. AvatarURL: "http://localhost:3000/user1/avatar",
  136. },
  137. Repository: &api.Repository{
  138. HTMLURL: "http://localhost:3000/test/repo",
  139. Name: "repo",
  140. FullName: "test/repo",
  141. },
  142. Comment: &api.Comment{
  143. HTMLURL: "http://localhost:3000/test/repo/issues/2#issuecomment-4",
  144. IssueURL: "http://localhost:3000/test/repo/issues/2",
  145. Body: "more info needed",
  146. },
  147. Issue: &api.Issue{
  148. ID: 2,
  149. Index: 2,
  150. URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2",
  151. HTMLURL: "http://localhost:3000/test/repo/issues/2",
  152. Title: "crash",
  153. Body: "this happened",
  154. },
  155. }
  156. }
  157. func pullRequestCommentTestPayload() *api.IssueCommentPayload {
  158. return &api.IssueCommentPayload{
  159. Action: api.HookIssueCommentCreated,
  160. Sender: &api.User{
  161. UserName: "user1",
  162. AvatarURL: "http://localhost:3000/user1/avatar",
  163. },
  164. Repository: &api.Repository{
  165. HTMLURL: "http://localhost:3000/test/repo",
  166. Name: "repo",
  167. FullName: "test/repo",
  168. },
  169. Comment: &api.Comment{
  170. HTMLURL: "http://localhost:3000/test/repo/pulls/12#issuecomment-4",
  171. PRURL: "http://localhost:3000/test/repo/pulls/12",
  172. Body: "changes requested",
  173. },
  174. Issue: &api.Issue{
  175. ID: 12,
  176. Index: 12,
  177. URL: "http://localhost:3000/api/v1/repos/test/repo/pulls/12",
  178. HTMLURL: "http://localhost:3000/test/repo/pulls/12",
  179. Title: "Fix bug",
  180. Body: "fixes bug #2",
  181. },
  182. IsPull: true,
  183. }
  184. }
  185. func wikiTestPayload() *api.WikiPayload {
  186. return &api.WikiPayload{
  187. Repository: &api.Repository{
  188. HTMLURL: "http://localhost:3000/test/repo",
  189. Name: "repo",
  190. FullName: "test/repo",
  191. },
  192. Sender: &api.User{
  193. UserName: "user1",
  194. AvatarURL: "http://localhost:3000/user1/avatar",
  195. },
  196. Page: "index",
  197. Comment: "Wiki change comment",
  198. }
  199. }
  200. func pullReleaseTestPayload() *api.ReleasePayload {
  201. return &api.ReleasePayload{
  202. Action: api.HookReleasePublished,
  203. Sender: &api.User{
  204. UserName: "user1",
  205. AvatarURL: "http://localhost:3000/user1/avatar",
  206. },
  207. Repository: &api.Repository{
  208. HTMLURL: "http://localhost:3000/test/repo",
  209. Name: "repo",
  210. FullName: "test/repo",
  211. },
  212. Release: &api.Release{
  213. TagName: "v1.0",
  214. Target: "master",
  215. Title: "First stable release",
  216. Note: "Note of first stable release",
  217. URL: "http://localhost:3000/api/v1/repos/test/repo/releases/2",
  218. },
  219. }
  220. }
  221. func pullRequestTestPayload() *api.PullRequestPayload {
  222. return &api.PullRequestPayload{
  223. Action: api.HookIssueOpened,
  224. Index: 12,
  225. Sender: &api.User{
  226. UserName: "user1",
  227. AvatarURL: "http://localhost:3000/user1/avatar",
  228. },
  229. Repository: &api.Repository{
  230. HTMLURL: "http://localhost:3000/test/repo",
  231. Name: "repo",
  232. FullName: "test/repo",
  233. },
  234. PullRequest: &api.PullRequest{
  235. ID: 12,
  236. Index: 12,
  237. URL: "http://localhost:3000/test/repo/pulls/12",
  238. HTMLURL: "http://localhost:3000/test/repo/pulls/12",
  239. Title: "Fix bug",
  240. Body: "fixes bug #2",
  241. Mergeable: true,
  242. Assignees: []*api.User{
  243. {
  244. UserName: "user1",
  245. AvatarURL: "http://localhost:3000/user1/avatar",
  246. },
  247. },
  248. Milestone: &api.Milestone{
  249. ID: 1,
  250. Title: "Milestone Title",
  251. Description: "Milestone Description",
  252. },
  253. },
  254. Review: &api.ReviewPayload{
  255. Content: "good job",
  256. },
  257. }
  258. }
  259. func repositoryTestPayload() *api.RepositoryPayload {
  260. return &api.RepositoryPayload{
  261. Action: api.HookRepoCreated,
  262. Sender: &api.User{
  263. UserName: "user1",
  264. AvatarURL: "http://localhost:3000/user1/avatar",
  265. },
  266. Repository: &api.Repository{
  267. HTMLURL: "http://localhost:3000/test/repo",
  268. Name: "repo",
  269. FullName: "test/repo",
  270. },
  271. }
  272. }
  273. func TestGetIssuesPayloadInfo(t *testing.T) {
  274. p := issueTestPayload()
  275. cases := []struct {
  276. action api.HookIssueAction
  277. text string
  278. issueTitle string
  279. attachmentText string
  280. color int
  281. }{
  282. {
  283. api.HookIssueOpened,
  284. "[test/repo] Issue opened: #2 crash by user1",
  285. "#2 crash",
  286. "issue body",
  287. orangeColor,
  288. },
  289. {
  290. api.HookIssueClosed,
  291. "[test/repo] Issue closed: #2 crash by user1",
  292. "#2 crash",
  293. "",
  294. redColor,
  295. },
  296. {
  297. api.HookIssueReOpened,
  298. "[test/repo] Issue re-opened: #2 crash by user1",
  299. "#2 crash",
  300. "",
  301. yellowColor,
  302. },
  303. {
  304. api.HookIssueEdited,
  305. "[test/repo] Issue edited: #2 crash by user1",
  306. "#2 crash",
  307. "issue body",
  308. yellowColor,
  309. },
  310. {
  311. api.HookIssueAssigned,
  312. "[test/repo] Issue assigned to user1: #2 crash by user1",
  313. "#2 crash",
  314. "",
  315. greenColor,
  316. },
  317. {
  318. api.HookIssueUnassigned,
  319. "[test/repo] Issue unassigned: #2 crash by user1",
  320. "#2 crash",
  321. "",
  322. yellowColor,
  323. },
  324. {
  325. api.HookIssueLabelUpdated,
  326. "[test/repo] Issue labels updated: #2 crash by user1",
  327. "#2 crash",
  328. "",
  329. yellowColor,
  330. },
  331. {
  332. api.HookIssueLabelCleared,
  333. "[test/repo] Issue labels cleared: #2 crash by user1",
  334. "#2 crash",
  335. "",
  336. yellowColor,
  337. },
  338. {
  339. api.HookIssueSynchronized,
  340. "[test/repo] Issue synchronized: #2 crash by user1",
  341. "#2 crash",
  342. "",
  343. yellowColor,
  344. },
  345. {
  346. api.HookIssueMilestoned,
  347. "[test/repo] Issue milestoned to Milestone Title: #2 crash by user1",
  348. "#2 crash",
  349. "",
  350. yellowColor,
  351. },
  352. {
  353. api.HookIssueDemilestoned,
  354. "[test/repo] Issue milestone cleared: #2 crash by user1",
  355. "#2 crash",
  356. "",
  357. yellowColor,
  358. },
  359. }
  360. for i, c := range cases {
  361. p.Action = c.action
  362. text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
  363. assert.Equal(t, c.text, text, "case %d", i)
  364. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  365. assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
  366. assert.Equal(t, c.color, color, "case %d", i)
  367. }
  368. }
  369. func TestGetPullRequestPayloadInfo(t *testing.T) {
  370. p := pullRequestTestPayload()
  371. cases := []struct {
  372. action api.HookIssueAction
  373. text string
  374. issueTitle string
  375. attachmentText string
  376. color int
  377. }{
  378. {
  379. api.HookIssueOpened,
  380. "[test/repo] Pull request opened: #12 Fix bug by user1",
  381. "#12 Fix bug",
  382. "fixes bug #2",
  383. greenColor,
  384. },
  385. {
  386. api.HookIssueClosed,
  387. "[test/repo] Pull request closed: #12 Fix bug by user1",
  388. "#12 Fix bug",
  389. "",
  390. redColor,
  391. },
  392. {
  393. api.HookIssueReOpened,
  394. "[test/repo] Pull request re-opened: #12 Fix bug by user1",
  395. "#12 Fix bug",
  396. "",
  397. yellowColor,
  398. },
  399. {
  400. api.HookIssueEdited,
  401. "[test/repo] Pull request edited: #12 Fix bug by user1",
  402. "#12 Fix bug",
  403. "fixes bug #2",
  404. yellowColor,
  405. },
  406. {
  407. api.HookIssueAssigned,
  408. "[test/repo] Pull request assigned to user1: #12 Fix bug by user1",
  409. "#12 Fix bug",
  410. "",
  411. greenColor,
  412. },
  413. {
  414. api.HookIssueUnassigned,
  415. "[test/repo] Pull request unassigned: #12 Fix bug by user1",
  416. "#12 Fix bug",
  417. "",
  418. yellowColor,
  419. },
  420. {
  421. api.HookIssueLabelUpdated,
  422. "[test/repo] Pull request labels updated: #12 Fix bug by user1",
  423. "#12 Fix bug",
  424. "",
  425. yellowColor,
  426. },
  427. {
  428. api.HookIssueLabelCleared,
  429. "[test/repo] Pull request labels cleared: #12 Fix bug by user1",
  430. "#12 Fix bug",
  431. "",
  432. yellowColor,
  433. },
  434. {
  435. api.HookIssueSynchronized,
  436. "[test/repo] Pull request synchronized: #12 Fix bug by user1",
  437. "#12 Fix bug",
  438. "",
  439. yellowColor,
  440. },
  441. {
  442. api.HookIssueMilestoned,
  443. "[test/repo] Pull request milestoned to Milestone Title: #12 Fix bug by user1",
  444. "#12 Fix bug",
  445. "",
  446. yellowColor,
  447. },
  448. {
  449. api.HookIssueDemilestoned,
  450. "[test/repo] Pull request milestone cleared: #12 Fix bug by user1",
  451. "#12 Fix bug",
  452. "",
  453. yellowColor,
  454. },
  455. }
  456. for i, c := range cases {
  457. p.Action = c.action
  458. text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
  459. assert.Equal(t, c.text, text, "case %d", i)
  460. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  461. assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
  462. assert.Equal(t, c.color, color, "case %d", i)
  463. }
  464. }
  465. func TestGetWikiPayloadInfo(t *testing.T) {
  466. p := wikiTestPayload()
  467. cases := []struct {
  468. action api.HookWikiAction
  469. text string
  470. color int
  471. link string
  472. }{
  473. {
  474. api.HookWikiCreated,
  475. "[test/repo] New wiki page 'index' (Wiki change comment) by user1",
  476. greenColor,
  477. "index",
  478. },
  479. {
  480. api.HookWikiEdited,
  481. "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1",
  482. yellowColor,
  483. "index",
  484. },
  485. {
  486. api.HookWikiDeleted,
  487. "[test/repo] Wiki page 'index' deleted by user1",
  488. redColor,
  489. "index",
  490. },
  491. }
  492. for i, c := range cases {
  493. p.Action = c.action
  494. text, color, link := getWikiPayloadInfo(p, noneLinkFormatter, true)
  495. assert.Equal(t, c.text, text, "case %d", i)
  496. assert.Equal(t, c.color, color, "case %d", i)
  497. assert.Equal(t, c.link, link, "case %d", i)
  498. }
  499. }
  500. func TestGetReleasePayloadInfo(t *testing.T) {
  501. p := pullReleaseTestPayload()
  502. cases := []struct {
  503. action api.HookReleaseAction
  504. text string
  505. color int
  506. }{
  507. {
  508. api.HookReleasePublished,
  509. "[test/repo] Release created: v1.0 by user1",
  510. greenColor,
  511. },
  512. {
  513. api.HookReleaseUpdated,
  514. "[test/repo] Release updated: v1.0 by user1",
  515. yellowColor,
  516. },
  517. {
  518. api.HookReleaseDeleted,
  519. "[test/repo] Release deleted: v1.0 by user1",
  520. redColor,
  521. },
  522. }
  523. for i, c := range cases {
  524. p.Action = c.action
  525. text, color := getReleasePayloadInfo(p, noneLinkFormatter, true)
  526. assert.Equal(t, c.text, text, "case %d", i)
  527. assert.Equal(t, c.color, color, "case %d", i)
  528. }
  529. }
  530. func TestGetIssueCommentPayloadInfo(t *testing.T) {
  531. p := pullRequestCommentTestPayload()
  532. cases := []struct {
  533. action api.HookIssueCommentAction
  534. text string
  535. issueTitle string
  536. color int
  537. }{
  538. {
  539. api.HookIssueCommentCreated,
  540. "[test/repo] New comment on pull request #12 Fix bug by user1",
  541. "#12 Fix bug",
  542. greenColorLight,
  543. },
  544. {
  545. api.HookIssueCommentEdited,
  546. "[test/repo] Comment edited on pull request #12 Fix bug by user1",
  547. "#12 Fix bug",
  548. yellowColor,
  549. },
  550. {
  551. api.HookIssueCommentDeleted,
  552. "[test/repo] Comment deleted on pull request #12 Fix bug by user1",
  553. "#12 Fix bug",
  554. redColor,
  555. },
  556. }
  557. for i, c := range cases {
  558. p.Action = c.action
  559. text, issueTitle, color := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
  560. assert.Equal(t, c.text, text, "case %d", i)
  561. assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
  562. assert.Equal(t, c.color, color, "case %d", i)
  563. }
  564. }