test_pagure_flask_ui_slash_branch_name.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. self.app = pagure.APP.test_client()
  36. def set_up_git_repo(self):
  37. """ Set up the git repo to play with. """
  38. # Create a git repo to play with
  39. gitrepo = os.path.join(self.path, 'repos', 'test.git')
  40. repo = pygit2.init_repository(gitrepo, bare=True)
  41. newpath = tempfile.mkdtemp(prefix='pagure-other-test')
  42. repopath = os.path.join(newpath, 'test')
  43. clone_repo = pygit2.clone_repository(gitrepo, repopath)
  44. # Create a file in that git repo
  45. with open(os.path.join(repopath, 'sources'), 'w') as stream:
  46. stream.write('foo\n bar')
  47. clone_repo.index.add('sources')
  48. clone_repo.index.write()
  49. # Commits the files added
  50. tree = clone_repo.index.write_tree()
  51. author = pygit2.Signature(
  52. 'Alice Author', 'alice@authors.tld')
  53. committer = pygit2.Signature(
  54. 'Cecil Committer', 'cecil@committers.tld')
  55. clone_repo.create_commit(
  56. 'refs/heads/master', # the name of the reference to update
  57. author,
  58. committer,
  59. 'Add sources file for testing',
  60. # binary string representing the tree object ID
  61. tree,
  62. # list of binary strings representing parents of the new commit
  63. []
  64. )
  65. refname = 'refs/heads/master'
  66. ori_remote = clone_repo.remotes[0]
  67. PagureRepo.push(ori_remote, refname)
  68. master_branch = clone_repo.lookup_branch('master')
  69. first_commit = master_branch.get_object().hex
  70. # Second commit
  71. with open(os.path.join(repopath, '.gitignore'), 'w') as stream:
  72. stream.write('*~')
  73. clone_repo.index.add('.gitignore')
  74. clone_repo.index.write()
  75. tree = clone_repo.index.write_tree()
  76. author = pygit2.Signature(
  77. 'Alice Author', 'alice@authors.tld')
  78. committer = pygit2.Signature(
  79. 'Cecil Committer', 'cecil@committers.tld')
  80. clone_repo.create_commit(
  81. 'refs/heads/maxamilion/feature',
  82. author,
  83. committer,
  84. 'Add .gitignore file for testing',
  85. # binary string representing the tree object ID
  86. tree,
  87. # list of binary strings representing parents of the new commit
  88. [first_commit]
  89. )
  90. refname = 'refs/heads/maxamilion/feature'
  91. ori_remote = clone_repo.remotes[0]
  92. PagureRepo.push(ori_remote, refname)
  93. shutil.rmtree(newpath)
  94. @patch('pagure.lib.notify.send_email')
  95. def test_view_repo(self, send_email):
  96. """ Test the view_repo endpoint when the git repo has no master
  97. branch.
  98. """
  99. send_email.return_value = True
  100. tests.create_projects(self.session)
  101. # Non-existant git repo
  102. output = self.app.get('/test')
  103. self.assertEqual(output.status_code, 404)
  104. self.set_up_git_repo()
  105. # With git repo
  106. output = self.app.get('/test')
  107. self.assertEqual(output.status_code, 200)
  108. self.assertIn(
  109. '<div class="card-block">\n '
  110. '<h5><strong>Contributors</strong></h5>', output.data)
  111. @patch('pagure.lib.notify.send_email')
  112. def test_view_repo_branch(self, send_email):
  113. """ Test the view_repo_branch endpoint when the git repo has no
  114. master branch.
  115. """
  116. send_email.return_value = True
  117. tests.create_projects(self.session)
  118. # Non-existant git repo
  119. output = self.app.get('/test/branch/master')
  120. self.assertEqual(output.status_code, 404)
  121. self.set_up_git_repo()
  122. # With git repo
  123. output = self.app.get('/test/branch/maxamilion/feature')
  124. self.assertEqual(output.status_code, 200)
  125. self.assertIn(
  126. '<div class="card-block">\n '
  127. '<h5><strong>Contributors</strong></h5>', output.data)
  128. @patch('pagure.lib.notify.send_email')
  129. def test_view_commits(self, send_email):
  130. """ Test the view_commits endpoint when the git repo has no
  131. master branch.
  132. """
  133. send_email.return_value = True
  134. tests.create_projects(self.session)
  135. # Non-existant git repo
  136. output = self.app.get('/test/commits')
  137. self.assertEqual(output.status_code, 404)
  138. self.set_up_git_repo()
  139. # With git repo
  140. output = self.app.get('/test/commits')
  141. self.assertEqual(output.status_code, 200)
  142. self.assertEqual(output.data.count('<span class="commitdate"'), 1)
  143. output = self.app.get('/test/commits/maxamilion/feature')
  144. self.assertEqual(output.status_code, 200)
  145. self.assertIn('<title>Commits - test - Pagure</title>', output.data)
  146. self.assertIn('Add sources file for testing', output.data)
  147. self.assertIn('Add .gitignore file for testing', output.data)
  148. self.assertEqual(output.data.count('<span class="commitdate"'), 3)
  149. @patch('pagure.lib.notify.send_email')
  150. def test_view_file(self, send_email):
  151. """ Test the view_file endpoint when the git repo has no
  152. master branch.
  153. """
  154. send_email.return_value = True
  155. tests.create_projects(self.session)
  156. # Non-existant git repo
  157. output = self.app.get('/test/blob/master/f/sources')
  158. self.assertEqual(output.status_code, 404)
  159. self.set_up_git_repo()
  160. # With git repo
  161. output = self.app.get('/test/blob/master/f/sources')
  162. self.assertEqual(output.status_code, 200)
  163. self.assertIn(
  164. '''
  165. <ol class="breadcrumb">
  166. <li>
  167. <a href="/test/tree/master">
  168. <span class="oi" data-glyph="random">
  169. </span>&nbsp; master
  170. </a>
  171. </li>
  172. <li class="active">
  173. <span class="oi" data-glyph="file">
  174. </span>&nbsp; sources
  175. </li>
  176. </ol>''', output.data)
  177. output = self.app.get('/test/blob/master/f/.gitignore')
  178. self.assertEqual(output.status_code, 404)
  179. output = self.app.get('/test/blob/maxamilion/feature/f/.gitignore')
  180. self.assertEqual(output.status_code, 200)
  181. self.assertIn(
  182. '''
  183. <ol class="breadcrumb">
  184. <li>
  185. <a href="/test/tree/maxamilion/feature">
  186. <span class="oi" data-glyph="random">
  187. </span>&nbsp; maxamilion/feature
  188. </a>
  189. </li>
  190. <li class="active">
  191. <span class="oi" data-glyph="file">
  192. </span>&nbsp; .gitignore
  193. </li>
  194. </ol>''', output.data)
  195. self.assertTrue(
  196. # new version of pygments
  197. '<td class="cell2"><pre><span></span>*~</pre></td>' in output.data
  198. or
  199. # old version of pygments
  200. '<td class="cell2"><pre>*~</pre></td>' in output.data)
  201. @patch('pagure.lib.notify.send_email')
  202. def test_view_raw_file(self, send_email):
  203. """ Test the view_raw_file endpoint when the git repo has no
  204. master branch.
  205. """
  206. send_email.return_value = True
  207. tests.create_projects(self.session)
  208. # Non-existant git repo
  209. output = self.app.get('/test/raw/master')
  210. self.assertEqual(output.status_code, 404)
  211. output = self.app.get('/test/raw/master/f/sources')
  212. self.assertEqual(output.status_code, 404)
  213. self.set_up_git_repo()
  214. # With git repo
  215. output = self.app.get('/test/raw/master')
  216. self.assertEqual(output.status_code, 200)
  217. self.assertIn('diff --git a/sources b/sources', output.data)
  218. self.assertIn('+foo\n+ bar', output.data)
  219. output = self.app.get('/test/raw/master/f/sources')
  220. self.assertEqual(output.status_code, 200)
  221. self.assertEqual(output.data, 'foo\n bar')
  222. output = self.app.get('/test/raw/maxamilion/feature')
  223. self.assertEqual(output.status_code, 200)
  224. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  225. self.assertIn('+*~', output.data)
  226. output = self.app.get('/test/raw/maxamilion/feature/f/sources')
  227. self.assertEqual(output.status_code, 200)
  228. self.assertEqual('foo\n bar', output.data)
  229. @patch('pagure.lib.notify.send_email')
  230. def test_view_tree(self, send_email):
  231. """ Test the view_tree endpoint when the git repo has no
  232. master branch.
  233. """
  234. send_email.return_value = True
  235. tests.create_projects(self.session)
  236. # Non-existant git repo
  237. output = self.app.get('/test/tree/')
  238. self.assertEqual(output.status_code, 404)
  239. output = self.app.get('/test/tree/master')
  240. self.assertEqual(output.status_code, 404)
  241. self.set_up_git_repo()
  242. # With git repo
  243. output = self.app.get('/test/tree/master')
  244. self.assertEqual(output.status_code, 200)
  245. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  246. self.assertEqual(
  247. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  248. output = self.app.get('/test/tree/master/sources')
  249. self.assertEqual(output.status_code, 200)
  250. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  251. self.assertEqual(
  252. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  253. output = self.app.get('/test/tree/feature')
  254. self.assertEqual(output.status_code, 200)
  255. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  256. self.assertEqual(
  257. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  258. output = self.app.get('/test/tree/maxamilion/feature')
  259. self.assertEqual(output.status_code, 200)
  260. self.assertIn(
  261. '<a href="/test/blob/maxamilion/feature/f/sources">',
  262. output.data)
  263. self.assertEqual(
  264. output.data.count('<span class="oi text-muted" data-glyph="file">'), 2)
  265. # Wrong identifier, back onto master
  266. output = self.app.get('/test/tree/maxamilion/feature/f/.gitignore')
  267. self.assertEqual(output.status_code, 200)
  268. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  269. self.assertEqual(
  270. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  271. @patch('pagure.lib.notify.send_email')
  272. def test_new_request_pull(self, send_email):
  273. """ Test the new_request_pull endpoint when the git repo has no
  274. master branch.
  275. """
  276. send_email.return_value = True
  277. tests.create_projects(self.session)
  278. # Non-existant git repo
  279. output = self.app.get('/test/diff/master..maxamilion/feature')
  280. # (used to be 302 but seeing a diff is allowed even logged out)
  281. self.assertEqual(output.status_code, 404)
  282. user = tests.FakeUser()
  283. with tests.user_set(pagure.APP, user):
  284. output = self.app.get('/test/diff/master..maxamilion/feature')
  285. self.assertEqual(output.status_code, 404)
  286. self.set_up_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, 200)
  290. self.assertEqual(
  291. output.data.count('<span class="commitdate" title='), 1)
  292. self.assertIn(
  293. '<span class="label label-success pull-xs-right text-mono">'
  294. '+1</span>', output.data)
  295. self.assertIn(
  296. '<div><small>file added</small></div></h5>', output.data)
  297. user = tests.FakeUser()
  298. with tests.user_set(pagure.APP, user):
  299. output = self.app.get('/test/diff/master..maxamilion/feature')
  300. self.assertEqual(output.status_code, 200)
  301. self.assertEqual(
  302. output.data.count('<span class="commitdate" title='), 1)
  303. self.assertIn(
  304. '<span class="label label-success pull-xs-right text-mono">'
  305. '+1</span>', output.data)
  306. self.assertIn(
  307. '<div><small>file added</small></div></h5>', output.data)
  308. if __name__ == '__main__':
  309. unittest.main(verbosity=2)