test_pagure_flask_ui_no_master_branch.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - 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.Modeltests):
  23. """ Tests for flask application when the git repo has no master branch.
  24. """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskNoMasterBranchtests, self).setUp()
  28. pagure.APP.config['TESTING'] = True
  29. pagure.SESSION = self.session
  30. pagure.lib.SESSION = self.session
  31. pagure.ui.app.SESSION = self.session
  32. pagure.ui.filters.SESSION = self.session
  33. pagure.ui.fork.SESSION = self.session
  34. pagure.ui.repo.SESSION = self.session
  35. pagure.APP.config['GIT_FOLDER'] = os.path.join(self.path, 'repos')
  36. pagure.APP.config['FORK_FOLDER'] = os.path.join(self.path, 'forks')
  37. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  38. self.path, 'tickets')
  39. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  40. self.path, 'docs')
  41. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  42. self.path, 'requests')
  43. self.app = pagure.APP.test_client()
  44. def set_up_git_repo(self):
  45. """ Set up the git repo to play with. """
  46. # Create a git repo to play with
  47. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  48. repo = pygit2.init_repository(gitrepo, bare=True)
  49. newpath = tempfile.mkdtemp(prefix='pagure-other-test')
  50. repopath = os.path.join(newpath, 'test')
  51. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  52. # Create a file in that git repo
  53. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  54. stream.write('foo\n bar')
  55. clone_repo.index.add('sources')
  56. clone_repo.index.write()
  57. # Commits the files added
  58. tree = clone_repo.index.write_tree()
  59. author = pygit2.Signature(
  60. 'Alice Author', 'alice@authors.tld')
  61. committer = pygit2.Signature(
  62. 'Cecil Committer', 'cecil@committers.tld')
  63. clone_repo.create_commit(
  64. 'refs/heads/feature', # the name of the reference to update
  65. author,
  66. committer,
  67. 'Add sources file for testing',
  68. # binary string representing the tree object ID
  69. tree,
  70. # list of binary strings representing parents of the new commit
  71. []
  72. )
  73. feature_branch = clone_repo.lookup_branch('feature')
  74. first_commit = feature_branch.get_object().hex
  75. # Second commit
  76. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  77. stream.write('*~')
  78. clone_repo.index.add('.gitignore')
  79. clone_repo.index.write()
  80. tree = clone_repo.index.write_tree()
  81. author = pygit2.Signature(
  82. 'Alice Author', 'alice@authors.tld')
  83. committer = pygit2.Signature(
  84. 'Cecil Committer', 'cecil@committers.tld')
  85. clone_repo.create_commit(
  86. 'refs/heads/feature',
  87. author,
  88. committer,
  89. 'Add .gitignore file for testing',
  90. # binary string representing the tree object ID
  91. tree,
  92. # list of binary strings representing parents of the new commit
  93. [first_commit]
  94. )
  95. refname = 'refs/heads/feature'
  96. ori_remote = clone_repo.remotes[0]
  97. PagureRepo.push(ori_remote, refname)
  98. shutil.rmtree(newpath)
  99. @patch('pagure.lib.notify.send_email')
  100. def test_view_repo(self, send_email):
  101. """ Test the view_repo endpoint when the git repo has no master
  102. branch.
  103. """
  104. send_email.return_value = True
  105. tests.create_projects(self.session)
  106. # Non-existant git repo
  107. output = self.app.get('/test')
  108. self.assertEqual(output.status_code, 404)
  109. self.set_up_git_repo()
  110. # With git repo
  111. output = self.app.get('/test')
  112. self.assertEqual(output.status_code, 200)
  113. self.assertIn(
  114. '<div class="container p-t-3">\n <div class="row">',
  115. output.get_data(as_text=True))
  116. self.assertEqual(
  117. output.get_data(as_text=True).count('<a class="dropdown-item" href="/test/branch/'),
  118. 0)
  119. @patch('pagure.lib.notify.send_email')
  120. def test_view_repo_branch(self, send_email):
  121. """ Test the view_repo_branch 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/branch/master')
  128. self.assertEqual(output.status_code, 404)
  129. self.set_up_git_repo()
  130. # With git repo
  131. output = self.app.get('/test/branch/feature')
  132. self.assertEqual(output.status_code, 200)
  133. self.assertIn(
  134. '<div class="container p-t-3">\n <div class="row">',
  135. output.get_data(as_text=True))
  136. @patch('pagure.lib.notify.send_email')
  137. def test_view_commits(self, send_email):
  138. """ Test the view_commits endpoint when the git repo has no
  139. master branch.
  140. """
  141. send_email.return_value = True
  142. tests.create_projects(self.session)
  143. # Non-existant git repo
  144. output = self.app.get('/test/commits')
  145. self.assertEqual(output.status_code, 404)
  146. self.set_up_git_repo()
  147. # With git repo
  148. output = self.app.get('/test/commits')
  149. self.assertEqual(output.status_code, 200)
  150. self.assertIn(
  151. '<div class="list-group m-t-1">\n </div>', output.get_data(as_text=True))
  152. self.assertNotIn(
  153. '<div class="btn-group pull-xs-right">', output.get_data(as_text=True))
  154. output = self.app.get('/test/commits/feature')
  155. self.assertEqual(output.status_code, 200)
  156. self.assertIn(
  157. '<div class="container p-t-3">\n <div class="row m-b-1">',
  158. output.get_data(as_text=True))
  159. self.assertIn('Add sources file for testing', output.get_data(as_text=True))
  160. self.assertIn('Add .gitignore file for testing', output.get_data(as_text=True))
  161. self.assertNotIn(
  162. '<div class="list-group m-t-1">\n </div>', output.get_data(as_text=True))
  163. self.assertEqual(
  164. output.get_data(as_text=True).count('class="list-group-item "'),
  165. 2)
  166. @patch('pagure.lib.notify.send_email')
  167. def test_view_file(self, send_email):
  168. """ Test the view_file endpoint when the git repo has no
  169. master branch.
  170. """
  171. send_email.return_value = True
  172. tests.create_projects(self.session)
  173. # Non-existant git repo
  174. output = self.app.get('/test/blob/master/f/sources')
  175. self.assertEqual(output.status_code, 404)
  176. self.set_up_git_repo()
  177. # With git repo
  178. output = self.app.get('/test/blob/master/f/sources')
  179. self.assertEqual(output.status_code, 404)
  180. output = self.app.get('/test/blob/feature/f/sources')
  181. self.assertEqual(output.status_code, 200)
  182. self.assertIn(
  183. '''
  184. <ol class="breadcrumb">
  185. <li>
  186. <a href="/test/tree/feature">
  187. <span class="oi" data-glyph="random">
  188. </span>&nbsp; feature
  189. </a>
  190. </li>
  191. <li class="active">
  192. <span class="oi" data-glyph="file">
  193. </span>&nbsp; sources
  194. </li>
  195. </ol>''', output.get_data(as_text=True))
  196. self.assertTrue(
  197. # new version of pygments
  198. '<td class="cell2"><pre><span></span>foo</pre></td>' in
  199. output.get_data(as_text=True)
  200. or
  201. # old version of pygments
  202. '<td class="cell2"><pre>foo</pre></td>' in
  203. output.get_data(as_text=True)
  204. )
  205. @patch('pagure.lib.notify.send_email')
  206. def test_view_raw_file(self, send_email):
  207. """ Test the view_raw_file endpoint when the git repo has no
  208. master branch.
  209. """
  210. send_email.return_value = True
  211. tests.create_projects(self.session)
  212. # Non-existant git repo
  213. output = self.app.get('/test/raw/master')
  214. self.assertEqual(output.status_code, 404)
  215. output = self.app.get('/test/raw/master/f/sources')
  216. self.assertEqual(output.status_code, 404)
  217. self.set_up_git_repo()
  218. # With git repo
  219. output = self.app.get('/test/raw/master')
  220. self.assertEqual(output.status_code, 404)
  221. output = self.app.get('/test/raw/master/f/sources')
  222. self.assertEqual(output.status_code, 404)
  223. output = self.app.get('/test/raw/feature')
  224. self.assertEqual(output.status_code, 200)
  225. self.assertIn('diff --git a/.gitignore b/.gitignore', output.get_data(as_text=True))
  226. self.assertIn('+*~', output.get_data(as_text=True))
  227. output = self.app.get('/test/raw/feature/f/sources')
  228. self.assertEqual(output.status_code, 200)
  229. self.assertEqual('foo\n bar', output.get_data(as_text=True))
  230. @patch('pagure.lib.notify.send_email')
  231. def test_view_tree(self, send_email):
  232. """ Test the view_tree endpoint when the git repo has no
  233. master branch.
  234. """
  235. send_email.return_value = True
  236. tests.create_projects(self.session)
  237. # Non-existant git repo
  238. output = self.app.get('/test/tree/')
  239. self.assertEqual(output.status_code, 404)
  240. output = self.app.get('/test/tree/master')
  241. self.assertEqual(output.status_code, 404)
  242. self.set_up_git_repo()
  243. # With git repo
  244. output = self.app.get('/test/tree/master')
  245. self.assertEqual(output.status_code, 200)
  246. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  247. output = self.app.get('/test/tree/master/sources')
  248. self.assertEqual(output.status_code, 200)
  249. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  250. output = self.app.get('/test/tree/feature')
  251. self.assertEqual(output.status_code, 200)
  252. self.assertIn('<a href="/test/blob/feature/f/sources">', output.get_data(as_text=True))
  253. output = self.app.get('/test/tree/feature/sources')
  254. self.assertEqual(output.status_code, 200)
  255. self.assertIn('No content found in this repository', output.get_data(as_text=True))
  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(pagure.APP, 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. user = tests.FakeUser()
  279. with tests.user_set(pagure.APP, user):
  280. output = self.app.get('/test/diff/master..feature')
  281. self.assertEqual(output.status_code, 400)
  282. self.assertIn(
  283. '<p>Branch master could not be found in the target repo</p>',
  284. output.get_data(as_text=True))
  285. if __name__ == '__main__':
  286. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  287. PagureFlaskNoMasterBranchtests)
  288. unittest.TextTestRunner(verbosity=2).run(SUITE)