test_pagure_flask_ui_issues_read_only.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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, absolute_import
  8. import json
  9. import unittest
  10. import sys
  11. import os
  12. from mock import patch, MagicMock
  13. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.lib.query # noqa
  17. import tests # noqa
  18. class PagureFlaskIssuesReadOnlytests(tests.Modeltests):
  19. """Tests for flask issues controller of pagure with read-only tickets"""
  20. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  21. def setUp(self):
  22. """ Set up the environnment, ran before every tests. """
  23. super(PagureFlaskIssuesReadOnlytests, self).setUp()
  24. tests.create_projects(self.session)
  25. tests.create_projects_git(os.path.join(self.path, "repos"))
  26. # Make the project's issue tracker read-only
  27. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  28. settings = repo.settings
  29. settings["issue_tracker_read_only"] = True
  30. repo.settings = settings
  31. self.session.add(repo)
  32. self.session.commit()
  33. # Create a couple of issue
  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 for the second time",
  39. user="foo",
  40. status="Open",
  41. private=True,
  42. )
  43. self.session.commit()
  44. self.assertEqual(msg.title, "Test issue #1")
  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 for the second time",
  50. user="foo",
  51. status="Open",
  52. private=False,
  53. )
  54. self.session.commit()
  55. self.assertEqual(msg.title, "Test issue #2")
  56. def test_issue_list_authenticated_commit(self):
  57. """Test the list of issues when user is authenticated and has
  58. access to the project.
  59. """
  60. user = tests.FakeUser(username="pingou")
  61. with tests.user_set(self.app.application, user):
  62. output = self.app.get("/test/issues")
  63. self.assertEqual(output.status_code, 200)
  64. output_text = output.get_data(as_text=True)
  65. self.assertIn("<title>Issues - test - Pagure</title>", output_text)
  66. self.assertIn(
  67. '<span class="fa fa-fw fa-exclamation-circle"></span>'
  68. " 2 Open Issues\n",
  69. output_text,
  70. )
  71. def test_field_comment(self):
  72. """Test if the field commit is present on the issue page."""
  73. user = tests.FakeUser(username="pingou")
  74. with tests.user_set(self.app.application, user):
  75. output = self.app.get("/test/issue/1")
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. "<title>Issue #1: Test issue #1 - test - Pagure</title>",
  80. output_text,
  81. )
  82. self.assertNotIn(
  83. 'value="Update Issue" title="Comment and Update Metadata" '
  84. "tabindex=2 />",
  85. output_text,
  86. )
  87. self.assertIn("This issue tracker is read-only.", output_text)
  88. def test_update_ticket(self):
  89. """Test updating a ticket."""
  90. user = tests.FakeUser(username="pingou")
  91. with tests.user_set(self.app.application, user):
  92. output = self.app.post(
  93. "/test/issue/1/update", data={}, follow_redirects=True
  94. )
  95. self.assertEqual(output.status_code, 401)
  96. output_text = output.get_data(as_text=True)
  97. self.assertIn(
  98. "<title>Unauthorized :'( - Pagure</title>", output_text
  99. )
  100. self.assertIn(
  101. "<p>The issue tracker for this project is read-only</p>",
  102. output_text,
  103. )
  104. def test_edit_comment(self):
  105. """Test editing a comment from a ticket."""
  106. user = tests.FakeUser(username="pingou")
  107. with tests.user_set(self.app.application, user):
  108. output = self.app.post(
  109. "/test/issue/1/comment/1/edit", data={}, follow_redirects=True
  110. )
  111. self.assertEqual(output.status_code, 401)
  112. output_text = output.get_data(as_text=True)
  113. self.assertIn(
  114. "<title>Unauthorized :'( - Pagure</title>", output_text
  115. )
  116. self.assertIn(
  117. "<p>The issue tracker for this project is read-only</p>",
  118. output_text,
  119. )
  120. def test_edit_ticket(self):
  121. """Test editing a ticket."""
  122. user = tests.FakeUser(username="pingou")
  123. with tests.user_set(self.app.application, user):
  124. output = self.app.post(
  125. "/test/issue/1/edit", data={}, follow_redirects=True
  126. )
  127. self.assertEqual(output.status_code, 401)
  128. output_text = output.get_data(as_text=True)
  129. self.assertIn(
  130. "<title>Unauthorized :'( - Pagure</title>", output_text
  131. )
  132. self.assertIn(
  133. "<p>The issue tracker for this project is read-only</p>",
  134. output_text,
  135. )
  136. def test_new_issue(self):
  137. """Test creating a new ticket."""
  138. user = tests.FakeUser(username="pingou")
  139. with tests.user_set(self.app.application, user):
  140. output = self.app.post("/test/new_issue/", data={})
  141. self.assertEqual(output.status_code, 401)
  142. output_text = output.get_data(as_text=True)
  143. self.assertIn(
  144. "<title>Unauthorized :'( - Pagure</title>", output_text
  145. )
  146. self.assertIn(
  147. "<p>The issue tracker for this project is read-only</p>",
  148. output_text,
  149. )
  150. def test_deleting_issue(self):
  151. """Test deleting a new ticket."""
  152. user = tests.FakeUser(username="pingou")
  153. with tests.user_set(self.app.application, user):
  154. output = self.app.post("/test/issue/1/drop", data={})
  155. self.assertEqual(output.status_code, 401)
  156. output_text = output.get_data(as_text=True)
  157. self.assertIn(
  158. "<title>Unauthorized :'( - Pagure</title>", output_text
  159. )
  160. self.assertIn(
  161. "<p>The issue tracker for this project is read-only</p>",
  162. output_text,
  163. )
  164. def test_uploading_to_issue(self):
  165. """Test uploading to a new ticket."""
  166. user = tests.FakeUser(username="pingou")
  167. with tests.user_set(self.app.application, user):
  168. output = self.app.post("/test/issue/1/upload", data={})
  169. self.assertEqual(output.status_code, 401)
  170. output_text = output.get_data(as_text=True)
  171. self.assertIn(
  172. "<title>Unauthorized :'( - Pagure</title>", output_text
  173. )
  174. self.assertIn(
  175. "<p>The issue tracker for this project is read-only</p>",
  176. output_text,
  177. )
  178. class PagureFlaskAPIIssuesReadOnlytests(PagureFlaskIssuesReadOnlytests):
  179. """Tests for flask API issues controller of pagure with read-only tickets"""
  180. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  181. def setUp(self):
  182. """ Set up the environnment, ran before every tests. """
  183. super(PagureFlaskAPIIssuesReadOnlytests, self).setUp()
  184. def test_api_new_issue(self):
  185. """Test creating a new ticket."""
  186. user = tests.FakeUser(username="pingou")
  187. with tests.user_set(self.app.application, user):
  188. output = self.app.post("/api/0/test/new_issue", data={})
  189. self.assertEqual(output.status_code, 401)
  190. data = json.loads(output.get_data(as_text=True))
  191. self.assertEqual(
  192. data,
  193. {
  194. "error": "The issue tracker of this project is read-only",
  195. "error_code": "ETRACKERREADONLY",
  196. },
  197. )
  198. def test_api_change_status_issue(self):
  199. """ Test closing a ticket. """
  200. user = tests.FakeUser(username="pingou")
  201. with tests.user_set(self.app.application, user):
  202. output = self.app.post("/api/0/test/issue/1/status", data={})
  203. self.assertEqual(output.status_code, 401)
  204. data = json.loads(output.get_data(as_text=True))
  205. self.assertEqual(
  206. data,
  207. {
  208. "error": "The issue tracker of this project is read-only",
  209. "error_code": "ETRACKERREADONLY",
  210. },
  211. )
  212. def test_api_change_milestone_issue(self):
  213. """ Test change the milestone of a ticket. """
  214. user = tests.FakeUser(username="pingou")
  215. with tests.user_set(self.app.application, user):
  216. output = self.app.post("/api/0/test/issue/1/milestone", data={})
  217. self.assertEqual(output.status_code, 401)
  218. data = json.loads(output.get_data(as_text=True))
  219. self.assertEqual(
  220. data,
  221. {
  222. "error": "The issue tracker of this project is read-only",
  223. "error_code": "ETRACKERREADONLY",
  224. },
  225. )
  226. def test_api_comment_issue(self):
  227. """ Test comment on a ticket. """
  228. user = tests.FakeUser(username="pingou")
  229. with tests.user_set(self.app.application, user):
  230. output = self.app.post("/api/0/test/issue/1/comment", data={})
  231. self.assertEqual(output.status_code, 401)
  232. data = json.loads(output.get_data(as_text=True))
  233. self.assertEqual(
  234. data,
  235. {
  236. "error": "The issue tracker of this project is read-only",
  237. "error_code": "ETRACKERREADONLY",
  238. },
  239. )
  240. def test_api_assign_issue(self):
  241. """ Test assigning a ticket. """
  242. user = tests.FakeUser(username="pingou")
  243. with tests.user_set(self.app.application, user):
  244. output = self.app.post("/api/0/test/issue/1/assign", data={})
  245. self.assertEqual(output.status_code, 401)
  246. data = json.loads(output.get_data(as_text=True))
  247. self.assertEqual(
  248. data,
  249. {
  250. "error": "The issue tracker of this project is read-only",
  251. "error_code": "ETRACKERREADONLY",
  252. },
  253. )
  254. def test_api_subscribe_issue(self):
  255. """ Test subscribing to a ticket. """
  256. user = tests.FakeUser(username="pingou")
  257. with tests.user_set(self.app.application, user):
  258. output = self.app.post("/api/0/test/issue/1/subscribe", data={})
  259. self.assertEqual(output.status_code, 401)
  260. data = json.loads(output.get_data(as_text=True))
  261. self.assertEqual(
  262. data,
  263. {
  264. "error": "The issue tracker of this project is read-only",
  265. "error_code": "ETRACKERREADONLY",
  266. },
  267. )
  268. def test_api_update_custom_field(self):
  269. """ Test updating a specific custom fields on a ticket. """
  270. user = tests.FakeUser(username="pingou")
  271. with tests.user_set(self.app.application, user):
  272. output = self.app.post("/api/0/test/issue/1/custom/foo", data={})
  273. self.assertEqual(output.status_code, 401)
  274. data = json.loads(output.get_data(as_text=True))
  275. self.assertEqual(
  276. data,
  277. {
  278. "error": "The issue tracker of this project is read-only",
  279. "error_code": "ETRACKERREADONLY",
  280. },
  281. )
  282. def test_api_update_custom_fields(self):
  283. """ Test updating custom fields on a ticket. """
  284. user = tests.FakeUser(username="pingou")
  285. with tests.user_set(self.app.application, user):
  286. output = self.app.post("/api/0/test/issue/1/custom", data={})
  287. self.assertEqual(output.status_code, 401)
  288. data = json.loads(output.get_data(as_text=True))
  289. self.assertEqual(
  290. data,
  291. {
  292. "error": "The issue tracker of this project is read-only",
  293. "error_code": "ETRACKERREADONLY",
  294. },
  295. )
  296. class PagureFlaskIssuesAndPRDisabledtests(tests.Modeltests):
  297. """Tests for flask issues controller of pagure with tickets and PRs
  298. disabled.
  299. """
  300. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  301. def setUp(self):
  302. """ Set up the environnment, ran before every tests. """
  303. super(PagureFlaskIssuesAndPRDisabledtests, self).setUp()
  304. tests.create_projects(self.session)
  305. tests.create_projects_git(os.path.join(self.path, "repos"))
  306. # Make the project's issue tracker read-only
  307. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  308. settings = repo.settings
  309. settings["pull_requests"] = False
  310. settings["issue_tracker_read_only"] = True
  311. repo.settings = settings
  312. self.session.add(repo)
  313. self.session.commit()
  314. # Create a couple of issue
  315. msg = pagure.lib.query.new_issue(
  316. session=self.session,
  317. repo=repo,
  318. title="Test issue #1",
  319. content="We should work on this for the second time",
  320. user="foo",
  321. status="Open",
  322. private=True,
  323. )
  324. self.session.commit()
  325. self.assertEqual(msg.title, "Test issue #1")
  326. msg = pagure.lib.query.new_issue(
  327. session=self.session,
  328. repo=repo,
  329. title="Test issue #2",
  330. content="We should work on this for the second time",
  331. user="foo",
  332. status="Open",
  333. private=False,
  334. )
  335. self.session.commit()
  336. self.assertEqual(msg.title, "Test issue #2")
  337. def test_edit_tag(self):
  338. """Test editing a ticket tag."""
  339. user = tests.FakeUser(username="pingou")
  340. with tests.user_set(self.app.application, user):
  341. output = self.app.post("/test/tag/tag1/edit", data={})
  342. self.assertEqual(output.status_code, 401)
  343. output_text = output.get_data(as_text=True)
  344. self.assertIn(
  345. "<title>Unauthorized :'( - Pagure</title>", output_text
  346. )
  347. self.assertIn(
  348. "<p>The issue tracker for this project is read-only</p>",
  349. output_text,
  350. )
  351. def test_drop_tags(self):
  352. """Test dropping a ticket tag."""
  353. user = tests.FakeUser(username="pingou")
  354. with tests.user_set(self.app.application, user):
  355. output = self.app.post("/test/droptag/", data={})
  356. self.assertEqual(output.status_code, 401)
  357. output_text = output.get_data(as_text=True)
  358. self.assertIn(
  359. "<title>Unauthorized :'( - Pagure</title>", output_text
  360. )
  361. self.assertIn(
  362. "<p>The issue tracker for this project is read-only</p>",
  363. output_text,
  364. )
  365. if __name__ == "__main__":
  366. unittest.main(verbosity=2)