test_pagure_flask_ui_no_master_branch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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('<section class="no-readme">', output_text)
  92. self.assertIn(
  93. "The test project's README file is empty or unavailable.",
  94. output_text,
  95. )
  96. self.assertEqual(
  97. output_text.count('<a class="dropdown-item" href="/test/branch/'),
  98. 0,
  99. )
  100. '''
  101. @patch('pagure.lib.notify.send_email')
  102. def test_view_repo_branch(self, send_email):
  103. """ Test the view_repo_branch endpoint when the git repo has no
  104. master branch.
  105. """
  106. send_email.return_value = True
  107. tests.create_projects(self.session)
  108. # Non-existant git repo
  109. output = self.app.get('/test/branch/master')
  110. self.assertEqual(output.status_code, 404)
  111. self.set_up_git_repo()
  112. # With git repo
  113. output = self.app.get('/test/branch/feature')
  114. output_text = output.get_data(as_text=True)
  115. print(output_text)
  116. self.assertEqual(output.status_code, 200)
  117. self.assertIn('<section class="no-readme">', output_text)
  118. self.assertIn(
  119. "The test project's README file is empty or unavailable.",
  120. output_text)
  121. '''
  122. @patch("pagure.lib.notify.send_email")
  123. def test_view_commits(self, send_email):
  124. """ Test the view_commits endpoint when the git repo has no
  125. master branch.
  126. """
  127. send_email.return_value = True
  128. tests.create_projects(self.session)
  129. # Non-existant git repo
  130. output = self.app.get("/test/commits")
  131. self.assertEqual(output.status_code, 404)
  132. self.set_up_git_repo()
  133. # With git repo
  134. output = self.app.get("/test/commits")
  135. self.assertEqual(output.status_code, 200)
  136. output_text = output.get_data(as_text=True)
  137. self.assertIn(
  138. '<div class="list-group my-2">\n\n\n </div>', output_text
  139. )
  140. self.assertNotIn('<div class="btn-group pull-xs-right">', output_text)
  141. output = self.app.get("/test/commits/feature")
  142. self.assertEqual(output.status_code, 200)
  143. output_text = output.get_data(as_text=True)
  144. self.assertIn(
  145. 'Commits <span class="badge badge-secondary"> 2</span>',
  146. output_text,
  147. )
  148. self.assertIn("Add sources file for testing", output_text)
  149. self.assertIn("Add .gitignore file for testing", output_text)
  150. self.assertNotIn(
  151. '<div class="list-group my-2">\n\n\n </div>', output_text
  152. )
  153. self.assertEqual(output_text.count('class="list-group-item "'), 2)
  154. @patch("pagure.lib.notify.send_email")
  155. def test_view_file(self, send_email):
  156. """ Test the view_file endpoint when the git repo has no
  157. master branch.
  158. """
  159. send_email.return_value = True
  160. tests.create_projects(self.session)
  161. # Non-existant git repo
  162. output = self.app.get("/test/blob/master/f/sources")
  163. self.assertEqual(output.status_code, 404)
  164. self.set_up_git_repo()
  165. # With git repo
  166. output = self.app.get("/test/blob/master/f/sources")
  167. self.assertEqual(output.status_code, 404)
  168. output = self.app.get("/test/blob/feature/f/sources")
  169. self.assertEqual(output.status_code, 200)
  170. output_text = output.get_data(as_text=True)
  171. self.assertIn(
  172. """
  173. <ol class="breadcrumb p-0 bg-transparent mb-0">
  174. <li class="breadcrumb-item">
  175. <a href="/test/tree/feature">
  176. <span class="fa fa-random">
  177. </span>&nbsp; feature
  178. </a>
  179. </li>
  180. <li class="active breadcrumb-item">
  181. <span class="fa fa-file" data-glyph="">
  182. </span>&nbsp; sources
  183. </li>
  184. </ol>""",
  185. output_text,
  186. )
  187. self.assertIn(
  188. '<pre class="syntaxhighlightblock">'
  189. '<code class="lang-plaintext">foo\n bar</code></pre>',
  190. output_text,
  191. )
  192. @patch("pagure.lib.notify.send_email")
  193. def test_view_raw_file(self, send_email):
  194. """ Test the view_raw_file endpoint when the git repo has no
  195. master branch.
  196. """
  197. send_email.return_value = True
  198. tests.create_projects(self.session)
  199. # Non-existant git repo
  200. output = self.app.get("/test/raw/master")
  201. self.assertEqual(output.status_code, 404)
  202. output = self.app.get("/test/raw/master/f/sources")
  203. self.assertEqual(output.status_code, 404)
  204. self.set_up_git_repo()
  205. # With git repo
  206. output = self.app.get("/test/raw/master")
  207. self.assertEqual(output.status_code, 404)
  208. output = self.app.get("/test/raw/master/f/sources")
  209. self.assertEqual(output.status_code, 404)
  210. output = self.app.get("/test/raw/feature")
  211. self.assertEqual(output.status_code, 200)
  212. output_text = output.get_data(as_text=True)
  213. self.assertIn("diff --git a/.gitignore b/.gitignore", output_text)
  214. self.assertIn("+*~", output_text)
  215. output = self.app.get("/test/raw/feature/f/sources")
  216. self.assertEqual(output.status_code, 200)
  217. self.assertEqual("foo\n bar", output.get_data(as_text=True))
  218. @patch("pagure.lib.notify.send_email")
  219. def test_view_tree(self, send_email):
  220. """ Test the view_tree endpoint when the git repo has no
  221. master branch.
  222. """
  223. send_email.return_value = True
  224. tests.create_projects(self.session)
  225. # Non-existant git repo
  226. output = self.app.get("/test/tree/")
  227. self.assertEqual(output.status_code, 404)
  228. output = self.app.get("/test/tree/master")
  229. self.assertEqual(output.status_code, 404)
  230. self.set_up_git_repo()
  231. # With git repo
  232. output = self.app.get("/test/tree/master")
  233. self.assertEqual(output.status_code, 200)
  234. self.assertIn(
  235. "No content found in this repository",
  236. output.get_data(as_text=True),
  237. )
  238. output = self.app.get("/test/tree/master/sources")
  239. self.assertEqual(output.status_code, 200)
  240. self.assertIn(
  241. "No content found in this repository",
  242. output.get_data(as_text=True),
  243. )
  244. output = self.app.get("/test/tree/feature")
  245. self.assertEqual(output.status_code, 200)
  246. self.assertIn(
  247. '<a href="/test/blob/feature/f/sources">',
  248. output.get_data(as_text=True),
  249. )
  250. output = self.app.get("/test/tree/feature/sources")
  251. self.assertEqual(output.status_code, 200)
  252. self.assertIn(
  253. "No content found in this repository",
  254. output.get_data(as_text=True),
  255. )
  256. @patch("pagure.lib.notify.send_email")
  257. def test_new_request_pull(self, send_email):
  258. """ Test the new_request_pull endpoint when the git repo has no
  259. master branch.
  260. """
  261. send_email.return_value = True
  262. tests.create_projects(self.session)
  263. # Non-existant git repo
  264. output = self.app.get("/test/diff/master..feature")
  265. # (used to be 302 but seeing a diff is allowed even logged out)
  266. self.assertEqual(output.status_code, 404)
  267. user = tests.FakeUser()
  268. with tests.user_set(self.app.application, user):
  269. output = self.app.get("/test/diff/master..feature")
  270. self.assertEqual(output.status_code, 404)
  271. self.set_up_git_repo()
  272. output = self.app.get("/test/diff/master..feature")
  273. # (used to be 302 but seeing a diff is allowed even logged out)
  274. self.assertEqual(output.status_code, 400)
  275. self.assertIn(
  276. "<p>Branch master could not be found in the target repo</p>",
  277. output.get_data(as_text=True),
  278. )
  279. user = tests.FakeUser()
  280. with tests.user_set(self.app.application, user):
  281. output = self.app.get("/test/diff/master..feature")
  282. self.assertEqual(output.status_code, 400)
  283. self.assertIn(
  284. "<p>Branch master could not be found in the target repo</p>",
  285. output.get_data(as_text=True),
  286. )
  287. if __name__ == "__main__":
  288. unittest.main(verbosity=2)