test_pagure_flask_ui_repo_slash_name.py 8.5 KB

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