test_pagure_flask_ui_issues_templates.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import sys
  12. import os
  13. import pygit2
  14. from mock import patch, MagicMock
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure
  18. import pagure.lib
  19. import tests
  20. def create_templates(repopath):
  21. """ Create a couple of templates at the specified repo.
  22. """
  23. clone_repo = pygit2.Repository(repopath)
  24. # Create the RFE template
  25. os.mkdir(os.path.join(repopath, 'templates'))
  26. template = os.path.join(repopath, 'templates', 'RFE.md')
  27. with open(template, 'w') as stream:
  28. stream.write('RFE\n###\n\n* Idea description')
  29. clone_repo.index.add(os.path.join('templates', 'RFE.md'))
  30. clone_repo.index.write()
  31. # Commit
  32. tree = clone_repo.index.write_tree()
  33. author = pygit2.Signature(
  34. 'Alice Author', 'alice@authors.tld')
  35. committer = pygit2.Signature(
  36. 'Cecil Committer', 'cecil@committers.tld')
  37. commit = clone_repo.create_commit(
  38. 'refs/heads/master', # the name of the reference to update
  39. author,
  40. committer,
  41. 'Add a RFE template',
  42. # binary string representing the tree object ID
  43. tree,
  44. # list of binary strings representing parents of the new commit
  45. []
  46. )
  47. # Create the 2018-bid.md template
  48. template = os.path.join(repopath, 'templates', '2018-bid.md')
  49. with open(template, 'w') as stream:
  50. stream.write('Bid for 2018\n############\n\n* Location:')
  51. clone_repo.index.add(os.path.join('templates', '2018-bid.md'))
  52. clone_repo.index.write()
  53. # Commit
  54. tree = clone_repo.index.write_tree()
  55. author = pygit2.Signature(
  56. 'Alice Author', 'alice@authors.tld')
  57. committer = pygit2.Signature(
  58. 'Cecil Committer', 'cecil@committers.tld')
  59. commit = clone_repo.create_commit(
  60. 'refs/heads/master', # the name of the reference to update
  61. author,
  62. committer,
  63. 'Add a RFE template',
  64. # binary string representing the tree object ID
  65. tree,
  66. # list of binary strings representing parents of the new commit
  67. [commit.hex]
  68. )
  69. # Create the default.md template
  70. template = os.path.join(repopath, 'templates', 'default.md')
  71. with open(template, 'w') as stream:
  72. stream.write('Report your issue')
  73. clone_repo.index.add(os.path.join('templates', 'default.md'))
  74. clone_repo.index.write()
  75. # Commit
  76. tree = clone_repo.index.write_tree()
  77. author = pygit2.Signature(
  78. 'Alice Author', 'alice@authors.tld')
  79. committer = pygit2.Signature(
  80. 'Cecil Committer', 'cecil@committers.tld')
  81. clone_repo.create_commit(
  82. 'refs/heads/master', # the name of the reference to update
  83. author,
  84. committer,
  85. 'Add a default template',
  86. # binary string representing the tree object ID
  87. tree,
  88. # list of binary strings representing parents of the new commit
  89. [commit.hex]
  90. )
  91. class PagureFlaskIssuestests(tests.Modeltests):
  92. """ Tests for flask issues controller of pagure """
  93. @patch('pagure.lib.git.update_git', MagicMock(return_value=True))
  94. @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
  95. def setUp(self):
  96. """ Set up the environnment, run before every tests. """
  97. super(PagureFlaskIssuestests, self).setUp()
  98. pagure.config.config['TICKETS_FOLDER'] = os.path.join(
  99. self.path, 'tickets')
  100. tests.create_projects(self.session)
  101. tests.create_projects_git(
  102. os.path.join(self.path, 'repos'), bare=True)
  103. tests.create_projects_git(
  104. os.path.join(self.path, 'tickets'))
  105. # Add a couple of templates to test2
  106. repopath = os.path.join(self.path, 'tickets', 'test2.git')
  107. create_templates(repopath)
  108. # Add a couple of templates to somenamespace/test3
  109. repopath = os.path.join(
  110. self.path, 'tickets', 'somenamespace', 'test3.git')
  111. create_templates(repopath)
  112. def test_new_issue_no_template(self):
  113. """ Test the new_issue endpoint when the project has no templates.
  114. """
  115. user = tests.FakeUser()
  116. with tests.user_set(self.app.application, user):
  117. output = self.app.get('/test/new_issue')
  118. self.assertEqual(output.status_code, 200)
  119. self.assertIn(
  120. '<div class="card-header">\n New issue',
  121. output.data)
  122. self.assertNotIn(
  123. '<strong><label for="status">Type</label></strong>',
  124. output.data)
  125. self.assertNotIn(
  126. '<select class="form-control c-select" id="type" name="type">',
  127. output.data)
  128. def test_new_issue_w_template(self):
  129. """ Test the new_issue endpoint when the project has templates. """
  130. user = tests.FakeUser()
  131. with tests.user_set(self.app.application, user):
  132. output = self.app.get('/test2/new_issue')
  133. self.assertEqual(output.status_code, 200)
  134. self.assertIn(
  135. '<div class="card-header">\n New issue',
  136. output.data)
  137. self.assertIn(
  138. '<strong><label for="status">Type</label></strong>',
  139. output.data)
  140. self.assertIn(
  141. '<select class="form-control c-select" id="type" name="type">',
  142. output.data)
  143. self.assertIn(
  144. '<option value="RFE">RFE</option>',
  145. output.data)
  146. self.assertIn(
  147. '<option value="2018-bid">2018-bid</option>',
  148. output.data)
  149. self.assertIn(
  150. '<option selected value="default">default</option>',
  151. output.data)
  152. def test_get_ticket_template_no_csrf(self):
  153. """ Test the get_ticket_template endpoint when the project has no
  154. templates.
  155. """
  156. user = tests.FakeUser()
  157. with tests.user_set(self.app.application, user):
  158. output = self.app.post('/pv/test/issue/template')
  159. self.assertEqual(output.status_code, 400)
  160. data = json.loads(output.data)
  161. self.assertEqual(
  162. data,
  163. {"code": "ERROR", "message": "Invalid input submitted"})
  164. def test_get_ticket_template_no_template_specified(self):
  165. """ Test the get_ticket_template endpoint when not specifying which
  166. template to get.
  167. """
  168. user = tests.FakeUser()
  169. with tests.user_set(self.app.application, user):
  170. csrf = self.get_csrf()
  171. data = {'csrf_token': csrf}
  172. output = self.app.post('/pv/test/issue/template', data=data)
  173. self.assertEqual(output.status_code, 400)
  174. data = json.loads(output.data)
  175. self.assertEqual(
  176. data,
  177. {"code": "ERROR", "message": "No template provided"})
  178. def test_get_ticket_template_no_project(self):
  179. """ Test the get_ticket_template endpoint when not specifying which
  180. template to get.
  181. """
  182. user = tests.FakeUser()
  183. with tests.user_set(self.app.application, user):
  184. csrf = self.get_csrf()
  185. data = {'csrf_token': csrf}
  186. output = self.app.post(
  187. '/pv/test/issue/template?template=RFE', data=data)
  188. self.assertEqual(output.status_code, 404)
  189. def test_get_ticket_template_no_template(self):
  190. """ Test the get_ticket_template endpoint when the project has no
  191. templates.
  192. """
  193. user = tests.FakeUser()
  194. with tests.user_set(self.app.application, user):
  195. csrf = self.get_csrf()
  196. data = {'csrf_token': csrf}
  197. output = self.app.post(
  198. '/pv/test/issue/template?template=RFE', data=data)
  199. self.assertEqual(output.status_code, 404)
  200. data = json.loads(output.data)
  201. self.assertEqual(
  202. data,
  203. {"code": "ERROR", "message": "No such template found"})
  204. def test_get_ticket_template_w_template(self):
  205. """ Test the get_ticket_template endpoint when the project has
  206. templates.
  207. """
  208. user = tests.FakeUser()
  209. with tests.user_set(self.app.application, user):
  210. csrf = self.get_csrf()
  211. data = {'csrf_token': csrf}
  212. output = self.app.post(
  213. '/pv/test2/issue/template?template=RFE', data=data)
  214. self.assertEqual(output.status_code, 200)
  215. data = json.loads(output.data)
  216. self.assertEqual(
  217. data,
  218. {
  219. "code": "OK",
  220. "message": "RFE\n###\n\n* Idea description"
  221. }
  222. )
  223. def test_get_ticket_template_w_template_namespace(self):
  224. """ Test the get_ticket_template endpoint when the project has
  225. templates and a namespace.
  226. """
  227. user = tests.FakeUser()
  228. with tests.user_set(self.app.application, user):
  229. csrf = self.get_csrf()
  230. data = {'csrf_token': csrf}
  231. output = self.app.post(
  232. '/pv/somenamespace/test3/issue/template?template=RFE',
  233. data=data)
  234. self.assertEqual(output.status_code, 200)
  235. data = json.loads(output.data)
  236. self.assertEqual(
  237. data,
  238. {
  239. "code": "OK",
  240. "message": "RFE\n###\n\n* Idea description"
  241. }
  242. )
  243. if __name__ == '__main__':
  244. unittest.main(verbosity=2)