test_pagure_flask_ui_no_master_branch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.get_object().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"><code>foo\n bar</code></pre>',
  189. output_text
  190. )
  191. @patch('pagure.lib.notify.send_email')
  192. def test_view_raw_file(self, send_email):
  193. """ Test the view_raw_file endpoint when the git repo has no
  194. master branch.
  195. """
  196. send_email.return_value = True
  197. tests.create_projects(self.session)
  198. # Non-existant git repo
  199. output = self.app.get('/test/raw/master')
  200. self.assertEqual(output.status_code, 404)
  201. output = self.app.get('/test/raw/master/f/sources')
  202. self.assertEqual(output.status_code, 404)
  203. self.set_up_git_repo()
  204. # With git repo
  205. output = self.app.get('/test/raw/master')
  206. self.assertEqual(output.status_code, 404)
  207. output = self.app.get('/test/raw/master/f/sources')
  208. self.assertEqual(output.status_code, 404)
  209. output = self.app.get('/test/raw/feature')
  210. self.assertEqual(output.status_code, 200)
  211. output_text = output.get_data(as_text=True)
  212. self.assertIn('diff --git a/.gitignore b/.gitignore', output_text)
  213. self.assertIn('+*~', output_text)
  214. output = self.app.get('/test/raw/feature/f/sources')
  215. self.assertEqual(output.status_code, 200)
  216. self.assertEqual('foo\n bar', output.get_data(as_text=True))
  217. @patch('pagure.lib.notify.send_email')
  218. def test_view_tree(self, send_email):
  219. """ Test the view_tree endpoint when the git repo has no
  220. master branch.
  221. """
  222. send_email.return_value = True
  223. tests.create_projects(self.session)
  224. # Non-existant git repo
  225. output = self.app.get('/test/tree/')
  226. self.assertEqual(output.status_code, 404)
  227. output = self.app.get('/test/tree/master')
  228. self.assertEqual(output.status_code, 404)
  229. self.set_up_git_repo()
  230. # With git repo
  231. output = self.app.get('/test/tree/master')
  232. self.assertEqual(output.status_code, 200)
  233. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  234. output = self.app.get('/test/tree/master/sources')
  235. self.assertEqual(output.status_code, 200)
  236. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  237. output = self.app.get('/test/tree/feature')
  238. self.assertEqual(output.status_code, 200)
  239. self.assertIn('<a href="/test/blob/feature/f/sources">', output.get_data(as_text=True))
  240. output = self.app.get('/test/tree/feature/sources')
  241. self.assertEqual(output.status_code, 200)
  242. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  243. @patch('pagure.lib.notify.send_email')
  244. def test_new_request_pull(self, send_email):
  245. """ Test the new_request_pull endpoint when the git repo has no
  246. master branch.
  247. """
  248. send_email.return_value = True
  249. tests.create_projects(self.session)
  250. # Non-existant git repo
  251. output = self.app.get('/test/diff/master..feature')
  252. # (used to be 302 but seeing a diff is allowed even logged out)
  253. self.assertEqual(output.status_code, 404)
  254. user = tests.FakeUser()
  255. with tests.user_set(self.app.application, user):
  256. output = self.app.get('/test/diff/master..feature')
  257. self.assertEqual(output.status_code, 404)
  258. self.set_up_git_repo()
  259. output = self.app.get('/test/diff/master..feature')
  260. # (used to be 302 but seeing a diff is allowed even logged out)
  261. self.assertEqual(output.status_code, 400)
  262. self.assertIn(
  263. '<p>Branch master could not be found in the target repo</p>',
  264. output.get_data(as_text=True))
  265. user = tests.FakeUser()
  266. with tests.user_set(self.app.application, user):
  267. output = self.app.get('/test/diff/master..feature')
  268. self.assertEqual(output.status_code, 400)
  269. self.assertIn(
  270. '<p>Branch master could not be found in the target repo</p>',
  271. output.get_data(as_text=True))
  272. if __name__ == '__main__':
  273. unittest.main(verbosity=2)