test_pagure_flask_api_issue_comment.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. import datetime
  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: E402
  16. import pagure.lib # noqa: E402
  17. import tests # noqa: E402
  18. class PagureFlaskApiIssueCommenttests(tests.Modeltests):
  19. """ Tests for the flask API of pagure for changing the status of an
  20. issue
  21. """
  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(PagureFlaskApiIssueCommenttests, self).setUp()
  26. pagure.APP.config['TESTING'] = True
  27. pagure.SESSION = self.session
  28. pagure.api.SESSION = self.session
  29. pagure.api.issue.SESSION = self.session
  30. pagure.lib.SESSION = self.session
  31. pagure.APP.config['TICKETS_FOLDER'] = None
  32. self.app = pagure.APP.test_client()
  33. tests.create_projects(self.session)
  34. tests.create_projects_git(os.path.join(self.path, 'tickets'))
  35. tests.create_tokens(self.session)
  36. tests.create_tokens_acl(self.session)
  37. # Create normal issue
  38. repo = pagure.get_authorized_project(self.session, 'test')
  39. msg = pagure.lib.new_issue(
  40. session=self.session,
  41. repo=repo,
  42. title='Test issue #1',
  43. content='We should work on this',
  44. user='pingou',
  45. ticketfolder=None,
  46. private=False,
  47. )
  48. self.session.commit()
  49. self.assertEqual(msg.title, 'Test issue #1')
  50. # Create private issue
  51. msg = pagure.lib.new_issue(
  52. session=self.session,
  53. repo=repo,
  54. title='Test issue #2',
  55. content='We should work on this',
  56. user='foo',
  57. ticketfolder=None,
  58. private=True,
  59. )
  60. self.session.commit()
  61. self.assertEqual(msg.title, 'Test issue #2')
  62. # Create project-less token for user foo
  63. item = pagure.lib.model.Token(
  64. id='project-less-foo',
  65. user_id=2,
  66. project_id=None,
  67. expiration=datetime.datetime.utcnow()
  68. + datetime.timedelta(days=30)
  69. )
  70. self.session.add(item)
  71. self.session.commit()
  72. tests.create_tokens_acl(self.session, token_id='project-less-foo')
  73. def test_api_comment_issue_invalid_project(self):
  74. """ Test the api_comment_issue method of the flask api. """
  75. headers = {'Authorization': 'token aaabbbcccddd'}
  76. # Invalid project
  77. output = self.app.post('/api/0/foo/issue/1/comment', headers=headers)
  78. self.assertEqual(output.status_code, 404)
  79. data = json.loads(output.data)
  80. self.assertDictEqual(
  81. data,
  82. {
  83. "error": "Project not found",
  84. "error_code": "ENOPROJECT",
  85. }
  86. )
  87. def test_api_comment_issue_invalid_project_token(self):
  88. """ Test the api_comment_issue method of the flask api. """
  89. headers = {'Authorization': 'token aaabbbcccddd'}
  90. # Valid token, wrong project
  91. output = self.app.post('/api/0/test2/issue/1/comment', headers=headers)
  92. self.assertEqual(output.status_code, 401)
  93. data = json.loads(output.data)
  94. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  95. data['error_code'])
  96. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  97. def test_api_comment_issue_invalid_issue(self):
  98. """ Test the api_comment_issue method of the flask api. """
  99. headers = {'Authorization': 'token aaabbbcccddd'}
  100. # Invalid issue
  101. output = self.app.post('/api/0/test/issue/10/comment', headers=headers)
  102. self.assertEqual(output.status_code, 404)
  103. data = json.loads(output.data)
  104. self.assertDictEqual(
  105. data,
  106. {
  107. "error": "Issue not found",
  108. "error_code": "ENOISSUE",
  109. }
  110. )
  111. def test_api_comment_issue_incomplete_request(self):
  112. """ Test the api_comment_issue method of the flask api. """
  113. headers = {'Authorization': 'token aaabbbcccddd'}
  114. # Check comments before
  115. repo = pagure.get_authorized_project(self.session, 'test')
  116. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  117. self.assertEqual(len(issue.comments), 0)
  118. data = {
  119. 'title': 'test issue',
  120. }
  121. # Incomplete request
  122. output = self.app.post(
  123. '/api/0/test/issue/1/comment', data=data, headers=headers)
  124. self.assertEqual(output.status_code, 400)
  125. data = json.loads(output.data)
  126. self.assertDictEqual(
  127. data,
  128. {
  129. "error": "Invalid or incomplete input submited",
  130. "error_code": "EINVALIDREQ",
  131. "errors": {"comment": ["This field is required."]}
  132. }
  133. )
  134. # No change
  135. repo = pagure.get_authorized_project(self.session, 'test')
  136. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  137. self.assertEqual(issue.status, 'Open')
  138. def test_api_comment_issue(self):
  139. """ Test the api_comment_issue method of the flask api. """
  140. headers = {'Authorization': 'token aaabbbcccddd'}
  141. data = {
  142. 'comment': 'This is a very interesting question',
  143. }
  144. # Valid request
  145. output = self.app.post(
  146. '/api/0/test/issue/1/comment', data=data, headers=headers)
  147. self.assertEqual(output.status_code, 200)
  148. data = json.loads(output.data)
  149. self.assertDictEqual(
  150. data,
  151. {'message': 'Comment added'}
  152. )
  153. # One comment added
  154. repo = pagure.get_authorized_project(self.session, 'test')
  155. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  156. self.assertEqual(len(issue.comments), 1)
  157. def test_api_comment_issue_private_un_authorized(self):
  158. """ Test the api_comment_issue method of the flask api. """
  159. # Check before
  160. repo = pagure.get_authorized_project(self.session, 'test')
  161. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  162. self.assertEqual(len(issue.comments), 0)
  163. data = {
  164. 'comment': 'This is a very interesting question',
  165. }
  166. headers = {'Authorization': 'token pingou_foo'}
  167. # Valid request but un-authorized
  168. output = self.app.post(
  169. '/api/0/test/issue/2/comment', data=data, headers=headers)
  170. self.assertEqual(output.status_code, 401)
  171. data = json.loads(output.data)
  172. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  173. data['error_code'])
  174. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  175. # No comment added
  176. repo = pagure.get_authorized_project(self.session, 'test')
  177. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  178. self.assertEqual(len(issue.comments), 0)
  179. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  180. def test_api_comment_issue_private(self):
  181. """ Test the api_comment_issue method of the flask api. """
  182. # Create token for user foo
  183. item = pagure.lib.model.Token(
  184. id='foo_token2',
  185. user_id=2,
  186. project_id=1,
  187. expiration=datetime.datetime.utcnow() + datetime.timedelta(days=30)
  188. )
  189. self.session.add(item)
  190. self.session.commit()
  191. tests.create_tokens_acl(self.session, token_id='foo_token2')
  192. data = {
  193. 'comment': 'This is a very interesting question',
  194. }
  195. headers = {'Authorization': 'token foo_token2'}
  196. # Valid request and authorized
  197. output = self.app.post(
  198. '/api/0/test/issue/2/comment', data=data, headers=headers)
  199. self.assertEqual(output.status_code, 200)
  200. data = json.loads(output.data)
  201. self.assertDictEqual(
  202. data,
  203. {'message': 'Comment added'}
  204. )
  205. def test_api_comment_issue_invalid_project_project_less(self):
  206. """ Test the api_comment_issue method of the flask api. """
  207. headers = {'Authorization': 'token project-less-foo'}
  208. # Invalid project
  209. output = self.app.post('/api/0/foo/issue/1/comment', headers=headers)
  210. self.assertEqual(output.status_code, 404)
  211. data = json.loads(output.data)
  212. self.assertDictEqual(
  213. data,
  214. {
  215. "error": "Project not found",
  216. "error_code": "ENOPROJECT",
  217. }
  218. )
  219. def test_api_comment_issue_invalid_project_token_project_less(self):
  220. """ Test the api_comment_issue method of the flask api. """
  221. headers = {'Authorization': 'token project-less-foo'}
  222. # Valid token, no such issue, project-less token so different failure
  223. output = self.app.post('/api/0/test2/issue/1/comment', headers=headers)
  224. self.assertEqual(output.status_code, 404)
  225. data = json.loads(output.data)
  226. self.assertDictEqual(
  227. data,
  228. {
  229. "error": "Issue not found",
  230. "error_code": "ENOISSUE",
  231. }
  232. )
  233. def test_api_comment_issue_invalid_issue_project_less(self):
  234. """ Test the api_comment_issue method of the flask api. """
  235. headers = {'Authorization': 'token project-less-foo'}
  236. # Invalid issue
  237. output = self.app.post('/api/0/test/issue/10/comment', headers=headers)
  238. self.assertEqual(output.status_code, 404)
  239. data = json.loads(output.data)
  240. self.assertDictEqual(
  241. data,
  242. {
  243. "error": "Issue not found",
  244. "error_code": "ENOISSUE",
  245. }
  246. )
  247. def test_api_comment_issue_incomplete_request_project_less(self):
  248. """ Test the api_comment_issue method of the flask api. """
  249. headers = {'Authorization': 'token project-less-foo'}
  250. # Check comments before
  251. repo = pagure.get_authorized_project(self.session, 'test')
  252. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  253. self.assertEqual(len(issue.comments), 0)
  254. data = {
  255. 'title': 'test issue',
  256. }
  257. # Incomplete request
  258. output = self.app.post(
  259. '/api/0/test/issue/1/comment', data=data, headers=headers)
  260. self.assertEqual(output.status_code, 400)
  261. data = json.loads(output.data)
  262. self.assertDictEqual(
  263. data,
  264. {
  265. "error": "Invalid or incomplete input submited",
  266. "error_code": "EINVALIDREQ",
  267. "errors": {"comment": ["This field is required."]}
  268. }
  269. )
  270. # No change
  271. repo = pagure.get_authorized_project(self.session, 'test')
  272. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  273. self.assertEqual(issue.status, 'Open')
  274. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  275. def test_api_comment_issue_project_less(self):
  276. """ Test the api_comment_issue method of the flask api. """
  277. headers = {'Authorization': 'token project-less-foo'}
  278. data = {
  279. 'comment': 'This is a very interesting question',
  280. }
  281. # Valid request
  282. output = self.app.post(
  283. '/api/0/test/issue/1/comment', data=data, headers=headers)
  284. self.assertEqual(output.status_code, 200)
  285. data = json.loads(output.data)
  286. self.assertDictEqual(
  287. data,
  288. {'message': 'Comment added'}
  289. )
  290. # One comment added
  291. repo = pagure.get_authorized_project(self.session, 'test')
  292. issue = pagure.lib.search_issues(self.session, repo, issueid=1)
  293. self.assertEqual(len(issue.comments), 1)
  294. def test_api_comment_issue_private_un_authorized_project_less(self):
  295. """ Test the api_comment_issue method of the flask api. """
  296. # Check before
  297. repo = pagure.get_authorized_project(self.session, 'test')
  298. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  299. self.assertEqual(len(issue.comments), 0)
  300. data = {
  301. 'comment': 'This is a very interesting question',
  302. }
  303. headers = {'Authorization': 'token pingou_foo'}
  304. # Valid request but un-authorized
  305. output = self.app.post(
  306. '/api/0/test/issue/2/comment', data=data, headers=headers)
  307. self.assertEqual(output.status_code, 401)
  308. data = json.loads(output.data)
  309. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
  310. data['error_code'])
  311. self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
  312. # No comment added
  313. repo = pagure.get_authorized_project(self.session, 'test')
  314. issue = pagure.lib.search_issues(self.session, repo, issueid=2)
  315. self.assertEqual(len(issue.comments), 0)
  316. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  317. def test_api_comment_issue_private_project_less(self):
  318. """ Test the api_comment_issue method of the flask api. """
  319. # Create token for user foo
  320. item = pagure.lib.model.Token(
  321. id='foo_token2',
  322. user_id=2,
  323. project_id=None,
  324. expiration=datetime.datetime.utcnow() + datetime.timedelta(days=30)
  325. )
  326. self.session.add(item)
  327. self.session.commit()
  328. tests.create_tokens_acl(self.session, token_id='foo_token2')
  329. data = {
  330. 'comment': 'This is a very interesting question',
  331. }
  332. headers = {'Authorization': 'token foo_token2'}
  333. # Valid request and authorized
  334. output = self.app.post(
  335. '/api/0/test/issue/2/comment', data=data, headers=headers)
  336. self.assertEqual(output.status_code, 200)
  337. data = json.loads(output.data)
  338. self.assertDictEqual(
  339. data,
  340. {'message': 'Comment added'}
  341. )
  342. if __name__ == '__main__':
  343. unittest.main(verbosity=2)