test_pagure_flask_ui_no_master_branch.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.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(tests.HERE, 'repos')
  36. pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks')
  37. pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
  38. tests.HERE, 'tickets')
  39. pagure.APP.config['DOCS_FOLDER'] = os.path.join(
  40. tests.HERE, 'docs')
  41. pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
  42. tests.HERE, '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(tests.HERE, '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.data)
  116. self.assertEqual(
  117. output.data.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.data)
  136. self.assertEqual(
  137. output.data.count('<a class="dropdown-item" href="/test/branch/'),
  138. 1)
  139. @patch('pagure.lib.notify.send_email')
  140. def test_view_commits(self, send_email):
  141. """ Test the view_commits endpoint when the git repo has no
  142. master branch.
  143. """
  144. send_email.return_value = True
  145. tests.create_projects(self.session)
  146. # Non-existant git repo
  147. output = self.app.get('/test/commits')
  148. self.assertEqual(output.status_code, 404)
  149. self.set_up_git_repo()
  150. # With git repo
  151. output = self.app.get('/test/commits')
  152. self.assertEqual(output.status_code, 200)
  153. self.assertIn(
  154. '<div class="list-group m-t-1">\n </div>', output.data)
  155. self.assertNotIn(
  156. '<div class="btn-group pull-xs-right">', output.data)
  157. output = self.app.get('/test/commits/feature')
  158. self.assertEqual(output.status_code, 200)
  159. self.assertIn(
  160. '<div class="container p-t-3">\n <div class="row">',
  161. output.data)
  162. self.assertIn('Add sources file for testing', output.data)
  163. self.assertIn('Add .gitignore file for testing', output.data)
  164. self.assertNotIn(
  165. '<div class="list-group m-t-1">\n </div>', output.data)
  166. self.assertEqual(
  167. output.data.count('class="list-group-item p-l-3"'), 2)
  168. @patch('pagure.lib.notify.send_email')
  169. def test_view_file(self, send_email):
  170. """ Test the view_file endpoint when the git repo has no
  171. master branch.
  172. """
  173. send_email.return_value = True
  174. tests.create_projects(self.session)
  175. # Non-existant git repo
  176. output = self.app.get('/test/blob/master/f/sources')
  177. self.assertEqual(output.status_code, 404)
  178. self.set_up_git_repo()
  179. # With git repo
  180. output = self.app.get('/test/blob/master/f/sources')
  181. self.assertEqual(output.status_code, 404)
  182. output = self.app.get('/test/blob/feature/f/sources')
  183. self.assertEqual(output.status_code, 200)
  184. self.assertIn(
  185. '<a href="/test/tree/feature">'
  186. '<span class="oi" data-glyph="random"></span>&nbsp; feature</a>',
  187. output.data)
  188. self.assertIn(
  189. '</li><li class="active"><span class="oi" data-glyph="file">'
  190. '</span>&nbsp; sources</li>',
  191. output.data)
  192. self.assertTrue(
  193. # new version of pygments
  194. '<td class="cell2"><pre><span></span>foo</pre></td>' in output.data
  195. or
  196. # old version of pygments
  197. '<td class="cell2"><pre>foo</pre></td>' in output.data
  198. )
  199. @patch('pagure.lib.notify.send_email')
  200. def test_view_raw_file(self, send_email):
  201. """ Test the view_raw_file endpoint when the git repo has no
  202. master branch.
  203. """
  204. send_email.return_value = True
  205. tests.create_projects(self.session)
  206. # Non-existant 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. self.set_up_git_repo()
  212. # With 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. output = self.app.get('/test/raw/feature')
  218. self.assertEqual(output.status_code, 200)
  219. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  220. self.assertIn('+*~', output.data)
  221. output = self.app.get('/test/raw/feature/f/sources')
  222. self.assertEqual(output.status_code, 200)
  223. self.assertEqual('foo\n bar', output.data)
  224. @patch('pagure.lib.notify.send_email')
  225. def test_view_tree(self, send_email):
  226. """ Test the view_tree endpoint when the git repo has no
  227. master branch.
  228. """
  229. send_email.return_value = True
  230. tests.create_projects(self.session)
  231. # Non-existant git repo
  232. output = self.app.get('/test/tree/')
  233. self.assertEqual(output.status_code, 404)
  234. output = self.app.get('/test/tree/master')
  235. self.assertEqual(output.status_code, 404)
  236. self.set_up_git_repo()
  237. # With git repo
  238. output = self.app.get('/test/tree/master')
  239. self.assertEqual(output.status_code, 200)
  240. self.assertIn('No content found in this repository', output.data)
  241. output = self.app.get('/test/tree/master/sources')
  242. self.assertEqual(output.status_code, 200)
  243. self.assertIn('No content found in this repository', output.data)
  244. output = self.app.get('/test/tree/feature')
  245. self.assertEqual(output.status_code, 200)
  246. self.assertIn('<a href="/test/blob/feature/f/sources">', output.data)
  247. self.assertIn(
  248. '<td class-"pagure-table-filehex">\n'
  249. ' 9f4435\n </td>',
  250. output.data)
  251. output = self.app.get('/test/tree/feature/sources')
  252. self.assertEqual(output.status_code, 200)
  253. self.assertIn('No content found in this repository', output.data)
  254. @patch('pagure.lib.notify.send_email')
  255. def test_new_request_pull(self, send_email):
  256. """ Test the new_request_pull endpoint when the git repo has no
  257. master branch.
  258. """
  259. send_email.return_value = True
  260. tests.create_projects(self.session)
  261. # Non-existant git repo
  262. output = self.app.get('/test/diff/master..feature')
  263. # (used to be 302 but seeing a diff is allowed even logged out)
  264. self.assertEqual(output.status_code, 404)
  265. user = tests.FakeUser()
  266. with tests.user_set(pagure.APP, user):
  267. output = self.app.get('/test/diff/master..feature')
  268. self.assertEqual(output.status_code, 404)
  269. self.set_up_git_repo()
  270. output = self.app.get('/test/diff/master..feature')
  271. # (used to be 302 but seeing a diff is allowed even logged out)
  272. self.assertEqual(output.status_code, 400)
  273. self.assertIn(
  274. '<p>Branch master could not be found in the target repo</p>',
  275. output.data)
  276. user = tests.FakeUser()
  277. with tests.user_set(pagure.APP, user):
  278. output = self.app.get('/test/diff/master..feature')
  279. self.assertEqual(output.status_code, 400)
  280. self.assertIn(
  281. '<p>Branch master could not be found in the target repo</p>',
  282. output.data)
  283. if __name__ == '__main__':
  284. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  285. PagureFlaskNoMasterBranchtests)
  286. unittest.TextTestRunner(verbosity=2).run(SUITE)