test_pagure_flask_api_issue_comment.py 15 KB

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