test_pagure_flask_ui_repo_view_file.py 15 KB

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