test_pagure_flask_ui_repo_view_file.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. self.assertNotIn(b"<html", output.data)
  109. def test_view_file_by_commit(self):
  110. """ Test the view_file in a specific commit. """
  111. # View by commit id
  112. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  113. commit = repo.revparse_single("HEAD")
  114. output = self.app.get("/test/blob/%s/f/test.jpg" % commit.oid.hex)
  115. self.assertEqual(output.status_code, 200)
  116. self.assertNotIn(b"<html", output.data)
  117. def test_view_file_invalid_branch(self):
  118. """ Test the view_file via a image name. """
  119. output = self.app.get("/test/blob/sources/f/test.jpg")
  120. self.assertEqual(output.status_code, 404)
  121. def test_view_file_invalid_branch2(self):
  122. """ Test the view_file with a binary file (2). """
  123. output = self.app.get("/test/blob/sources/f/test_binary")
  124. self.assertEqual(output.status_code, 404)
  125. def test_view_file_invalid_branch(self):
  126. """ Test the view_file via a image name. """
  127. output = self.app.get("/test/blob/master/f/test.jpg")
  128. self.assertEqual(output.status_code, 200)
  129. self.assertNotIn(b"<html", output.data)
  130. def test_view_file_invalid_branch2(self):
  131. """ Test the view_file with a binary file (2). """
  132. output = self.app.get("/test/blob/master/f/test_binary")
  133. self.assertEqual(output.status_code, 200)
  134. self.assertNotIn(b"<html", output.data)
  135. def test_view_file_for_folder(self):
  136. """ Test the view_file with a folder. """
  137. # View folder
  138. output = self.app.get("/test/blob/master/f/folder1")
  139. self.assertEqual(output.status_code, 200)
  140. output_text = output.get_data(as_text=True)
  141. self.assertIn(
  142. '<span class="fa fa-folder text-muted"></span>', output_text
  143. )
  144. self.assertIn("<title>Tree - test - Pagure</title>", output_text)
  145. self.assertIn(
  146. '<a href="/test/blob/master/f/folder1/folder2">', output_text
  147. )
  148. def test_view_file_nested_file(self):
  149. """ Test the view_file with a nested file. """
  150. # Verify the nav links correctly when viewing a nested folder/file.
  151. output = self.app.get("/test/blob/master/f/folder1/folder2/file")
  152. self.assertEqual(output.status_code, 200)
  153. output_text = output.get_data(as_text=True)
  154. self.assertIn(
  155. '<li class="breadcrumb-item"><a '
  156. 'href="/test/blob/master/f/folder1/folder2">\n '
  157. '<span class="fa fa-folder"></span>&nbsp; folder2</a>'
  158. "\n </li>",
  159. output_text,
  160. )
  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,
  167. filename="Šource",
  168. )
  169. output = self.app.get("/test/blob/master/f/Šource")
  170. self.assertEqual(output.status_code, 200)
  171. output_text = output.get_data(as_text=True)
  172. self.assertEqual(
  173. output.headers["Content-Type"].lower(), "text/html; charset=utf-8"
  174. )
  175. self.assertIn("</span>&nbsp; Šource", output_text)
  176. self.assertIn(
  177. '<pre class="syntaxhighlightblock">'
  178. '<code class="lang-plaintext">Row 0\n</code></pre>',
  179. output_text,
  180. )
  181. def test_view_file_fork_and_edit_logged_out(self):
  182. """ Test the view_file fork and edit button presence when logged
  183. out.
  184. """
  185. # not logged in, no edit button but fork & edit is there
  186. output = self.app.get("/test/blob/master/f/sources")
  187. self.assertEqual(output.status_code, 200)
  188. output_text = output.get_data(as_text=True)
  189. self.assertNotIn(
  190. '<a class="btn btn-sm btn-secondary" '
  191. 'href="/test/edit/master/f/sources" title="Edit file">'
  192. "Edit</a>",
  193. output_text,
  194. )
  195. self.assertIn(
  196. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  197. "\n Fork and Edit",
  198. output_text,
  199. )
  200. def test_view_file_fork_and_edit_logged_in(self):
  201. """ Test the view_file fork and edit button presence when logged
  202. in.
  203. """
  204. # logged in, both buttons are there
  205. user = tests.FakeUser(username="pingou")
  206. with tests.user_set(self.app.application, user):
  207. output = self.app.get("/test/blob/master/f/sources")
  208. self.assertEqual(output.status_code, 200)
  209. output_text = output.get_data(as_text=True)
  210. self.assertIn(
  211. '<a class="btn btn-sm btn-secondary" '
  212. 'href="/test/edit/master/f/sources" title="Edit file">'
  213. "Edit</a>",
  214. output_text,
  215. )
  216. self.assertIn(
  217. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  218. "\n Fork and Edit",
  219. output_text,
  220. )
  221. class PagureFlaskRepoViewFileForktests(LocalBasetests):
  222. """ Tests for view_file endpoint of the flask pagure app for a fork """
  223. def setUp(self):
  224. """ Set up the environnment, ran before every tests. """
  225. super(PagureFlaskRepoViewFileForktests, self).setUp()
  226. tests.create_projects(self.session)
  227. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  228. # Add some content to the git repo
  229. tests.add_content_git_repo(
  230. os.path.join(self.path, "repos", "test.git")
  231. )
  232. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  233. tests.add_binary_git_repo(
  234. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  235. )
  236. tests.add_binary_git_repo(
  237. os.path.join(self.path, "repos", "test.git"), "test_binary"
  238. )
  239. # Add a fork of a fork
  240. item = pagure.lib.model.Project(
  241. user_id=1, # pingou
  242. name="test",
  243. description="test project #3",
  244. is_fork=True,
  245. parent_id=1,
  246. hook_token="aaabbbppp",
  247. )
  248. self.session.add(item)
  249. self.session.commit()
  250. tests.add_content_git_repo(
  251. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  252. )
  253. tests.add_readme_git_repo(
  254. os.path.join(self.path, "repos", "forks", "pingou", "test.git")
  255. )
  256. tests.add_commit_git_repo(
  257. os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
  258. ncommits=10,
  259. )
  260. def test_view_file_nested_file_in_fork(self):
  261. """ Test the view_file with a nested file in fork. """
  262. # Verify the nav links correctly when viewing a file/folder in a fork.
  263. output = self.app.get(
  264. "/fork/pingou/test/blob/master/f/folder1/folder2/file"
  265. )
  266. self.assertEqual(output.status_code, 200)
  267. self.assertIn(
  268. '<li class="breadcrumb-item"><a '
  269. 'href="/fork/pingou/test/blob/master/f/folder1/folder2">'
  270. '\n <span class="fa fa-folder"></span>&nbsp; folder2'
  271. "</a>\n </li>",
  272. output.get_data(as_text=True),
  273. )
  274. def test_view_file_in_branch_in_fork(self):
  275. """ Test the view_file in a specific branch of a fork. """
  276. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  277. self.assertEqual(output.status_code, 200)
  278. output_text = output.get_data(as_text=True)
  279. self.assertIn(
  280. '<pre class="syntaxhighlightblock"><code class="lang-plaintext">'
  281. "foo\n barRow 0\n"
  282. "Row 1\nRow 2\nRow 3\nRow 4\nRow 5\nRow 6\nRow 7\nRow 8\n"
  283. "Row 9\n</code></pre>",
  284. output_text,
  285. )
  286. def test_view_file_fork_and_edit_on_fork_logged_out(self):
  287. """ Test the view_file on a text file on a fork when logged out. """
  288. # not logged in, no edit button but fork & edit is there
  289. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  290. self.assertEqual(output.status_code, 200)
  291. output_text = output.get_data(as_text=True)
  292. self.assertNotIn(
  293. '<a class="btn btn-sm btn-secondary" '
  294. 'href="/test/edit/master/f/sources" title="Edit file">'
  295. "Edit</a>",
  296. output_text,
  297. )
  298. self.assertIn(
  299. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  300. "\n Fork and Edit",
  301. output_text,
  302. )
  303. def test_view_file_fork_and_edit_on_your_fork(self):
  304. """ Test the view_file on a text file on your fork when logged in.
  305. """
  306. # logged in, but it's your own fork, so just edit button is there
  307. user = tests.FakeUser(username="pingou")
  308. with tests.user_set(self.app.application, user):
  309. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  310. self.assertEqual(output.status_code, 200)
  311. output_text = output.get_data(as_text=True)
  312. self.assertIn(
  313. '<a class="btn btn-sm btn-secondary" '
  314. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  315. "Edit</a>",
  316. output_text,
  317. )
  318. self.assertNotIn(
  319. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  320. "\n Fork and Edit",
  321. output_text,
  322. )
  323. def test_view_file_fork_and_edit_on_a_fork(self):
  324. """ Test the view_file on a text file on somone else's fork when
  325. logged in.
  326. """
  327. # logged in, but it's not your fork, so only fork and edit button
  328. # is there
  329. user = tests.FakeUser(username="foo")
  330. with tests.user_set(self.app.application, user):
  331. output = self.app.get("/fork/pingou/test/blob/master/f/sources")
  332. self.assertEqual(output.status_code, 200)
  333. output_text = output.get_data(as_text=True)
  334. self.assertNotIn(
  335. '<a class="btn btn-sm btn-secondary" '
  336. 'href="/fork/pingou/test/edit/master/f/sources" title="Edit file">'
  337. "Edit</a>",
  338. output_text,
  339. )
  340. self.assertIn(
  341. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  342. "\n Fork and Edit",
  343. output_text,
  344. )
  345. def test_view_file_fork_and_edit_on_project(self):
  346. """ Test the view_file on a text file on somone else's fork when
  347. logged in.
  348. """
  349. # logged in and seeing the project you forked
  350. user = tests.FakeUser(username="pingou")
  351. with tests.user_set(self.app.application, user):
  352. output = self.app.get("/test/blob/master/f/sources")
  353. self.assertEqual(output.status_code, 200)
  354. output_text = output.get_data(as_text=True)
  355. self.assertIn(
  356. '<a class="btn btn-sm btn-secondary" '
  357. 'href="/test/edit/master/f/sources" title="Edit file">'
  358. "Edit</a>",
  359. output_text,
  360. )
  361. self.assertIn(
  362. '<button class="btn btn-sm btn-secondary fork_project_btn">'
  363. "\n Edit in your fork",
  364. output_text,
  365. )
  366. if __name__ == "__main__":
  367. unittest.main(verbosity=2)