test_pagure_flask_ui_slash_branch_name.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. self.assertEqual(output.data.count(
  120. '<a class="dropdown-item" href="/test/branch/maxamilion/feature'),
  121. 1)
  122. @patch('pagure.lib.notify.send_email')
  123. def test_view_repo_branch(self, send_email):
  124. """ Test the view_repo_branch endpoint when the git repo has no
  125. master branch.
  126. """
  127. send_email.return_value = True
  128. tests.create_projects(self.session)
  129. # Non-existant git repo
  130. output = self.app.get('/test/branch/master')
  131. self.assertEqual(output.status_code, 404)
  132. self.set_up_git_repo()
  133. # With git repo
  134. output = self.app.get('/test/branch/maxamilion/feature')
  135. self.assertEqual(output.status_code, 200)
  136. self.assertIn(
  137. '<div class="card-block">\n '
  138. '<h5><strong>Owners</strong></h5>', output.data)
  139. self.assertEqual(output.data.count(
  140. '<a class="dropdown-item" href="/test/branch/maxamilion/feature'),
  141. 1)
  142. @patch('pagure.lib.notify.send_email')
  143. def test_view_commits(self, send_email):
  144. """ Test the view_commits endpoint when the git repo has no
  145. master branch.
  146. """
  147. send_email.return_value = True
  148. tests.create_projects(self.session)
  149. # Non-existant git repo
  150. output = self.app.get('/test/commits')
  151. self.assertEqual(output.status_code, 404)
  152. self.set_up_git_repo()
  153. # With git repo
  154. output = self.app.get('/test/commits')
  155. self.assertEqual(output.status_code, 200)
  156. self.assertEqual(output.data.count('<span class="commitdate"'), 1)
  157. output = self.app.get('/test/commits/maxamilion/feature')
  158. self.assertEqual(output.status_code, 200)
  159. self.assertIn('<title>Commits - test - Pagure</title>', output.data)
  160. self.assertIn('Add sources file for testing', output.data)
  161. self.assertIn('Add .gitignore file for testing', output.data)
  162. self.assertEqual(output.data.count('<span class="commitdate"'), 3)
  163. @patch('pagure.lib.notify.send_email')
  164. def test_view_file(self, send_email):
  165. """ Test the view_file endpoint when the git repo has no
  166. master branch.
  167. """
  168. send_email.return_value = True
  169. tests.create_projects(self.session)
  170. # Non-existant git repo
  171. output = self.app.get('/test/blob/master/f/sources')
  172. self.assertEqual(output.status_code, 404)
  173. self.set_up_git_repo()
  174. # With git repo
  175. output = self.app.get('/test/blob/master/f/sources')
  176. self.assertEqual(output.status_code, 200)
  177. self.assertIn(
  178. '<li><a href="/test/tree/master"><span class="oi" '
  179. 'data-glyph="random"></span>&nbsp; master</a></li>'
  180. '<li class="active"><span class="oi" data-glyph="file">'
  181. '</span>&nbsp; sources</li>',
  182. output.data)
  183. output = self.app.get('/test/blob/master/f/.gitignore')
  184. self.assertEqual(output.status_code, 404)
  185. output = self.app.get('/test/blob/maxamilion/feature/f/.gitignore')
  186. self.assertEqual(output.status_code, 200)
  187. self.assertIn(
  188. '<li><a href="/test/tree/maxamilion/feature">'
  189. '<span class="oi" data-glyph="random"></span>'
  190. '&nbsp; maxamilion/feature</a></li>'
  191. '<li class="active"><span class="oi" data-glyph="file">'
  192. '</span>&nbsp; .gitignore</li>',
  193. output.data)
  194. self.assertTrue(
  195. # new version of pygments
  196. '<td class="cell2"><pre><span></span>*~</pre></td>' in output.data
  197. or
  198. # old version of pygments
  199. '<td class="cell2"><pre>*~</pre></td>' in output.data)
  200. @patch('pagure.lib.notify.send_email')
  201. def test_view_raw_file(self, send_email):
  202. """ Test the view_raw_file endpoint when the git repo has no
  203. master branch.
  204. """
  205. send_email.return_value = True
  206. tests.create_projects(self.session)
  207. # Non-existant git repo
  208. output = self.app.get('/test/raw/master')
  209. self.assertEqual(output.status_code, 404)
  210. output = self.app.get('/test/raw/master/f/sources')
  211. self.assertEqual(output.status_code, 404)
  212. self.set_up_git_repo()
  213. # With git repo
  214. output = self.app.get('/test/raw/master')
  215. self.assertEqual(output.status_code, 200)
  216. self.assertIn('diff --git a/sources b/sources', output.data)
  217. self.assertIn('+foo\n+ bar', output.data)
  218. output = self.app.get('/test/raw/master/f/sources')
  219. self.assertEqual(output.status_code, 200)
  220. self.assertEqual(output.data, 'foo\n bar')
  221. output = self.app.get('/test/raw/maxamilion/feature')
  222. self.assertEqual(output.status_code, 200)
  223. self.assertIn('diff --git a/.gitignore b/.gitignore', output.data)
  224. self.assertIn('+*~', output.data)
  225. output = self.app.get('/test/raw/maxamilion/feature/f/sources')
  226. self.assertEqual(output.status_code, 200)
  227. self.assertEqual('foo\n bar', output.data)
  228. @patch('pagure.lib.notify.send_email')
  229. def test_view_tree(self, send_email):
  230. """ Test the view_tree endpoint when the git repo has no
  231. master branch.
  232. """
  233. send_email.return_value = True
  234. tests.create_projects(self.session)
  235. # Non-existant git repo
  236. output = self.app.get('/test/tree/')
  237. self.assertEqual(output.status_code, 404)
  238. output = self.app.get('/test/tree/master')
  239. self.assertEqual(output.status_code, 404)
  240. self.set_up_git_repo()
  241. # With git repo
  242. output = self.app.get('/test/tree/master')
  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/master/sources')
  248. self.assertEqual(output.status_code, 200)
  249. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  250. self.assertEqual(
  251. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  252. output = self.app.get('/test/tree/feature')
  253. self.assertEqual(output.status_code, 200)
  254. self.assertIn('<a href="/test/blob/master/f/sources">', output.data)
  255. self.assertIn(
  256. '<td class-"pagure-table-filehex">\n'
  257. ' 9f4435', output.data)
  258. self.assertEqual(
  259. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  260. output = self.app.get('/test/tree/maxamilion/feature')
  261. self.assertEqual(output.status_code, 200)
  262. self.assertIn(
  263. '<a href="/test/blob/maxamilion/feature/f/sources">',
  264. output.data)
  265. self.assertIn(
  266. '<td class-"pagure-table-filehex">\n'
  267. ' 9f4435', output.data)
  268. self.assertIn(
  269. '<td class-"pagure-table-filehex">\n'
  270. ' e4e5f6', 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.assertIn(
  278. '<td class-"pagure-table-filehex">\n'
  279. ' 9f4435', output.data)
  280. self.assertEqual(
  281. output.data.count('<span class="oi text-muted" data-glyph="file">'), 1)
  282. @patch('pagure.lib.notify.send_email')
  283. def test_new_request_pull(self, send_email):
  284. """ Test the new_request_pull endpoint when the git repo has no
  285. master branch.
  286. """
  287. send_email.return_value = True
  288. tests.create_projects(self.session)
  289. # Non-existant git repo
  290. output = self.app.get('/test/diff/master..maxamilion/feature')
  291. # (used to be 302 but seeing a diff is allowed even logged out)
  292. self.assertEqual(output.status_code, 404)
  293. user = tests.FakeUser()
  294. with tests.user_set(pagure.APP, user):
  295. output = self.app.get('/test/diff/master..maxamilion/feature')
  296. self.assertEqual(output.status_code, 404)
  297. self.set_up_git_repo()
  298. output = self.app.get('/test/diff/master..maxamilion/feature')
  299. # (used to be 302 but seeing a diff is allowed even logged out)
  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. user = tests.FakeUser()
  309. with tests.user_set(pagure.APP, user):
  310. output = self.app.get('/test/diff/master..maxamilion/feature')
  311. self.assertEqual(output.status_code, 200)
  312. self.assertEqual(
  313. output.data.count('<span class="commitdate" title='), 1)
  314. self.assertIn(
  315. '<span class="label label-success pull-xs-right text-mono">'
  316. '+1</span>', output.data)
  317. self.assertIn(
  318. '<div><small>file added</small></div></h5>', output.data)
  319. if __name__ == '__main__':
  320. SUITE = unittest.TestLoader().loadTestsFromTestCase(
  321. PagureFlaskSlashInBranchtests)
  322. unittest.TextTestRunner(verbosity=2).run(SUITE)