test_pagure_flask_ui_no_master_branch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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 PagureFlaskNoMasterBranchtests(tests.SimplePagureTest):
  23. """Tests for flask application when the git repo has no master branch."""
  24. def set_up_git_repo(self):
  25. """Set up the git repo to play with."""
  26. # Create a git repo to play with
  27. gitrepo = os.path.join(self.path, "repos", "test.git")
  28. repo = pygit2.init_repository(gitrepo, bare=True)
  29. newpath = tempfile.mkdtemp(prefix="pagure-other-test")
  30. repopath = os.path.join(newpath, "test")
  31. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  32. # Create a file in that git repo
  33. with open(os.path.join(repopath, "sources"), "w") as stream:
  34. stream.write("foo\n bar")
  35. clone_repo.index.add("sources")
  36. clone_repo.index.write()
  37. # Commits the files added
  38. tree = clone_repo.index.write_tree()
  39. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  40. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  41. clone_repo.create_commit(
  42. "refs/heads/feature", # the name of the reference to update
  43. author,
  44. committer,
  45. "Add sources file for testing",
  46. # binary string representing the tree object ID
  47. tree,
  48. # list of binary strings representing parents of the new commit
  49. [],
  50. )
  51. feature_branch = clone_repo.lookup_branch("feature")
  52. first_commit = feature_branch.peel().hex
  53. # Second commit
  54. with open(os.path.join(repopath, ".gitignore"), "w") as stream:
  55. stream.write("*~")
  56. clone_repo.index.add(".gitignore")
  57. clone_repo.index.write()
  58. tree = clone_repo.index.write_tree()
  59. author = pygit2.Signature("Alice Author", "alice@authors.tld")
  60. committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
  61. clone_repo.create_commit(
  62. "refs/heads/feature",
  63. author,
  64. committer,
  65. "Add .gitignore file for testing",
  66. # binary string representing the tree object ID
  67. tree,
  68. # list of binary strings representing parents of the new commit
  69. [first_commit],
  70. )
  71. refname = "refs/heads/feature"
  72. ori_remote = clone_repo.remotes[0]
  73. PagureRepo.push(ori_remote, refname)
  74. shutil.rmtree(newpath)
  75. @patch("pagure.lib.notify.send_email")
  76. def test_view_repo(self, send_email):
  77. """Test the view_repo endpoint when the git repo has no master
  78. branch.
  79. """
  80. send_email.return_value = True
  81. tests.create_projects(self.session)
  82. # Non-existant git repo
  83. output = self.app.get("/test")
  84. self.assertEqual(output.status_code, 404)
  85. self.set_up_git_repo()
  86. # With git repo
  87. output = self.app.get("/test")
  88. self.assertEqual(output.status_code, 200)
  89. output_text = output.get_data(as_text=True)
  90. self.assertIn("<title>Overview - test - Pagure</title>", output_text)
  91. self.assertIn("<p>This repo is brand new!</p>", output_text)
  92. self.assertEqual(
  93. output_text.count('<a class="dropdown-item" href="/test/branch/'),
  94. 0,
  95. )
  96. '''
  97. @patch('pagure.lib.notify.send_email')
  98. def test_view_repo_branch(self, send_email):
  99. """ Test the view_repo_branch endpoint when the git repo has no
  100. master branch.
  101. """
  102. send_email.return_value = True
  103. tests.create_projects(self.session)
  104. # Non-existant git repo
  105. output = self.app.get('/test/branch/master')
  106. self.assertEqual(output.status_code, 404)
  107. self.set_up_git_repo()
  108. # With git repo
  109. output = self.app.get('/test/branch/feature')
  110. output_text = output.get_data(as_text=True)
  111. print(output_text)
  112. self.assertEqual(output.status_code, 200)
  113. self.assertIn('<section class="no-readme">', output_text)
  114. self.assertIn(
  115. "The test project's README file is empty or unavailable.",
  116. output_text)
  117. '''
  118. @patch("pagure.lib.notify.send_email")
  119. def test_view_commits(self, send_email):
  120. """Test the view_commits endpoint when the git repo has no
  121. master branch.
  122. """
  123. send_email.return_value = True
  124. tests.create_projects(self.session)
  125. # Non-existant git repo
  126. output = self.app.get("/test/commits")
  127. self.assertEqual(output.status_code, 404)
  128. self.set_up_git_repo()
  129. # With git repo
  130. output = self.app.get("/test/commits")
  131. self.assertEqual(output.status_code, 200)
  132. output_text = output.get_data(as_text=True)
  133. self.assertIn(
  134. '<div class="list-group my-2">\n\n\n </div>', output_text
  135. )
  136. self.assertNotIn('<div class="btn-group pull-xs-right">', output_text)
  137. output = self.app.get("/test/commits/feature")
  138. self.assertEqual(output.status_code, 200)
  139. output_text = output.get_data(as_text=True)
  140. self.assertIn(
  141. 'Commits <span class="badge badge-secondary"> 2</span>',
  142. output_text,
  143. )
  144. self.assertIn("Add sources file for testing", output_text)
  145. self.assertIn("Add .gitignore file for testing", output_text)
  146. self.assertNotIn(
  147. '<div class="list-group my-2">\n\n\n </div>', output_text
  148. )
  149. self.assertEqual(output_text.count('class="list-group-item "'), 2)
  150. @patch("pagure.lib.notify.send_email")
  151. def test_view_file(self, send_email):
  152. """Test the view_file endpoint when the git repo has no
  153. master branch.
  154. """
  155. send_email.return_value = True
  156. tests.create_projects(self.session)
  157. # Non-existant git repo
  158. output = self.app.get("/test/blob/master/f/sources")
  159. self.assertEqual(output.status_code, 404)
  160. self.set_up_git_repo()
  161. # With git repo
  162. output = self.app.get("/test/blob/master/f/sources")
  163. self.assertEqual(output.status_code, 404)
  164. output = self.app.get("/test/blob/feature/f/sources")
  165. self.assertEqual(output.status_code, 200)
  166. output_text = output.get_data(as_text=True)
  167. self.assertIn(
  168. """
  169. <ol class="breadcrumb p-0 bg-transparent mb-0">
  170. <li class="breadcrumb-item">
  171. <a href="/test/tree/feature">
  172. <span class="fa fa-random">
  173. </span>&nbsp; feature
  174. </a>
  175. </li>
  176. <li class="active breadcrumb-item">
  177. <span class="fa fa-file" data-glyph="">
  178. </span>&nbsp; sources
  179. </li>
  180. </ol>""",
  181. output_text,
  182. )
  183. self.assertIn(
  184. '<pre class="syntaxhighlightblock">'
  185. '<code class="lang-plaintext">foo\n bar</code></pre>',
  186. output_text,
  187. )
  188. @patch("pagure.lib.notify.send_email")
  189. def test_view_raw_file(self, send_email):
  190. """Test the view_raw_file endpoint when the git repo has no
  191. master branch.
  192. """
  193. send_email.return_value = True
  194. tests.create_projects(self.session)
  195. # Non-existant git repo
  196. output = self.app.get("/test/raw/master")
  197. self.assertEqual(output.status_code, 404)
  198. output = self.app.get("/test/raw/master/f/sources")
  199. self.assertEqual(output.status_code, 404)
  200. self.set_up_git_repo()
  201. # With git repo
  202. output = self.app.get("/test/raw/master")
  203. self.assertEqual(output.status_code, 404)
  204. output = self.app.get("/test/raw/master/f/sources")
  205. self.assertEqual(output.status_code, 404)
  206. output = self.app.get("/test/raw/feature")
  207. self.assertEqual(output.status_code, 200)
  208. output_text = output.get_data(as_text=True)
  209. self.assertIn("diff --git a/.gitignore b/.gitignore", output_text)
  210. self.assertIn("+*~", output_text)
  211. output = self.app.get("/test/raw/feature/f/sources")
  212. self.assertEqual(output.status_code, 200)
  213. self.assertEqual("foo\n bar", output.get_data(as_text=True))
  214. @patch("pagure.lib.notify.send_email")
  215. def test_view_tree(self, send_email):
  216. """Test the view_tree endpoint when the git repo has no
  217. master branch.
  218. """
  219. send_email.return_value = True
  220. tests.create_projects(self.session)
  221. # Non-existant git repo
  222. output = self.app.get("/test/tree/")
  223. self.assertEqual(output.status_code, 404)
  224. output = self.app.get("/test/tree/master")
  225. self.assertEqual(output.status_code, 404)
  226. self.set_up_git_repo()
  227. # With git repo
  228. output = self.app.get("/test/tree/master")
  229. self.assertEqual(output.status_code, 200)
  230. self.assertIn(
  231. "No content found in this repository",
  232. output.get_data(as_text=True),
  233. )
  234. output = self.app.get("/test/tree/master/sources")
  235. self.assertEqual(output.status_code, 200)
  236. self.assertIn(
  237. "No content found in this repository",
  238. output.get_data(as_text=True),
  239. )
  240. output = self.app.get("/test/tree/feature")
  241. self.assertEqual(output.status_code, 200)
  242. self.assertIn(
  243. '<a href="/test/blob/feature/f/sources">',
  244. output.get_data(as_text=True),
  245. )
  246. output = self.app.get("/test/tree/feature/sources")
  247. self.assertEqual(output.status_code, 200)
  248. self.assertIn(
  249. "No content found in this repository",
  250. output.get_data(as_text=True),
  251. )
  252. @patch("pagure.lib.notify.send_email")
  253. def test_new_request_pull(self, send_email):
  254. """Test the new_request_pull endpoint when the git repo has no
  255. master branch.
  256. """
  257. send_email.return_value = True
  258. tests.create_projects(self.session)
  259. # Non-existant git repo
  260. output = self.app.get("/test/diff/master..feature")
  261. # (used to be 302 but seeing a diff is allowed even logged out)
  262. self.assertEqual(output.status_code, 404)
  263. user = tests.FakeUser()
  264. with tests.user_set(self.app.application, user):
  265. output = self.app.get("/test/diff/master..feature")
  266. self.assertEqual(output.status_code, 404)
  267. self.set_up_git_repo()
  268. output = self.app.get("/test/diff/master..feature")
  269. # (used to be 302 but seeing a diff is allowed even logged out)
  270. self.assertEqual(output.status_code, 400)
  271. self.assertIn(
  272. "<p>Branch master could not be found in the target repo</p>",
  273. output.get_data(as_text=True),
  274. )
  275. user = tests.FakeUser()
  276. with tests.user_set(self.app.application, user):
  277. output = self.app.get("/test/diff/master..feature")
  278. self.assertEqual(output.status_code, 400)
  279. self.assertIn(
  280. "<p>Branch master could not be found in the target repo</p>",
  281. output.get_data(as_text=True),
  282. )
  283. if __name__ == "__main__":
  284. unittest.main(verbosity=2)