1
0

test_pagure_flask_api_project_view_file.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2020 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import datetime
  9. import json
  10. import unittest
  11. import shutil
  12. import sys
  13. import tempfile
  14. import os
  15. import pygit2
  16. from celery.result import EagerResult
  17. from mock import patch, Mock
  18. sys.path.insert(
  19. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  20. )
  21. import pagure.flask_app
  22. import pagure.lib.query
  23. import tests
  24. from pagure.lib.repo import PagureRepo
  25. class PagureFlaskApiProjectViewFiletests(tests.Modeltests):
  26. """Tests for the flask API of pagure for issue"""
  27. maxDiff = None
  28. def setUp(self):
  29. super(PagureFlaskApiProjectViewFiletests, self).setUp()
  30. tests.create_projects(self.session)
  31. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  32. tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
  33. def test_view_file_invalid_project(self):
  34. output = self.app.get("/api/0/invalid/tree")
  35. self.assertEqual(output.status_code, 404)
  36. data = json.loads(output.get_data(as_text=True))
  37. self.assertDictEqual(
  38. data, {"error": "Project not found", "error_code": "ENOPROJECT"}
  39. )
  40. def test_view_file_invalid_ref_and_path(self):
  41. output = self.app.get("/api/0/test/tree/branchname/f/foldername")
  42. self.assertEqual(output.status_code, 404)
  43. data = json.loads(output.get_data(as_text=True))
  44. self.assertDictEqual(
  45. data,
  46. {
  47. "error": "File not found in this git repository",
  48. "error_code": "EFILENOTFOUND",
  49. },
  50. )
  51. def test_view_file_empty_project(self):
  52. output = self.app.get("/api/0/test2/tree")
  53. self.assertEqual(output.status_code, 404)
  54. data = json.loads(output.get_data(as_text=True))
  55. self.assertDictEqual(
  56. data,
  57. {
  58. "error": "This git repository is empty",
  59. "error_code": "EEMPTYGIT",
  60. },
  61. )
  62. def test_view_file_basic(self):
  63. output = self.app.get("/api/0/test/tree")
  64. self.assertEqual(output.status_code, 200)
  65. data = json.loads(output.get_data(as_text=True))
  66. self.assertDictEqual(
  67. data,
  68. {
  69. "content": [
  70. {
  71. "content_url": "http://localhost/test/raw/master/"
  72. "f/README.rst",
  73. "name": "README.rst",
  74. "path": "README.rst",
  75. "type": "file",
  76. }
  77. ],
  78. "name": None,
  79. "type": "folder",
  80. },
  81. )
  82. def test_view_file_with_folder(self):
  83. tests.add_content_git_repo(
  84. os.path.join(self.path, "repos", "test.git")
  85. )
  86. output = self.app.get("/api/0/test/tree")
  87. self.assertEqual(output.status_code, 200)
  88. data = json.loads(output.get_data(as_text=True))
  89. self.assertDictEqual(
  90. data,
  91. {
  92. "content": [
  93. {
  94. "content_url": "http://localhost/api/0/test/tree/"
  95. "master/f/folder1",
  96. "name": "folder1",
  97. "path": "folder1",
  98. "type": "folder",
  99. },
  100. {
  101. "content_url": "http://localhost/test/raw/master/f/"
  102. "README.rst",
  103. "name": "README.rst",
  104. "path": "README.rst",
  105. "type": "file",
  106. },
  107. {
  108. "content_url": "http://localhost/test/raw/master/f/"
  109. "sources",
  110. "name": "sources",
  111. "path": "sources",
  112. "type": "file",
  113. },
  114. ],
  115. "name": None,
  116. "type": "folder",
  117. },
  118. )
  119. def test_view_file_specific_file(self):
  120. tests.add_content_git_repo(
  121. os.path.join(self.path, "repos", "test.git")
  122. )
  123. output = self.app.get("/api/0/test/tree/master/f/README.rst")
  124. self.assertEqual(output.status_code, 200)
  125. data = json.loads(output.get_data(as_text=True))
  126. self.assertDictEqual(
  127. data,
  128. {
  129. "content_url": "http://localhost/test/raw/master/f/README.rst",
  130. "name": "README.rst",
  131. "type": "file",
  132. },
  133. )
  134. def test_view_file_invalid_ref(self):
  135. tests.add_content_git_repo(
  136. os.path.join(self.path, "repos", "test.git")
  137. )
  138. output = self.app.get("/api/0/test/tree/invalid/f/folder1")
  139. print(output.data)
  140. self.assertEqual(output.status_code, 404)
  141. data = json.loads(output.get_data(as_text=True))
  142. self.assertDictEqual(
  143. data,
  144. {
  145. "error": "File not found in this git repository",
  146. "error_code": "EFILENOTFOUND",
  147. },
  148. )
  149. def test_view_file_invalid_folder(self):
  150. tests.add_content_git_repo(
  151. os.path.join(self.path, "repos", "test.git")
  152. )
  153. output = self.app.get("/api/0/test/tree/master/f/inv/invalid")
  154. self.assertEqual(output.status_code, 404)
  155. data = json.loads(output.get_data(as_text=True))
  156. self.assertDictEqual(
  157. data,
  158. {
  159. "error": "File not found in this git repository",
  160. "error_code": "EFILENOTFOUND",
  161. },
  162. )
  163. def test_view_file_valid_branch(self):
  164. tests.add_content_git_repo(
  165. os.path.join(self.path, "repos", "test.git")
  166. )
  167. output = self.app.get("/api/0/test/tree/master/f/folder1")
  168. self.assertEqual(output.status_code, 200)
  169. data = json.loads(output.get_data(as_text=True))
  170. self.assertDictEqual(
  171. data,
  172. {
  173. "content": [
  174. {
  175. "content_url": "http://localhost/api/0/test/tree/"
  176. "master/f/folder1/folder2",
  177. "name": "folder2",
  178. "path": "folder1/folder2",
  179. "type": "folder",
  180. }
  181. ],
  182. "name": "folder1",
  183. "type": "folder",
  184. },
  185. )
  186. def test_view_file_non_ascii_name(self):
  187. # View file with a non-ascii name
  188. tests.add_commit_git_repo(
  189. os.path.join(self.path, "repos", "test.git"),
  190. ncommits=1,
  191. filename="Šource",
  192. )
  193. output = self.app.get("/api/0/test/tree")
  194. self.assertEqual(output.status_code, 200)
  195. data = json.loads(output.get_data(as_text=True).encode("utf-8"))
  196. self.assertDictEqual(
  197. data,
  198. {
  199. "content": [
  200. {
  201. "content_url": "http://localhost/test/raw/master/f/"
  202. "README.rst",
  203. "name": "README.rst",
  204. "path": "README.rst",
  205. "type": "file",
  206. },
  207. {
  208. "content_url": "http://localhost/test/raw/master/f/%C5%A0ource",
  209. "name": "Šource",
  210. "path": "Šource",
  211. "type": "file",
  212. },
  213. ],
  214. "name": None,
  215. "type": "folder",
  216. },
  217. )
  218. def test_view_file_from_commit(self):
  219. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  220. commit = repo.revparse_single("HEAD")
  221. output = self.app.get("/api/0/test/tree/%s" % commit.oid.hex)
  222. self.assertEqual(output.status_code, 200)
  223. data = json.loads(output.get_data(as_text=True))
  224. self.assertDictEqual(
  225. data,
  226. {
  227. "content": [
  228. {
  229. "content_url": "http://localhost/test/raw/"
  230. "%s/f/README.rst" % commit.oid.hex,
  231. "name": "README.rst",
  232. "path": "README.rst",
  233. "type": "file",
  234. }
  235. ],
  236. "name": None,
  237. "type": "folder",
  238. },
  239. )
  240. def test_view_file_from_tree(self):
  241. tests.add_content_git_repo(
  242. os.path.join(self.path, "repos", "test.git")
  243. )
  244. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  245. commit = repo.revparse_single("HEAD")
  246. output = self.app.get(
  247. "/api/0/test/tree/%s/f/folder1" % commit.tree.oid.hex
  248. )
  249. self.assertEqual(output.status_code, 200)
  250. data = json.loads(output.get_data(as_text=True))
  251. self.assertDictEqual(
  252. data,
  253. {
  254. "content": [
  255. {
  256. "content_url": "http://localhost/api/0/test/tree/"
  257. "%s/f/folder1/folder2" % commit.tree.oid.hex,
  258. "name": "folder2",
  259. "path": "folder1/folder2",
  260. "type": "folder",
  261. }
  262. ],
  263. "name": "folder1",
  264. "type": "folder",
  265. },
  266. )
  267. def test_view_file_from_tag_hex(self):
  268. repo = pygit2.Repository(os.path.join(self.path, "repos", "test.git"))
  269. commit = repo.revparse_single("HEAD")
  270. tagger = pygit2.Signature("Alice Doe", "adoe@example.com", 12347, 0)
  271. tag = repo.create_tag(
  272. "v1.0_tag",
  273. commit.oid.hex,
  274. pygit2.GIT_OBJ_COMMIT,
  275. tagger,
  276. "Release v1.0",
  277. )
  278. output = self.app.get("/api/0/test/tree/%s" % tag.hex)
  279. self.assertEqual(output.status_code, 200)
  280. data = json.loads(output.get_data(as_text=True))
  281. self.assertDictEqual(
  282. data,
  283. {
  284. "content": [
  285. {
  286. "content_url": "http://localhost/test/raw/"
  287. "%s/f/README.rst" % tag.hex,
  288. "name": "README.rst",
  289. "path": "README.rst",
  290. "type": "file",
  291. }
  292. ],
  293. "name": None,
  294. "type": "folder",
  295. },
  296. )
  297. if __name__ == "__main__":
  298. unittest.main(verbosity=2)