test_pagure_flask_ui_no_master_branch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(0, os.path.join(os.path.dirname(
  17. os.path.abspath(__file__)), '..'))
  18. import pagure.lib.query
  19. import tests
  20. from pagure.lib.repo import PagureRepo
  21. class PagureFlaskNoMasterBranchtests(tests.SimplePagureTest):
  22. """ Tests for flask application when the git repo has no master branch.
  23. """
  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(
  40. 'Alice Author', 'alice@authors.tld')
  41. committer = pygit2.Signature(
  42. 'Cecil Committer', 'cecil@committers.tld')
  43. clone_repo.create_commit(
  44. 'refs/heads/feature', # the name of the reference to update
  45. author,
  46. committer,
  47. 'Add sources file for testing',
  48. # binary string representing the tree object ID
  49. tree,
  50. # list of binary strings representing parents of the new commit
  51. []
  52. )
  53. feature_branch = clone_repo.lookup_branch('feature')
  54. first_commit = feature_branch.peel().hex
  55. # Second commit
  56. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  57. stream.write('*~')
  58. clone_repo.index.add('.gitignore')
  59. clone_repo.index.write()
  60. tree = clone_repo.index.write_tree()
  61. author = pygit2.Signature(
  62. 'Alice Author', 'alice@authors.tld')
  63. committer = pygit2.Signature(
  64. 'Cecil Committer', 'cecil@committers.tld')
  65. clone_repo.create_commit(
  66. 'refs/heads/feature',
  67. author,
  68. committer,
  69. 'Add .gitignore file for testing',
  70. # binary string representing the tree object ID
  71. tree,
  72. # list of binary strings representing parents of the new commit
  73. [first_commit]
  74. )
  75. refname = 'refs/heads/feature'
  76. ori_remote = clone_repo.remotes[0]
  77. PagureRepo.push(ori_remote, refname)
  78. shutil.rmtree(newpath)
  79. @patch('pagure.lib.notify.send_email')
  80. def test_view_repo(self, send_email):
  81. """ Test the view_repo endpoint when the git repo has no master
  82. branch.
  83. """
  84. send_email.return_value = True
  85. tests.create_projects(self.session)
  86. # Non-existant git repo
  87. output = self.app.get('/test')
  88. self.assertEqual(output.status_code, 404)
  89. self.set_up_git_repo()
  90. # With git repo
  91. output = self.app.get('/test')
  92. self.assertEqual(output.status_code, 200)
  93. output_text = output.get_data(as_text=True)
  94. self.assertIn('<section class="no-readme">', output_text)
  95. self.assertIn(
  96. "The test project's README file is empty or unavailable.",
  97. output_text)
  98. self.assertEqual(
  99. output_text.count('<a class="dropdown-item" href="/test/branch/'),
  100. 0)
  101. '''
  102. @patch('pagure.lib.notify.send_email')
  103. def test_view_repo_branch(self, send_email):
  104. """ Test the view_repo_branch endpoint when the git repo has no
  105. master branch.
  106. """
  107. send_email.return_value = True
  108. tests.create_projects(self.session)
  109. # Non-existant git repo
  110. output = self.app.get('/test/branch/master')
  111. self.assertEqual(output.status_code, 404)
  112. self.set_up_git_repo()
  113. # With git repo
  114. output = self.app.get('/test/branch/feature')
  115. output_text = output.get_data(as_text=True)
  116. print(output_text)
  117. self.assertEqual(output.status_code, 200)
  118. self.assertIn('<section class="no-readme">', output_text)
  119. self.assertIn(
  120. "The test project's README file is empty or unavailable.",
  121. output_text)
  122. '''
  123. @patch('pagure.lib.notify.send_email')
  124. def test_view_commits(self, send_email):
  125. """ Test the view_commits endpoint when the git repo has no
  126. master branch.
  127. """
  128. send_email.return_value = True
  129. tests.create_projects(self.session)
  130. # Non-existant git repo
  131. output = self.app.get('/test/commits')
  132. self.assertEqual(output.status_code, 404)
  133. self.set_up_git_repo()
  134. # With git repo
  135. output = self.app.get('/test/commits')
  136. self.assertEqual(output.status_code, 200)
  137. output_text = output.get_data(as_text=True)
  138. self.assertIn(
  139. '<div class="list-group my-2">\n\n\n </div>',
  140. output_text)
  141. self.assertNotIn(
  142. '<div class="btn-group pull-xs-right">', output_text)
  143. output = self.app.get('/test/commits/feature')
  144. self.assertEqual(output.status_code, 200)
  145. output_text = output.get_data(as_text=True)
  146. self.assertIn(
  147. 'Commits <span class="badge badge-secondary"> 2</span>',
  148. output_text)
  149. self.assertIn('Add sources file for testing', output_text)
  150. self.assertIn('Add .gitignore file for testing', output_text)
  151. self.assertNotIn(
  152. '<div class="list-group my-2">\n\n\n </div>',
  153. output_text)
  154. self.assertEqual(
  155. output_text.count('class="list-group-item "'), 2)
  156. @patch('pagure.lib.notify.send_email')
  157. def test_view_file(self, send_email):
  158. """ Test the view_file endpoint when the git repo has no
  159. master branch.
  160. """
  161. send_email.return_value = True
  162. tests.create_projects(self.session)
  163. # Non-existant git repo
  164. output = self.app.get('/test/blob/master/f/sources')
  165. self.assertEqual(output.status_code, 404)
  166. self.set_up_git_repo()
  167. # With git repo
  168. output = self.app.get('/test/blob/master/f/sources')
  169. self.assertEqual(output.status_code, 404)
  170. output = self.app.get('/test/blob/feature/f/sources')
  171. self.assertEqual(output.status_code, 200)
  172. output_text = output.get_data(as_text=True)
  173. self.assertIn(
  174. '''
  175. <ol class="breadcrumb p-0 bg-transparent mb-0">
  176. <li class="breadcrumb-item">
  177. <a href="/test/tree/feature">
  178. <span class="fa fa-random">
  179. </span>&nbsp; feature
  180. </a>
  181. </li>
  182. <li class="active breadcrumb-item">
  183. <span class="fa fa-file" data-glyph="">
  184. </span>&nbsp; sources
  185. </li>
  186. </ol>''', output_text)
  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('No content found in this repository', output.get_data(as_text=True))
  235. output = self.app.get('/test/tree/master/sources')
  236. self.assertEqual(output.status_code, 200)
  237. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  238. output = self.app.get('/test/tree/feature')
  239. self.assertEqual(output.status_code, 200)
  240. self.assertIn('<a href="/test/blob/feature/f/sources">', output.get_data(as_text=True))
  241. output = self.app.get('/test/tree/feature/sources')
  242. self.assertEqual(output.status_code, 200)
  243. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  244. @patch('pagure.lib.notify.send_email')
  245. def test_new_request_pull(self, send_email):
  246. """ Test the new_request_pull endpoint when the git repo has no
  247. master branch.
  248. """
  249. send_email.return_value = True
  250. tests.create_projects(self.session)
  251. # Non-existant git repo
  252. output = self.app.get('/test/diff/master..feature')
  253. # (used to be 302 but seeing a diff is allowed even logged out)
  254. self.assertEqual(output.status_code, 404)
  255. user = tests.FakeUser()
  256. with tests.user_set(self.app.application, user):
  257. output = self.app.get('/test/diff/master..feature')
  258. self.assertEqual(output.status_code, 404)
  259. self.set_up_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, 400)
  263. self.assertIn(
  264. '<p>Branch master could not be found in the target repo</p>',
  265. output.get_data(as_text=True))
  266. user = tests.FakeUser()
  267. with tests.user_set(self.app.application, user):
  268. output = self.app.get('/test/diff/master..feature')
  269. self.assertEqual(output.status_code, 400)
  270. self.assertIn(
  271. '<p>Branch master could not be found in the target repo</p>',
  272. output.get_data(as_text=True))
  273. if __name__ == '__main__':
  274. unittest.main(verbosity=2)