test_pagure_flask_ui_repo_view_file.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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.assertTrue('<table class="code_table">' in output_text)
  77. self.assertTrue(
  78. '<tr><td class="cell1"><a id="_1" href="#_1" '
  79. 'data-line-number="1"></a></td>'
  80. in output_text)
  81. self.assertTrue(
  82. '<td class="cell2"><pre> bar</pre></td>' in output_text)
  83. def test_view_file_empty_file(self):
  84. """ Test the view_file with an empty file. """
  85. # Empty files should also be displayed
  86. tests.add_content_to_git(
  87. os.path.join(self.path, 'repos', 'test.git'),
  88. filename="emptyfile.md",
  89. content="")
  90. output = self.app.get('/test/blob/master/f/emptyfile.md')
  91. self.assertEqual(output.status_code, 200)
  92. output_text = output.get_data(as_text=True)
  93. self.assertIn(
  94. '<a class="btn btn-secondary btn-sm" '
  95. 'href="/test/raw/master/f/emptyfile.md" '
  96. 'title="View as raw">Raw</a>', output_text)
  97. self.assertIn(
  98. '<div class="m-2">\n'
  99. ' \n </div>', output_text)
  100. def test_view_file_binary_file(self):
  101. """ Test the view_file with a binary file. """
  102. # View what's supposed to be an image
  103. output = self.app.get('/test/blob/master/f/test.jpg')
  104. self.assertEqual(output.status_code, 200)
  105. output_text = output.get_data(as_text=True)
  106. self.assertIn(
  107. 'Binary files cannot be rendered.<br/>', output_text)
  108. self.assertIn(
  109. '<a href="/test/raw/master/f/test.jpg">view the raw version',
  110. output_text)
  111. def test_view_file_by_commit(self):
  112. """ Test the view_file in a specific commit. """
  113. # View by commit id
  114. repo = pygit2.Repository(os.path.join(self.path, 'repos', 'test.git'))
  115. commit = repo.revparse_single('HEAD')
  116. output = self.app.get('/test/blob/%s/f/test.jpg' % commit.oid.hex)
  117. self.assertEqual(output.status_code, 200)
  118. output_text = output.get_data(as_text=True)
  119. self.assertIn(
  120. 'Binary files cannot be rendered.<br/>', output_text)
  121. self.assertIn('/f/test.jpg">view the raw version', output_text)
  122. def test_view_file_by_name(self):
  123. """ Test the view_file via a image name. """
  124. # View by image name -- somehow we support this
  125. output = self.app.get('/test/blob/sources/f/test.jpg')
  126. self.assertEqual(output.status_code, 200)
  127. output_text = output.get_data(as_text=True)
  128. self.assertIn(
  129. 'Binary files cannot be rendered.<br/>', output_text)
  130. self.assertIn('/f/test.jpg">view the raw version', output_text)
  131. def test_view_file_binary_file2(self):
  132. """ Test the view_file with a binary file (2). """
  133. # View binary file
  134. output = self.app.get('/test/blob/sources/f/test_binary')
  135. self.assertEqual(output.status_code, 200)
  136. output_text = output.get_data(as_text=True)
  137. self.assertIn('/f/test_binary">view the raw version', output_text)
  138. self.assertTrue(
  139. 'Binary files cannot be rendered.<br/>'
  140. in output_text)
  141. def test_view_file_for_folder(self):
  142. """ Test the view_file with a folder. """
  143. # View folder
  144. output = self.app.get('/test/blob/master/f/folder1')
  145. self.assertEqual(output.status_code, 200)
  146. output_text = output.get_data(as_text=True)
  147. self.assertIn(
  148. '<span class="oi text-muted" data-glyph="folder"></span>',
  149. output_text)
  150. self.assertIn('<title>Tree - test - Pagure</title>', output_text)
  151. self.assertIn(
  152. '<a href="/test/blob/master/f/folder1/folder2">', output_text)
  153. def test_view_file_nested_file(self):
  154. """ Test the view_file with a nested file. """
  155. # Verify the nav links correctly when viewing a nested folder/file.
  156. output = self.app.get('/test/blob/master/f/folder1/folder2/file')
  157. self.assertEqual(output.status_code, 200)
  158. output_text = output.get_data(as_text=True)
  159. self.assertIn(
  160. '<li><a href="/test/blob/master/f/folder1/folder2">\n'
  161. ' <span class="oi" data-glyph="folder">'
  162. '</span>&nbsp; folder2</a>\n'
  163. ' </li>', output_text)
  164. def test_view_file_non_ascii_file(self):
  165. """ Test the view_file with a non-ascii file name. """
  166. # View file with a non-ascii name
  167. tests.add_commit_git_repo(
  168. os.path.join(self.path, 'repos', 'test.git'),
  169. ncommits=1, filename='Šource')
  170. output = self.app.get('/test/blob/master/f/Šource')
  171. self.assertEqual(output.status_code, 200)
  172. output_text = output.get_data(as_text=True)
  173. self.assertEqual(output.headers['Content-Type'].lower(),
  174. 'text/html; charset=utf-8')
  175. self.assertIn('</span>&nbsp; Šource', output_text)
  176. self.assertIn('<table class="code_table">', output_text)
  177. self.assertIn(
  178. '<tr><td class="cell1"><a id="_1" href="#_1" '
  179. 'data-line-number="1"></a></td>', output_text)
  180. self.assertTrue(
  181. '<td class="cell2"><pre><span></span>Row 0</pre></td>'
  182. in output_text
  183. or
  184. '<td class="cell2"><pre>Row 0</pre></td>' in output_text
  185. )
  186. def test_view_file_fork_and_edit_logged_out(self):
  187. """ Test the view_file fork and edit button presence when logged
  188. out.
  189. """
  190. # not logged in, no edit button but fork & edit is there
  191. output = self.app.get('/test/blob/master/f/sources')
  192. self.assertEqual(output.status_code, 200)
  193. output_text = output.get_data(as_text=True)
  194. self.assertNotIn(
  195. '<a class="btn btn-sm btn-secondary" '
  196. 'href="/test/edit/master/f/sources" title="Edit file">'
  197. 'Edit</a>', output_text)
  198. self.assertIn(
  199. 'onclick="fork_project.submit();">\n '
  200. ' Fork and Edit', output_text)
  201. def test_view_file_fork_and_edit_logged_in(self):
  202. """ Test the view_file fork and edit button presence when logged
  203. in.
  204. """
  205. # logged in, both buttons are there
  206. user = tests.FakeUser(username='pingou')
  207. with tests.user_set(self.app.application, user):
  208. output = self.app.get('/test/blob/master/f/sources')
  209. self.assertEqual(output.status_code, 200)
  210. output_text = output.get_data(as_text=True)
  211. self.assertIn(
  212. '<a class="btn btn-sm btn-secondary" '
  213. 'href="/test/edit/master/f/sources" title="Edit file">'
  214. 'Edit</a>', output_text)
  215. self.assertIn(
  216. 'onclick="fork_project.submit();">\n '
  217. ' Fork and Edit', output_text)
  218. class PagureFlaskRepoViewFileForktests(LocalBasetests):
  219. """ Tests for view_file endpoint of the flask pagure app for a fork """
  220. def setUp(self):
  221. """ Set up the environnment, ran before every tests. """
  222. super(PagureFlaskRepoViewFileForktests, self).setUp()
  223. tests.create_projects(self.session)
  224. tests.create_projects_git(
  225. os.path.join(self.path, 'repos'), bare=True)
  226. # Add some content to the git repo
  227. tests.add_content_git_repo(
  228. os.path.join(self.path, 'repos', 'test.git'))
  229. tests.add_readme_git_repo(
  230. os.path.join(self.path, 'repos', 'test.git'))
  231. tests.add_binary_git_repo(
  232. os.path.join(self.path, 'repos', 'test.git'), 'test.jpg')
  233. tests.add_binary_git_repo(
  234. os.path.join(self.path, 'repos', 'test.git'), 'test_binary')
  235. # Add a fork of a fork
  236. item = pagure.lib.model.Project(
  237. user_id=1, # pingou
  238. name='test',
  239. description='test project #3',
  240. is_fork=True,
  241. parent_id=1,
  242. hook_token='aaabbbppp',
  243. )
  244. self.session.add(item)
  245. self.session.commit()
  246. tests.add_content_git_repo(
  247. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  248. tests.add_readme_git_repo(
  249. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'))
  250. tests.add_commit_git_repo(
  251. os.path.join(self.path, 'repos', 'forks', 'pingou', 'test.git'),
  252. ncommits=10)
  253. def test_view_file_nested_file_in_fork(self):
  254. """ Test the view_file with a nested file in fork. """
  255. # Verify the nav links correctly when viewing a file/folder in a fork.
  256. output = self.app.get(
  257. '/fork/pingou/test/blob/master/f/folder1/folder2/file')
  258. self.assertEqual(output.status_code, 200)
  259. self.assertIn(
  260. '<li><a href="/fork/pingou/test/blob/master/f/folder1/folder2">\n'
  261. ' <span class="oi" data-glyph="folder"></span>&nbsp; '
  262. 'folder2</a>\n </li>', output.get_data(as_text=True))
  263. def test_view_file_in_branch_in_fork(self):
  264. """ Test the view_file in a specific branch of a fork. """
  265. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  266. self.assertEqual(output.status_code, 200)
  267. output_text = output.get_data(as_text=True)
  268. self.assertIn('<table class="code_table">', output_text)
  269. self.assertIn(
  270. '<tr><td class="cell1"><a id="_1" href="#_1" '
  271. 'data-line-number="1"></a></td>',
  272. output_text)
  273. self.assertIn(
  274. '<td class="cell2"><pre> barRow 0</pre></td>', output_text)
  275. def test_view_file_fork_and_edit_on_fork_logged_out(self):
  276. """ Test the view_file on a text file on a fork when logged out. """
  277. # not logged in, no edit button but fork & edit is there
  278. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  279. self.assertEqual(output.status_code, 200)
  280. output_text = output.get_data(as_text=True)
  281. self.assertNotIn(
  282. '<a class="btn btn-sm btn-secondary" '
  283. 'href="/test/edit/master/f/sources" title="Edit file">'
  284. 'Edit</a>', output_text)
  285. self.assertIn(
  286. 'onclick="fork_project.submit();">\n '
  287. ' Fork and Edit', output_text)
  288. def test_view_file_fork_and_edit_on_your_fork(self):
  289. """ Test the view_file on a text file on your fork when logged in.
  290. """
  291. # logged in, but it's your own fork, so just edit button is there
  292. user = tests.FakeUser(username='pingou')
  293. with tests.user_set(self.app.application, user):
  294. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  295. self.assertEqual(output.status_code, 200)
  296. output_text = output.get_data(as_text=True)
  297. self.assertIn(
  298. '<a class="btn btn-sm btn-secondary" '
  299. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  300. 'Edit</a>', output_text)
  301. self.assertNotIn(
  302. 'onclick="fork_project.submit();">\n '
  303. ' Fork and Edit', output_text)
  304. def test_view_file_fork_and_edit_on_a_fork(self):
  305. """ Test the view_file on a text file on somone else's fork when
  306. logged in.
  307. """
  308. # logged in, but it's not your fork, so only fork and edit button
  309. # is there
  310. user = tests.FakeUser(username='foo')
  311. with tests.user_set(self.app.application, user):
  312. output = self.app.get('/fork/pingou/test/blob/master/f/sources')
  313. self.assertEqual(output.status_code, 200)
  314. output_text = output.get_data(as_text=True)
  315. self.assertNotIn(
  316. '<a class="btn btn-sm btn-secondary" '
  317. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  318. 'Edit</a>', output_text)
  319. self.assertIn(
  320. 'onclick="fork_project.submit();">\n '
  321. ' Fork and Edit', output_text)
  322. def test_view_file_fork_and_edit_on_project(self):
  323. """ Test the view_file on a text file on somone else's fork when
  324. logged in.
  325. """
  326. # logged in and seeing the project you forked
  327. user = tests.FakeUser(username='pingou')
  328. with tests.user_set(self.app.application, user):
  329. output = self.app.get('/test/blob/master/f/sources')
  330. self.assertEqual(output.status_code, 200)
  331. output_text = output.get_data(as_text=True)
  332. self.assertIn(
  333. '<a class="btn btn-sm btn-secondary" '
  334. 'href="/test/edit/master/f/sources" title="Edit file">'
  335. 'Edit</a>', output_text)
  336. self.assertIn(
  337. 'onclick="fork_project.submit();">\n '
  338. ' Edit in your fork', output_text)
  339. if __name__ == '__main__':
  340. unittest.main(verbosity=2)