test_pagure_flask_ui_repo_view_blame.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # -*- coding: utf-8 -*-
  2. """
  3. Authors:
  4. Julen Landa Alustiza <julen@landa.eus>
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import re
  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 tests
  16. import pagure.lib.model
  17. from pagure.utils import __get_file_in_tree as get_file_in_tree
  18. class PagureFlaskRepoViewBlameFileSimpletests(tests.Modeltests):
  19. """ Tests for view_blame_file endpoint of the flask pagure app """
  20. def test_view_blame_file_no_project(self):
  21. """ Test the view_blame_file endpoint """
  22. output = self.app.get("/foo/blame/sources")
  23. # No project registered in the DB
  24. self.assertEqual(output.status_code, 404)
  25. output_text = output.get_data(as_text=True)
  26. self.assertIn(
  27. "<title>Page not found :'( - Pagure</title>", output_text
  28. )
  29. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  30. self.assertIn("<p>Project not found</p>", output_text)
  31. def test_view_blame_file_no_git_repo(self):
  32. """ Test the view_blame_file endpoint """
  33. tests.create_projects(self.session)
  34. output = self.app.get("/test/blame/sources")
  35. # No git repo associated
  36. self.assertEqual(output.status_code, 404)
  37. def test_view_blame_file_no_git_content(self):
  38. """ Test the view_blame_file endpoint """
  39. tests.create_projects(self.session)
  40. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  41. output = self.app.get("/test/blame/sources")
  42. # project and associated repo, but no file
  43. self.assertEqual(output.status_code, 404)
  44. output_text = output.get_data(as_text=True)
  45. self.assertIn(
  46. "<title>Page not found :'( - Pagure</title>", output_text
  47. )
  48. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  49. self.assertIn("<p>Empty repo cannot have a file</p>", output_text)
  50. class PagureFlaskRepoViewBlameFiletests(tests.Modeltests):
  51. """ Tests for view_blame_file endpoint of the flask pagure app """
  52. def setUp(self):
  53. """ Set up the environment, ran before every tests. """
  54. super(PagureFlaskRepoViewBlameFiletests, self).setUp()
  55. self.regex = re.compile(r'>(\w+)</a></td>\n<td class="cell2">')
  56. tests.create_projects(self.session)
  57. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  58. # Add some content to the git repo
  59. tests.add_content_to_git(
  60. os.path.join(self.path, "repos", "test.git"),
  61. message="initial commit",
  62. )
  63. tests.add_content_to_git(
  64. os.path.join(self.path, "repos", "test.git"), message="foo"
  65. )
  66. tests.add_content_to_git(
  67. os.path.join(self.path, "repos", "test.git"),
  68. branch="feature",
  69. content="bar",
  70. message="bar",
  71. author=("Aritz Author", "aritz@authors.tld"),
  72. )
  73. def test_view_blame_file_default_branch_master(self):
  74. """ Test the view_blame_file endpoint """
  75. output = self.app.get("/test/blame/sources")
  76. self.assertEqual(output.status_code, 200)
  77. output_text = output.get_data(as_text=True)
  78. self.assertIn('<table class="code_table">', output_text)
  79. self.assertTrue(
  80. '<tr><td class="cell1"><a id="1" href="#1" '
  81. 'data-line-number="1"></a></td>' in output_text
  82. or '<tr><td class="cell1"><a data-line-number="1" '
  83. 'href="#1" id="1"></a></td>' in output_text
  84. )
  85. self.assertIn(
  86. '<td class="cell2"><pre><code>foo</code></pre></td>', output_text
  87. )
  88. self.assertIn('<td class="cell_user">Alice Author</td>', output_text)
  89. data = self.regex.findall(output_text)
  90. self.assertEqual(len(data), 2)
  91. def test_view_blame_file_default_branch_non_master(self):
  92. """ Test the view_blame_file endpoint """
  93. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  94. reference = repo.lookup_reference("refs/heads/feature").resolve()
  95. repo.set_head(reference.name)
  96. output = self.app.get("/test/blame/sources")
  97. self.assertEqual(output.status_code, 200)
  98. output_text = output.get_data(as_text=True)
  99. self.assertIn('<table class="code_table">', output_text)
  100. self.assertTrue(
  101. '<tr><td class="cell1"><a id="1" href="#1" '
  102. 'data-line-number="1"></a></td>' in output_text
  103. or '<tr><td class="cell1"><a data-line-number="1" '
  104. 'href="#1" id="1"></a></td>' in output_text
  105. )
  106. self.assertIn(
  107. '<td class="cell2"><pre><code>bar</code></pre></td>', output_text
  108. )
  109. self.assertIn('<td class="cell_user">Aritz Author</td>', output_text)
  110. data = self.regex.findall(output_text)
  111. self.assertEqual(len(data), 3)
  112. def test_view_blame_file_on_commit(self):
  113. """ Test the view_blame_file endpoint """
  114. repo_obj = pygit2.Repository(
  115. os.path.join(self.path, "repos", "test.git")
  116. )
  117. commit = repo_obj[repo_obj.head.target]
  118. parent = commit.parents[0].oid.hex
  119. output = self.app.get(
  120. "/test/blame/sources?identifier={}".format(parent)
  121. )
  122. self.assertEqual(output.status_code, 200)
  123. output_text = output.get_data(as_text=True)
  124. self.assertIn('<table class="code_table">', output_text)
  125. self.assertTrue(
  126. '<tr><td class="cell1"><a id="1" href="#1" '
  127. 'data-line-number="1"></a></td>' in output_text
  128. or '<tr><td class="cell1"><a data-line-number="1" '
  129. 'href="#1" id="1"></a></td>' in output_text
  130. )
  131. self.assertIn(
  132. '<td class="cell2"><pre><code>foo</code></pre></td>', output_text
  133. )
  134. self.assertIn('<td class="cell_user">Alice Author</td>', output_text)
  135. data = self.regex.findall(output_text)
  136. self.assertEqual(len(data), 1)
  137. def test_view_blame_file_on_branch(self):
  138. """ Test the view_blame_file endpoint """
  139. output = self.app.get("/test/blame/sources?identifier=feature")
  140. self.assertEqual(output.status_code, 200)
  141. output_text = output.get_data(as_text=True)
  142. self.assertIn('<table class="code_table">', output_text)
  143. self.assertTrue(
  144. '<tr><td class="cell1"><a id="1" href="#1" '
  145. 'data-line-number="1"></a></td>' in output_text
  146. or '<tr><td class="cell1"><a data-line-number="1" '
  147. 'href="#1" id="1"></a></td>' in output_text
  148. )
  149. self.assertIn(
  150. '<td class="cell2"><pre><code>bar</code></pre></td>', output_text
  151. )
  152. self.assertIn('<td class="cell_user">Aritz Author</td>', output_text)
  153. data = self.regex.findall(output_text)
  154. self.assertEqual(len(data), 3)
  155. def test_view_blame_file_on_tag(self):
  156. """ Test the view_blame_file endpoint """
  157. # set a tag on the head's parent commit
  158. repo_obj = pygit2.Repository(
  159. os.path.join(self.path, "repos", "test.git")
  160. )
  161. commit = repo_obj[repo_obj.head.target]
  162. parent = commit.parents[0].oid.hex
  163. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  164. repo_obj.create_tag(
  165. "v1.0", parent, pygit2.GIT_OBJ_COMMIT, tagger, "Release v1.0"
  166. )
  167. output = self.app.get("/test/blame/sources?identifier=v1.0")
  168. self.assertEqual(output.status_code, 200)
  169. output_text = output.get_data(as_text=True)
  170. self.assertIn('<table class="code_table">', output_text)
  171. self.assertTrue(
  172. '<tr><td class="cell1"><a id="1" href="#1" '
  173. 'data-line-number="1"></a></td>' in output_text
  174. or '<tr><td class="cell1"><a data-line-number="1" '
  175. 'href="#1" id="1"></a></td>' in output_text
  176. )
  177. self.assertIn(
  178. '<td class="cell2"><pre><code>foo</code></pre></td>', output_text
  179. )
  180. self.assertIn('<td class="cell_user">Alice Author</td>', output_text)
  181. data = self.regex.findall(output_text)
  182. self.assertEqual(len(data), 1)
  183. def test_view_blame_file_on_blob(self):
  184. """ Test the view_blame_file endpoint """
  185. # Retrieve the blob of the `sources` file in head
  186. repo_obj = pygit2.Repository(
  187. os.path.join(self.path, "repos", "test.git")
  188. )
  189. commit = repo_obj[repo_obj.head.target]
  190. content = get_file_in_tree(
  191. repo_obj, commit.tree, ["sources"], bail_on_tree=True
  192. )
  193. output = self.app.get(
  194. "/test/blame/sources?identifier=%s" % content.oid.hex
  195. )
  196. self.assertEqual(output.status_code, 404)
  197. output_text = output.get_data(as_text=True)
  198. self.assertIn("Invalid identified provided", output_text)
  199. def test_view_blame_file_binary(self):
  200. """ Test the view_blame_file endpoint """
  201. # Add binary content
  202. tests.add_binary_git_repo(
  203. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  204. )
  205. output = self.app.get("/test/blame/test.jpg")
  206. self.assertEqual(output.status_code, 400)
  207. output_text = output.get_data(as_text=True)
  208. self.assertIn("<title>400 Bad Request</title>", output_text)
  209. self.assertIn("<p>Binary files cannot be blamed</p>", output_text)
  210. def test_view_blame_file_non_ascii_name(self):
  211. """ Test the view_blame_file endpoint """
  212. tests.add_commit_git_repo(
  213. os.path.join(self.path, "repos", "test.git"),
  214. ncommits=1,
  215. filename="Šource",
  216. )
  217. output = self.app.get("/test/blame/Šource")
  218. self.assertEqual(output.status_code, 200)
  219. output_text = output.get_data(as_text=True)
  220. self.assertEqual(
  221. output.headers["Content-Type"].lower(), "text/html; charset=utf-8"
  222. )
  223. self.assertIn("</span>&nbsp; Šource", output_text)
  224. self.assertIn('<table class="code_table">', output_text)
  225. self.assertTrue(
  226. '<tr><td class="cell1"><a id="1" href="#1" '
  227. 'data-line-number="1"></a></td>' in output_text
  228. or '<tr><td class="cell1"><a data-line-number="1" '
  229. 'href="#1" id="1"></a></td>' in output_text
  230. )
  231. self.assertIn(
  232. '<td class="cell2"><pre><code>Row 0</code></pre></td>', output_text
  233. )
  234. def test_view_blame_file_fork_of_a_fork(self):
  235. """ Test the view_blame_file endpoint """
  236. item = pagure.lib.model.Project(
  237. user_id=1, # pingou
  238. name="test3",
  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", "test3.git")
  248. )
  249. tests.add_readme_git_repo(
  250. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  251. )
  252. tests.add_commit_git_repo(
  253. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  254. ncommits=10,
  255. )
  256. tests.add_content_to_git(
  257. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  258. content="✨☃🍰☃✨",
  259. )
  260. output = self.app.get("/fork/pingou/test3/blame/sources")
  261. self.assertEqual(output.status_code, 200)
  262. output_text = output.get_data(as_text=True)
  263. self.assertIn('<table class="code_table">', output_text)
  264. self.assertTrue(
  265. '<tr><td class="cell1"><a id="1" href="#1" '
  266. 'data-line-number="1"></a></td>' in output_text
  267. or '<tr><td class="cell1"><a data-line-number="1" '
  268. 'href="#1" id="1"></a></td>' in output_text
  269. )
  270. self.assertIn(
  271. '<td class="cell2"><pre><code> barRow 0</code></pre></td>',
  272. output_text,
  273. )
  274. def test_view_blame_file_no_file(self):
  275. """ Test the view_blame_file endpoint """
  276. output = self.app.get("/test/blame/foofile")
  277. self.assertEqual(output.status_code, 404)
  278. output_text = output.get_data(as_text=True)
  279. self.assertIn(
  280. "<title>Page not found :'( - Pagure</title>", output_text
  281. )
  282. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  283. self.assertIn("<p>File not found</p>", output_text)
  284. def test_view_blame_file_folder(self):
  285. """ Test the view_blame_file endpoint """
  286. tests.add_commit_git_repo(
  287. os.path.join(self.path, "repos", "test.git/folder1"),
  288. ncommits=1,
  289. filename="sources",
  290. )
  291. output = self.app.get("/test/blame/folder1")
  292. self.assertEqual(output.status_code, 404)
  293. output_text = output.get_data(as_text=True)
  294. self.assertIn(
  295. "<title>Page not found :'( - Pagure</title>", output_text
  296. )
  297. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  298. self.assertIn("<p>File not found</p>", output_text)
  299. def test_view_blame_file_unborn_head_no_identifier(self):
  300. repo_obj = pygit2.Repository(
  301. os.path.join(self.path, "repos", "test.git")
  302. )
  303. repo_obj.set_head("refs/heads/unexistent")
  304. output = self.app.get("/test/blame/sources")
  305. self.assertEqual(output.status_code, 404)
  306. output_text = output.get_data(as_text=True)
  307. self.assertIn(
  308. "<title>Page not found :'( - Pagure</title>", output_text
  309. )
  310. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  311. self.assertIn(
  312. "<p>Identifier is mandatory on unborn HEAD repos</p>", output_text
  313. )