test_pagure_flask_ui_repo_slash_name.py 8.7 KB

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