test_pagure_flask_ui_issues_templates.py 12 KB

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