test_pagure_flask_api_fork.py 25 KB

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