test_pagure_flask_api_pr_flag.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import json
  12. import pagure_messages
  13. from fedora_messaging import api, testing
  14. from mock import ANY, patch, MagicMock
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.config # noqa
  19. import pagure.lib.query # noqa
  20. import tests # noqa
  21. class PagureFlaskApiPRFlagtests(tests.Modeltests):
  22. """Tests for the flask API of pagure for flagging pull-requests"""
  23. maxDiff = None
  24. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  25. def setUp(self):
  26. """Set up the environnment, ran before every tests."""
  27. super(PagureFlaskApiPRFlagtests, self).setUp()
  28. pagure.config.config["REQUESTS_FOLDER"] = None
  29. tests.create_projects(self.session)
  30. tests.create_tokens(self.session)
  31. tests.create_tokens_acl(self.session)
  32. # Create a pull-request
  33. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  34. forked_repo = pagure.lib.query.get_authorized_project(
  35. self.session, "test"
  36. )
  37. req = pagure.lib.query.new_pull_request(
  38. session=self.session,
  39. repo_from=forked_repo,
  40. branch_from="master",
  41. repo_to=repo,
  42. branch_to="master",
  43. title="test pull-request",
  44. user="pingou",
  45. )
  46. self.session.commit()
  47. self.assertEqual(req.id, 1)
  48. self.assertEqual(req.title, "test pull-request")
  49. # Check flags before
  50. self.session.commit()
  51. request = pagure.lib.query.search_pull_requests(
  52. self.session, project_id=1, requestid=1
  53. )
  54. request.commit_stop = "hash_commit_stop"
  55. self.session.add(request)
  56. self.session.commit()
  57. self.assertEqual(len(request.flags), 0)
  58. def test_invalid_project(self):
  59. """Test the flagging a PR on an invalid project."""
  60. headers = {"Authorization": "token aaabbbcccddd"}
  61. # Invalid project
  62. output = self.app.post(
  63. "/api/0/foo/pull-request/1/flag", headers=headers
  64. )
  65. self.assertEqual(output.status_code, 404)
  66. data = json.loads(output.get_data(as_text=True))
  67. self.assertDictEqual(
  68. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  69. )
  70. def test_incorrect_project(self):
  71. """Test the flagging a PR on the wrong project."""
  72. headers = {"Authorization": "token aaabbbcccddd"}
  73. # Valid token, wrong project
  74. output = self.app.post(
  75. "/api/0/test2/pull-request/1/flag", headers=headers
  76. )
  77. self.assertEqual(output.status_code, 401)
  78. data = json.loads(output.get_data(as_text=True))
  79. self.assertEqual(
  80. pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
  81. )
  82. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
  83. def test_pr_disabled(self):
  84. """Test the flagging a PR when PRs are disabled."""
  85. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  86. settings = repo.settings
  87. settings["pull_requests"] = False
  88. repo.settings = settings
  89. self.session.add(repo)
  90. self.session.commit()
  91. headers = {"Authorization": "token aaabbbcccddd"}
  92. # PRs disabled
  93. output = self.app.post(
  94. "/api/0/test/pull-request/1/flag", headers=headers
  95. )
  96. self.assertEqual(output.status_code, 404)
  97. data = json.loads(output.get_data(as_text=True))
  98. self.assertDictEqual(
  99. data,
  100. {
  101. "error": "Pull-Request have been deactivated for this project",
  102. "error_code": "EPULLREQUESTSDISABLED",
  103. },
  104. )
  105. def test_no_pr(self):
  106. """Test the flagging a PR when the PR doesn't exist."""
  107. headers = {"Authorization": "token aaabbbcccddd"}
  108. # No PR
  109. output = self.app.post(
  110. "/api/0/test/pull-request/10/flag", headers=headers
  111. )
  112. self.assertEqual(output.status_code, 404)
  113. data = json.loads(output.get_data(as_text=True))
  114. self.assertDictEqual(
  115. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  116. )
  117. def test_no_input(self):
  118. """Test the flagging an existing PR but with no data."""
  119. headers = {"Authorization": "token aaabbbcccddd"}
  120. # No input
  121. output = self.app.post(
  122. "/api/0/test/pull-request/1/flag", headers=headers
  123. )
  124. self.assertEqual(output.status_code, 400)
  125. data = json.loads(output.get_data(as_text=True))
  126. self.assertDictEqual(
  127. data,
  128. {
  129. "error": "Invalid or incomplete input submitted",
  130. "error_code": "EINVALIDREQ",
  131. "errors": {
  132. "comment": ["This field is required."],
  133. "url": ["This field is required."],
  134. "username": ["This field is required."],
  135. },
  136. },
  137. )
  138. def test_no_comment(self):
  139. """Test the flagging an existing PR but with incomplete data."""
  140. headers = {"Authorization": "token aaabbbcccddd"}
  141. data = {
  142. "username": "Jenkins",
  143. "percent": 100,
  144. "url": "http://jenkins.cloud.fedoraproject.org/",
  145. "uid": "jenkins_build_pagure_100+seed",
  146. }
  147. # Incomplete request
  148. output = self.app.post(
  149. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  150. )
  151. self.assertEqual(output.status_code, 400)
  152. data = json.loads(output.get_data(as_text=True))
  153. self.assertDictEqual(
  154. data,
  155. {
  156. "error": "Invalid or incomplete input submitted",
  157. "error_code": "EINVALIDREQ",
  158. "errors": {"comment": ["This field is required."]},
  159. },
  160. )
  161. # No change
  162. self.session.commit()
  163. request = pagure.lib.query.search_pull_requests(
  164. self.session, project_id=1, requestid=1
  165. )
  166. self.assertEqual(len(request.flags), 0)
  167. @patch(
  168. "pagure.lib.query.add_pull_request_flag",
  169. MagicMock(side_effect=pagure.exceptions.PagureException("error")),
  170. )
  171. def test_raise_exception(self):
  172. """Test the flagging a PR when adding a flag raises an exception."""
  173. headers = {"Authorization": "token aaabbbcccddd"}
  174. data = {
  175. "username": "Jenkins",
  176. "comment": "Tests running",
  177. "url": "http://jenkins.cloud.fedoraproject.org/",
  178. "uid": "jenkins_build_pagure_100+seed",
  179. }
  180. # Adding a flag raises an exception
  181. output = self.app.post(
  182. "/api/0/test/pull-request/1/flag", headers=headers, data=data
  183. )
  184. self.assertEqual(output.status_code, 400)
  185. data = json.loads(output.get_data(as_text=True))
  186. self.assertDictEqual(data, {"error": "error", "error_code": "ENOCODE"})
  187. @patch.dict(
  188. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  189. )
  190. @patch("pagure.lib.notify.send_email")
  191. def test_flagging_a_pul_request_with_notification(self, mock_email):
  192. """Test the flagging a PR."""
  193. headers = {"Authorization": "token aaabbbcccddd"}
  194. # Enable PR notifications
  195. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  196. settings = repo.settings
  197. settings["notify_on_pull-request_flag"] = True
  198. repo.settings = settings
  199. self.session.add(repo)
  200. self.session.commit()
  201. data = {
  202. "username": "Jenkins",
  203. "comment": "Tests running",
  204. "url": "http://jenkins.cloud.fedoraproject.org/",
  205. "uid": "jenkins_build_pagure_100+seed",
  206. }
  207. # Valid request
  208. with testing.mock_sends(
  209. pagure_messages.CommitFlagAddedV1,
  210. pagure_messages.PullRequestFlagAddedV1(
  211. topic="pagure.pull-request.flag.added",
  212. body={
  213. "pullrequest": {
  214. "id": 1,
  215. "uid": ANY,
  216. "title": "test pull-request",
  217. "branch": "master",
  218. "project": {
  219. "id": 1,
  220. "name": "test",
  221. "fullname": "test",
  222. "url_path": "test",
  223. "description": "test project #1",
  224. "full_url": "http://localhost.localdomain/test",
  225. "namespace": None,
  226. "parent": None,
  227. "date_created": ANY,
  228. "date_modified": ANY,
  229. "user": {
  230. "name": "pingou",
  231. "fullname": "PY C",
  232. "full_url": "http://localhost.localdomain/user/pingou",
  233. "url_path": "user/pingou",
  234. },
  235. "access_users": {
  236. "owner": ["pingou"],
  237. "admin": [],
  238. "commit": [],
  239. "collaborator": [],
  240. "ticket": [],
  241. },
  242. "access_groups": {
  243. "admin": [],
  244. "commit": [],
  245. "collaborator": [],
  246. "ticket": [],
  247. },
  248. "tags": [],
  249. "priorities": {},
  250. "custom_keys": [],
  251. "close_status": [
  252. "Invalid",
  253. "Insufficient data",
  254. "Fixed",
  255. "Duplicate",
  256. ],
  257. "milestones": {},
  258. },
  259. "branch_from": "master",
  260. "repo_from": {
  261. "id": 1,
  262. "name": "test",
  263. "fullname": "test",
  264. "url_path": "test",
  265. "description": "test project #1",
  266. "full_url": "http://localhost.localdomain/test",
  267. "namespace": None,
  268. "parent": None,
  269. "date_created": ANY,
  270. "date_modified": ANY,
  271. "user": {
  272. "name": "pingou",
  273. "fullname": "PY C",
  274. "full_url": "http://localhost.localdomain/user/pingou",
  275. "url_path": "user/pingou",
  276. },
  277. "access_users": {
  278. "owner": ["pingou"],
  279. "admin": [],
  280. "commit": [],
  281. "collaborator": [],
  282. "ticket": [],
  283. },
  284. "access_groups": {
  285. "admin": [],
  286. "commit": [],
  287. "collaborator": [],
  288. "ticket": [],
  289. },
  290. "tags": [],
  291. "priorities": {},
  292. "custom_keys": [],
  293. "close_status": [
  294. "Invalid",
  295. "Insufficient data",
  296. "Fixed",
  297. "Duplicate",
  298. ],
  299. "milestones": {},
  300. },
  301. "remote_git": None,
  302. "date_created": ANY,
  303. "full_url": "http://localhost.localdomain/test/pull-request/1",
  304. "updated_on": ANY,
  305. "last_updated": ANY,
  306. "closed_at": None,
  307. "user": {
  308. "name": "pingou",
  309. "fullname": "PY C",
  310. "full_url": "http://localhost.localdomain/user/pingou",
  311. "url_path": "user/pingou",
  312. },
  313. "assignee": None,
  314. "status": "Open",
  315. "commit_start": None,
  316. "commit_stop": "hash_commit_stop",
  317. "closed_by": None,
  318. "initial_comment": None,
  319. "cached_merge_status": "unknown",
  320. "threshold_reached": None,
  321. "tags": [],
  322. "comments": [],
  323. },
  324. "flag": {
  325. "commit_hash": "hash_commit_stop",
  326. "username": "Jenkins",
  327. "percent": None,
  328. "comment": "Tests running",
  329. "status": "pending",
  330. "url": "http://jenkins.cloud.fedoraproject.org/",
  331. "date_created": ANY,
  332. "date_updated": ANY,
  333. "user": {
  334. "name": "pingou",
  335. "fullname": "PY C",
  336. "full_url": "http://localhost.localdomain/user/pingou",
  337. "url_path": "user/pingou",
  338. },
  339. },
  340. "agent": "pingou",
  341. },
  342. ),
  343. ):
  344. output = self.app.post(
  345. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  346. )
  347. self.assertEqual(output.status_code, 200)
  348. data = json.loads(output.get_data(as_text=True))
  349. data["flag"]["date_created"] = "1510742565"
  350. data["flag"]["date_updated"] = "1510742565"
  351. commit_hash = data["flag"]["commit_hash"]
  352. data["flag"]["commit_hash"] = "62b49f00d489452994de5010565fab81"
  353. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  354. self.assertDictEqual(
  355. data,
  356. {
  357. "flag": {
  358. "comment": "Tests running",
  359. "date_created": "1510742565",
  360. "date_updated": "1510742565",
  361. "percent": None,
  362. "commit_hash": "62b49f00d489452994de5010565fab81",
  363. "status": "pending",
  364. "url": "http://jenkins.cloud.fedoraproject.org/",
  365. "user": {
  366. "default_email": "bar@pingou.com",
  367. "emails": ["bar@pingou.com", "foo@pingou.com"],
  368. "full_url": "http://localhost.localdomain/user/pingou",
  369. "fullname": "PY C",
  370. "name": "pingou",
  371. "url_path": "user/pingou",
  372. },
  373. "username": "Jenkins",
  374. },
  375. "message": "Flag added",
  376. "uid": "jenkins_build_pagure_100+seed",
  377. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  378. "user": "pingou",
  379. },
  380. )
  381. # One flag added
  382. self.session.commit()
  383. request = pagure.lib.query.search_pull_requests(
  384. self.session, project_id=1, requestid=1
  385. )
  386. self.assertEqual(len(request.flags), 0)
  387. flags = pagure.lib.query.get_commit_flag(
  388. self.session, request.project, commit_hash
  389. )
  390. self.assertEqual(flags[0].comment, "Tests running")
  391. self.assertEqual(flags[0].percent, None)
  392. # Check the notification sent
  393. mock_email.assert_called_once_with(
  394. "\nJenkins flagged the pull-request `test pull-request` "
  395. "as pending: Tests running\n\n"
  396. "http://localhost.localdomain/test/pull-request/1\n",
  397. "PR #1 - Jenkins: pending",
  398. "bar@pingou.com",
  399. assignee=None,
  400. in_reply_to="test-pull-request-%s" % request.uid,
  401. mail_id="test-commit-1-1",
  402. project_name="test",
  403. reporter="pingou",
  404. user_from="Jenkins",
  405. )
  406. @patch.dict(
  407. "pagure.config.config", {"FEDORA_MESSAGING_NOTIFICATIONS": True}
  408. )
  409. def test_updating_flag(self):
  410. """Test the updating the flag of a PR."""
  411. headers = {"Authorization": "token aaabbbcccddd"}
  412. data = {
  413. "username": "Jenkins",
  414. "comment": "Tests running",
  415. "url": "http://jenkins.cloud.fedoraproject.org/",
  416. "uid": "jenkins_build_pagure_100+seed",
  417. }
  418. # Valid request
  419. output = self.app.post(
  420. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  421. )
  422. self.assertEqual(output.status_code, 200)
  423. data = json.loads(output.get_data(as_text=True))
  424. data["flag"]["date_created"] = "1510742565"
  425. data["flag"]["date_updated"] = "1510742565"
  426. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  427. self.assertDictEqual(
  428. data,
  429. {
  430. "flag": {
  431. "comment": "Tests running",
  432. "commit_hash": "hash_commit_stop",
  433. "date_created": "1510742565",
  434. "date_updated": "1510742565",
  435. "percent": None,
  436. "status": "pending",
  437. "url": "http://jenkins.cloud.fedoraproject.org/",
  438. "user": {
  439. "default_email": "bar@pingou.com",
  440. "emails": ["bar@pingou.com", "foo@pingou.com"],
  441. "full_url": "http://localhost.localdomain/user/pingou",
  442. "fullname": "PY C",
  443. "name": "pingou",
  444. "url_path": "user/pingou",
  445. },
  446. "username": "Jenkins",
  447. },
  448. "message": "Flag added",
  449. "uid": "jenkins_build_pagure_100+seed",
  450. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  451. "user": "pingou",
  452. },
  453. )
  454. # One flag added
  455. self.session.commit()
  456. request = pagure.lib.query.search_pull_requests(
  457. self.session, project_id=1, requestid=1
  458. )
  459. self.assertEqual(len(request.flags), 0)
  460. flags = pagure.lib.query.get_commit_flag(
  461. self.session, request.project, "hash_commit_stop"
  462. )
  463. self.assertEqual(flags[0].comment, "Tests running")
  464. self.assertEqual(flags[0].percent, None)
  465. # Update flag - w/o providing the status
  466. data = {
  467. "username": "Jenkins",
  468. "percent": 100,
  469. "comment": "Tests passed",
  470. "url": "http://jenkins.cloud.fedoraproject.org/",
  471. "uid": "jenkins_build_pagure_100+seed",
  472. }
  473. with testing.mock_sends(
  474. pagure_messages.CommitFlagUpdatedV1,
  475. pagure_messages.PullRequestFlagUpdatedV1(
  476. topic="pagure.pull-request.flag.updated",
  477. body={
  478. "pullrequest": {
  479. "id": 1,
  480. "uid": ANY,
  481. "title": "test pull-request",
  482. "full_url": "http://localhost.localdomain/test/pull-request/1",
  483. "branch": "master",
  484. "project": {
  485. "id": 1,
  486. "name": "test",
  487. "fullname": "test",
  488. "url_path": "test",
  489. "description": "test project #1",
  490. "full_url": "http://localhost.localdomain/test",
  491. "namespace": None,
  492. "parent": None,
  493. "date_created": ANY,
  494. "date_modified": ANY,
  495. "user": {
  496. "name": "pingou",
  497. "fullname": "PY C",
  498. "full_url": "http://localhost.localdomain/user/pingou",
  499. "url_path": "user/pingou",
  500. },
  501. "access_users": {
  502. "owner": ["pingou"],
  503. "admin": [],
  504. "commit": [],
  505. "collaborator": [],
  506. "ticket": [],
  507. },
  508. "access_groups": {
  509. "admin": [],
  510. "commit": [],
  511. "collaborator": [],
  512. "ticket": [],
  513. },
  514. "tags": [],
  515. "priorities": {},
  516. "custom_keys": [],
  517. "close_status": [
  518. "Invalid",
  519. "Insufficient data",
  520. "Fixed",
  521. "Duplicate",
  522. ],
  523. "milestones": {},
  524. },
  525. "branch_from": "master",
  526. "repo_from": {
  527. "id": 1,
  528. "name": "test",
  529. "fullname": "test",
  530. "url_path": "test",
  531. "description": "test project #1",
  532. "full_url": "http://localhost.localdomain/test",
  533. "namespace": None,
  534. "parent": None,
  535. "date_created": ANY,
  536. "date_modified": ANY,
  537. "user": {
  538. "name": "pingou",
  539. "fullname": "PY C",
  540. "full_url": "http://localhost.localdomain/user/pingou",
  541. "url_path": "user/pingou",
  542. },
  543. "access_users": {
  544. "owner": ["pingou"],
  545. "admin": [],
  546. "commit": [],
  547. "collaborator": [],
  548. "ticket": [],
  549. },
  550. "access_groups": {
  551. "admin": [],
  552. "commit": [],
  553. "collaborator": [],
  554. "ticket": [],
  555. },
  556. "tags": [],
  557. "priorities": {},
  558. "custom_keys": [],
  559. "close_status": [
  560. "Invalid",
  561. "Insufficient data",
  562. "Fixed",
  563. "Duplicate",
  564. ],
  565. "milestones": {},
  566. },
  567. "remote_git": None,
  568. "date_created": ANY,
  569. "updated_on": ANY,
  570. "last_updated": ANY,
  571. "closed_at": None,
  572. "user": {
  573. "name": "pingou",
  574. "fullname": "PY C",
  575. "full_url": "http://localhost.localdomain/user/pingou",
  576. "url_path": "user/pingou",
  577. },
  578. "assignee": None,
  579. "status": "Open",
  580. "commit_start": None,
  581. "commit_stop": "hash_commit_stop",
  582. "closed_by": None,
  583. "initial_comment": None,
  584. "cached_merge_status": "unknown",
  585. "threshold_reached": None,
  586. "tags": [],
  587. "comments": [],
  588. },
  589. "flag": {
  590. "commit_hash": "hash_commit_stop",
  591. "username": "Jenkins",
  592. "percent": 100,
  593. "comment": "Tests passed",
  594. "status": "success",
  595. "url": "http://jenkins.cloud.fedoraproject.org/",
  596. "date_created": ANY,
  597. "date_updated": ANY,
  598. "user": {
  599. "name": "pingou",
  600. "fullname": "PY C",
  601. "full_url": "http://localhost.localdomain/user/pingou",
  602. "url_path": "user/pingou",
  603. },
  604. },
  605. "agent": "pingou",
  606. },
  607. ),
  608. ):
  609. output = self.app.post(
  610. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  611. )
  612. self.assertEqual(output.status_code, 200)
  613. data = json.loads(output.get_data(as_text=True))
  614. data["flag"]["date_created"] = "1510742565"
  615. data["flag"]["date_updated"] = "1510742565"
  616. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  617. self.assertDictEqual(
  618. data,
  619. {
  620. "flag": {
  621. "comment": "Tests passed",
  622. "commit_hash": "hash_commit_stop",
  623. "date_created": "1510742565",
  624. "date_updated": "1510742565",
  625. "percent": 100,
  626. "status": "success",
  627. "url": "http://jenkins.cloud.fedoraproject.org/",
  628. "user": {
  629. "default_email": "bar@pingou.com",
  630. "emails": ["bar@pingou.com", "foo@pingou.com"],
  631. "full_url": "http://localhost.localdomain/user/pingou",
  632. "fullname": "PY C",
  633. "name": "pingou",
  634. "url_path": "user/pingou",
  635. },
  636. "username": "Jenkins",
  637. },
  638. "message": "Flag updated",
  639. "uid": "jenkins_build_pagure_100+seed",
  640. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  641. "user": "pingou",
  642. },
  643. )
  644. # One flag added
  645. self.session.commit()
  646. request = pagure.lib.query.search_pull_requests(
  647. self.session, project_id=1, requestid=1
  648. )
  649. self.assertEqual(len(request.flags), 0)
  650. flags = pagure.lib.query.get_commit_flag(
  651. self.session, request.project, "hash_commit_stop"
  652. )
  653. self.assertEqual(flags[0].comment, "Tests passed")
  654. self.assertEqual(flags[0].percent, 100)
  655. def test_adding_two_flags(self):
  656. """Test the adding two flags to a PR."""
  657. headers = {"Authorization": "token aaabbbcccddd"}
  658. data = {
  659. "username": "Jenkins",
  660. "comment": "Tests passed",
  661. "status": "success",
  662. "percent": "100",
  663. "url": "http://jenkins.cloud.fedoraproject.org/",
  664. "uid": "jenkins_build_pagure_100+seed",
  665. }
  666. # Valid request
  667. output = self.app.post(
  668. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  669. )
  670. self.assertEqual(output.status_code, 200)
  671. data = json.loads(output.get_data(as_text=True))
  672. data["flag"]["date_created"] = "1510742565"
  673. data["flag"]["date_updated"] = "1510742565"
  674. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  675. self.assertDictEqual(
  676. data,
  677. {
  678. "flag": {
  679. "comment": "Tests passed",
  680. "commit_hash": "hash_commit_stop",
  681. "date_created": "1510742565",
  682. "date_updated": "1510742565",
  683. "percent": 100,
  684. "status": "success",
  685. "url": "http://jenkins.cloud.fedoraproject.org/",
  686. "user": {
  687. "default_email": "bar@pingou.com",
  688. "emails": ["bar@pingou.com", "foo@pingou.com"],
  689. "full_url": "http://localhost.localdomain/user/pingou",
  690. "fullname": "PY C",
  691. "name": "pingou",
  692. "url_path": "user/pingou",
  693. },
  694. "username": "Jenkins",
  695. },
  696. "message": "Flag added",
  697. "uid": "jenkins_build_pagure_100+seed",
  698. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  699. "user": "pingou",
  700. },
  701. )
  702. # One flag added - but no longer on the request object
  703. self.session.commit()
  704. request = pagure.lib.query.search_pull_requests(
  705. self.session, project_id=1, requestid=1
  706. )
  707. self.assertEqual(len(request.flags), 0)
  708. flags = pagure.lib.query.get_commit_flag(
  709. self.session, request.project, "hash_commit_stop"
  710. )
  711. self.assertEqual(len(flags), 1)
  712. self.assertEqual(flags[0].comment, "Tests passed")
  713. self.assertEqual(flags[0].percent, 100)
  714. data = {
  715. "username": "Jenkins",
  716. "comment": "Tests running again",
  717. "url": "http://jenkins.cloud.fedoraproject.org/",
  718. }
  719. # Valid request
  720. output = self.app.post(
  721. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  722. )
  723. self.assertEqual(output.status_code, 200)
  724. data = json.loads(output.get_data(as_text=True))
  725. data["flag"]["date_created"] = "1510742565"
  726. data["flag"]["date_updated"] = "1510742565"
  727. self.assertNotEqual(data["uid"], "jenkins_build_pagure_100+seed")
  728. data["uid"] = "jenkins_build_pagure_100+seed"
  729. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  730. self.assertDictEqual(
  731. data,
  732. {
  733. "flag": {
  734. "comment": "Tests running again",
  735. "commit_hash": "hash_commit_stop",
  736. "date_created": "1510742565",
  737. "date_updated": "1510742565",
  738. "percent": None,
  739. "status": "pending",
  740. "url": "http://jenkins.cloud.fedoraproject.org/",
  741. "user": {
  742. "default_email": "bar@pingou.com",
  743. "emails": ["bar@pingou.com", "foo@pingou.com"],
  744. "full_url": "http://localhost.localdomain/user/pingou",
  745. "fullname": "PY C",
  746. "name": "pingou",
  747. "url_path": "user/pingou",
  748. },
  749. "username": "Jenkins",
  750. },
  751. "message": "Flag added",
  752. "uid": "jenkins_build_pagure_100+seed",
  753. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  754. "user": "pingou",
  755. },
  756. )
  757. # Two flags added - but no longer on the request object
  758. self.session.commit()
  759. request = pagure.lib.query.search_pull_requests(
  760. self.session, project_id=1, requestid=1
  761. )
  762. self.assertEqual(len(request.flags), 0)
  763. flags = pagure.lib.query.get_commit_flag(
  764. self.session, request.project, "hash_commit_stop"
  765. )
  766. self.assertEqual(len(flags), 2)
  767. self.assertEqual(flags[1].comment, "Tests running again")
  768. self.assertEqual(flags[1].percent, None)
  769. self.assertEqual(flags[0].comment, "Tests passed")
  770. self.assertEqual(flags[0].percent, 100)
  771. @patch.dict(
  772. "pagure.config.config",
  773. {
  774. "FLAG_STATUSES_LABELS": {
  775. "pend!": "label-info",
  776. "succeed!": "label-success",
  777. "fail!": "label-danger",
  778. "what?": "label-warning",
  779. },
  780. "FLAG_PENDING": "pend!",
  781. "FLAG_SUCCESS": "succeed!",
  782. "FLAG_FAILURE": "fail!",
  783. },
  784. )
  785. def test_flagging_a_pull_request_while_having_custom_statuses(self):
  786. """Test flagging a PR while having custom statuses."""
  787. headers = {"Authorization": "token aaabbbcccddd"}
  788. # No status and no percent => should use FLAG_PENDING
  789. send_data = {
  790. "username": "Jenkins",
  791. "comment": "Tests running",
  792. "url": "http://jenkins.cloud.fedoraproject.org/",
  793. "uid": "jenkins_build_pagure_100+seed",
  794. }
  795. output = self.app.post(
  796. "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
  797. )
  798. data = json.loads(output.get_data(as_text=True))
  799. self.assertEqual(output.status_code, 200)
  800. self.assertEqual(data["flag"]["status"], "pend!")
  801. # No status and 50 % => should use FLAG_SUCCESS
  802. send_data["percent"] = 50
  803. output = self.app.post(
  804. "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
  805. )
  806. data = json.loads(output.get_data(as_text=True))
  807. self.assertEqual(output.status_code, 200)
  808. self.assertEqual(data["flag"]["status"], "succeed!")
  809. # No status and 0 % => should use FLAG_FAILURE
  810. send_data["percent"] = 0
  811. output = self.app.post(
  812. "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
  813. )
  814. data = json.loads(output.get_data(as_text=True))
  815. self.assertEqual(output.status_code, 200)
  816. self.assertEqual(data["flag"]["status"], "fail!")
  817. # Explicitly set status
  818. send_data["status"] = "what?"
  819. output = self.app.post(
  820. "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
  821. )
  822. data = json.loads(output.get_data(as_text=True))
  823. self.assertEqual(output.status_code, 200)
  824. self.assertEqual(data["flag"]["status"], "what?")
  825. # Explicitly set wrong status
  826. send_data["status"] = "nooo....."
  827. output = self.app.post(
  828. "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
  829. )
  830. data = json.loads(output.get_data(as_text=True))
  831. self.assertEqual(output.status_code, 400)
  832. expected_output = {
  833. "error": "Invalid or incomplete input submitted",
  834. "error_code": "EINVALIDREQ",
  835. "errors": {"status": ["Not a valid choice"]},
  836. }
  837. if self.get_wtforms_version() >= (3, 0):
  838. expected_output["errors"]["status"] = ["Not a valid choice."]
  839. self.assertDictEqual(data, expected_output)
  840. class PagureFlaskApiPRFlagUserTokentests(tests.Modeltests):
  841. """Tests for the flask API of pagure for flagging pull-requests using
  842. an user token (ie: not restricted to a specific project).
  843. """
  844. maxDiff = None
  845. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  846. def setUp(self):
  847. """Set up the environnment, ran before every tests."""
  848. super(PagureFlaskApiPRFlagUserTokentests, self).setUp()
  849. pagure.config.config["REQUESTS_FOLDER"] = None
  850. tests.create_projects(self.session)
  851. tests.create_tokens(self.session, project_id=None)
  852. tests.create_tokens_acl(self.session)
  853. # Create a pull-request
  854. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  855. forked_repo = pagure.lib.query.get_authorized_project(
  856. self.session, "test"
  857. )
  858. req = pagure.lib.query.new_pull_request(
  859. session=self.session,
  860. repo_from=forked_repo,
  861. branch_from="master",
  862. repo_to=repo,
  863. branch_to="master",
  864. title="test pull-request",
  865. user="pingou",
  866. )
  867. self.session.commit()
  868. self.assertEqual(req.id, 1)
  869. self.assertEqual(req.title, "test pull-request")
  870. # Check flags before
  871. self.session.commit()
  872. request = pagure.lib.query.search_pull_requests(
  873. self.session, project_id=1, requestid=1
  874. )
  875. request.commit_stop = "hash_commit_stop"
  876. self.session.add(request)
  877. self.session.commit()
  878. self.assertEqual(len(request.flags), 0)
  879. def test_no_pr(self):
  880. """Test flagging a non-existing PR."""
  881. headers = {"Authorization": "token aaabbbcccddd"}
  882. # Invalid project
  883. output = self.app.post(
  884. "/api/0/foo/pull-request/1/flag", headers=headers
  885. )
  886. self.assertEqual(output.status_code, 404)
  887. data = json.loads(output.get_data(as_text=True))
  888. self.assertDictEqual(
  889. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  890. )
  891. def test_no_pr_other_project(self):
  892. """Test flagging a non-existing PR on a different project."""
  893. headers = {"Authorization": "token aaabbbcccddd"}
  894. # Valid token, wrong project
  895. output = self.app.post(
  896. "/api/0/test2/pull-request/1/flag", headers=headers
  897. )
  898. self.assertEqual(output.status_code, 404)
  899. data = json.loads(output.get_data(as_text=True))
  900. self.assertDictEqual(
  901. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  902. )
  903. def test_no_input(self):
  904. """Test flagging an existing PR but without submitting any data."""
  905. headers = {"Authorization": "token aaabbbcccddd"}
  906. # No input
  907. output = self.app.post(
  908. "/api/0/test/pull-request/1/flag", headers=headers
  909. )
  910. self.assertEqual(output.status_code, 400)
  911. data = json.loads(output.get_data(as_text=True))
  912. self.assertDictEqual(
  913. data,
  914. {
  915. "error": "Invalid or incomplete input submitted",
  916. "error_code": "EINVALIDREQ",
  917. "errors": {
  918. "comment": ["This field is required."],
  919. "url": ["This field is required."],
  920. "username": ["This field is required."],
  921. },
  922. },
  923. )
  924. def test_no_comment(self):
  925. """Test flagging an existing PR but without all the required info."""
  926. headers = {"Authorization": "token aaabbbcccddd"}
  927. data = {
  928. "username": "Jenkins",
  929. "percent": 100,
  930. "url": "http://jenkins.cloud.fedoraproject.org/",
  931. "uid": "jenkins_build_pagure_100+seed",
  932. }
  933. # Incomplete request
  934. output = self.app.post(
  935. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  936. )
  937. self.assertEqual(output.status_code, 400)
  938. data = json.loads(output.get_data(as_text=True))
  939. self.assertDictEqual(
  940. data,
  941. {
  942. "error": "Invalid or incomplete input submitted",
  943. "error_code": "EINVALIDREQ",
  944. "errors": {"comment": ["This field is required."]},
  945. },
  946. )
  947. # No change
  948. self.session.commit()
  949. request = pagure.lib.query.search_pull_requests(
  950. self.session, project_id=1, requestid=1
  951. )
  952. self.assertEqual(len(request.flags), 0)
  953. def test_invalid_status(self):
  954. """Test flagging an existing PR but with an invalid status."""
  955. headers = {"Authorization": "token aaabbbcccddd"}
  956. data = {
  957. "username": "Jenkins",
  958. "status": "failed",
  959. "comment": "Failed to run the tests",
  960. "url": "http://jenkins.cloud.fedoraproject.org/",
  961. "uid": "jenkins_build_pagure_100+seed",
  962. }
  963. # Invalid status submitted
  964. output = self.app.post(
  965. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  966. )
  967. self.assertEqual(output.status_code, 400)
  968. data = json.loads(output.get_data(as_text=True))
  969. expected_output = {
  970. "error": "Invalid or incomplete input submitted",
  971. "error_code": "EINVALIDREQ",
  972. "errors": {"status": ["Not a valid choice"]},
  973. }
  974. if self.get_wtforms_version() >= (3, 0):
  975. expected_output["errors"]["status"] = ["Not a valid choice."]
  976. self.assertDictEqual(data, expected_output)
  977. # No change
  978. self.session.commit()
  979. request = pagure.lib.query.search_pull_requests(
  980. self.session, project_id=1, requestid=1
  981. )
  982. self.assertEqual(len(request.flags), 0)
  983. @patch("pagure.lib.notify.send_email")
  984. def test_flag_pr_no_status(self, mock_email):
  985. """Test flagging an existing PR without providing a status.
  986. Also check that no notifications have been sent.
  987. """
  988. headers = {"Authorization": "token aaabbbcccddd"}
  989. data = {
  990. "username": "Jenkins",
  991. "percent": 0,
  992. "comment": "Tests failed",
  993. "url": "http://jenkins.cloud.fedoraproject.org/",
  994. "uid": "jenkins_build_pagure_100+seed",
  995. }
  996. # Valid request - w/o providing the status
  997. output = self.app.post(
  998. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  999. )
  1000. self.assertEqual(output.status_code, 200)
  1001. data = json.loads(output.get_data(as_text=True))
  1002. data["flag"]["date_created"] = "1510742565"
  1003. data["flag"]["date_updated"] = "1510742565"
  1004. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1005. self.assertDictEqual(
  1006. data,
  1007. {
  1008. "flag": {
  1009. "comment": "Tests failed",
  1010. "commit_hash": "hash_commit_stop",
  1011. "date_created": "1510742565",
  1012. "date_updated": "1510742565",
  1013. "percent": 0,
  1014. "status": "failure",
  1015. "url": "http://jenkins.cloud.fedoraproject.org/",
  1016. "user": {
  1017. "default_email": "bar@pingou.com",
  1018. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1019. "full_url": "http://localhost.localdomain/user/pingou",
  1020. "fullname": "PY C",
  1021. "name": "pingou",
  1022. "url_path": "user/pingou",
  1023. },
  1024. "username": "Jenkins",
  1025. },
  1026. "message": "Flag added",
  1027. "uid": "jenkins_build_pagure_100+seed",
  1028. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1029. "user": "pingou",
  1030. },
  1031. )
  1032. # One flag added
  1033. self.session.commit()
  1034. request = pagure.lib.query.search_pull_requests(
  1035. self.session, project_id=1, requestid=1
  1036. )
  1037. self.assertEqual(len(request.flags), 0)
  1038. flags = pagure.lib.query.get_commit_flag(
  1039. self.session, request.project, "hash_commit_stop"
  1040. )
  1041. self.assertEqual(len(flags), 1)
  1042. self.assertEqual(flags[0].comment, "Tests failed")
  1043. self.assertEqual(flags[0].percent, 0)
  1044. # no notifications sent
  1045. mock_email.assert_not_called()
  1046. def test_editing_flag(self):
  1047. """Test flagging an existing PR without providing a status."""
  1048. headers = {"Authorization": "token aaabbbcccddd"}
  1049. data = {
  1050. "username": "Jenkins",
  1051. "status": "failure",
  1052. "comment": "Tests failed",
  1053. "url": "http://jenkins.cloud.fedoraproject.org/",
  1054. "uid": "jenkins_build_pagure_100+seed",
  1055. }
  1056. # Valid request - w/o providing the status
  1057. output = self.app.post(
  1058. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  1059. )
  1060. self.assertEqual(output.status_code, 200)
  1061. data = json.loads(output.get_data(as_text=True))
  1062. data["flag"]["date_created"] = "1510742565"
  1063. data["flag"]["date_updated"] = "1510742565"
  1064. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1065. self.assertDictEqual(
  1066. data,
  1067. {
  1068. "flag": {
  1069. "comment": "Tests failed",
  1070. "commit_hash": "hash_commit_stop",
  1071. "date_created": "1510742565",
  1072. "date_updated": "1510742565",
  1073. "percent": None,
  1074. "status": "failure",
  1075. "url": "http://jenkins.cloud.fedoraproject.org/",
  1076. "user": {
  1077. "default_email": "bar@pingou.com",
  1078. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1079. "full_url": "http://localhost.localdomain/user/pingou",
  1080. "fullname": "PY C",
  1081. "name": "pingou",
  1082. "url_path": "user/pingou",
  1083. },
  1084. "username": "Jenkins",
  1085. },
  1086. "message": "Flag added",
  1087. "uid": "jenkins_build_pagure_100+seed",
  1088. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1089. "user": "pingou",
  1090. },
  1091. )
  1092. # One flag added
  1093. self.session.commit()
  1094. request = pagure.lib.query.search_pull_requests(
  1095. self.session, project_id=1, requestid=1
  1096. )
  1097. self.assertEqual(len(request.flags), 0)
  1098. flags = pagure.lib.query.get_commit_flag(
  1099. self.session, request.project, "hash_commit_stop"
  1100. )
  1101. self.assertEqual(len(flags), 1)
  1102. self.assertEqual(flags[0].comment, "Tests failed")
  1103. self.assertEqual(flags[0].percent, None)
  1104. # Update flag
  1105. data = {
  1106. "username": "Jenkins",
  1107. "percent": 100,
  1108. "comment": "Tests passed",
  1109. "url": "http://jenkins.cloud.fedoraproject.org/",
  1110. "uid": "jenkins_build_pagure_100+seed",
  1111. "status": "success",
  1112. }
  1113. output = self.app.post(
  1114. "/api/0/test/pull-request/1/flag", data=data, headers=headers
  1115. )
  1116. self.assertEqual(output.status_code, 200)
  1117. data = json.loads(output.get_data(as_text=True))
  1118. data["flag"]["date_created"] = "1510742565"
  1119. data["flag"]["date_updated"] = "1510742565"
  1120. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  1121. self.assertDictEqual(
  1122. data,
  1123. {
  1124. "flag": {
  1125. "comment": "Tests passed",
  1126. "commit_hash": "hash_commit_stop",
  1127. "date_created": "1510742565",
  1128. "date_updated": "1510742565",
  1129. "percent": 100,
  1130. "status": "success",
  1131. "url": "http://jenkins.cloud.fedoraproject.org/",
  1132. "user": {
  1133. "default_email": "bar@pingou.com",
  1134. "emails": ["bar@pingou.com", "foo@pingou.com"],
  1135. "full_url": "http://localhost.localdomain/user/pingou",
  1136. "fullname": "PY C",
  1137. "name": "pingou",
  1138. "url_path": "user/pingou",
  1139. },
  1140. "username": "Jenkins",
  1141. },
  1142. "message": "Flag updated",
  1143. "uid": "jenkins_build_pagure_100+seed",
  1144. "avatar_url": "https://seccdn.libravatar.org/avatar/...",
  1145. "user": "pingou",
  1146. },
  1147. )
  1148. # Still only one flag
  1149. self.session.commit()
  1150. request = pagure.lib.query.search_pull_requests(
  1151. self.session, project_id=1, requestid=1
  1152. )
  1153. self.assertEqual(len(request.flags), 0)
  1154. flags = pagure.lib.query.get_commit_flag(
  1155. self.session, request.project, "hash_commit_stop"
  1156. )
  1157. self.assertEqual(len(flags), 1)
  1158. self.assertEqual(flags[0].comment, "Tests passed")
  1159. self.assertEqual(flags[0].percent, 100)
  1160. class PagureFlaskApiGetPRFlagtests(tests.Modeltests):
  1161. """Tests for the flask API of pagure for retrieving pull-requests flags"""
  1162. maxDiff = None
  1163. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  1164. def setUp(self):
  1165. """Set up the environnment, ran before every tests."""
  1166. super(PagureFlaskApiGetPRFlagtests, self).setUp()
  1167. pagure.config.config["REQUESTS_FOLDER"] = None
  1168. tests.create_projects(self.session)
  1169. tests.create_tokens(self.session)
  1170. tests.create_tokens_acl(self.session)
  1171. # Create a pull-request
  1172. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1173. forked_repo = pagure.lib.query.get_authorized_project(
  1174. self.session, "test"
  1175. )
  1176. req = pagure.lib.query.new_pull_request(
  1177. session=self.session,
  1178. repo_from=forked_repo,
  1179. branch_from="master",
  1180. repo_to=repo,
  1181. branch_to="master",
  1182. title="test pull-request",
  1183. user="pingou",
  1184. )
  1185. self.session.commit()
  1186. self.assertEqual(req.id, 1)
  1187. self.assertEqual(req.title, "test pull-request")
  1188. # Check flags before
  1189. self.session.commit()
  1190. request = pagure.lib.query.search_pull_requests(
  1191. self.session, project_id=1, requestid=1
  1192. )
  1193. request.commit_stop = "hash_commit_stop"
  1194. self.session.add(request)
  1195. self.session.commit()
  1196. self.assertEqual(len(request.flags), 0)
  1197. def test_invalid_project(self):
  1198. """Test the retrieving the flags of a PR on an invalid project."""
  1199. # Invalid project
  1200. output = self.app.get("/api/0/foo/pull-request/1/flag")
  1201. self.assertEqual(output.status_code, 404)
  1202. data = json.loads(output.get_data(as_text=True))
  1203. self.assertDictEqual(
  1204. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  1205. )
  1206. def test_pr_disabled(self):
  1207. """Test the retrieving the flags of a PR when PRs are disabled."""
  1208. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  1209. settings = repo.settings
  1210. settings["pull_requests"] = False
  1211. repo.settings = settings
  1212. self.session.add(repo)
  1213. self.session.commit()
  1214. # PRs disabled
  1215. output = self.app.get("/api/0/test/pull-request/1/flag")
  1216. self.assertEqual(output.status_code, 404)
  1217. data = json.loads(output.get_data(as_text=True))
  1218. self.assertDictEqual(
  1219. data,
  1220. {
  1221. "error": "Pull-Request have been deactivated for this project",
  1222. "error_code": "EPULLREQUESTSDISABLED",
  1223. },
  1224. )
  1225. def test_no_pr(self):
  1226. """Test the retrieving the flags of a PR when the PR doesn't exist."""
  1227. # No PR
  1228. output = self.app.get("/api/0/test/pull-request/10/flag")
  1229. self.assertEqual(output.status_code, 404)
  1230. data = json.loads(output.get_data(as_text=True))
  1231. self.assertDictEqual(
  1232. data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
  1233. )
  1234. def test_no_flag(self):
  1235. """Test the retrieving the flags of a PR when the PR has no flags."""
  1236. # No flag
  1237. output = self.app.get("/api/0/test/pull-request/1/flag")
  1238. self.assertEqual(output.status_code, 200)
  1239. data = json.loads(output.get_data(as_text=True))
  1240. self.assertDictEqual(data, {"flags": []})
  1241. def test_get_flag(self):
  1242. """Test the retrieving the flags of a PR when the PR has one flag."""
  1243. # Add a flag to the PR
  1244. request = pagure.lib.query.search_pull_requests(
  1245. self.session, project_id=1, requestid=1
  1246. )
  1247. msg = pagure.lib.query.add_pull_request_flag(
  1248. session=self.session,
  1249. request=request,
  1250. username="jenkins",
  1251. percent=None,
  1252. comment="Build passes",
  1253. status="success",
  1254. url="http://jenkins.cloud.fedoraproject.org",
  1255. uid="jenkins_build_pagure_34",
  1256. user="foo",
  1257. token="aaabbbcccddd",
  1258. )
  1259. self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
  1260. self.session.commit()
  1261. self.assertEqual(len(request.flags), 0)
  1262. flags = pagure.lib.query.get_commit_flag(
  1263. self.session, request.project, "hash_commit_stop"
  1264. )
  1265. self.assertEqual(len(flags), 1)
  1266. self.assertEqual(flags[0].token_id, "aaabbbcccddd")
  1267. # 1 flag
  1268. output = self.app.get("/api/0/test/pull-request/1/flag")
  1269. self.assertEqual(output.status_code, 200)
  1270. data = json.loads(output.get_data(as_text=True))
  1271. data["flags"][0]["date_created"] = "1541413645"
  1272. data["flags"][0]["date_updated"] = "1541413645"
  1273. self.assertDictEqual(
  1274. data,
  1275. {
  1276. "flags": [
  1277. {
  1278. "comment": "Build passes",
  1279. "commit_hash": "hash_commit_stop",
  1280. "date_created": "1541413645",
  1281. "date_updated": "1541413645",
  1282. "percent": None,
  1283. "status": "success",
  1284. "url": "http://jenkins.cloud.fedoraproject.org",
  1285. "user": {
  1286. "fullname": "foo bar",
  1287. "full_url": "http://localhost.localdomain/user/foo",
  1288. "name": "foo",
  1289. "url_path": "user/foo",
  1290. },
  1291. "username": "jenkins",
  1292. }
  1293. ]
  1294. },
  1295. )
  1296. def test_get_flags(self):
  1297. """Test the retrieving the flags of a PR when the PR has one flag."""
  1298. # Add two flags to the PR
  1299. request = pagure.lib.query.search_pull_requests(
  1300. self.session, project_id=1, requestid=1
  1301. )
  1302. msg = pagure.lib.query.add_pull_request_flag(
  1303. session=self.session,
  1304. request=request,
  1305. username="jenkins",
  1306. percent=None,
  1307. comment="Build passes",
  1308. status="success",
  1309. url="http://jenkins.cloud.fedoraproject.org",
  1310. uid="jenkins_build_pagure_34",
  1311. user="foo",
  1312. token="aaabbbcccddd",
  1313. )
  1314. self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
  1315. self.session.commit()
  1316. msg = pagure.lib.query.add_pull_request_flag(
  1317. session=self.session,
  1318. request=request,
  1319. username="travis",
  1320. percent=None,
  1321. comment="Build pending",
  1322. status="pending",
  1323. url="http://travis.io",
  1324. uid="travis_build_pagure_34",
  1325. user="foo",
  1326. token="aaabbbcccddd",
  1327. )
  1328. self.assertEqual(msg, ("Flag added", "travis_build_pagure_34"))
  1329. self.session.commit()
  1330. self.assertEqual(len(request.flags), 0)
  1331. flags = pagure.lib.query.get_commit_flag(
  1332. self.session, request.project, "hash_commit_stop"
  1333. )
  1334. self.assertEqual(len(flags), 2)
  1335. self.assertEqual(flags[1].token_id, "aaabbbcccddd")
  1336. self.assertEqual(flags[0].token_id, "aaabbbcccddd")
  1337. # 1 flag
  1338. output = self.app.get("/api/0/test/pull-request/1/flag")
  1339. self.assertEqual(output.status_code, 200)
  1340. data = json.loads(output.get_data(as_text=True))
  1341. data["flags"][0]["date_created"] = "1541413645"
  1342. data["flags"][0]["date_updated"] = "1541413645"
  1343. data["flags"][1]["date_created"] = "1541413645"
  1344. data["flags"][1]["date_updated"] = "1541413645"
  1345. self.assertDictEqual(
  1346. data,
  1347. {
  1348. "flags": [
  1349. {
  1350. "comment": "Build passes",
  1351. "commit_hash": "hash_commit_stop",
  1352. "date_created": "1541413645",
  1353. "date_updated": "1541413645",
  1354. "percent": None,
  1355. "status": "success",
  1356. "url": "http://jenkins.cloud.fedoraproject.org",
  1357. "user": {
  1358. "fullname": "foo bar",
  1359. "name": "foo",
  1360. "full_url": "http://localhost.localdomain/user/foo",
  1361. "url_path": "user/foo",
  1362. },
  1363. "username": "jenkins",
  1364. },
  1365. {
  1366. "comment": "Build pending",
  1367. "commit_hash": "hash_commit_stop",
  1368. "date_created": "1541413645",
  1369. "date_updated": "1541413645",
  1370. "percent": None,
  1371. "status": "pending",
  1372. "url": "http://travis.io",
  1373. "user": {
  1374. "fullname": "foo bar",
  1375. "name": "foo",
  1376. "full_url": "http://localhost.localdomain/user/foo",
  1377. "url_path": "user/foo",
  1378. },
  1379. "username": "travis",
  1380. },
  1381. ]
  1382. },
  1383. )
  1384. if __name__ == "__main__":
  1385. unittest.main(verbosity=2)