test_pagure_flask_ui_issues_templates.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 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 json
  9. import unittest
  10. import sys
  11. import os
  12. import pygit2
  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
  18. import tests
  19. def create_templates(repopath):
  20. """Create a couple of templates at the specified repo."""
  21. clone_repo = pygit2.Repository(repopath)
  22. # Create the RFE template
  23. os.mkdir(os.path.join(repopath, "templates"))
  24. template = os.path.join(repopath, "templates", "RFE.md")
  25. with open(template, "w") as stream:
  26. stream.write("RFE\n###\n\n* Idea description")
  27. clone_repo.index.add(os.path.join("templates", "RFE.md"))
  28. clone_repo.index.write()
  29. # Commit
  30. tree = clone_repo.index.write_tree()
  31. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  32. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  33. commit = clone_repo.create_commit(
  34. "refs/heads/master", # the name of the reference to update
  35. author,
  36. committer,
  37. "Add a RFE template",
  38. # binary string representing the tree object ID
  39. tree,
  40. # list of binary strings representing parents of the new commit
  41. [],
  42. )
  43. # Create the 2018-bid.md template
  44. template = os.path.join(repopath, "templates", "2018-bid.md")
  45. with open(template, "w") as stream:
  46. stream.write("Bid for 2018\n############\n\n* Location:")
  47. clone_repo.index.add(os.path.join("templates", "2018-bid.md"))
  48. clone_repo.index.write()
  49. # Commit
  50. tree = clone_repo.index.write_tree()
  51. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  52. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  53. commit = clone_repo.create_commit(
  54. "refs/heads/master", # the name of the reference to update
  55. author,
  56. committer,
  57. "Add a RFE template",
  58. # binary string representing the tree object ID
  59. tree,
  60. # list of binary strings representing parents of the new commit
  61. [commit.hex],
  62. )
  63. # Create the default.md template
  64. template = os.path.join(repopath, "templates", "default.md")
  65. with open(template, "w") as stream:
  66. stream.write("Report your issue")
  67. clone_repo.index.add(os.path.join("templates", "default.md"))
  68. clone_repo.index.write()
  69. # Commit
  70. tree = clone_repo.index.write_tree()
  71. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  72. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  73. clone_repo.create_commit(
  74. "refs/heads/master", # the name of the reference to update
  75. author,
  76. committer,
  77. "Add a default template",
  78. # binary string representing the tree object ID
  79. tree,
  80. # list of binary strings representing parents of the new commit
  81. [commit.hex],
  82. )
  83. class PagureFlaskIssuesTemplatetests(tests.Modeltests):
  84. """ Tests for flask issues controller of pagure """
  85. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  86. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  87. def setUp(self):
  88. """ Set up the environnment, run before every tests. """
  89. super(PagureFlaskIssuesTemplatetests, self).setUp()
  90. pagure.config.config["TICKETS_FOLDER"] = os.path.join(
  91. self.path, "tickets"
  92. )
  93. tests.create_projects(self.session)
  94. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  95. tests.create_projects_git(os.path.join(self.path, "tickets"))
  96. # Add a couple of templates to test2
  97. repopath = os.path.join(self.path, "tickets", "test2.git")
  98. create_templates(repopath)
  99. # Add a couple of templates to somenamespace/test3
  100. repopath = os.path.join(
  101. self.path, "tickets", "somenamespace", "test3.git"
  102. )
  103. create_templates(repopath)
  104. def test_new_issue_no_template(self):
  105. """Test the new_issue endpoint when the project has no templates."""
  106. user = tests.FakeUser()
  107. with tests.user_set(self.app.application, user):
  108. output = self.app.get("/test/new_issue")
  109. self.assertEqual(output.status_code, 200)
  110. output_text = output.get_data(as_text=True)
  111. self.assertIn(
  112. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  113. output_text,
  114. )
  115. self.assertNotIn("Issue Templates", output_text)
  116. def test_new_issue_w_template(self):
  117. """ Test the new_issue endpoint when the project has templates. """
  118. user = tests.FakeUser()
  119. with tests.user_set(self.app.application, user):
  120. output = self.app.get("/test2/new_issue")
  121. self.assertEqual(output.status_code, 200)
  122. output_text = output.get_data(as_text=True)
  123. self.assertIn(
  124. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  125. output_text,
  126. )
  127. self.assertIn("Issue Templates", output_text)
  128. self.assertIn(
  129. '<a class="issue-template dropdown-item pointer" data-value="RFE">RFE</a>',
  130. output_text,
  131. )
  132. self.assertIn(
  133. '<a class="issue-template dropdown-item pointer" data-value="2018-bid">2018-bid</a>',
  134. output_text,
  135. )
  136. self.assertIn(
  137. '<a class="issue-template dropdown-item pointer" data-value="default">default</a>',
  138. output_text,
  139. )
  140. self.assertIn(
  141. 'placeholder="Enter your comment here" tabindex=2 required>'
  142. "Report your issue</textarea>",
  143. output_text,
  144. )
  145. def test_new_issue_w_specific_template(self):
  146. """ Test the new_issue endpoint when the project has templates. """
  147. user = tests.FakeUser()
  148. with tests.user_set(self.app.application, user):
  149. output = self.app.get("/test2/new_issue?template=2018-bid")
  150. self.assertEqual(output.status_code, 200)
  151. output_text = output.get_data(as_text=True)
  152. self.assertIn(
  153. '<h4 class="font-weight-bold mb-4">New Issue</h4>\n',
  154. output_text,
  155. )
  156. self.assertIn("Issue Templates", output_text)
  157. self.assertIn(
  158. '<a class="issue-template dropdown-item pointer" data-value="RFE">RFE</a>',
  159. output_text,
  160. )
  161. self.assertIn(
  162. '<a class="issue-template dropdown-item pointer" data-value="2018-bid">2018-bid</a>',
  163. output_text,
  164. )
  165. self.assertIn(
  166. '<a class="issue-template dropdown-item pointer" data-value="default">default</a>',
  167. output_text,
  168. )
  169. self.assertIn(
  170. 'placeholder="Enter your comment here" tabindex=2 required>'
  171. "Bid for 2018\n############",
  172. output_text,
  173. )
  174. def test_get_ticket_template_no_csrf(self):
  175. """Test the get_ticket_template endpoint when the project has no
  176. templates.
  177. """
  178. user = tests.FakeUser()
  179. with tests.user_set(self.app.application, user):
  180. output = self.app.post("/pv/test/issue/template")
  181. self.assertEqual(output.status_code, 400)
  182. data = json.loads(output.get_data(as_text=True))
  183. self.assertEqual(
  184. data, {"code": "ERROR", "message": "Invalid input submitted"}
  185. )
  186. def test_get_ticket_template_no_template_specified(self):
  187. """Test the get_ticket_template endpoint when not specifying which
  188. template to get.
  189. """
  190. user = tests.FakeUser()
  191. with tests.user_set(self.app.application, user):
  192. csrf = self.get_csrf()
  193. data = {"csrf_token": csrf}
  194. output = self.app.post("/pv/test/issue/template", data=data)
  195. self.assertEqual(output.status_code, 400)
  196. data = json.loads(output.get_data(as_text=True))
  197. self.assertEqual(
  198. data, {"code": "ERROR", "message": "No template provided"}
  199. )
  200. def test_get_ticket_template_no_project(self):
  201. """Test the get_ticket_template endpoint when the project does not
  202. exist.
  203. """
  204. user = tests.FakeUser()
  205. with tests.user_set(self.app.application, user):
  206. csrf = self.get_csrf()
  207. data = {"csrf_token": csrf}
  208. output = self.app.post("/pv/foobar/issue/template", data=data)
  209. self.assertEqual(output.status_code, 404)
  210. def test_get_ticket_template_no_template(self):
  211. """Test the get_ticket_template endpoint when the project has no
  212. templates.
  213. """
  214. user = tests.FakeUser()
  215. with tests.user_set(self.app.application, user):
  216. csrf = self.get_csrf()
  217. data = {"csrf_token": csrf}
  218. output = self.app.post(
  219. "/pv/test/issue/template?template=RFE", data=data
  220. )
  221. self.assertEqual(output.status_code, 404)
  222. data = json.loads(output.get_data(as_text=True))
  223. self.assertEqual(
  224. data, {"code": "ERROR", "message": "No such template found"}
  225. )
  226. def test_get_ticket_template_issue_tracker_disabled(self):
  227. """Test the get_ticket_template endpoint when the project has
  228. disabled its issue tracker.
  229. """
  230. repo = pagure.lib.query.get_authorized_project(self.session, "test")
  231. settings = repo.settings
  232. settings["issue_tracker"] = False
  233. repo.settings = settings
  234. self.session.add(repo)
  235. self.session.commit()
  236. user = tests.FakeUser()
  237. with tests.user_set(self.app.application, user):
  238. csrf = self.get_csrf()
  239. data = {"csrf_token": csrf}
  240. output = self.app.post(
  241. "/pv/test/issue/template?template=RFE", data=data
  242. )
  243. self.assertEqual(output.status_code, 404)
  244. data = json.loads(output.get_data(as_text=True))
  245. self.assertEqual(
  246. data,
  247. {
  248. "code": "ERROR",
  249. "message": "No issue tracker found for this project",
  250. },
  251. )
  252. def test_get_ticket_template_w_template(self):
  253. """Test the get_ticket_template endpoint when the project has
  254. templates.
  255. """
  256. user = tests.FakeUser()
  257. with tests.user_set(self.app.application, user):
  258. csrf = self.get_csrf()
  259. data = {"csrf_token": csrf}
  260. output = self.app.post(
  261. "/pv/test2/issue/template?template=RFE", data=data
  262. )
  263. self.assertEqual(output.status_code, 200)
  264. data = json.loads(output.get_data(as_text=True))
  265. self.assertEqual(
  266. data,
  267. {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
  268. )
  269. def test_get_ticket_template_w_template_namespace(self):
  270. """Test the get_ticket_template endpoint when the project has
  271. templates and a namespace.
  272. """
  273. user = tests.FakeUser()
  274. with tests.user_set(self.app.application, user):
  275. csrf = self.get_csrf()
  276. data = {"csrf_token": csrf}
  277. output = self.app.post(
  278. "/pv/somenamespace/test3/issue/template?template=RFE",
  279. data=data,
  280. )
  281. self.assertEqual(output.status_code, 200)
  282. data = json.loads(output.get_data(as_text=True))
  283. self.assertEqual(
  284. data,
  285. {"code": "OK", "message": "RFE\n###\n\n* Idea description"},
  286. )
  287. if __name__ == "__main__":
  288. unittest.main(verbosity=2)