test_pagure_flask_ui_no_master_branch.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from mock import patch
  17. sys.path.insert(0, os.path.join(os.path.dirname(
  18. os.path.abspath(__file__)), '..'))
  19. import pagure.lib
  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(
  41. 'Alice Author', 'alice@authors.tld')
  42. committer = pygit2.Signature(
  43. 'Cecil Committer', 'cecil@committers.tld')
  44. clone_repo.create_commit(
  45. 'refs/heads/feature', # the name of the reference to update
  46. author,
  47. committer,
  48. 'Add sources file for testing',
  49. # binary string representing the tree object ID
  50. tree,
  51. # list of binary strings representing parents of the new commit
  52. []
  53. )
  54. feature_branch = clone_repo.lookup_branch('feature')
  55. first_commit = feature_branch.get_object().hex
  56. # Second commit
  57. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  58. stream.write('*~')
  59. clone_repo.index.add('.gitignore')
  60. clone_repo.index.write()
  61. tree = clone_repo.index.write_tree()
  62. author = pygit2.Signature(
  63. 'Alice Author', 'alice@authors.tld')
  64. committer = pygit2.Signature(
  65. 'Cecil Committer', 'cecil@committers.tld')
  66. clone_repo.create_commit(
  67. 'refs/heads/feature',
  68. author,
  69. committer,
  70. 'Add .gitignore file for testing',
  71. # binary string representing the tree object ID
  72. tree,
  73. # list of binary strings representing parents of the new commit
  74. [first_commit]
  75. )
  76. refname = 'refs/heads/feature'
  77. ori_remote = clone_repo.remotes[0]
  78. PagureRepo.push(ori_remote, refname)
  79. shutil.rmtree(newpath)
  80. @patch('pagure.lib.notify.send_email')
  81. def test_view_repo(self, send_email):
  82. """ Test the view_repo endpoint when the git repo has no master
  83. branch.
  84. """
  85. send_email.return_value = True
  86. tests.create_projects(self.session)
  87. # Non-existant git repo
  88. output = self.app.get('/test')
  89. self.assertEqual(output.status_code, 404)
  90. self.set_up_git_repo()
  91. # With git repo
  92. output = self.app.get('/test')
  93. self.assertEqual(output.status_code, 200)
  94. self.assertIn('<section class="no-readme">', output.data)
  95. self.assertIn(
  96. "The test project's README file is empty or unavailable.",
  97. output.data)
  98. self.assertEqual(
  99. output.data.count('<a class="dropdown-item" href="/test/branch/'),
  100. 0)
  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. self.assertEqual(output.status_code, 200)
  115. self.assertIn('<section class="no-readme">', output.data)
  116. self.assertIn(
  117. "The test project's README file is empty or unavailable.",
  118. output.data)
  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. self.assertIn(
  134. '<div class="list-group m-t-1">\n </div>', output.data)
  135. self.assertNotIn(
  136. '<div class="btn-group pull-xs-right">', output.data)
  137. output = self.app.get('/test/commits/feature')
  138. self.assertEqual(output.status_code, 200)
  139. self.assertIn(
  140. 'Commits <span class="label label-default"> 2</span>',
  141. output.data)
  142. self.assertIn('Add sources file for testing', output.data)
  143. self.assertIn('Add .gitignore file for testing', output.data)
  144. self.assertNotIn(
  145. '<div class="list-group m-t-1">\n </div>', output.data)
  146. self.assertEqual(
  147. output.data.count('class="list-group-item"'), 2)
  148. @patch('pagure.lib.notify.send_email')
  149. def test_view_file(self, send_email):
  150. """ Test the view_file endpoint when the git repo has no
  151. master branch.
  152. """
  153. send_email.return_value = True
  154. tests.create_projects(self.session)
  155. # Non-existant git repo
  156. output = self.app.get('/test/blob/master/f/sources')
  157. self.assertEqual(output.status_code, 404)
  158. self.set_up_git_repo()
  159. # With git repo
  160. output = self.app.get('/test/blob/master/f/sources')
  161. self.assertEqual(output.status_code, 404)
  162. output = self.app.get('/test/blob/feature/f/sources')
  163. self.assertEqual(output.status_code, 200)
  164. self.assertIn(
  165. '''
  166. <ol class="breadcrumb">
  167. <li>
  168. <a href="/test/tree/feature">
  169. <span class="oi" data-glyph="random">
  170. </span>&nbsp; feature
  171. </a>
  172. </li>
  173. <li class="active">
  174. <span class="oi" data-glyph="file">
  175. </span>&nbsp; sources
  176. </li>
  177. </ol>''', output.data)
  178. self.assertTrue(
  179. # new version of pygments
  180. '<td class="cell2"><pre><span></span>foo</pre></td>' in output.data
  181. or
  182. # old version of pygments
  183. '<td class="cell2"><pre>foo</pre></td>' in output.data
  184. )
  185. @patch('pagure.lib.notify.send_email')
  186. def test_view_raw_file(self, send_email):
  187. """ Test the view_raw_file endpoint when the git repo has no
  188. master branch.
  189. """
  190. send_email.return_value = True
  191. tests.create_projects(self.session)
  192. # Non-existant git repo
  193. output = self.app.get('/test/raw/master')
  194. self.assertEqual(output.status_code, 404)
  195. output = self.app.get('/test/raw/master/f/sources')
  196. self.assertEqual(output.status_code, 404)
  197. self.set_up_git_repo()
  198. # With 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. output = self.app.get('/test/raw/feature')
  204. self.assertEqual(output.status_code, 200)
  205. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  206. self.assertIn('+*~', output.data)
  207. output = self.app.get('/test/raw/feature/f/sources')
  208. self.assertEqual(output.status_code, 200)
  209. self.assertEqual('foo\n bar', output.data)
  210. @patch('pagure.lib.notify.send_email')
  211. def test_view_tree(self, send_email):
  212. """ Test the view_tree endpoint when the git repo has no
  213. master branch.
  214. """
  215. send_email.return_value = True
  216. tests.create_projects(self.session)
  217. # Non-existant git repo
  218. output = self.app.get('/test/tree/')
  219. self.assertEqual(output.status_code, 404)
  220. output = self.app.get('/test/tree/master')
  221. self.assertEqual(output.status_code, 404)
  222. self.set_up_git_repo()
  223. # With git repo
  224. output = self.app.get('/test/tree/master')
  225. self.assertEqual(output.status_code, 200)
  226. self.assertIn('No content found in this repository', output.data)
  227. output = self.app.get('/test/tree/master/sources')
  228. self.assertEqual(output.status_code, 200)
  229. self.assertIn('No content found in this repository', output.data)
  230. output = self.app.get('/test/tree/feature')
  231. self.assertEqual(output.status_code, 200)
  232. self.assertIn('<a href="/test/blob/feature/f/sources">', output.data)
  233. output = self.app.get('/test/tree/feature/sources')
  234. self.assertEqual(output.status_code, 200)
  235. self.assertIn('No content found in this repository', output.data)
  236. @patch('pagure.lib.notify.send_email')
  237. def test_new_request_pull(self, send_email):
  238. """ Test the new_request_pull endpoint when the git repo has no
  239. master branch.
  240. """
  241. send_email.return_value = True
  242. tests.create_projects(self.session)
  243. # Non-existant git repo
  244. output = self.app.get('/test/diff/master..feature')
  245. # (used to be 302 but seeing a diff is allowed even logged out)
  246. self.assertEqual(output.status_code, 404)
  247. user = tests.FakeUser()
  248. with tests.user_set(self.app.application, user):
  249. output = self.app.get('/test/diff/master..feature')
  250. self.assertEqual(output.status_code, 404)
  251. self.set_up_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, 400)
  255. self.assertIn(
  256. '<p>Branch master could not be found in the target repo</p>',
  257. output.data)
  258. user = tests.FakeUser()
  259. with tests.user_set(self.app.application, user):
  260. output = self.app.get('/test/diff/master..feature')
  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.data)
  265. if __name__ == '__main__':
  266. unittest.main(verbosity=2)