test_pagure_flask_api_fork.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import datetime
  10. import unittest
  11. import shutil
  12. import sys
  13. import os
  14. import json
  15. from mock import patch
  16. sys.path.insert(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.api
  19. import pagure.lib
  20. import tests
  21. class PagureFlaskApiForktests(tests.Modeltests):
  22. """ Tests for the flask API of pagure for issue """
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskApiForktests, self).setUp()
  26. pagure.APP.config['TESTING'] = True
  27. pagure.SESSION = self.session
  28. pagure.api.SESSION = self.session
  29. pagure.api.fork.SESSION = self.session
  30. pagure.lib.SESSION = self.session
  31. pagure.APP.config['REQUESTS_FOLDER'] = None
  32. self.app = pagure.APP.test_client()
  33. @patch('pagure.lib.notify.send_email')
  34. def test_api_pull_request_views(self, send_email):
  35. """ Test the api_pull_request_views method of the flask api. """
  36. send_email.return_value = True
  37. tests.create_projects(self.session)
  38. tests.create_tokens(self.session)
  39. tests.create_tokens_acl(self.session)
  40. # Create a pull-request
  41. repo = pagure.lib.get_project(self.session, 'test')
  42. forked_repo = pagure.lib.get_project(self.session, 'test')
  43. req = pagure.lib.new_pull_request(
  44. session=self.session,
  45. repo_from=forked_repo,
  46. branch_from='master',
  47. repo_to=repo,
  48. branch_to='master',
  49. title='test pull-request',
  50. user='pingou',
  51. requestfolder=None,
  52. )
  53. self.session.commit()
  54. self.assertEqual(req.id, 1)
  55. self.assertEqual(req.title, 'test pull-request')
  56. # Invalid repo
  57. output = self.app.get('/api/0/foo/pull-requests')
  58. self.assertEqual(output.status_code, 404)
  59. data = json.loads(output.get_data(as_text=True))
  60. self.assertDictEqual(
  61. data,
  62. {
  63. "error": "Project not found",
  64. "error_code": "ENOPROJECT",
  65. }
  66. )
  67. # List pull-requests
  68. output = self.app.get('/api/0/test/pull-requests')
  69. self.assertEqual(output.status_code, 200)
  70. data = json.loads(output.get_data(as_text=True))
  71. data['requests'][0]['date_created'] = '1431414800'
  72. data['requests'][0]['updated_on'] = '1431414800'
  73. data['requests'][0]['project']['date_created'] = '1431414800'
  74. data['requests'][0]['repo_from']['date_created'] = '1431414800'
  75. data['requests'][0]['uid'] = '1431414800'
  76. self.assertDictEqual(
  77. data,
  78. {
  79. "args": {
  80. "assignee": None,
  81. "author": None,
  82. "status": True
  83. },
  84. "total_requests": 1,
  85. "requests": [
  86. {
  87. "assignee": None,
  88. "branch": "master",
  89. "branch_from": "master",
  90. "closed_at": None,
  91. "closed_by": None,
  92. "comments": [],
  93. "commit_start": None,
  94. "commit_stop": None,
  95. "date_created": "1431414800",
  96. "id": 1,
  97. "initial_comment": None,
  98. "project": {
  99. "date_created": "1431414800",
  100. "description": "test project #1",
  101. "id": 1,
  102. "name": "test",
  103. "namespace": None,
  104. "parent": None,
  105. "priorities": {},
  106. "tags": [],
  107. "user": {
  108. "fullname": "PY C",
  109. "name": "pingou"
  110. }
  111. },
  112. "remote_git": None,
  113. "repo_from": {
  114. "date_created": "1431414800",
  115. "description": "test project #1",
  116. "id": 1,
  117. "name": "test",
  118. "namespace": None,
  119. "parent": None,
  120. "priorities": {},
  121. "tags": [],
  122. "user": {
  123. "fullname": "PY C",
  124. "name": "pingou"
  125. }
  126. },
  127. "status": 'Open',
  128. "title": "test pull-request",
  129. "uid": "1431414800",
  130. "updated_on": "1431414800",
  131. "user": {
  132. "fullname": "PY C",
  133. "name": "pingou"
  134. }
  135. }
  136. ]
  137. }
  138. )
  139. headers = {'Authorization': 'token aaabbbcccddd'}
  140. # Access Pull-Request authenticated
  141. output = self.app.get('/api/0/test/pull-requests', headers=headers)
  142. self.assertEqual(output.status_code, 200)
  143. data2 = json.loads(output.get_data(as_text=True))
  144. data2['requests'][0]['date_created'] = '1431414800'
  145. data2['requests'][0]['updated_on'] = '1431414800'
  146. data2['requests'][0]['project']['date_created'] = '1431414800'
  147. data2['requests'][0]['repo_from']['date_created'] = '1431414800'
  148. data2['requests'][0]['uid'] = '1431414800'
  149. self.assertDictEqual(data, data2)
  150. @patch('pagure.lib.notify.send_email')
  151. def test_api_pull_request_view(self, send_email):
  152. """ Test the api_pull_request_view method of the flask api. """
  153. send_email.return_value = True
  154. tests.create_projects(self.session)
  155. tests.create_tokens(self.session)
  156. tests.create_tokens_acl(self.session)
  157. # Create a pull-request
  158. repo = pagure.lib.get_project(self.session, 'test')
  159. forked_repo = pagure.lib.get_project(self.session, 'test')
  160. req = pagure.lib.new_pull_request(
  161. session=self.session,
  162. repo_from=forked_repo,
  163. branch_from='master',
  164. repo_to=repo,
  165. branch_to='master',
  166. title='test pull-request',
  167. user='pingou',
  168. requestfolder=None,
  169. )
  170. self.session.commit()
  171. self.assertEqual(req.id, 1)
  172. self.assertEqual(req.title, 'test pull-request')
  173. # Invalid repo
  174. output = self.app.get('/api/0/foo/pull-request/1')
  175. self.assertEqual(output.status_code, 404)
  176. data = json.loads(output.get_data(as_text=True))
  177. self.assertDictEqual(
  178. data,
  179. {
  180. "error": "Project not found",
  181. "error_code": "ENOPROJECT",
  182. }
  183. )
  184. # Invalid issue for this repo
  185. output = self.app.get('/api/0/test2/pull-request/1')
  186. self.assertEqual(output.status_code, 404)
  187. data = json.loads(output.get_data(as_text=True))
  188. self.assertDictEqual(
  189. data,
  190. {
  191. "error": "Pull-Request not found",
  192. "error_code": "ENOREQ",
  193. }
  194. )
  195. # Valid issue
  196. output = self.app.get('/api/0/test/pull-request/1')
  197. self.assertEqual(output.status_code, 200)
  198. data = json.loads(output.get_data(as_text=True))
  199. data['date_created'] = '1431414800'
  200. data['updated_on'] = '1431414800'
  201. data['project']['date_created'] = '1431414800'
  202. data['repo_from']['date_created'] = '1431414800'
  203. data['uid'] = '1431414800'
  204. self.assertDictEqual(
  205. data,
  206. {
  207. "assignee": None,
  208. "branch": "master",
  209. "branch_from": "master",
  210. "closed_at": None,
  211. "closed_by": None,
  212. "comments": [],
  213. "commit_start": None,
  214. "commit_stop": None,
  215. "date_created": "1431414800",
  216. "id": 1,
  217. "initial_comment": None,
  218. "project": {
  219. "date_created": "1431414800",
  220. "description": "test project #1",
  221. "id": 1,
  222. "name": "test",
  223. "namespace": None,
  224. "parent": None,
  225. "priorities": {},
  226. "tags": [],
  227. "user": {
  228. "fullname": "PY C",
  229. "name": "pingou"
  230. }
  231. },
  232. "remote_git": None,
  233. "repo_from": {
  234. "date_created": "1431414800",
  235. "description": "test project #1",
  236. "id": 1,
  237. "name": "test",
  238. "namespace": None,
  239. "parent": None,
  240. "priorities": {},
  241. "tags": [],
  242. "user": {
  243. "fullname": "PY C",
  244. "name": "pingou"
  245. }
  246. },
  247. "status": 'Open',
  248. "title": "test pull-request",
  249. "uid": "1431414800",
  250. "updated_on": "1431414800",
  251. "user": {
  252. "fullname": "PY C",
  253. "name": "pingou"
  254. }
  255. }
  256. )
  257. headers = {'Authorization': 'token aaabbbcccddd'}
  258. # Access Pull-Request authenticated
  259. output = self.app.get('/api/0/test/pull-request/1', headers=headers)
  260. self.assertEqual(output.status_code, 200)
  261. data2 = json.loads(output.get_data(as_text=True))
  262. data2['date_created'] = '1431414800'
  263. data2['project']['date_created'] = '1431414800'
  264. data2['repo_from']['date_created'] = '1431414800'
  265. data2['uid'] = '1431414800'
  266. data2['date_created'] = '1431414800'
  267. data2['updated_on'] = '1431414800'
  268. self.assertDictEqual(data, data2)
  269. @patch('pagure.lib.notify.send_email')
  270. def test_api_pull_request_close(self, send_email):
  271. """ Test the api_pull_request_close method of the flask api. """
  272. send_email.return_value = True
  273. tests.create_projects(self.session)
  274. tests.create_tokens(self.session)
  275. tests.create_tokens_acl(self.session)
  276. # Create the pull-request to close
  277. repo = pagure.lib.get_project(self.session, 'test')
  278. forked_repo = pagure.lib.get_project(self.session, 'test')
  279. req = pagure.lib.new_pull_request(
  280. session=self.session,
  281. repo_from=forked_repo,
  282. branch_from='master',
  283. repo_to=repo,
  284. branch_to='master',
  285. title='test pull-request',
  286. user='pingou',
  287. requestfolder=None,
  288. )
  289. self.session.commit()
  290. self.assertEqual(req.id, 1)
  291. self.assertEqual(req.title, 'test pull-request')
  292. headers = {'Authorization': 'token aaabbbcccddd'}
  293. # Invalid project
  294. output = self.app.post(
  295. '/api/0/foo/pull-request/1/close', headers=headers)
  296. self.assertEqual(output.status_code, 404)
  297. data = json.loads(output.get_data(as_text=True))
  298. self.assertDictEqual(
  299. data,
  300. {
  301. "error": "Project not found",
  302. "error_code": "ENOPROJECT",
  303. }
  304. )
  305. # Valid token, wrong project
  306. output = self.app.post(
  307. '/api/0/test2/pull-request/1/close', headers=headers)
  308. self.assertEqual(output.status_code, 401)
  309. data = json.loads(output.get_data(as_text=True))
  310. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  311. data['error_code'])
  312. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  313. # Invalid PR
  314. output = self.app.post(
  315. '/api/0/test/pull-request/2/close', headers=headers)
  316. self.assertEqual(output.status_code, 404)
  317. data = json.loads(output.get_data(as_text=True))
  318. self.assertDictEqual(
  319. data,
  320. {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
  321. )
  322. # Create a token for foo for this project
  323. item = pagure.lib.model.Token(
  324. id='foobar_token',
  325. user_id=2,
  326. project_id=1,
  327. expiration=datetime.datetime.utcnow() + datetime.timedelta(
  328. days=30)
  329. )
  330. self.session.add(item)
  331. self.session.commit()
  332. # Allow the token to close PR
  333. item = pagure.lib.model.TokenAcl(
  334. token_id='foobar_token',
  335. acl_id=7,
  336. )
  337. self.session.add(item)
  338. self.session.commit()
  339. headers = {'Authorization': 'token foobar_token'}
  340. # User not admin
  341. output = self.app.post(
  342. '/api/0/test/pull-request/1/close', headers=headers)
  343. self.assertEqual(output.status_code, 403)
  344. data = json.loads(output.get_data(as_text=True))
  345. self.assertDictEqual(
  346. data,
  347. {
  348. 'error': 'You are not allowed to merge/close pull-request '
  349. 'for this project',
  350. 'error_code': "ENOPRCLOSE",
  351. }
  352. )
  353. headers = {'Authorization': 'token aaabbbcccddd'}
  354. # Close PR
  355. output = self.app.post(
  356. '/api/0/test/pull-request/1/close', headers=headers)
  357. self.assertEqual(output.status_code, 200)
  358. data = json.loads(output.get_data(as_text=True))
  359. self.assertDictEqual(
  360. data,
  361. {"message": "Pull-request closed!"}
  362. )
  363. @patch('pagure.lib.notify.send_email')
  364. @patch('pagure.lib.git.merge_pull_request')
  365. def test_api_pull_request_merge(self, mpr, send_email):
  366. """ Test the api_pull_request_merge method of the flask api. """
  367. mpr.return_value = 'Changes merged!'
  368. send_email.return_value = True
  369. tests.create_projects(self.session)
  370. tests.create_tokens(self.session)
  371. tests.create_tokens_acl(self.session)
  372. # Create the pull-request to close
  373. repo = pagure.lib.get_project(self.session, 'test')
  374. forked_repo = pagure.lib.get_project(self.session, 'test')
  375. req = pagure.lib.new_pull_request(
  376. session=self.session,
  377. repo_from=forked_repo,
  378. branch_from='master',
  379. repo_to=repo,
  380. branch_to='master',
  381. title='test pull-request',
  382. user='pingou',
  383. requestfolder=None,
  384. )
  385. self.session.commit()
  386. self.assertEqual(req.id, 1)
  387. self.assertEqual(req.title, 'test pull-request')
  388. headers = {'Authorization': 'token aaabbbcccddd'}
  389. # Invalid project
  390. output = self.app.post(
  391. '/api/0/foo/pull-request/1/merge', headers=headers)
  392. self.assertEqual(output.status_code, 404)
  393. data = json.loads(output.get_data(as_text=True))
  394. self.assertDictEqual(
  395. data,
  396. {
  397. "error": "Project not found",
  398. "error_code": "ENOPROJECT",
  399. }
  400. )
  401. # Valid token, wrong project
  402. output = self.app.post(
  403. '/api/0/test2/pull-request/1/merge', headers=headers)
  404. self.assertEqual(output.status_code, 401)
  405. data = json.loads(output.get_data(as_text=True))
  406. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  407. data['error_code'])
  408. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  409. # Invalid PR
  410. output = self.app.post(
  411. '/api/0/test/pull-request/2/merge', headers=headers)
  412. self.assertEqual(output.status_code, 404)
  413. data = json.loads(output.get_data(as_text=True))
  414. self.assertDictEqual(
  415. data,
  416. {'error': 'Pull-Request not found', 'error_code': "ENOREQ"}
  417. )
  418. # Create a token for foo for this project
  419. item = pagure.lib.model.Token(
  420. id='foobar_token',
  421. user_id=2,
  422. project_id=1,
  423. expiration=datetime.datetime.utcnow() + datetime.timedelta(
  424. days=30)
  425. )
  426. self.session.add(item)
  427. self.session.commit()
  428. # Allow the token to merge PR
  429. item = pagure.lib.model.TokenAcl(
  430. token_id='foobar_token',
  431. acl_id=10,
  432. )
  433. self.session.add(item)
  434. self.session.commit()
  435. headers = {'Authorization': 'token foobar_token'}
  436. # User not admin
  437. output = self.app.post(
  438. '/api/0/test/pull-request/1/merge', headers=headers)
  439. self.assertEqual(output.status_code, 403)
  440. data = json.loads(output.get_data(as_text=True))
  441. self.assertDictEqual(
  442. data,
  443. {
  444. 'error': 'You are not allowed to merge/close pull-request '
  445. 'for this project',
  446. 'error_code': "ENOPRCLOSE",
  447. }
  448. )
  449. headers = {'Authorization': 'token aaabbbcccddd'}
  450. # Merge PR
  451. output = self.app.post(
  452. '/api/0/test/pull-request/1/merge', headers=headers)
  453. self.assertEqual(output.status_code, 200)
  454. data = json.loads(output.get_data(as_text=True))
  455. self.assertDictEqual(
  456. data,
  457. {"message": "Changes merged!"}
  458. )
  459. @patch('pagure.lib.notify.send_email')
  460. def test_api_pull_request_add_comment(self, mockemail):
  461. """ Test the api_pull_request_add_comment method of the flask api. """
  462. mockemail.return_value = True
  463. tests.create_projects(self.session)
  464. tests.create_tokens(self.session)
  465. tests.create_tokens_acl(self.session)
  466. headers = {'Authorization': 'token aaabbbcccddd'}
  467. # Invalid project
  468. output = self.app.post(
  469. '/api/0/foo/pull-request/1/comment', headers=headers)
  470. self.assertEqual(output.status_code, 404)
  471. data = json.loads(output.get_data(as_text=True))
  472. self.assertDictEqual(
  473. data,
  474. {
  475. "error": "Project not found",
  476. "error_code": "ENOPROJECT",
  477. }
  478. )
  479. # Valid token, wrong project
  480. output = self.app.post(
  481. '/api/0/test2/pull-request/1/comment', headers=headers)
  482. self.assertEqual(output.status_code, 401)
  483. data = json.loads(output.get_data(as_text=True))
  484. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  485. data['error_code'])
  486. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  487. # No input
  488. output = self.app.post(
  489. '/api/0/test/pull-request/1/comment', headers=headers)
  490. self.assertEqual(output.status_code, 404)
  491. data = json.loads(output.get_data(as_text=True))
  492. self.assertDictEqual(
  493. data,
  494. {
  495. "error": "Pull-Request not found",
  496. "error_code": "ENOREQ",
  497. }
  498. )
  499. # Create a pull-request
  500. repo = pagure.lib.get_project(self.session, 'test')
  501. forked_repo = pagure.lib.get_project(self.session, 'test')
  502. req = pagure.lib.new_pull_request(
  503. session=self.session,
  504. repo_from=forked_repo,
  505. branch_from='master',
  506. repo_to=repo,
  507. branch_to='master',
  508. title='test pull-request',
  509. user='pingou',
  510. requestfolder=None,
  511. )
  512. self.session.commit()
  513. self.assertEqual(req.id, 1)
  514. self.assertEqual(req.title, 'test pull-request')
  515. # Check comments before
  516. request = pagure.lib.search_pull_requests(
  517. self.session, project_id=1, requestid=1)
  518. self.assertEqual(len(request.comments), 0)
  519. data = {
  520. 'title': 'test issue',
  521. }
  522. # Incomplete request
  523. output = self.app.post(
  524. '/api/0/test/pull-request/1/comment', data=data, headers=headers)
  525. self.assertEqual(output.status_code, 400)
  526. data = json.loads(output.get_data(as_text=True))
  527. self.assertDictEqual(
  528. data,
  529. {
  530. "error": "Invalid or incomplete input submited",
  531. "error_code": "EINVALIDREQ",
  532. }
  533. )
  534. # No change
  535. request = pagure.lib.search_pull_requests(
  536. self.session, project_id=1, requestid=1)
  537. self.assertEqual(len(request.comments), 0)
  538. data = {
  539. 'comment': 'This is a very interesting question',
  540. }
  541. # Valid request
  542. output = self.app.post(
  543. '/api/0/test/pull-request/1/comment', data=data, headers=headers)
  544. self.assertEqual(output.status_code, 200)
  545. data = json.loads(output.get_data(as_text=True))
  546. self.assertDictEqual(
  547. data,
  548. {'message': 'Comment added'}
  549. )
  550. # One comment added
  551. request = pagure.lib.search_pull_requests(
  552. self.session, project_id=1, requestid=1)
  553. self.assertEqual(len(request.comments), 1)
  554. @patch('pagure.lib.notify.send_email')
  555. def test_api_pull_request_add_flag(self, mockemail):
  556. """ Test the api_pull_request_add_flag method of the flask api. """
  557. mockemail.return_value = True
  558. tests.create_projects(self.session)
  559. tests.create_tokens(self.session)
  560. tests.create_tokens_acl(self.session)
  561. headers = {'Authorization': 'token aaabbbcccddd'}
  562. # Invalid project
  563. output = self.app.post(
  564. '/api/0/foo/pull-request/1/flag', headers=headers)
  565. self.assertEqual(output.status_code, 404)
  566. data = json.loads(output.get_data(as_text=True))
  567. self.assertDictEqual(
  568. data,
  569. {
  570. "error": "Project not found",
  571. "error_code": "ENOPROJECT",
  572. }
  573. )
  574. # Valid token, wrong project
  575. output = self.app.post(
  576. '/api/0/test2/pull-request/1/flag', headers=headers)
  577. self.assertEqual(output.status_code, 401)
  578. data = json.loads(output.get_data(as_text=True))
  579. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  580. data['error_code'])
  581. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  582. # No input
  583. output = self.app.post(
  584. '/api/0/test/pull-request/1/flag', headers=headers)
  585. self.assertEqual(output.status_code, 404)
  586. data = json.loads(output.get_data(as_text=True))
  587. self.assertDictEqual(
  588. data,
  589. {
  590. "error": "Pull-Request not found",
  591. "error_code": "ENOREQ",
  592. }
  593. )
  594. # Create a pull-request
  595. repo = pagure.lib.get_project(self.session, 'test')
  596. forked_repo = pagure.lib.get_project(self.session, 'test')
  597. req = pagure.lib.new_pull_request(
  598. session=self.session,
  599. repo_from=forked_repo,
  600. branch_from='master',
  601. repo_to=repo,
  602. branch_to='master',
  603. title='test pull-request',
  604. user='pingou',
  605. requestfolder=None,
  606. )
  607. self.session.commit()
  608. self.assertEqual(req.id, 1)
  609. self.assertEqual(req.title, 'test pull-request')
  610. # Check comments before
  611. request = pagure.lib.search_pull_requests(
  612. self.session, project_id=1, requestid=1)
  613. self.assertEqual(len(request.flags), 0)
  614. data = {
  615. 'username': 'Jenkins',
  616. 'percent': 100,
  617. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  618. 'uid': 'jenkins_build_pagure_100+seed',
  619. }
  620. # Incomplete request
  621. output = self.app.post(
  622. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  623. self.assertEqual(output.status_code, 400)
  624. data = json.loads(output.get_data(as_text=True))
  625. self.assertDictEqual(
  626. data,
  627. {
  628. "error": "Invalid or incomplete input submited",
  629. "error_code": "EINVALIDREQ",
  630. }
  631. )
  632. # No change
  633. request = pagure.lib.search_pull_requests(
  634. self.session, project_id=1, requestid=1)
  635. self.assertEqual(len(request.flags), 0)
  636. data = {
  637. 'username': 'Jenkins',
  638. 'percent': 0,
  639. 'comment': 'Tests failed',
  640. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  641. 'uid': 'jenkins_build_pagure_100+seed',
  642. }
  643. # Valid request
  644. output = self.app.post(
  645. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  646. self.assertEqual(output.status_code, 200)
  647. data = json.loads(output.get_data(as_text=True))
  648. self.assertDictEqual(
  649. data,
  650. {'message': 'Flag added'}
  651. )
  652. # One flag added
  653. request = pagure.lib.search_pull_requests(
  654. self.session, project_id=1, requestid=1)
  655. self.assertEqual(len(request.flags), 1)
  656. self.assertEqual(request.flags[0].comment, 'Tests failed')
  657. self.assertEqual(request.flags[0].percent, 0)
  658. # Update flag
  659. data = {
  660. 'username': 'Jenkins',
  661. 'percent': 100,
  662. 'comment': 'Tests passed',
  663. 'url': 'http://jenkins.cloud.fedoraproject.org/',
  664. 'uid': 'jenkins_build_pagure_100+seed',
  665. }
  666. output = self.app.post(
  667. '/api/0/test/pull-request/1/flag', data=data, headers=headers)
  668. self.assertEqual(output.status_code, 200)
  669. data = json.loads(output.get_data(as_text=True))
  670. self.assertDictEqual(
  671. data,
  672. {'message': 'Flag updated'}
  673. )
  674. # One flag added
  675. request = pagure.lib.search_pull_requests(
  676. self.session, project_id=1, requestid=1)
  677. self.assertEqual(len(request.flags), 1)
  678. self.assertEqual(request.flags[0].comment, 'Tests passed')
  679. self.assertEqual(request.flags[0].percent, 100)
  680. if __name__ == '__main__':
  681. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  682. PagureFlaskApiForktests)
  683. unittest.TextTestRunner(verbosity=2).run(SUITE)