test_pagure_flask_ui_slash_branch_name.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 PagureFlaskSlashInBranchtests(tests.Modeltests):
  23. """ Tests for flask application when the branch name contains a '/'.
  24. """
  25. def setUp(self):
  26. """ Set up the environnment, ran before every tests. """
  27. super(PagureFlaskSlashInBranchtests, 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/master', # 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. refname = 'refs/heads/master'
  74. ori_remote = clone_repo.remotes[0]
  75. PagureRepo.push(ori_remote, refname)
  76. master_branch = clone_repo.lookup_branch('master')
  77. first_commit = master_branch.get_object().hex
  78. # Second commit
  79. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  80. stream.write('*~')
  81. clone_repo.index.add('.gitignore')
  82. clone_repo.index.write()
  83. tree = clone_repo.index.write_tree()
  84. author = pygit2.Signature(
  85. 'Alice Author', 'alice@authors.tld')
  86. committer = pygit2.Signature(
  87. 'Cecil Committer', 'cecil@committers.tld')
  88. clone_repo.create_commit(
  89. 'refs/heads/maxamilion/feature',
  90. author,
  91. committer,
  92. 'Add .gitignore file for testing',
  93. # binary string representing the tree object ID
  94. tree,
  95. # list of binary strings representing parents of the new commit
  96. [first_commit]
  97. )
  98. refname = 'refs/heads/maxamilion/feature'
  99. ori_remote = clone_repo.remotes[0]
  100. PagureRepo.push(ori_remote, refname)
  101. shutil.rmtree(newpath)
  102. @patch('pagure.lib.notify.send_email')
  103. def test_view_repo(self, send_email):
  104. """ Test the view_repo endpoint when the git repo has no master
  105. branch.
  106. """
  107. send_email.return_value = True
  108. tests.create_projects(self.session)
  109. # Non-existant git repo
  110. output = self.app.get('/test')
  111. self.assertEqual(output.status_code, 404)
  112. self.set_up_git_repo()
  113. # With git repo
  114. output = self.app.get('/test')
  115. self.assertEqual(output.status_code, 200)
  116. self.assertIn(
  117. '<div class="card-block">\n '
  118. '<h5><strong>Owners</strong></h5>', output.data)
  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/maxamilion/feature')
  132. self.assertEqual(output.status_code, 200)
  133. self.assertIn(
  134. '<div class="card-block">\n '
  135. '<h5><strong>Owners</strong></h5>', 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.assertEqual(output.data.count('<span class="commitdate"'), 1)
  151. output = self.app.get('/test/commits/maxamilion/feature')
  152. self.assertEqual(output.status_code, 200)
  153. self.assertIn('<title>Commits - test - Pagure</title>', output.data)
  154. self.assertIn('Add sources file for testing', output.data)
  155. self.assertIn('Add .gitignore file for testing', output.data)
  156. self.assertEqual(output.data.count('<span class="commitdate"'), 3)
  157. @patch('pagure.lib.notify.send_email')
  158. def test_view_file(self, send_email):
  159. """ Test the view_file endpoint when the git repo has no
  160. master branch.
  161. """
  162. send_email.return_value = True
  163. tests.create_projects(self.session)
  164. # Non-existant git repo
  165. output = self.app.get('/test/blob/master/f/sources')
  166. self.assertEqual(output.status_code, 404)
  167. self.set_up_git_repo()
  168. # With git repo
  169. output = self.app.get('/test/blob/master/f/sources')
  170. self.assertEqual(output.status_code, 200)
  171. self.assertIn(
  172. '''
  173. <ol class="breadcrumb">
  174. <li>
  175. <a href="/test/tree/master">
  176. <span class="oi" data-glyph="random">
  177. </span>&nbsp; master
  178. </a>
  179. </li>
  180. <li class="active">
  181. <span class="oi" data-glyph="file">
  182. </span>&nbsp; sources
  183. </li>
  184. </ol>''', output.data)
  185. output = self.app.get('/test/blob/master/f/.gitignore')
  186. self.assertEqual(output.status_code, 404)
  187. output = self.app.get('/test/blob/maxamilion/feature/f/.gitignore')
  188. self.assertEqual(output.status_code, 200)
  189. self.assertIn(
  190. '''
  191. <ol class="breadcrumb">
  192. <li>
  193. <a href="/test/tree/maxamilion/feature">
  194. <span class="oi" data-glyph="random">
  195. </span>&nbsp; maxamilion/feature
  196. </a>
  197. </li>
  198. <li class="active">
  199. <span class="oi" data-glyph="file">
  200. </span>&nbsp; .gitignore
  201. </li>
  202. </ol>''', output.data)
  203. self.assertTrue(
  204. # new version of pygments
  205. '<td class="cell2"><pre><span></span>*~</pre></td>' in output.data
  206. or
  207. # old version of pygments
  208. '<td class="cell2"><pre>*~</pre></td>' in output.data)
  209. @patch('pagure.lib.notify.send_email')
  210. def test_view_raw_file(self, send_email):
  211. """ Test the view_raw_file endpoint when the git repo has no
  212. master branch.
  213. """
  214. send_email.return_value = True
  215. tests.create_projects(self.session)
  216. # Non-existant git repo
  217. output = self.app.get('/test/raw/master')
  218. self.assertEqual(output.status_code, 404)
  219. output = self.app.get('/test/raw/master/f/sources')
  220. self.assertEqual(output.status_code, 404)
  221. self.set_up_git_repo()
  222. # With git repo
  223. output = self.app.get('/test/raw/master')
  224. self.assertEqual(output.status_code, 200)
  225. self.assertIn('diff --git a/sources b/sources', output.data)
  226. self.assertIn('+foo\n+ bar', output.data)
  227. output = self.app.get('/test/raw/master/f/sources')
  228. self.assertEqual(output.status_code, 200)
  229. self.assertEqual(output.data, 'foo\n bar')
  230. output = self.app.get('/test/raw/maxamilion/feature')
  231. self.assertEqual(output.status_code, 200)
  232. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  233. self.assertIn('+*~', output.data)
  234. output = self.app.get('/test/raw/maxamilion/feature/f/sources')
  235. self.assertEqual(output.status_code, 200)
  236. self.assertEqual('foo\n bar', output.data)
  237. @patch('pagure.lib.notify.send_email')
  238. def test_view_tree(self, send_email):
  239. """ Test the view_tree endpoint when the git repo has no
  240. master branch.
  241. """
  242. send_email.return_value = True
  243. tests.create_projects(self.session)
  244. # Non-existant git repo
  245. output = self.app.get('/test/tree/')
  246. self.assertEqual(output.status_code, 404)
  247. output = self.app.get('/test/tree/master')
  248. self.assertEqual(output.status_code, 404)
  249. self.set_up_git_repo()
  250. # With git repo
  251. output = self.app.get('/test/tree/master')
  252. self.assertEqual(output.status_code, 200)
  253. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  254. self.assertEqual(
  255. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  256. output = self.app.get('/test/tree/master/sources')
  257. self.assertEqual(output.status_code, 200)
  258. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  259. self.assertEqual(
  260. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  261. output = self.app.get('/test/tree/feature')
  262. self.assertEqual(output.status_code, 200)
  263. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  264. self.assertEqual(
  265. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  266. output = self.app.get('/test/tree/maxamilion/feature')
  267. self.assertEqual(output.status_code, 200)
  268. self.assertIn(
  269. '<a href="/test/blob/maxamilion/feature/f/sources">',
  270. output.data)
  271. self.assertEqual(
  272. output.data.count('<span class="oi text-muted" data-glyph="file">'), 2)
  273. # Wrong identifier, back onto master
  274. output = self.app.get('/test/tree/maxamilion/feature/f/.gitignore')
  275. self.assertEqual(output.status_code, 200)
  276. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  277. self.assertEqual(
  278. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  279. @patch('pagure.lib.notify.send_email')
  280. def test_new_request_pull(self, send_email):
  281. """ Test the new_request_pull endpoint when the git repo has no
  282. master branch.
  283. """
  284. send_email.return_value = True
  285. tests.create_projects(self.session)
  286. # Non-existant git repo
  287. output = self.app.get('/test/diff/master..maxamilion/feature')
  288. # (used to be 302 but seeing a diff is allowed even logged out)
  289. self.assertEqual(output.status_code, 404)
  290. user = tests.FakeUser()
  291. with tests.user_set(pagure.APP, user):
  292. output = self.app.get('/test/diff/master..maxamilion/feature')
  293. self.assertEqual(output.status_code, 404)
  294. self.set_up_git_repo()
  295. output = self.app.get('/test/diff/master..maxamilion/feature')
  296. # (used to be 302 but seeing a diff is allowed even logged out)
  297. self.assertEqual(output.status_code, 200)
  298. self.assertEqual(
  299. output.data.count('<span class="commitdate" title='), 1)
  300. self.assertIn(
  301. '<span class="label label-success pull-xs-right text-mono">'
  302. '+1</span>', output.data)
  303. self.assertIn(
  304. '<div><small>file added</small></div></h5>', output.data)
  305. user = tests.FakeUser()
  306. with tests.user_set(pagure.APP, user):
  307. output = self.app.get('/test/diff/master..maxamilion/feature')
  308. self.assertEqual(output.status_code, 200)
  309. self.assertEqual(
  310. output.data.count('<span class="commitdate" title='), 1)
  311. self.assertIn(
  312. '<span class="label label-success pull-xs-right text-mono">'
  313. '+1</span>', output.data)
  314. self.assertIn(
  315. '<div><small>file added</small></div></h5>', output.data)
  316. if __name__ == '__main__':
  317. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  318. PagureFlaskSlashInBranchtests)
  319. unittest.TextTestRunner(verbosity=2).run(SUITE)