test_pagure_flask_ui_repo_view_history.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. # -*- coding: utf-8 -*-
  2. """
  3. Authors:
  4. Pierre-Yves Chibon <pingou@pingoured.fr>
  5. """
  6. from __future__ import unicode_literals, absolute_import
  7. import re
  8. import sys
  9. import os
  10. import pygit2
  11. sys.path.insert(
  12. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  13. )
  14. import tests
  15. import pagure.lib.model
  16. class PagureFlaskRepoViewHistoryFileSimpletests(tests.Modeltests):
  17. """ Tests for view_history_file endpoint of the flask pagure app """
  18. def test_view_history_file_no_project(self):
  19. """ Test the view_history_file endpoint """
  20. output = self.app.get("/foo/history/sources")
  21. # No project registered in the DB
  22. self.assertEqual(output.status_code, 404)
  23. output_text = output.get_data(as_text=True)
  24. self.assertIn(
  25. "<title>Page not found :'( - Pagure</title>", output_text
  26. )
  27. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  28. self.assertIn("<p>Project not found</p>", output_text)
  29. def test_view_history_file_no_git_repo(self):
  30. """ Test the view_history_file endpoint """
  31. tests.create_projects(self.session)
  32. output = self.app.get("/test/history/sources")
  33. # No git repo associated
  34. self.assertEqual(output.status_code, 404)
  35. def test_view_history_file_no_git_content(self):
  36. """ Test the view_history_file endpoint """
  37. tests.create_projects(self.session)
  38. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  39. output = self.app.get("/test/history/sources")
  40. # project and associated repo, but no file
  41. self.assertEqual(output.status_code, 404)
  42. output_text = output.get_data(as_text=True)
  43. self.assertIn(
  44. "<title>Page not found :'( - Pagure</title>", output_text
  45. )
  46. self.assertIn("<h2>Page not found (404)</h2>", output_text)
  47. self.assertIn("<p>Empty repo cannot have a file</p>", output_text)
  48. class PagureFlaskRepoViewHistoryFiletests(tests.Modeltests):
  49. """ Tests for view_history_file endpoint of the flask pagure app """
  50. def setUp(self):
  51. """ Set up the environment, ran before every tests. """
  52. super(PagureFlaskRepoViewHistoryFiletests, self).setUp()
  53. self.regex = re.compile(r' <div class="list-group-item " id="c_')
  54. tests.create_projects(self.session)
  55. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  56. # Add some content to the git repo
  57. tests.add_content_to_git(
  58. os.path.join(self.path, "repos", "test.git"),
  59. message="initial commit",
  60. )
  61. tests.add_content_to_git(
  62. os.path.join(self.path, "repos", "test.git"), message="foo"
  63. )
  64. tests.add_content_to_git(
  65. os.path.join(self.path, "repos", "test.git"),
  66. branch="feature",
  67. content="bar",
  68. message="bar",
  69. author=("Aritz Author", "aritz@authors.tld"),
  70. )
  71. def test_view_history_file_default_branch_master(self):
  72. """ Test the view_history_file endpoint """
  73. output = self.app.get("/test/history/sources")
  74. self.assertEqual(output.status_code, 200)
  75. output_text = output.get_data(as_text=True)
  76. self.assertIn("<strong>foo</strong>", output_text)
  77. data = self.regex.findall(output_text)
  78. self.assertEqual(len(data), 2)
  79. def test_view_history_file_default_branch_non_master(self):
  80. """ Test the view_history_file endpoint """
  81. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  82. reference = repo.lookup_reference("refs/heads/feature").resolve()
  83. repo.set_head(reference.name)
  84. output = self.app.get("/test/history/sources")
  85. self.assertEqual(output.status_code, 200)
  86. output_text = output.get_data(as_text=True)
  87. self.assertIn("<strong>bar</strong>", output_text)
  88. data = self.regex.findall(output_text)
  89. self.assertEqual(len(data), 3)
  90. def test_view_history_file_on_commit(self):
  91. """ Test the view_history_file endpoint """
  92. repo_obj = pygit2.Repository(
  93. os.path.join(self.path, "repos", "test.git")
  94. )
  95. commit = repo_obj[repo_obj.head.target]
  96. parent = commit.parents[0].oid.hex
  97. output = self.app.get(
  98. "/test/history/sources?identifier={}".format(parent)
  99. )
  100. self.assertEqual(output.status_code, 200)
  101. output_text = output.get_data(as_text=True)
  102. self.assertIn("<strong>initial commit</strong>", output_text)
  103. data = self.regex.findall(output_text)
  104. self.assertEqual(len(data), 1)
  105. def test_view_history_file_on_branch(self):
  106. """ Test the view_history_file endpoint """
  107. output = self.app.get("/test/history/sources?identifier=feature")
  108. self.assertEqual(output.status_code, 200)
  109. output_text = output.get_data(as_text=True)
  110. self.assertIn("<strong>bar</strong>", output_text)
  111. data = self.regex.findall(output_text)
  112. self.assertEqual(len(data), 3)
  113. def test_view_history_file_on_tag(self):
  114. """ Test the view_history_file endpoint """
  115. # set a tag on the head's parent commit
  116. repo_obj = pygit2.Repository(
  117. os.path.join(self.path, "repos", "test.git")
  118. )
  119. commit = repo_obj[repo_obj.head.target]
  120. parent = commit.parents[0].oid.hex
  121. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  122. repo_obj.create_tag(
  123. "v1.0", parent, pygit2.GIT_OBJ_COMMIT, tagger, "Release v1.0"
  124. )
  125. output = self.app.get("/test/history/sources?identifier=v1.0")
  126. self.assertEqual(output.status_code, 200)
  127. output_text = output.get_data(as_text=True)
  128. self.assertIn("<strong>initial commit</strong>", output_text)
  129. data = self.regex.findall(output_text)
  130. self.assertEqual(len(data), 1)
  131. def test_view_history_file_binary(self):
  132. """ Test the view_history_file endpoint """
  133. # Add binary content
  134. tests.add_binary_git_repo(
  135. os.path.join(self.path, "repos", "test.git"), "test.jpg"
  136. )
  137. output = self.app.get("/test/history/test.jpg")
  138. self.assertEqual(output.status_code, 200)
  139. output_text = output.get_data(as_text=True)
  140. self.assertIn("<strong>Add a fake image file</strong>", output_text)
  141. def test_view_history_file_non_ascii_name(self):
  142. """ Test the view_history_file endpoint """
  143. tests.add_commit_git_repo(
  144. os.path.join(self.path, "repos", "test.git"),
  145. ncommits=1,
  146. filename="Šource",
  147. )
  148. output = self.app.get("/test/history/Šource")
  149. self.assertEqual(output.status_code, 200)
  150. output_text = output.get_data(as_text=True)
  151. self.assertEqual(
  152. output.headers["Content-Type"].lower(), "text/html; charset=utf-8"
  153. )
  154. self.assertIn("</span>&nbsp; Šource", output_text)
  155. self.assertIn("<strong>Add row 0 to Šource file</strong>", output_text)
  156. def test_view_history_file_fork_of_a_fork(self):
  157. """ Test the view_history_file endpoint """
  158. item = pagure.lib.model.Project(
  159. user_id=1, # pingou
  160. name="test3",
  161. description="test project #3",
  162. is_fork=True,
  163. parent_id=1,
  164. hook_token="aaabbbppp",
  165. )
  166. self.session.add(item)
  167. self.session.commit()
  168. tests.add_content_git_repo(
  169. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  170. )
  171. tests.add_readme_git_repo(
  172. os.path.join(self.path, "repos", "forks", "pingou", "test3.git")
  173. )
  174. tests.add_commit_git_repo(
  175. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  176. ncommits=10,
  177. )
  178. tests.add_content_to_git(
  179. os.path.join(self.path, "repos", "forks", "pingou", "test3.git"),
  180. content="✨☃🍰☃✨",
  181. )
  182. output = self.app.get("/fork/pingou/test3/history/sources")
  183. self.assertEqual(output.status_code, 200)
  184. output_text = output.get_data(as_text=True)
  185. self.assertIn(
  186. "<strong>Add row 2 to sources file</strong>", output_text
  187. )
  188. def test_view_history_file_no_file(self):
  189. """ Test the view_history_file endpoint """
  190. output = self.app.get("/test/history/foofile")
  191. self.assertEqual(output.status_code, 400)
  192. output_text = output.get_data(as_text=True)
  193. self.assertIn("No history could be found for this file", output_text)
  194. def test_view_history_file_folder(self):
  195. """ Test the view_history_file endpoint """
  196. tests.add_commit_git_repo(
  197. os.path.join(self.path, "repos", "test.git/folder1"),
  198. ncommits=1,
  199. filename="sources",
  200. )
  201. output = self.app.get("/test/history/folder1")
  202. self.assertEqual(output.status_code, 400)
  203. output_text = output.get_data(as_text=True)
  204. self.assertIn("No history could be found for this file", output_text)
  205. def test_view_history_file_existing_folder(self):
  206. """ Test the view_history_file endpoint """
  207. tests.add_content_to_git(
  208. os.path.join(self.path, "repos", "test.git"), folders="foo/bar"
  209. )
  210. output = self.app.get("/test/history/foo/bar/")
  211. self.assertEqual(output.status_code, 200)
  212. output_text = output.get_data(as_text=True)
  213. self.assertIn(
  214. "<strong>Add content to file foo/bar/sources</strong>", output_text
  215. )
  216. data = self.regex.findall(output_text)
  217. self.assertEqual(len(data), 1)
  218. def test_view_history_file_unborn_head_no_identifier(self):
  219. repo_obj = pygit2.Repository(
  220. os.path.join(self.path, "repos", "test.git")
  221. )
  222. repo_obj.set_head("refs/heads/unexistent")
  223. output = self.app.get("/test/history/sources")
  224. self.assertEqual(output.status_code, 400)
  225. output_text = output.get_data(as_text=True)
  226. self.assertIn("Invalid repository", output_text)