test_pagure_flask_ui_slash_branch_name.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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
  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. '<td class="cell2"><pre><code>*~</code></pre></td>', output_text)
  192. @patch('pagure.lib.notify.send_email')
  193. def test_view_raw_file(self, send_email):
  194. """ Test the view_raw_file endpoint when the git repo has no
  195. master branch.
  196. """
  197. send_email.return_value = True
  198. tests.create_projects(self.session)
  199. # Non-existant git repo
  200. output = self.app.get('/test/raw/master')
  201. self.assertEqual(output.status_code, 404)
  202. output = self.app.get('/test/raw/master/f/sources')
  203. self.assertEqual(output.status_code, 404)
  204. self.set_up_git_repo()
  205. # With git repo
  206. output = self.app.get('/test/raw/master')
  207. self.assertEqual(output.status_code, 200)
  208. output_text = output.get_data(as_text=True)
  209. self.assertIn('diff --git a/sources b/sources', output_text)
  210. self.assertIn('+foo\n+ bar', output_text)
  211. output = self.app.get('/test/raw/master/f/sources')
  212. self.assertEqual(output.status_code, 200)
  213. self.assertEqual(output.get_data(as_text=True), 'foo\n bar')
  214. output = self.app.get('/test/raw/maxamilion/feature')
  215. self.assertEqual(output.status_code, 200)
  216. output_text = output.get_data(as_text=True)
  217. self.assertIn('diff --git a/.gitignore b/.gitignore', output_text)
  218. self.assertIn('+*~', output_text)
  219. output = self.app.get('/test/raw/maxamilion/feature/f/sources')
  220. self.assertEqual(output.status_code, 200)
  221. self.assertEqual('foo\n bar', output.get_data(as_text=True))
  222. @patch('pagure.lib.notify.send_email')
  223. def test_view_tree(self, send_email):
  224. """ Test the view_tree endpoint when the git repo has no
  225. master branch.
  226. """
  227. send_email.return_value = True
  228. tests.create_projects(self.session)
  229. # Non-existant git repo
  230. output = self.app.get('/test/tree/')
  231. self.assertEqual(output.status_code, 404)
  232. output = self.app.get('/test/tree/master')
  233. self.assertEqual(output.status_code, 404)
  234. self.set_up_git_repo()
  235. # With git repo
  236. output = self.app.get('/test/tree/master')
  237. self.assertEqual(output.status_code, 200)
  238. output_text = output.get_data(as_text=True)
  239. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  240. output = self.app.get('/test/tree/master/sources')
  241. self.assertEqual(output.status_code, 200)
  242. output_text = output.get_data(as_text=True)
  243. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  244. output = self.app.get('/test/tree/feature')
  245. self.assertEqual(output.status_code, 200)
  246. output_text = output.get_data(as_text=True)
  247. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  248. output = self.app.get('/test/tree/maxamilion/feature')
  249. self.assertEqual(output.status_code, 200)
  250. output_text = output.get_data(as_text=True)
  251. self.assertIn(
  252. '<a href="/test/blob/maxamilion/feature/f/sources">',
  253. output_text)
  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. output_text = output.get_data(as_text=True)
  258. self.assertIn('<a href="/test/blob/master/f/sources">', output_text)
  259. @patch('pagure.lib.notify.send_email')
  260. def test_new_request_pull(self, send_email):
  261. """ Test the new_request_pull endpoint when the git repo has no
  262. master branch.
  263. """
  264. send_email.return_value = True
  265. tests.create_projects(self.session)
  266. # Non-existant git repo
  267. output = self.app.get('/test/diff/master..maxamilion/feature')
  268. # (used to be 302 but seeing a diff is allowed even logged out)
  269. self.assertEqual(output.status_code, 404)
  270. user = tests.FakeUser()
  271. with tests.user_set(self.app.application, user):
  272. output = self.app.get('/test/diff/master..maxamilion/feature')
  273. self.assertEqual(output.status_code, 404)
  274. self.set_up_git_repo()
  275. output = self.app.get('/test/diff/master..maxamilion/feature')
  276. # (used to be 302 but seeing a diff is allowed even logged out)
  277. self.assertEqual(output.status_code, 200)
  278. output_text = output.get_data(as_text=True)
  279. self.assertEqual(
  280. output_text.count('<span class="commitdate"'), 1)
  281. self.assertIn(
  282. '<span class="font-weight-bold btn btn-sm btn-success disabled opacity-100">'
  283. '+1</span>\n', output_text)
  284. self.assertIn(
  285. '<div class="btn btn-outline-success disabled opacity-100 border-0 font-weight-bold">\n'
  286. ' file added\n', output_text)
  287. user = tests.FakeUser()
  288. with tests.user_set(self.app.application, user):
  289. output = self.app.get('/test/diff/master..maxamilion/feature')
  290. self.assertEqual(output.status_code, 200)
  291. output_text = output.get_data(as_text=True)
  292. self.assertEqual(
  293. output_text.count('<span class="commitdate"'), 1)
  294. self.assertIn(
  295. '<span class="font-weight-bold btn btn-sm btn-success disabled opacity-100">'
  296. '+1</span>\n', output_text)
  297. self.assertIn(
  298. '<div class="btn btn-outline-success disabled opacity-100 border-0 font-weight-bold">\n'
  299. ' file added\n', output_text)
  300. if __name__ == '__main__':
  301. unittest.main(verbosity=2)