test_pagure_flask_ui_slash_branch_name.py 13 KB

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