test_pagure_flask_ui_repo_slash_name.py 8.4 KB

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