test_pagure_flask_ui_no_master_branch.py 11 KB

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