test_pagure_flask_api_pr_flag.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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
  8. import unittest
  9. import sys
  10. import os
  11. import json
  12. from mock import patch, MagicMock
  13. sys.path.insert(0, os.path.join(os.path.dirname(
  14. os.path.abspath(__file__)), '..'))
  15. import pagure # noqa
  16. import pagure.config # noqa
  17. import pagure.lib # noqa
  18. import tests # noqa
  19. class PagureFlaskApiPRFlagtests(tests.Modeltests):
  20. """ Tests for the flask API of pagure for flagging pull-requests """
  21. maxDiff = None
  22. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskApiPRFlagtests, self).setUp()
  26. pagure.config.config['REQUESTS_FOLDER'] = None
  27. tests.create_projects(self.session)
  28. tests.create_tokens(self.session)
  29. tests.create_tokens_acl(self.session)
  30. # Create a pull-request
  31. repo = pagure.lib.get_authorized_project(self.session, 'test')
  32. forked_repo = pagure.lib.get_authorized_project(self.session, 'test')
  33. req = pagure.lib.new_pull_request(
  34. session=self.session,
  35. repo_from=forked_repo,
  36. branch_from='master',
  37. repo_to=repo,
  38. branch_to='master',
  39. title='test pull-request',
  40. user='pingou',
  41. )
  42. self.session.commit()
  43. self.assertEqual(req.id, 1)
  44. self.assertEqual(req.title, 'test pull-request')
  45. # Check flags before
  46. self.session.commit()
  47. request = pagure.lib.search_pull_requests(
  48. self.session, project_id=1, requestid=1)
  49. self.assertEqual(len(request.flags), 0)
  50. def test_invalid_project(self):
  51. """ Test the flagging a PR on an invalid project. """
  52. headers = {'Authorization': 'token aaabbbcccddd'}
  53. # Invalid project
  54. output = self.app.post(
  55. '/api/0/foo/pull-request/1/flag', headers=headers)
  56. self.assertEqual(output.status_code, 404)
  57. data = json.loads(output.get_data(as_text=True))
  58. self.assertDictEqual(
  59. data,
  60. {
  61. "error": "Project not found",
  62. "error_code": "ENOPROJECT",
  63. }
  64. )
  65. def test_incorrect_project(self):
  66. """ Test the flagging a PR on the wrong project. """
  67. headers = {'Authorization': 'token aaabbbcccddd'}
  68. # Valid token, wrong project
  69. output = self.app.post(
  70. '/api/0/test2/pull-request/1/flag', headers=headers)
  71. self.assertEqual(output.status_code, 401)
  72. data = json.loads(output.get_data(as_text=True))
  73. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  74. data['error_code'])
  75. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  76. def test_pr_disabled(self):
  77. """ Test the flagging a PR when PRs are disabled. """
  78. repo = pagure.lib.get_authorized_project(self.session, 'test')
  79. settings = repo.settings
  80. settings['pull_requests'] = False
  81. repo.settings = settings
  82. self.session.add(repo)
  83. self.session.commit()
  84. headers = {'Authorization': 'token aaabbbcccddd'}
  85. # PRs disabled
  86. output = self.app.post(
  87. '/api/0/test/pull-request/1/flag', headers=headers)
  88. self.assertEqual(output.status_code, 404)
  89. data = json.loads(output.get_data(as_text=True))
  90. self.assertDictEqual(
  91. data,
  92. {
  93. u'error': u'Pull-Request have been deactivated for this project',
  94. u'error_code': u'EPULLREQUESTSDISABLED'
  95. }
  96. )
  97. def test_no_pr(self):
  98. """ Test the flagging a PR when the PR doesn't exist. """
  99. headers = {'Authorization': 'token aaabbbcccddd'}
  100. # No PR
  101. output = self.app.post(
  102. '/api/0/test/pull-request/10/flag', headers=headers)
  103. self.assertEqual(output.status_code, 404)
  104. data = json.loads(output.get_data(as_text=True))
  105. self.assertDictEqual(
  106. data,
  107. {
  108. "error": "Pull-Request not found",
  109. "error_code": "ENOREQ",
  110. }
  111. )
  112. def test_no_input(self):
  113. """ Test the flagging an existing PR but with no data. """
  114. headers = {'Authorization': 'token aaabbbcccddd'}
  115. # No input
  116. output = self.app.post(
  117. '/api/0/test/pull-request/1/flag', headers=headers)
  118. self.assertEqual(output.status_code, 400)
  119. data = json.loads(output.get_data(as_text=True))
  120. self.assertDictEqual(
  121. data,
  122. {
  123. 'error': 'Invalid or incomplete input submitted',
  124. 'error_code': 'EINVALIDREQ',
  125. 'errors': {
  126. 'comment': ['This field is required.'],
  127. 'url': ['This field is required.'],
  128. 'username': ['This field is required.']
  129. }
  130. }
  131. )
  132. def test_no_comment(self):
  133. """ Test the flagging an existing PR but with incomplete data. """
  134. headers = {'Authorization': 'token aaabbbcccddd'}
  135. data = {
  136. 'username': 'Jenkins',
  137. 'percent': 100,
  138. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  139. 'uid': 'jenkins_build_pagure_100+seed',
  140. }
  141. # Incomplete request
  142. output = self.app.post(
  143. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  144. self.assertEqual(output.status_code, 400)
  145. data = json.loads(output.get_data(as_text=True))
  146. self.assertDictEqual(
  147. data,
  148. {
  149. "error": "Invalid or incomplete input submitted",
  150. "error_code": "EINVALIDREQ",
  151. "errors": {"comment": ["This field is required."]}
  152. }
  153. )
  154. # No change
  155. self.session.commit()
  156. request = pagure.lib.search_pull_requests(
  157. self.session, project_id=1, requestid=1)
  158. self.assertEqual(len(request.flags), 0)
  159. @patch(
  160. 'pagure.lib.add_pull_request_flag',
  161. MagicMock(side_effect=pagure.exceptions.PagureException('error')))
  162. def test_raise_exception(self):
  163. """ Test the flagging a PR when adding a flag raises an exception. """
  164. headers = {'Authorization': 'token aaabbbcccddd'}
  165. data = {
  166. 'username': 'Jenkins',
  167. 'comment': 'Tests running',
  168. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  169. 'uid': 'jenkins_build_pagure_100+seed',
  170. }
  171. # Adding a flag raises an exception
  172. output = self.app.post(
  173. '/api/0/test/pull-request/1/flag', headers=headers, data=data)
  174. self.assertEqual(output.status_code, 400)
  175. data = json.loads(output.get_data(as_text=True))
  176. self.assertDictEqual(
  177. data, {u'error': u'error', u'error_code': u'ENOCODE'})
  178. @patch('pagure.lib.notify.send_email')
  179. def test_flagging_a_pul_request_with_notification(self, mock_email):
  180. """ Test the flagging a PR. """
  181. headers = {'Authorization': 'token aaabbbcccddd'}
  182. # Enable PR notifications
  183. repo = pagure.lib.get_authorized_project(self.session, 'test')
  184. settings = repo.settings
  185. settings['notify_on_pull-request_flag'] = True
  186. repo.settings = settings
  187. self.session.add(repo)
  188. self.session.commit()
  189. data = {
  190. 'username': 'Jenkins',
  191. 'comment': 'Tests running',
  192. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  193. 'uid': 'jenkins_build_pagure_100+seed',
  194. }
  195. # Valid request
  196. output = self.app.post(
  197. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  198. self.assertEqual(output.status_code, 200)
  199. data = json.loads(output.get_data(as_text=True))
  200. data['flag']['date_created'] = '1510742565'
  201. pr_uid = data['flag']['pull_request_uid']
  202. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  203. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  204. self.assertDictEqual(
  205. data,
  206. {
  207. 'flag': {
  208. 'comment': 'Tests running',
  209. 'date_created': '1510742565',
  210. 'percent': None,
  211. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  212. 'status': 'pending',
  213. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  214. 'user': {
  215. 'default_email': 'bar@pingou.com',
  216. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  217. 'fullname': 'PY C',
  218. 'name': 'pingou'
  219. },
  220. 'username': 'Jenkins'},
  221. 'message': 'Flag added',
  222. 'uid': 'jenkins_build_pagure_100+seed',
  223. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  224. 'user': 'pingou'
  225. }
  226. )
  227. # One flag added
  228. self.session.commit()
  229. request = pagure.lib.search_pull_requests(
  230. self.session, project_id=1, requestid=1)
  231. self.assertEqual(len(request.flags), 1)
  232. self.assertEqual(request.flags[0].comment, 'Tests running')
  233. self.assertEqual(request.flags[0].percent, None)
  234. # Check the notification sent
  235. mock_email.assert_called_once_with(
  236. '\nJenkins flagged the pull-request `test pull-request` '
  237. 'as pending: Tests running\n\n'
  238. 'http://localhost.localdomain/test/pull-request/1\n',
  239. 'PR #1 - Jenkins: pending',
  240. 'bar@pingou.com',
  241. assignee=None,
  242. in_reply_to='test-pull-request-' + pr_uid,
  243. mail_id='test-pull-request-' + pr_uid + '-1',
  244. project_name='test',
  245. reporter='pingou',
  246. user_from='Jenkins'
  247. )
  248. def test_updating_flag(self):
  249. """ Test the updating the flag of a PR. """
  250. headers = {'Authorization': 'token aaabbbcccddd'}
  251. data = {
  252. 'username': 'Jenkins',
  253. 'comment': 'Tests running',
  254. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  255. 'uid': 'jenkins_build_pagure_100+seed',
  256. }
  257. # Valid request
  258. output = self.app.post(
  259. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  260. self.assertEqual(output.status_code, 200)
  261. data = json.loads(output.get_data(as_text=True))
  262. data['flag']['date_created'] = '1510742565'
  263. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  264. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  265. self.assertDictEqual(
  266. data,
  267. {
  268. 'flag': {
  269. 'comment': 'Tests running',
  270. 'date_created': '1510742565',
  271. 'percent': None,
  272. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  273. 'status': 'pending',
  274. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  275. 'user': {
  276. 'default_email': 'bar@pingou.com',
  277. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  278. 'fullname': 'PY C',
  279. 'name': 'pingou'
  280. },
  281. 'username': 'Jenkins'},
  282. 'message': 'Flag added',
  283. 'uid': 'jenkins_build_pagure_100+seed',
  284. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  285. 'user': 'pingou'
  286. }
  287. )
  288. # One flag added
  289. self.session.commit()
  290. request = pagure.lib.search_pull_requests(
  291. self.session, project_id=1, requestid=1)
  292. self.assertEqual(len(request.flags), 1)
  293. self.assertEqual(request.flags[0].comment, 'Tests running')
  294. self.assertEqual(request.flags[0].percent, None)
  295. # Update flag - w/o providing the status
  296. data = {
  297. 'username': 'Jenkins',
  298. 'percent': 100,
  299. 'comment': 'Tests passed',
  300. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  301. 'uid': 'jenkins_build_pagure_100+seed',
  302. }
  303. output = self.app.post(
  304. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  305. self.assertEqual(output.status_code, 200)
  306. data = json.loads(output.get_data(as_text=True))
  307. data['flag']['date_created'] = '1510742565'
  308. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  309. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  310. self.assertDictEqual(
  311. data,
  312. {
  313. 'flag': {
  314. 'comment': 'Tests passed',
  315. 'date_created': '1510742565',
  316. 'percent': 100,
  317. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  318. 'status': 'success',
  319. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  320. 'user': {
  321. 'default_email': 'bar@pingou.com',
  322. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  323. 'fullname': 'PY C',
  324. 'name': 'pingou'
  325. },
  326. 'username': 'Jenkins'},
  327. 'message': 'Flag updated',
  328. 'uid': 'jenkins_build_pagure_100+seed',
  329. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  330. 'user': 'pingou'
  331. }
  332. )
  333. # One flag added
  334. self.session.commit()
  335. request = pagure.lib.search_pull_requests(
  336. self.session, project_id=1, requestid=1)
  337. self.assertEqual(len(request.flags), 1)
  338. self.assertEqual(request.flags[0].comment, 'Tests passed')
  339. self.assertEqual(request.flags[0].percent, 100)
  340. def test_adding_two_flags(self):
  341. """ Test the adding two flags to a PR. """
  342. headers = {'Authorization': 'token aaabbbcccddd'}
  343. data = {
  344. 'username': 'Jenkins',
  345. 'comment': 'Tests passed',
  346. 'status': 'success',
  347. 'percent': '100',
  348. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  349. 'uid': 'jenkins_build_pagure_100+seed',
  350. }
  351. # Valid request
  352. output = self.app.post(
  353. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  354. self.assertEqual(output.status_code, 200)
  355. data = json.loads(output.get_data(as_text=True))
  356. data['flag']['date_created'] = '1510742565'
  357. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  358. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  359. self.assertDictEqual(
  360. data,
  361. {
  362. 'flag': {
  363. 'comment': 'Tests passed',
  364. 'date_created': '1510742565',
  365. 'percent': 100,
  366. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  367. 'status': 'success',
  368. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  369. 'user': {
  370. 'default_email': 'bar@pingou.com',
  371. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  372. 'fullname': 'PY C',
  373. 'name': 'pingou'
  374. },
  375. 'username': 'Jenkins'},
  376. 'message': 'Flag added',
  377. 'uid': 'jenkins_build_pagure_100+seed',
  378. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  379. 'user': 'pingou'
  380. }
  381. )
  382. # One flag added
  383. self.session.commit()
  384. request = pagure.lib.search_pull_requests(
  385. self.session, project_id=1, requestid=1)
  386. self.assertEqual(len(request.flags), 1)
  387. self.assertEqual(request.flags[0].comment, 'Tests passed')
  388. self.assertEqual(request.flags[0].percent, 100)
  389. data = {
  390. 'username': 'Jenkins',
  391. 'comment': 'Tests running again',
  392. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  393. }
  394. # Valid request
  395. output = self.app.post(
  396. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  397. self.assertEqual(output.status_code, 200)
  398. data = json.loads(output.get_data(as_text=True))
  399. data['flag']['date_created'] = '1510742565'
  400. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  401. self.assertNotEqual(
  402. data['uid'], 'jenkins_build_pagure_100+seed')
  403. data['uid'] = 'jenkins_build_pagure_100+seed'
  404. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  405. self.assertDictEqual(
  406. data,
  407. {
  408. 'flag': {
  409. 'comment': 'Tests running again',
  410. 'date_created': '1510742565',
  411. 'percent': None,
  412. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  413. 'status': 'pending',
  414. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  415. 'user': {
  416. 'default_email': 'bar@pingou.com',
  417. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  418. 'fullname': 'PY C',
  419. 'name': 'pingou'
  420. },
  421. 'username': 'Jenkins'},
  422. 'message': 'Flag added',
  423. 'uid': 'jenkins_build_pagure_100+seed',
  424. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  425. 'user': 'pingou'
  426. }
  427. )
  428. # Two flag added
  429. self.session.commit()
  430. request = pagure.lib.search_pull_requests(
  431. self.session, project_id=1, requestid=1)
  432. self.assertEqual(len(request.flags), 2)
  433. self.assertEqual(request.flags[0].comment, 'Tests running again')
  434. self.assertEqual(request.flags[0].percent, None)
  435. self.assertEqual(request.flags[1].comment, 'Tests passed')
  436. self.assertEqual(request.flags[1].percent, 100)
  437. @patch.dict('pagure.config.config',
  438. {
  439. 'FLAG_STATUSES_LABELS':
  440. {
  441. 'pend!': 'label-info',
  442. 'succeed!': 'label-success',
  443. 'fail!': 'label-danger',
  444. 'what?': 'label-warning',
  445. },
  446. 'FLAG_PENDING': 'pend!',
  447. 'FLAG_SUCCESS': 'succeed!',
  448. 'FLAG_FAILURE': 'fail!',
  449. })
  450. def test_flagging_a_pull_request_while_having_custom_statuses(self):
  451. """ Test flagging a PR while having custom statuses. """
  452. headers = {'Authorization': 'token aaabbbcccddd'}
  453. # No status and no percent => should use FLAG_PENDING
  454. send_data = {
  455. 'username': 'Jenkins',
  456. 'comment': 'Tests running',
  457. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  458. 'uid': 'jenkins_build_pagure_100+seed',
  459. }
  460. output = self.app.post(
  461. '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
  462. data = json.loads(output.get_data(as_text=True))
  463. self.assertEqual(output.status_code, 200)
  464. self.assertEqual(data['flag']['status'], 'pend!')
  465. # No status and 50 % => should use FLAG_SUCCESS
  466. send_data['percent'] = 50
  467. output = self.app.post(
  468. '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
  469. data = json.loads(output.get_data(as_text=True))
  470. self.assertEqual(output.status_code, 200)
  471. self.assertEqual(data['flag']['status'], 'succeed!')
  472. # No status and 0 % => should use FLAG_FAILURE
  473. send_data['percent'] = 0
  474. output = self.app.post(
  475. '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
  476. data = json.loads(output.get_data(as_text=True))
  477. self.assertEqual(output.status_code, 200)
  478. self.assertEqual(data['flag']['status'], 'fail!')
  479. # Explicitly set status
  480. send_data['status'] = 'what?'
  481. output = self.app.post(
  482. '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
  483. data = json.loads(output.get_data(as_text=True))
  484. self.assertEqual(output.status_code, 200)
  485. self.assertEqual(data['flag']['status'], 'what?')
  486. # Explicitly set wrong status
  487. send_data['status'] = 'nooo.....'
  488. output = self.app.post(
  489. '/api/0/test/pull-request/1/flag', data=send_data, headers=headers)
  490. data = json.loads(output.get_data(as_text=True))
  491. self.assertEqual(output.status_code, 400)
  492. self.assertDictEqual(
  493. data,
  494. {
  495. "error": "Invalid or incomplete input submitted",
  496. "error_code": "EINVALIDREQ",
  497. "errors": {"status": ["Not a valid choice"]}
  498. }
  499. )
  500. class PagureFlaskApiPRFlagUserTokentests(tests.Modeltests):
  501. """ Tests for the flask API of pagure for flagging pull-requests using
  502. an user token (ie: not restricted to a specific project).
  503. """
  504. maxDiff = None
  505. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  506. def setUp(self):
  507. """ Set up the environnment, ran before every tests. """
  508. super(PagureFlaskApiPRFlagUserTokentests, self).setUp()
  509. pagure.config.config['REQUESTS_FOLDER'] = None
  510. tests.create_projects(self.session)
  511. tests.create_tokens(self.session, project_id=None)
  512. tests.create_tokens_acl(self.session)
  513. # Create a pull-request
  514. repo = pagure.lib.get_authorized_project(self.session, 'test')
  515. forked_repo = pagure.lib.get_authorized_project(self.session, 'test')
  516. req = pagure.lib.new_pull_request(
  517. session=self.session,
  518. repo_from=forked_repo,
  519. branch_from='master',
  520. repo_to=repo,
  521. branch_to='master',
  522. title='test pull-request',
  523. user='pingou',
  524. )
  525. self.session.commit()
  526. self.assertEqual(req.id, 1)
  527. self.assertEqual(req.title, 'test pull-request')
  528. # Check flags before
  529. self.session.commit()
  530. request = pagure.lib.search_pull_requests(
  531. self.session, project_id=1, requestid=1)
  532. self.assertEqual(len(request.flags), 0)
  533. def test_no_pr(self):
  534. """ Test flagging a non-existing PR. """
  535. headers = {'Authorization': 'token aaabbbcccddd'}
  536. # Invalid project
  537. output = self.app.post(
  538. '/api/0/foo/pull-request/1/flag', headers=headers)
  539. self.assertEqual(output.status_code, 404)
  540. data = json.loads(output.get_data(as_text=True))
  541. self.assertDictEqual(
  542. data,
  543. {
  544. "error": "Project not found",
  545. "error_code": "ENOPROJECT",
  546. }
  547. )
  548. def test_no_pr_other_project(self):
  549. """ Test flagging a non-existing PR on a different project. """
  550. headers = {'Authorization': 'token aaabbbcccddd'}
  551. # Valid token, wrong project
  552. output = self.app.post(
  553. '/api/0/test2/pull-request/1/flag', headers=headers)
  554. self.assertEqual(output.status_code, 404)
  555. data = json.loads(output.get_data(as_text=True))
  556. self.assertDictEqual(
  557. data,
  558. {
  559. "error": "Pull-Request not found",
  560. "error_code": "ENOREQ",
  561. }
  562. )
  563. def test_no_input(self):
  564. """ Test flagging an existing PR but without submitting any data. """
  565. headers = {'Authorization': 'token aaabbbcccddd'}
  566. # No input
  567. output = self.app.post(
  568. '/api/0/test/pull-request/1/flag', headers=headers)
  569. self.assertEqual(output.status_code, 400)
  570. data = json.loads(output.get_data(as_text=True))
  571. self.assertDictEqual(
  572. data,
  573. {
  574. 'error': 'Invalid or incomplete input submitted',
  575. 'error_code': 'EINVALIDREQ',
  576. 'errors': {
  577. 'comment': ['This field is required.'],
  578. 'url': ['This field is required.'],
  579. 'username': ['This field is required.']
  580. }
  581. }
  582. )
  583. def test_no_comment(self):
  584. """ Test flagging an existing PR but without all the required info.
  585. """
  586. headers = {'Authorization': 'token aaabbbcccddd'}
  587. data = {
  588. 'username': 'Jenkins',
  589. 'percent': 100,
  590. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  591. 'uid': 'jenkins_build_pagure_100+seed',
  592. }
  593. # Incomplete request
  594. output = self.app.post(
  595. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  596. self.assertEqual(output.status_code, 400)
  597. data = json.loads(output.get_data(as_text=True))
  598. self.assertDictEqual(
  599. data,
  600. {
  601. "error": "Invalid or incomplete input submitted",
  602. "error_code": "EINVALIDREQ",
  603. "errors": {"comment": ["This field is required."]}
  604. }
  605. )
  606. # No change
  607. self.session.commit()
  608. request = pagure.lib.search_pull_requests(
  609. self.session, project_id=1, requestid=1)
  610. self.assertEqual(len(request.flags), 0)
  611. def test_invalid_status(self):
  612. """ Test flagging an existing PR but with an invalid status.
  613. """
  614. headers = {'Authorization': 'token aaabbbcccddd'}
  615. data = {
  616. 'username': 'Jenkins',
  617. 'status': 'failed',
  618. 'comment': 'Failed to run the tests',
  619. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  620. 'uid': 'jenkins_build_pagure_100+seed',
  621. }
  622. # Invalid status submitted
  623. output = self.app.post(
  624. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  625. self.assertEqual(output.status_code, 400)
  626. data = json.loads(output.get_data(as_text=True))
  627. self.assertDictEqual(
  628. data,
  629. {
  630. "error": "Invalid or incomplete input submitted",
  631. "error_code": "EINVALIDREQ",
  632. "errors": {"status": ["Not a valid choice"]}
  633. }
  634. )
  635. # No change
  636. self.session.commit()
  637. request = pagure.lib.search_pull_requests(
  638. self.session, project_id=1, requestid=1)
  639. self.assertEqual(len(request.flags), 0)
  640. @patch('pagure.lib.notify.send_email')
  641. def test_flag_pr_no_status(self, mock_email):
  642. """ Test flagging an existing PR without providing a status.
  643. Also check that no notifications have been sent.
  644. """
  645. headers = {'Authorization': 'token aaabbbcccddd'}
  646. data = {
  647. 'username': 'Jenkins',
  648. 'percent': 0,
  649. 'comment': 'Tests failed',
  650. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  651. 'uid': 'jenkins_build_pagure_100+seed',
  652. }
  653. # Valid request - w/o providing the status
  654. output = self.app.post(
  655. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  656. self.assertEqual(output.status_code, 200)
  657. data = json.loads(output.get_data(as_text=True))
  658. data['flag']['date_created'] = '1510742565'
  659. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  660. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  661. self.assertDictEqual(
  662. data,
  663. {
  664. 'flag': {
  665. 'comment': 'Tests failed',
  666. 'date_created': '1510742565',
  667. 'percent': 0,
  668. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  669. 'status': 'failure',
  670. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  671. 'user': {
  672. 'default_email': 'bar@pingou.com',
  673. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  674. 'fullname': 'PY C',
  675. 'name': 'pingou'
  676. },
  677. 'username': 'Jenkins'},
  678. 'message': 'Flag added',
  679. 'uid': 'jenkins_build_pagure_100+seed',
  680. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  681. 'user': 'pingou'
  682. }
  683. )
  684. # One flag added
  685. self.session.commit()
  686. request = pagure.lib.search_pull_requests(
  687. self.session, project_id=1, requestid=1)
  688. self.assertEqual(len(request.flags), 1)
  689. self.assertEqual(request.flags[0].comment, 'Tests failed')
  690. self.assertEqual(request.flags[0].percent, 0)
  691. # no notifications sent
  692. mock_email.assert_not_called()
  693. def test_editing_flag(self):
  694. """ Test flagging an existing PR without providing a status.
  695. """
  696. headers = {'Authorization': 'token aaabbbcccddd'}
  697. data = {
  698. 'username': 'Jenkins',
  699. 'status': 'failure',
  700. 'comment': 'Tests failed',
  701. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  702. 'uid': 'jenkins_build_pagure_100+seed',
  703. }
  704. # Valid request - w/o providing the status
  705. output = self.app.post(
  706. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  707. self.assertEqual(output.status_code, 200)
  708. data = json.loads(output.get_data(as_text=True))
  709. data['flag']['date_created'] = '1510742565'
  710. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  711. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  712. self.assertDictEqual(
  713. data,
  714. {
  715. 'flag': {
  716. 'comment': 'Tests failed',
  717. 'date_created': '1510742565',
  718. 'percent': None,
  719. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  720. 'status': 'failure',
  721. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  722. 'user': {
  723. 'default_email': 'bar@pingou.com',
  724. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  725. 'fullname': 'PY C',
  726. 'name': 'pingou'
  727. },
  728. 'username': 'Jenkins'},
  729. 'message': 'Flag added',
  730. 'uid': 'jenkins_build_pagure_100+seed',
  731. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  732. 'user': 'pingou'
  733. }
  734. )
  735. # One flag added
  736. self.session.commit()
  737. request = pagure.lib.search_pull_requests(
  738. self.session, project_id=1, requestid=1)
  739. self.assertEqual(len(request.flags), 1)
  740. self.assertEqual(request.flags[0].comment, 'Tests failed')
  741. self.assertEqual(request.flags[0].percent, None)
  742. # Update flag
  743. data = {
  744. 'username': 'Jenkins',
  745. 'percent': 100,
  746. 'comment': 'Tests passed',
  747. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  748. 'uid': 'jenkins_build_pagure_100+seed',
  749. 'status': 'success',
  750. }
  751. output = self.app.post(
  752. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  753. self.assertEqual(output.status_code, 200)
  754. data = json.loads(output.get_data(as_text=True))
  755. data['flag']['date_created'] = '1510742565'
  756. data['flag']['pull_request_uid'] = '62b49f00d489452994de5010565fab81'
  757. data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
  758. self.assertDictEqual(
  759. data,
  760. {
  761. 'flag': {
  762. 'comment': 'Tests passed',
  763. 'date_created': '1510742565',
  764. 'percent': 100,
  765. 'pull_request_uid': '62b49f00d489452994de5010565fab81',
  766. 'status': 'success',
  767. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  768. 'user': {
  769. 'default_email': 'bar@pingou.com',
  770. 'emails': ['bar@pingou.com', 'foo@pingou.com'],
  771. 'fullname': 'PY C',
  772. 'name': 'pingou'
  773. },
  774. 'username': 'Jenkins'},
  775. 'message': 'Flag updated',
  776. 'uid': 'jenkins_build_pagure_100+seed',
  777. 'avatar_url': 'https://seccdn.libravatar.org/avatar/...',
  778. 'user': 'pingou'
  779. }
  780. )
  781. # Still only one flag
  782. self.session.commit()
  783. request = pagure.lib.search_pull_requests(
  784. self.session, project_id=1, requestid=1)
  785. self.assertEqual(len(request.flags), 1)
  786. self.assertEqual(request.flags[0].comment, 'Tests passed')
  787. self.assertEqual(request.flags[0].percent, 100)
  788. if __name__ == '__main__':
  789. unittest.main(verbosity=2)