test_pagure_flask_ui_repo_slash_name.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - 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 shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib
  20. import tests
  21. from pagure.lib.repo import PagureRepo
  22. class PagureFlaskSlashInNametests(tests.Modeltests):
  23. """ Tests for flask application when the project contains a '/'.
  24. """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskSlashInNametests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.lib.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.fork.SESSION = self.session
  34. pagure.ui.repo.SESSION = self.session
  35. pagure.ui.issues.SESSION = self.session
  36. pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
  37. pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks')
  38. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  39. tests.HERE, 'tickets')
  40. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  41. tests.HERE, 'docs')
  42. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  43. tests.HERE, 'requests')
  44. self.app = pagure.APP.test_client()
  45. def set_up_git_repo(self, name='test'):
  46. """ Set up the git repo to play with. """
  47. # Create a git repo to play with
  48. gitrepo = os.path.join(tests.HERE, 'repos', '%s.git' % name)
  49. repo = pygit2.init_repository(gitrepo, bare=True)
  50. newpath = tempfile.mkdtemp(prefix='pagure-other-test')
  51. repopath = os.path.join(newpath, 'test')
  52. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  53. # Create a file in that git repo
  54. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  55. stream.write('foo\n bar')
  56. clone_repo.index.add('sources')
  57. clone_repo.index.write()
  58. # Commits the files added
  59. tree = clone_repo.index.write_tree()
  60. author = pygit2.Signature(
  61. 'Alice Author', 'alice@authors.tld')
  62. committer = pygit2.Signature(
  63. 'Cecil Committer', 'cecil@committers.tld')
  64. clone_repo.create_commit(
  65. 'refs/heads/master', # the name of the reference to update
  66. author,
  67. committer,
  68. 'Add sources file for testing',
  69. # binary string representing the tree object ID
  70. tree,
  71. # list of binary strings representing parents of the new commit
  72. []
  73. )
  74. refname = 'refs/heads/master'
  75. ori_remote = clone_repo.remotes[0]
  76. PagureRepo.push(ori_remote, refname)
  77. @patch('pagure.lib.notify.send_email')
  78. def test_view_repo_empty(self, send_email):
  79. """ Test the view_repo endpoint when the project has a slash in its
  80. name.
  81. """
  82. send_email.return_value = True
  83. tests.create_projects(self.session)
  84. # Non-existant git repo
  85. output = self.app.get('/test')
  86. self.assertEqual(output.status_code, 404)
  87. # Create a git repo to play with
  88. gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
  89. repo = pygit2.init_repository(gitrepo, bare=True)
  90. # With git repo
  91. output = self.app.get('/test')
  92. self.assertEqual(output.status_code, 200)
  93. self.assertIn(
  94. '<div class="card-block">\n '
  95. '<h5><strong>Owners</strong></h5>', output.data)
  96. self.assertIn(
  97. '<p>The Project Creator has not pushed any code yet</p>',
  98. output.data)
  99. # We can't create the project `forks/test` the normal way
  100. self.assertRaises(
  101. pagure.exceptions.PagureException,
  102. pagure.lib.new_project,
  103. self.session,
  104. name='forks/test',
  105. description='test project forks/test',
  106. url='',
  107. avatar_email='',
  108. user='pingou',
  109. blacklist=pagure.APP.config['BLACKLISTED_PROJECTS'],
  110. allowed_prefix=pagure.APP.config['ALLOWED_PREFIX'],
  111. gitfolder=pagure.APP.config['GIT_FOLDER'],
  112. docfolder=pagure.APP.config['DOCS_FOLDER'],
  113. ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
  114. requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
  115. )
  116. # So just put it in the DB
  117. item = pagure.lib.model.Project(
  118. user_id=1, # pingou
  119. name='forks/test',
  120. description='test project forks/test',
  121. hook_token='aaabbbcccddd',
  122. )
  123. self.session.add(item)
  124. self.session.commit()
  125. # Create a git repo to play with
  126. gitrepo = os.path.join(tests.HERE, 'repos', 'forks/test.git')
  127. repo = pygit2.init_repository(gitrepo, bare=True)
  128. output = self.app.get('/forks/test')
  129. self.assertEqual(output.status_code, 200)
  130. self.assertIn(
  131. '<div class="card-block">\n '
  132. '<h5><strong>Owners</strong></h5>', output.data)
  133. self.assertIn(
  134. '<p>The Project Creator has not pushed any code yet</p>',
  135. output.data)
  136. output = self.app.get('/forks/test/issues')
  137. self.assertEqual(output.status_code, 200)
  138. self.assertIn(
  139. '<title>Issues - forks/test - Pagure</title>', output.data)
  140. self.assertIn(
  141. '<td colspan="5" class="noresult">No issues found</td>',
  142. output.data)
  143. @patch('pagure.lib.notify.send_email')
  144. def test_view_repo(self, send_email):
  145. """ Test the view_repo endpoint when the project has a slash in its
  146. name.
  147. """
  148. send_email.return_value = True
  149. tests.create_projects(self.session)
  150. # Non-existant git repo
  151. output = self.app.get('/test')
  152. self.assertEqual(output.status_code, 404)
  153. self.set_up_git_repo()
  154. # With git repo
  155. output = self.app.get('/test')
  156. self.assertEqual(output.status_code, 200)
  157. self.assertIn(
  158. '<div class="card-block">\n '
  159. '<h5><strong>Owners</strong></h5>', output.data)
  160. # We can't create the project `forks/test` the normal way
  161. self.assertRaises(
  162. pagure.exceptions.PagureException,
  163. pagure.lib.new_project,
  164. self.session,
  165. name='forks/test',
  166. description='test project forks/test',
  167. url='',
  168. avatar_email='',
  169. user='pingou',
  170. blacklist=pagure.APP.config['BLACKLISTED_PROJECTS'],
  171. allowed_prefix=pagure.APP.config['ALLOWED_PREFIX'],
  172. gitfolder=pagure.APP.config['GIT_FOLDER'],
  173. docfolder=pagure.APP.config['DOCS_FOLDER'],
  174. ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
  175. requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
  176. )
  177. # So just put it in the DB
  178. item = pagure.lib.model.Project(
  179. user_id=1, # pingou
  180. name='forks/test',
  181. description='test project forks/test',
  182. hook_token='aaabbbcccddd',
  183. )
  184. self.session.add(item)
  185. self.session.commit()
  186. self.set_up_git_repo(name='forks/test')
  187. # Front page shows fine
  188. output = self.app.get('/forks/test')
  189. self.assertEqual(output.status_code, 200)
  190. self.assertIn(
  191. '<div class="card-block">\n '
  192. '<h5><strong>Owners</strong></h5>', output.data)
  193. self.assertIn('Add sources file for testing', output.data)
  194. self.assertIn(
  195. '<title>Overview - forks/test - Pagure</title>', output.data)
  196. # Issues list shows fine
  197. output = self.app.get('/forks/test/issues')
  198. self.assertEqual(output.status_code, 200)
  199. self.assertIn(
  200. '<title>Issues - forks/test - Pagure</title>', output.data)
  201. self.assertIn(
  202. '<td colspan="5" class="noresult">No issues found</td>',
  203. output.data)
  204. # Try accessing the commit
  205. gitrepo = os.path.join(tests.HERE, 'repos', 'forks/test.git')
  206. repo = pygit2.Repository(gitrepo)
  207. master_branch = repo.lookup_branch('master')
  208. first_commit = master_branch.get_object().hex
  209. output = self.app.get('/forks/test/commits')
  210. self.assertEqual(output.status_code, 200)
  211. self.assertIn(first_commit, output.data)
  212. self.assertIn(
  213. '<a href="/forks/test/c/%s?branch=master"' % first_commit,
  214. output.data)
  215. output = self.app.get('/forks/test/c/%s' % first_commit)
  216. self.assertEqual(output.status_code, 200)
  217. self.assertIn('<title>Commit - forks/test ', output.data)
  218. self.assertIn(
  219. '<span class="label label-success">+2</span> </span>',
  220. output.data)
  221. if __name__ == '__main__':
  222. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  223. PagureFlaskSlashInNametests)
  224. unittest.TextTestRunner(verbosity=2).run(SUITE)