test_pagure_flask_ui_repo_slash_name.py 8.4 KB

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