test_pagure_flask_ui_repo_view_file.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals
  8. import unittest
  9. import sys
  10. import os
  11. import pygit2
  12. sys.path.insert(0, os.path.join(os.path.dirname(
  13. os.path.abspath(__file__)), '..'))
  14. import pagure # noqa
  15. import pagure.lib # noqa
  16. import tests # noqa
  17. from pagure.lib.repo import PagureRepo # noqa
  18. class LocalBasetests(tests.Modeltests):
  19. """ Tests for view_file endpoint of the flask pagure app """
  20. def setUp(self):
  21. """ Set up the environnment, ran before every tests. """
  22. super(LocalBasetests, self).setUp()
  23. pagure.config.config['VIRUS_SCAN_ATTACHMENTS'] = False
  24. pagure.config.config['UPLOAD_FOLDER_URL'] = '/releases/'
  25. pagure.config.config['UPLOAD_FOLDER_PATH'] = os.path.join(
  26. self.path, 'releases')
  27. class PagureFlaskRepoViewFileSimpletests(LocalBasetests):
  28. """ Tests for view_file endpoint of the flask pagure app """
  29. def test_view_file_no_project(self):
  30. """ Test the view_file when the project is unknown. """
  31. output = self.app.get('/foo/blob/foo/f/sources')
  32. # No project registered in the DB
  33. self.assertEqual(output.status_code, 404)
  34. def test_view_file_no_git(self):
  35. """ Test the view_file when the project has no git repo. """
  36. tests.create_projects(self.session)
  37. output = self.app.get('/test/blob/foo/f/sources')
  38. # No git repo associated
  39. self.assertEqual(output.status_code, 404)
  40. def test_view_file_no_git_content(self):
  41. """ Test the view_file when the file doesn't exist. """
  42. tests.create_projects(self.session)
  43. tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
  44. output = self.app.get('/test/blob/foo/f/sources')
  45. self.assertEqual(output.status_code, 404)
  46. class PagureFlaskRepoViewFiletests(LocalBasetests):
  47. """ Tests for view_file endpoint of the flask pagure app """
  48. def setUp(self):
  49. """ Set up the environnment, ran before every tests. """
  50. super(PagureFlaskRepoViewFiletests, self).setUp()
  51. tests.create_projects(self.session)
  52. tests.create_projects_git(
  53. os.path.join(self.path, 'repos'), bare=True)
  54. # Add some content to the git repo
  55. tests.add_content_git_repo(
  56. os.path.join(self.path, 'repos', 'test.git'))
  57. tests.add_readme_git_repo(
  58. os.path.join(self.path, 'repos', 'test.git'))
  59. tests.add_binary_git_repo(
  60. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  61. tests.add_binary_git_repo(
  62. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  63. def test_view_file_invalid_file(self):
  64. """ Test the view_file when the file doesn't exist. """
  65. output = self.app.get('/test/blob/master/foofile')
  66. self.assertEqual(output.status_code, 404)
  67. output = self.app.get('/test/blob/sources/f/testfoo.jpg')
  68. self.assertEqual(output.status_code, 404)
  69. output = self.app.get('/test/blob/master/f/folder1/testfoo.jpg')
  70. self.assertEqual(output.status_code, 404)
  71. def test_view_file_basic_text(self):
  72. """ Test the view_file with a basic text file. """
  73. output = self.app.get('/test/blob/master/f/sources')
  74. self.assertEqual(output.status_code, 200)
  75. output_text = output.get_data(as_text=True)
  76. self.assertIn(
  77. '<pre class="syntaxhighlightblock"><code>foo\n bar</code></pre>',
  78. output_text
  79. )
  80. def test_view_file_empty_file(self):
  81. """ Test the view_file with an empty file. """
  82. # Empty files should also be displayed
  83. tests.add_content_to_git(
  84. os.path.join(self.path, 'repos', 'test.git'),
  85. filename="emptyfile.md",
  86. content="")
  87. output = self.app.get('/test/blob/master/f/emptyfile.md')
  88. self.assertEqual(output.status_code, 200)
  89. output_text = output.get_data(as_text=True)
  90. self.assertIn(
  91. '<a class="btn btn-secondary btn-sm" '
  92. 'href="/test/raw/master/f/emptyfile.md" '
  93. 'title="View as raw">Raw</a>', output_text)
  94. self.assertIn(
  95. '<div class="m-2">\n'
  96. ' \n </div>', output_text)
  97. def test_view_file_binary_file(self):
  98. """ Test the view_file with a binary file. """
  99. # View what's supposed to be an image
  100. output = self.app.get('/test/blob/master/f/test.jpg')
  101. self.assertEqual(output.status_code, 200)
  102. output_text = output.get_data(as_text=True)
  103. self.assertIn(
  104. 'Binary files cannot be rendered.<br/>', output_text)
  105. self.assertIn(
  106. '<a href="/test/raw/master/f/test.jpg">view the raw version',
  107. output_text)
  108. def test_view_file_by_commit(self):
  109. """ Test the view_file in a specific commit. """
  110. # View by commit id
  111. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  112. commit = repo.revparse_single('HEAD')
  113. output = self.app.get('/test/blob/%s/f/test.jpg' % commit.oid.hex)
  114. self.assertEqual(output.status_code, 200)
  115. output_text = output.get_data(as_text=True)
  116. self.assertIn(
  117. 'Binary files cannot be rendered.<br/>', output_text)
  118. self.assertIn('/f/test.jpg">view the raw version', output_text)
  119. def test_view_file_by_name(self):
  120. """ Test the view_file via a image name. """
  121. # View by image name -- somehow we support this
  122. output = self.app.get('/test/blob/sources/f/test.jpg')
  123. self.assertEqual(output.status_code, 200)
  124. output_text = output.get_data(as_text=True)
  125. self.assertIn(
  126. 'Binary files cannot be rendered.<br/>', output_text)
  127. self.assertIn('/f/test.jpg">view the raw version', output_text)
  128. def test_view_file_binary_file2(self):
  129. """ Test the view_file with a binary file (2). """
  130. # View binary file
  131. output = self.app.get('/test/blob/sources/f/test_binary')
  132. self.assertEqual(output.status_code, 200)
  133. output_text = output.get_data(as_text=True)
  134. self.assertIn('/f/test_binary">view the raw version', output_text)
  135. self.assertTrue(
  136. 'Binary files cannot be rendered.<br/>'
  137. in output_text)
  138. def test_view_file_for_folder(self):
  139. """ Test the view_file with a folder. """
  140. # View folder
  141. output = self.app.get('/test/blob/master/f/folder1')
  142. self.assertEqual(output.status_code, 200)
  143. output_text = output.get_data(as_text=True)
  144. self.assertIn(
  145. '<span class="fa fa-folder text-muted"></span>',
  146. output_text)
  147. self.assertIn('<title>Tree - test - Pagure</title>', output_text)
  148. self.assertIn(
  149. '<a href="/test/blob/master/f/folder1/folder2">', output_text)
  150. def test_view_file_nested_file(self):
  151. """ Test the view_file with a nested file. """
  152. # Verify the nav links correctly when viewing a nested folder/file.
  153. output = self.app.get('/test/blob/master/f/folder1/folder2/file')
  154. self.assertEqual(output.status_code, 200)
  155. output_text = output.get_data(as_text=True)
  156. self.assertIn(
  157. '<li class="breadcrumb-item"><a '
  158. 'href="/test/blob/master/f/folder1/folder2">\n '
  159. '<span class="fa fa-folder"></span>&nbsp; folder2</a>'
  160. '\n </li>', output_text)
  161. def test_view_file_non_ascii_file(self):
  162. """ Test the view_file with a non-ascii file name. """
  163. # View file with a non-ascii name
  164. tests.add_commit_git_repo(
  165. os.path.join(self.path, 'repos', 'test.git'),
  166. ncommits=1, filename='Šource')
  167. output = self.app.get('/test/blob/master/f/Šource')
  168. self.assertEqual(output.status_code, 200)
  169. output_text = output.get_data(as_text=True)
  170. self.assertEqual(output.headers['Content-Type'].lower(),
  171. 'text/html; charset=utf-8')
  172. self.assertIn('</span>&nbsp; Šource', output_text)
  173. self.assertIn(
  174. '<pre class="syntaxhighlightblock"><code>Row 0\n</code></pre>',
  175. output_text
  176. )
  177. def test_view_file_fork_and_edit_logged_out(self):
  178. """ Test the view_file fork and edit button presence when logged
  179. out.
  180. """
  181. # not logged in, no edit button but fork & edit is there
  182. output = self.app.get('/test/blob/master/f/sources')
  183. self.assertEqual(output.status_code, 200)
  184. output_text = output.get_data(as_text=True)
  185. self.assertNotIn(
  186. '<a class="btn btn-sm btn-secondary" '
  187. 'href="/test/edit/master/f/sources" title="Edit file">'
  188. 'Edit</a>', output_text)
  189. self.assertIn(
  190. 'onclick="fork_project.submit();">\n '
  191. ' Fork and Edit', output_text)
  192. def test_view_file_fork_and_edit_logged_in(self):
  193. """ Test the view_file fork and edit button presence when logged
  194. in.
  195. """
  196. # logged in, both buttons are there
  197. user = tests.FakeUser(username='pingou')
  198. with tests.user_set(self.app.application, user):
  199. output = self.app.get('/test/blob/master/f/sources')
  200. self.assertEqual(output.status_code, 200)
  201. output_text = output.get_data(as_text=True)
  202. self.assertIn(
  203. '<a class="btn btn-sm btn-secondary" '
  204. 'href="/test/edit/master/f/sources" title="Edit file">'
  205. 'Edit</a>', output_text)
  206. self.assertIn(
  207. 'onclick="fork_project.submit();">\n '
  208. ' Fork and Edit', output_text)
  209. class PagureFlaskRepoViewFileForktests(LocalBasetests):
  210. """ Tests for view_file endpoint of the flask pagure app for a fork """
  211. def setUp(self):
  212. """ Set up the environnment, ran before every tests. """
  213. super(PagureFlaskRepoViewFileForktests, self).setUp()
  214. tests.create_projects(self.session)
  215. tests.create_projects_git(
  216. os.path.join(self.path, 'repos'), bare=True)
  217. # Add some content to the git repo
  218. tests.add_content_git_repo(
  219. os.path.join(self.path, 'repos', 'test.git'))
  220. tests.add_readme_git_repo(
  221. os.path.join(self.path, 'repos', 'test.git'))
  222. tests.add_binary_git_repo(
  223. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  224. tests.add_binary_git_repo(
  225. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  226. # Add a fork of a fork
  227. item = pagure.lib.model.Project(
  228. user_id=1, # pingou
  229. name='test',
  230. description='test project #3',
  231. is_fork=True,
  232. parent_id=1,
  233. hook_token='aaabbbppp',
  234. )
  235. self.session.add(item)
  236. self.session.commit()
  237. tests.add_content_git_repo(
  238. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  239. tests.add_readme_git_repo(
  240. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  241. tests.add_commit_git_repo(
  242. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'),
  243. ncommits=10)
  244. def test_view_file_nested_file_in_fork(self):
  245. """ Test the view_file with a nested file in fork. """
  246. # Verify the nav links correctly when viewing a file/folder in a fork.
  247. output = self.app.get(
  248. '/fork/pingou/test/blob/master/f/folder1/folder2/file')
  249. self.assertEqual(output.status_code, 200)
  250. self.assertIn(
  251. '<li class="breadcrumb-item"><a '
  252. 'href="/fork/pingou/test/blob/master/f/folder1/folder2">'
  253. '\n <span class="fa fa-folder"></span>&nbsp; folder2'
  254. '</a>\n </li>', output.get_data(as_text=True))
  255. def test_view_file_in_branch_in_fork(self):
  256. """ Test the view_file in a specific branch of a fork. """
  257. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  258. self.assertEqual(output.status_code, 200)
  259. output_text = output.get_data(as_text=True)
  260. self.assertIn(
  261. '<pre class="syntaxhighlightblock"><code>foo\n barRow 0\n'
  262. 'Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n'
  263. 'Row 9\n</code></pre>', output_text
  264. )
  265. def test_view_file_fork_and_edit_on_fork_logged_out(self):
  266. """ Test the view_file on a text file on a fork when logged out. """
  267. # not logged in, no edit button but fork & edit is there
  268. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  269. self.assertEqual(output.status_code, 200)
  270. output_text = output.get_data(as_text=True)
  271. self.assertNotIn(
  272. '<a class="btn btn-sm btn-secondary" '
  273. 'href="/test/edit/master/f/sources" title="Edit file">'
  274. 'Edit</a>', output_text)
  275. self.assertIn(
  276. 'onclick="fork_project.submit();">\n '
  277. ' Fork and Edit', output_text)
  278. def test_view_file_fork_and_edit_on_your_fork(self):
  279. """ Test the view_file on a text file on your fork when logged in.
  280. """
  281. # logged in, but it's your own fork, so just edit button is there
  282. user = tests.FakeUser(username='pingou')
  283. with tests.user_set(self.app.application, user):
  284. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  285. self.assertEqual(output.status_code, 200)
  286. output_text = output.get_data(as_text=True)
  287. self.assertIn(
  288. '<a class="btn btn-sm btn-secondary" '
  289. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  290. 'Edit</a>', output_text)
  291. self.assertNotIn(
  292. 'onclick="fork_project.submit();">\n '
  293. ' Fork and Edit', output_text)
  294. def test_view_file_fork_and_edit_on_a_fork(self):
  295. """ Test the view_file on a text file on somone else's fork when
  296. logged in.
  297. """
  298. # logged in, but it's not your fork, so only fork and edit button
  299. # is there
  300. user = tests.FakeUser(username='foo')
  301. with tests.user_set(self.app.application, user):
  302. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  303. self.assertEqual(output.status_code, 200)
  304. output_text = output.get_data(as_text=True)
  305. self.assertNotIn(
  306. '<a class="btn btn-sm btn-secondary" '
  307. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  308. 'Edit</a>', output_text)
  309. self.assertIn(
  310. 'onclick="fork_project.submit();">\n '
  311. ' Fork and Edit', output_text)
  312. def test_view_file_fork_and_edit_on_project(self):
  313. """ Test the view_file on a text file on somone else's fork when
  314. logged in.
  315. """
  316. # logged in and seeing the project you forked
  317. user = tests.FakeUser(username='pingou')
  318. with tests.user_set(self.app.application, user):
  319. output = self.app.get('/test/blob/master/f/sources')
  320. self.assertEqual(output.status_code, 200)
  321. output_text = output.get_data(as_text=True)
  322. self.assertIn(
  323. '<a class="btn btn-sm btn-secondary" '
  324. 'href="/test/edit/master/f/sources" title="Edit file">'
  325. 'Edit</a>', output_text)
  326. self.assertIn(
  327. 'onclick="fork_project.submit();">\n '
  328. ' Edit in your fork', output_text)
  329. if __name__ == '__main__':
  330. unittest.main(verbosity=2)