test_pagure_flask_ui_no_master_branch.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. @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.data)
  152. self.assertNotIn(
  153. '<div class="btn-group pull-xs-right">', output.data)
  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.data)
  159. self.assertIn('Add sources file for testing', output.data)
  160. self.assertIn('Add .gitignore file for testing', output.data)
  161. self.assertNotIn(
  162. '<div class="list-group m-t-1">\n </div>', output.data)
  163. self.assertEqual(
  164. output.data.count('class="list-group-item p-l-3"'), 2)
  165. @patch('pagure.lib.notify.send_email')
  166. def test_view_file(self, send_email):
  167. """ Test the view_file endpoint when the git repo has no
  168. master branch.
  169. """
  170. send_email.return_value = True
  171. tests.create_projects(self.session)
  172. # Non-existant git repo
  173. output = self.app.get('/test/blob/master/f/sources')
  174. self.assertEqual(output.status_code, 404)
  175. self.set_up_git_repo()
  176. # With git repo
  177. output = self.app.get('/test/blob/master/f/sources')
  178. self.assertEqual(output.status_code, 404)
  179. output = self.app.get('/test/blob/feature/f/sources')
  180. self.assertEqual(output.status_code, 200)
  181. self.assertIn(
  182. '''
  183. <ol class="breadcrumb">
  184. <li>
  185. <a href="/test/tree/feature">
  186. <span class="oi" data-glyph="random">
  187. </span>&nbsp; feature
  188. </a>
  189. </li>
  190. <li class="active">
  191. <span class="oi" data-glyph="file">
  192. </span>&nbsp; sources
  193. </li>
  194. </ol>''', output.data)
  195. self.assertTrue(
  196. # new version of pygments
  197. '<td class="cell2"><pre><span></span>foo</pre></td>' in output.data
  198. or
  199. # old version of pygments
  200. '<td class="cell2"><pre>foo</pre></td>' in output.data
  201. )
  202. @patch('pagure.lib.notify.send_email')
  203. def test_view_raw_file(self, send_email):
  204. """ Test the view_raw_file endpoint when the git repo has no
  205. master branch.
  206. """
  207. send_email.return_value = True
  208. tests.create_projects(self.session)
  209. # Non-existant git repo
  210. output = self.app.get('/test/raw/master')
  211. self.assertEqual(output.status_code, 404)
  212. output = self.app.get('/test/raw/master/f/sources')
  213. self.assertEqual(output.status_code, 404)
  214. self.set_up_git_repo()
  215. # With git repo
  216. output = self.app.get('/test/raw/master')
  217. self.assertEqual(output.status_code, 404)
  218. output = self.app.get('/test/raw/master/f/sources')
  219. self.assertEqual(output.status_code, 404)
  220. output = self.app.get('/test/raw/feature')
  221. self.assertEqual(output.status_code, 200)
  222. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  223. self.assertIn('+*~', output.data)
  224. output = self.app.get('/test/raw/feature/f/sources')
  225. self.assertEqual(output.status_code, 200)
  226. self.assertEqual('foo\n bar', output.data)
  227. @patch('pagure.lib.notify.send_email')
  228. def test_view_tree(self, send_email):
  229. """ Test the view_tree endpoint when the git repo has no
  230. master branch.
  231. """
  232. send_email.return_value = True
  233. tests.create_projects(self.session)
  234. # Non-existant git repo
  235. output = self.app.get('/test/tree/')
  236. self.assertEqual(output.status_code, 404)
  237. output = self.app.get('/test/tree/master')
  238. self.assertEqual(output.status_code, 404)
  239. self.set_up_git_repo()
  240. # With git repo
  241. output = self.app.get('/test/tree/master')
  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/master/sources')
  245. self.assertEqual(output.status_code, 200)
  246. self.assertIn('No content found in this repository', output.data)
  247. output = self.app.get('/test/tree/feature')
  248. self.assertEqual(output.status_code, 200)
  249. self.assertIn('<a href="/test/blob/feature/f/sources">', output.data)
  250. output = self.app.get('/test/tree/feature/sources')
  251. self.assertEqual(output.status_code, 200)
  252. self.assertIn('No content found in this repository', output.data)
  253. @patch('pagure.lib.notify.send_email')
  254. def test_new_request_pull(self, send_email):
  255. """ Test the new_request_pull endpoint when the git repo has no
  256. master branch.
  257. """
  258. send_email.return_value = True
  259. tests.create_projects(self.session)
  260. # Non-existant 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, 404)
  264. user = tests.FakeUser()
  265. with tests.user_set(pagure.APP, user):
  266. output = self.app.get('/test/diff/master..feature')
  267. self.assertEqual(output.status_code, 404)
  268. self.set_up_git_repo()
  269. output = self.app.get('/test/diff/master..feature')
  270. # (used to be 302 but seeing a diff is allowed even logged out)
  271. self.assertEqual(output.status_code, 400)
  272. self.assertIn(
  273. '<p>Branch master could not be found in the target repo</p>',
  274. output.data)
  275. user = tests.FakeUser()
  276. with tests.user_set(pagure.APP, user):
  277. output = self.app.get('/test/diff/master..feature')
  278. self.assertEqual(output.status_code, 400)
  279. self.assertIn(
  280. '<p>Branch master could not be found in the target repo</p>',
  281. output.data)
  282. if __name__ == '__main__':
  283. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  284. PagureFlaskNoMasterBranchtests)
  285. unittest.TextTestRunner(verbosity=2).run(SUITE)