test_pagure_flask_ui_slash_branch_name.py 13 KB

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