test_pagure_flask_ui_repo_slash_name.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016-2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. __requires__ = ['SQLAlchemy >= 0.8']
  9. import pkg_resources
  10. import json
  11. import unittest
  12. import shutil
  13. import sys
  14. import tempfile
  15. import os
  16. import pygit2
  17. from mock import patch
  18. sys.path.insert(0, os.path.join(os.path.dirname(
  19. os.path.abspath(__file__)), '..'))
  20. import pagure.lib
  21. import tests
  22. from pagure.lib.repo import PagureRepo
  23. class PagureFlaskSlashInNametests(tests.SimplePagureTest):
  24. """ Tests for flask application when the project contains a '/'.
  25. """
  26. def setUp(self):
  27. """ Set up the environnment, ran before every tests. """
  28. super(PagureFlaskSlashInNametests, self).setUp()
  29. def set_up_git_repo(self, name='test'):
  30. """ Set up the git repo to play with. """
  31. # Create a git repo to play with
  32. gitrepo = os.path.join(self.path, 'repos', '%s.git' % name)
  33. repo = pygit2.init_repository(gitrepo, bare=True)
  34. newpath = tempfile.mkdtemp(prefix='pagure-other-test')
  35. repopath = os.path.join(newpath, 'test')
  36. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  37. # Create a file in that git repo
  38. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  39. stream.write('foo\n bar')
  40. clone_repo.index.add('sources')
  41. clone_repo.index.write()
  42. # Commits the files added
  43. tree = clone_repo.index.write_tree()
  44. author = pygit2.Signature(
  45. 'Alice Author', 'alice@authors.tld')
  46. committer = pygit2.Signature(
  47. 'Cecil Committer', 'cecil@committers.tld')
  48. clone_repo.create_commit(
  49. 'refs/heads/master', # the name of the reference to update
  50. author,
  51. committer,
  52. 'Add sources file for testing',
  53. # binary string representing the tree object ID
  54. tree,
  55. # list of binary strings representing parents of the new commit
  56. []
  57. )
  58. refname = 'refs/heads/master'
  59. ori_remote = clone_repo.remotes[0]
  60. PagureRepo.push(ori_remote, refname)
  61. @patch('pagure.lib.notify.send_email')
  62. def test_view_repo_empty(self, send_email):
  63. """ Test the view_repo endpoint when the project has a slash in its
  64. name.
  65. """
  66. send_email.return_value = True
  67. tests.create_projects(self.session)
  68. # Non-existant git repo
  69. output = self.app.get('/test')
  70. self.assertEqual(output.status_code, 404)
  71. # Create a git repo to play with
  72. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  73. repo = pygit2.init_repository(gitrepo, bare=True)
  74. # With git repo
  75. output = self.app.get('/test')
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. '<input class="form-control bg-white" type="text" '
  80. 'value="git://localhost.localdomain/test.git" readonly>',
  81. output_text)
  82. self.assertIn(
  83. '<p>The Project Creator has not pushed any code yet</p>',
  84. output_text)
  85. # We can't create the project `forks/test` the normal way
  86. self.assertRaises(
  87. pagure.exceptions.PagureException,
  88. pagure.lib.new_project,
  89. self.session,
  90. name='test',
  91. namespace='forks',
  92. repospanner_region=None,
  93. description='test project forks/test',
  94. url='',
  95. avatar_email='',
  96. user='pingou',
  97. blacklist=pagure.config.config['BLACKLISTED_PROJECTS'],
  98. allowed_prefix=pagure.config.config['ALLOWED_PREFIX'],
  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. output_text = output.get_data(as_text=True)
  116. self.assertIn(
  117. '<input class="form-control bg-white" type="text" '
  118. 'value="git://localhost.localdomain/forks/test.git" readonly>',
  119. output_text)
  120. self.assertIn(
  121. '<p>The Project Creator has not pushed any code yet</p>',
  122. output_text)
  123. output = self.app.get('/forks/test/issues')
  124. self.assertEqual(output.status_code, 200)
  125. output_text = output.get_data(as_text=True)
  126. self.assertIn(
  127. '<title>Issues - forks/test - Pagure</title>', output_text)
  128. self.assertIn(
  129. '<span class="font-weight-bold">no open issues found</span>\n',
  130. output_text)
  131. @patch('pagure.lib.notify.send_email')
  132. def test_view_repo(self, send_email):
  133. """ Test the view_repo endpoint when the project has a slash in its
  134. name.
  135. """
  136. send_email.return_value = True
  137. tests.create_projects(self.session)
  138. # Non-existant git repo
  139. output = self.app.get('/test')
  140. self.assertEqual(output.status_code, 404)
  141. self.set_up_git_repo()
  142. # With git repo
  143. output = self.app.get('/test')
  144. self.assertEqual(output.status_code, 200)
  145. output_text = output.get_data(as_text=True)
  146. self.assertIn(
  147. '<input class="form-control bg-white" type="text" '
  148. 'value="git://localhost.localdomain/test.git" readonly>',
  149. output_text)
  150. # We can't create the project `forks/test` the normal way
  151. self.assertRaises(
  152. pagure.exceptions.PagureException,
  153. pagure.lib.new_project,
  154. self.session,
  155. name='test',
  156. namespace='forks',
  157. repospanner_region=None,
  158. description='test project forks/test',
  159. url='',
  160. avatar_email='',
  161. user='pingou',
  162. blacklist=pagure.config.config['BLACKLISTED_PROJECTS'],
  163. allowed_prefix=pagure.config.config['ALLOWED_PREFIX'],
  164. )
  165. # So just put it in the DB
  166. item = pagure.lib.model.Project(
  167. user_id=1, # pingou
  168. name='test',
  169. namespace='forks',
  170. description='test project forks/test',
  171. hook_token='aaabbbcccddd',
  172. )
  173. self.session.add(item)
  174. self.session.commit()
  175. self.set_up_git_repo(name='forks/test')
  176. # Front page shows fine
  177. output = self.app.get('/forks/test')
  178. self.assertEqual(output.status_code, 200)
  179. output_text = output.get_data(as_text=True)
  180. self.assertIn(
  181. '<input class="form-control bg-white" type="text" '
  182. 'value="git://localhost.localdomain/forks/test.git" readonly>',
  183. output_text)
  184. self.assertIn(
  185. '<title>Overview - forks/test - Pagure</title>', output_text)
  186. # Issues list shows fine
  187. output = self.app.get('/forks/test/issues')
  188. self.assertEqual(output.status_code, 200)
  189. output_text = output.get_data(as_text=True)
  190. self.assertIn(
  191. '<title>Issues - forks/test - Pagure</title>', output_text)
  192. self.assertIn(
  193. '<span class="font-weight-bold">no open issues found</span>\n',
  194. output_text)
  195. # Try accessing the commit
  196. gitrepo = os.path.join(self.path, 'repos', 'forks/test.git')
  197. repo = pygit2.Repository(gitrepo)
  198. master_branch = repo.lookup_branch('master')
  199. first_commit = master_branch.get_object().hex
  200. output = self.app.get('/forks/test/commits')
  201. self.assertEqual(output.status_code, 200)
  202. output_text = output.get_data(as_text=True)
  203. self.assertIn(first_commit, output_text)
  204. self.assertIn(
  205. '<a href="/forks/test/c/%s?branch=master"' % first_commit,
  206. output_text)
  207. output = self.app.get('/forks/test/c/%s' % first_commit)
  208. self.assertEqual(output.status_code, 200)
  209. output_text = output.get_data(as_text=True)
  210. self.assertIn('<title>Commit - forks/test ', output_text)
  211. if __name__ == '__main__':
  212. unittest.main(verbosity=2)