test_pagure_flask_ui_no_master_branch.py 11 KB

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