test_pagure_flask_ui_repo_slash_name.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(
  17. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  18. )
  19. import pagure.lib.query
  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. 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("Alice Author", "alice@authors.tld")
  43. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  44. clone_repo.create_commit(
  45. "refs/heads/master", # the name of the reference to update
  46. author,
  47. committer,
  48. "Add sources file for testing",
  49. # binary string representing the tree object ID
  50. tree,
  51. # list of binary strings representing parents of the new commit
  52. [],
  53. )
  54. refname = "refs/heads/master"
  55. ori_remote = clone_repo.remotes[0]
  56. PagureRepo.push(ori_remote, refname)
  57. @patch("pagure.lib.notify.send_email")
  58. def test_view_repo_empty(self, send_email):
  59. """Test the view_repo endpoint when the project has a slash in its
  60. name.
  61. """
  62. send_email.return_value = True
  63. tests.create_projects(self.session)
  64. # Non-existant git repo
  65. output = self.app.get("/test")
  66. self.assertEqual(output.status_code, 404)
  67. # Create a git repo to play with
  68. gitrepo = os.path.join(self.path, "repos", "test.git")
  69. repo = pygit2.init_repository(gitrepo, bare=True)
  70. # With git repo
  71. output = self.app.get("/test")
  72. self.assertEqual(output.status_code, 200)
  73. output_text = output.get_data(as_text=True)
  74. self.assertIn(
  75. '<input class="form-control bg-white select-on-focus" type="text" '
  76. 'value="git://localhost.localdomain/test.git" readonly>',
  77. output_text,
  78. )
  79. self.assertIn(
  80. "<p>The Project Creator has not pushed any code yet</p>",
  81. output_text,
  82. )
  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. description="test project forks/test",
  91. url="",
  92. avatar_email="",
  93. user="pingou",
  94. blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
  95. allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
  96. )
  97. # So just put it in the DB
  98. item = pagure.lib.model.Project(
  99. user_id=1, # pingou
  100. name="test",
  101. namespace="forks",
  102. description="test project forks/test",
  103. hook_token="aaabbbcccddd",
  104. )
  105. self.session.add(item)
  106. self.session.commit()
  107. # Create a git repo to play with
  108. gitrepo = os.path.join(self.path, "repos", "forks/test.git")
  109. repo = pygit2.init_repository(gitrepo, bare=True)
  110. output = self.app.get("/forks/test")
  111. self.assertEqual(output.status_code, 200)
  112. output_text = output.get_data(as_text=True)
  113. self.assertIn(
  114. '<input class="form-control bg-white select-on-focus" type="text" '
  115. 'value="git://localhost.localdomain/forks/test.git" readonly>',
  116. output_text,
  117. )
  118. self.assertIn(
  119. "<p>The Project Creator has not pushed any code yet</p>",
  120. output_text,
  121. )
  122. output = self.app.get("/forks/test/issues")
  123. self.assertEqual(output.status_code, 200)
  124. output_text = output.get_data(as_text=True)
  125. self.assertIn(
  126. "<title>Issues - forks/test - Pagure</title>", output_text
  127. )
  128. self.assertIn(
  129. '<span class="font-weight-bold">no open issues found</span>\n',
  130. output_text,
  131. )
  132. @patch("pagure.lib.notify.send_email")
  133. def test_view_repo(self, send_email):
  134. """Test the view_repo endpoint when the project has a slash in its
  135. name.
  136. """
  137. send_email.return_value = True
  138. tests.create_projects(self.session)
  139. # Non-existant git repo
  140. output = self.app.get("/test")
  141. self.assertEqual(output.status_code, 404)
  142. self.set_up_git_repo()
  143. # With git repo
  144. output = self.app.get("/test")
  145. self.assertEqual(output.status_code, 200)
  146. output_text = output.get_data(as_text=True)
  147. self.assertIn(
  148. '<input class="form-control bg-white select-on-focus" type="text" '
  149. 'value="git://localhost.localdomain/test.git" readonly>',
  150. output_text,
  151. )
  152. # We can't create the project `forks/test` the normal way
  153. self.assertRaises(
  154. pagure.exceptions.PagureException,
  155. pagure.lib.query.new_project,
  156. self.session,
  157. name="test",
  158. namespace="forks",
  159. description="test project forks/test",
  160. url="",
  161. avatar_email="",
  162. user="pingou",
  163. blacklist=pagure.config.config["BLACKLISTED_PROJECTS"],
  164. allowed_prefix=pagure.config.config["ALLOWED_PREFIX"],
  165. )
  166. # So just put it in the DB
  167. item = pagure.lib.model.Project(
  168. user_id=1, # pingou
  169. name="test",
  170. namespace="forks",
  171. description="test project forks/test",
  172. hook_token="aaabbbcccddd",
  173. )
  174. self.session.add(item)
  175. self.session.commit()
  176. self.set_up_git_repo(name="forks/test")
  177. # Front page shows fine
  178. output = self.app.get("/forks/test")
  179. self.assertEqual(output.status_code, 200)
  180. output_text = output.get_data(as_text=True)
  181. self.assertIn(
  182. '<input class="form-control bg-white select-on-focus" type="text" '
  183. 'value="git://localhost.localdomain/forks/test.git" readonly>',
  184. output_text,
  185. )
  186. self.assertIn(
  187. "<title>Overview - forks/test - Pagure</title>", output_text
  188. )
  189. # Issues list shows fine
  190. output = self.app.get("/forks/test/issues")
  191. self.assertEqual(output.status_code, 200)
  192. output_text = output.get_data(as_text=True)
  193. self.assertIn(
  194. "<title>Issues - forks/test - Pagure</title>", output_text
  195. )
  196. self.assertIn(
  197. '<span class="font-weight-bold">no open issues found</span>\n',
  198. output_text,
  199. )
  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.peel().hex
  205. output = self.app.get("/forks/test/commits")
  206. self.assertEqual(output.status_code, 200)
  207. output_text = output.get_data(as_text=True)
  208. self.assertIn(first_commit, output_text)
  209. self.assertIn(
  210. '<a href="/forks/test/c/%s?branch=master"' % first_commit,
  211. output_text,
  212. )
  213. output = self.app.get("/forks/test/c/%s" % first_commit)
  214. self.assertEqual(output.status_code, 200)
  215. output_text = output.get_data(as_text=True)
  216. self.assertIn("<title>Commit - forks/test ", output_text)
  217. if __name__ == "__main__":
  218. unittest.main(verbosity=2)