test_pagure_flask_ui_repo_view_file.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import pygit2
  12. sys.path.insert(
  13. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  14. )
  15. import pagure.lib.model # 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. )
  28. class PagureFlaskRepoViewFileSimpletests(LocalBasetests):
  29. """ Tests for view_file endpoint of the flask pagure app """
  30. def test_view_file_no_project(self):
  31. """ Test the view_file when the project is unknown. """
  32. output = self.app.get("/foo/blob/foo/f/sources")
  33. # No project registered in the DB
  34. self.assertEqual(output.status_code, 404)
  35. def test_view_file_no_git(self):
  36. """ Test the view_file when the project has no git repo. """
  37. tests.create_projects(self.session)
  38. output = self.app.get("/test/blob/foo/f/sources")
  39. # No git repo associated
  40. self.assertEqual(output.status_code, 404)
  41. def test_view_file_no_git_content(self):
  42. """ Test the view_file when the file doesn't exist. """
  43. tests.create_projects(self.session)
  44. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  45. output = self.app.get("/test/blob/foo/f/sources")
  46. self.assertEqual(output.status_code, 404)
  47. class PagureFlaskRepoViewFiletests(LocalBasetests):
  48. """ Tests for view_file endpoint of the flask pagure app """
  49. def setUp(self):
  50. """ Set up the environnment, ran before every tests. """
  51. super(PagureFlaskRepoViewFiletests, self).setUp()
  52. tests.create_projects(self.session)
  53. tests.create_projects_git(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. )
  58. tests.add_readme_git_repo(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. )
  62. tests.add_binary_git_repo(
  63. os.path.join(self.path, "repos", "test.git"), "test_binary"
  64. )
  65. def test_view_file_invalid_file(self):
  66. """ Test the view_file when the file doesn't exist. """
  67. output = self.app.get("/test/blob/master/foofile")
  68. self.assertEqual(output.status_code, 404)
  69. output = self.app.get("/test/blob/sources/f/testfoo.jpg")
  70. self.assertEqual(output.status_code, 404)
  71. output = self.app.get("/test/blob/master/f/folder1/testfoo.jpg")
  72. self.assertEqual(output.status_code, 404)
  73. def test_view_file_basic_text(self):
  74. """ Test the view_file with a basic text file. """
  75. output = self.app.get("/test/blob/master/f/sources")
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn(
  79. '<pre class="syntaxhighlightblock">'
  80. '<code class="lang-plaintext">foo\n bar</code></pre>',
  81. output_text,
  82. )
  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. )
  91. output = self.app.get("/test/blob/master/f/emptyfile.md")
  92. self.assertEqual(output.status_code, 200)
  93. output_text = output.get_data(as_text=True)
  94. self.assertIn(
  95. '<a class="btn btn-secondary btn-sm" '
  96. 'href="/test/raw/master/f/emptyfile.md" '
  97. 'title="View as raw">Raw</a>',
  98. output_text,
  99. )
  100. self.assertIn(
  101. '<div class="m-2">\n' " \n </div>", output_text
  102. )
  103. def test_view_file_binary_file(self):
  104. """ Test the view_file with a binary file. """
  105. # View what's supposed to be an image
  106. output = self.app.get("/test/blob/master/f/test.jpg")
  107. self.assertEqual(output.status_code, 200)
  108. output_text = output.get_data(as_text=True)
  109. self.assertIn("Binary files cannot be rendered.<br/>", output_text)
  110. self.assertIn(
  111. '<a href="/test/raw/master/f/test.jpg">view the raw version',
  112. output_text,
  113. )
  114. def test_view_file_by_commit(self):
  115. """ Test the view_file in a specific commit. """
  116. # View by commit id
  117. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  118. commit = repo.revparse_single("HEAD")
  119. output = self.app.get("/test/blob/%s/f/test.jpg" % commit.oid.hex)
  120. self.assertEqual(output.status_code, 200)
  121. output_text = output.get_data(as_text=True)
  122. self.assertIn("Binary files cannot be rendered.<br/>", output_text)
  123. self.assertIn('/f/test.jpg">view the raw version', output_text)
  124. def test_view_file_by_name(self):
  125. """ Test the view_file via a image name. """
  126. # View by image name -- somehow we support this
  127. output = self.app.get("/test/blob/sources/f/test.jpg")
  128. self.assertEqual(output.status_code, 200)
  129. output_text = output.get_data(as_text=True)
  130. self.assertIn("Binary files cannot be rendered.<br/>", output_text)
  131. self.assertIn('/f/test.jpg">view the raw version', output_text)
  132. def test_view_file_binary_file2(self):
  133. """ Test the view_file with a binary file (2). """
  134. # View binary file
  135. output = self.app.get("/test/blob/sources/f/test_binary")
  136. self.assertEqual(output.status_code, 200)
  137. output_text = output.get_data(as_text=True)
  138. self.assertIn('/f/test_binary">view the raw version', output_text)
  139. self.assertTrue("Binary files cannot be rendered.<br/>" in output_text)
  140. def test_view_file_for_folder(self):
  141. """ Test the view_file with a folder. """
  142. # View folder
  143. output = self.app.get("/test/blob/master/f/folder1")
  144. self.assertEqual(output.status_code, 200)
  145. output_text = output.get_data(as_text=True)
  146. self.assertIn(
  147. '<span class="fa fa-folder text-muted"></span>', output_text
  148. )
  149. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  150. self.assertIn(
  151. '<a href="/test/blob/master/f/folder1/folder2">', output_text
  152. )
  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 class="breadcrumb-item"><a '
  161. 'href="/test/blob/master/f/folder1/folder2">\n '
  162. '<span class="fa fa-folder"></span>&nbsp; folder2</a>'
  163. "\n </li>",
  164. output_text,
  165. )
  166. def test_view_file_non_ascii_file(self):
  167. """ Test the view_file with a non-ascii file name. """
  168. # View file with a non-ascii name
  169. tests.add_commit_git_repo(
  170. os.path.join(self.path, "repos", "test.git"),
  171. ncommits=1,
  172. filename="Šource",
  173. )
  174. output = self.app.get("/test/blob/master/f/Šource")
  175. self.assertEqual(output.status_code, 200)
  176. output_text = output.get_data(as_text=True)
  177. self.assertEqual(
  178. output.headers["Content-Type"].lower(), "text/html; charset=utf-8"
  179. )
  180. self.assertIn("</span>&nbsp; Šource", output_text)
  181. self.assertIn(
  182. '<pre class="syntaxhighlightblock">'
  183. '<code class="lang-plaintext">Row 0\n</code></pre>',
  184. 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>",
  198. output_text,
  199. )
  200. self.assertIn(
  201. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  202. "\n Fork and Edit",
  203. output_text,
  204. )
  205. def test_view_file_fork_and_edit_logged_in(self):
  206. """ Test the view_file fork and edit button presence when logged
  207. in.
  208. """
  209. # logged in, both buttons are there
  210. user = tests.FakeUser(username="pingou")
  211. with tests.user_set(self.app.application, user):
  212. output = self.app.get("/test/blob/master/f/sources")
  213. self.assertEqual(output.status_code, 200)
  214. output_text = output.get_data(as_text=True)
  215. self.assertIn(
  216. '<a class="btn btn-sm btn-secondary" '
  217. 'href="/test/edit/master/f/sources" title="Edit file">'
  218. "Edit</a>",
  219. output_text,
  220. )
  221. self.assertIn(
  222. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  223. "\n Fork and Edit",
  224. output_text,
  225. )
  226. class PagureFlaskRepoViewFileForktests(LocalBasetests):
  227. """ Tests for view_file endpoint of the flask pagure app for a fork """
  228. def setUp(self):
  229. """ Set up the environnment, ran before every tests. """
  230. super(PagureFlaskRepoViewFileForktests, self).setUp()
  231. tests.create_projects(self.session)
  232. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  233. # Add some content to the git repo
  234. tests.add_content_git_repo(
  235. os.path.join(self.path, "repos", "test.git")
  236. )
  237. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  238. tests.add_binary_git_repo(
  239. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  240. )
  241. tests.add_binary_git_repo(
  242. os.path.join(self.path, "repos", "test.git"), "test_binary"
  243. )
  244. # Add a fork of a fork
  245. item = pagure.lib.model.Project(
  246. user_id=1, # pingou
  247. name="test",
  248. description="test project #3",
  249. is_fork=True,
  250. parent_id=1,
  251. hook_token="aaabbbppp",
  252. )
  253. self.session.add(item)
  254. self.session.commit()
  255. tests.add_content_git_repo(
  256. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  257. )
  258. tests.add_readme_git_repo(
  259. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  260. )
  261. tests.add_commit_git_repo(
  262. os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
  263. ncommits=10,
  264. )
  265. def test_view_file_nested_file_in_fork(self):
  266. """ Test the view_file with a nested file in fork. """
  267. # Verify the nav links correctly when viewing a file/folder in a fork.
  268. output = self.app.get(
  269. "/fork/pingou/test/blob/master/f/folder1/folder2/file"
  270. )
  271. self.assertEqual(output.status_code, 200)
  272. self.assertIn(
  273. '<li class="breadcrumb-item"><a '
  274. 'href="/fork/pingou/test/blob/master/f/folder1/folder2">'
  275. '\n <span class="fa fa-folder"></span>&nbsp; folder2'
  276. "</a>\n </li>",
  277. output.get_data(as_text=True),
  278. )
  279. def test_view_file_in_branch_in_fork(self):
  280. """ Test the view_file in a specific branch of a fork. """
  281. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  282. self.assertEqual(output.status_code, 200)
  283. output_text = output.get_data(as_text=True)
  284. self.assertIn(
  285. '<pre class="syntaxhighlightblock"><code class="lang-plaintext">'
  286. "foo\n barRow 0\n"
  287. "Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n"
  288. "Row 9\n</code></pre>",
  289. output_text,
  290. )
  291. def test_view_file_fork_and_edit_on_fork_logged_out(self):
  292. """ Test the view_file on a text file on a fork when logged out. """
  293. # not logged in, no edit button but fork & edit is there
  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.assertNotIn(
  298. '<a class="btn btn-sm btn-secondary" '
  299. 'href="/test/edit/master/f/sources" title="Edit file">'
  300. "Edit</a>",
  301. output_text,
  302. )
  303. self.assertIn(
  304. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  305. "\n Fork and Edit",
  306. output_text,
  307. )
  308. def test_view_file_fork_and_edit_on_your_fork(self):
  309. """ Test the view_file on a text file on your fork when logged in.
  310. """
  311. # logged in, but it's your own fork, so just edit button is there
  312. user = tests.FakeUser(username="pingou")
  313. with tests.user_set(self.app.application, user):
  314. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  315. self.assertEqual(output.status_code, 200)
  316. output_text = output.get_data(as_text=True)
  317. self.assertIn(
  318. '<a class="btn btn-sm btn-secondary" '
  319. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  320. "Edit</a>",
  321. output_text,
  322. )
  323. self.assertNotIn(
  324. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  325. "\n Fork and Edit",
  326. output_text,
  327. )
  328. def test_view_file_fork_and_edit_on_a_fork(self):
  329. """ Test the view_file on a text file on somone else's fork when
  330. logged in.
  331. """
  332. # logged in, but it's not your fork, so only fork and edit button
  333. # is there
  334. user = tests.FakeUser(username="foo")
  335. with tests.user_set(self.app.application, user):
  336. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  337. self.assertEqual(output.status_code, 200)
  338. output_text = output.get_data(as_text=True)
  339. self.assertNotIn(
  340. '<a class="btn btn-sm btn-secondary" '
  341. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  342. "Edit</a>",
  343. output_text,
  344. )
  345. self.assertIn(
  346. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  347. "\n Fork and Edit",
  348. output_text,
  349. )
  350. def test_view_file_fork_and_edit_on_project(self):
  351. """ Test the view_file on a text file on somone else's fork when
  352. logged in.
  353. """
  354. # logged in and seeing the project you forked
  355. user = tests.FakeUser(username="pingou")
  356. with tests.user_set(self.app.application, user):
  357. output = self.app.get("/test/blob/master/f/sources")
  358. self.assertEqual(output.status_code, 200)
  359. output_text = output.get_data(as_text=True)
  360. self.assertIn(
  361. '<a class="btn btn-sm btn-secondary" '
  362. 'href="/test/edit/master/f/sources" title="Edit file">'
  363. "Edit</a>",
  364. output_text,
  365. )
  366. self.assertIn(
  367. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  368. "\n Edit in your fork",
  369. output_text,
  370. )
  371. if __name__ == "__main__":
  372. unittest.main(verbosity=2)