test_pagure_flask_ui_archives.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Clement Verna <cverna@tutanota.com>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import unittest
  9. import sys
  10. import os
  11. import time
  12. import mock
  13. import pygit2
  14. sys.path.insert(
  15. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  16. )
  17. import pagure.lib.git
  18. import pagure.lib.query
  19. import tests
  20. from tests.test_pagure_lib_git_get_tags_objects import add_repo_tag
  21. class PagureFlaskUiArchivesTest(tests.Modeltests):
  22. """ Tests checking the archiving mechanism. """
  23. def setUp(self):
  24. """ Set up the environnment, ran before every tests. """
  25. super(PagureFlaskUiArchivesTest, self).setUp()
  26. tests.create_projects(self.session)
  27. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  28. project = pagure.lib.query._get_project(self.session, "test")
  29. # test has both commits and tags
  30. repopath = os.path.join(self.path, "repos", "test.git")
  31. tests.add_readme_git_repo(repopath)
  32. repo = pygit2.Repository(repopath)
  33. add_repo_tag(self.path, repo, ["v1.0", "v1.1"], "test.git")
  34. # test2 has only commits
  35. tests.add_readme_git_repo(
  36. os.path.join(self.path, "repos", "test2.git")
  37. )
  38. # somenamespace/test3 has neither commits nor tags
  39. # Create the archive folder:
  40. self.archive_path = os.path.join(self.path, "archives")
  41. os.mkdir(self.archive_path)
  42. def test_project_no_conf(self):
  43. """ Test getting the archive when pagure isn't configured. """
  44. output = self.app.get(
  45. "/somenamespace/test3/archive/tag1/test3-tag1.zip",
  46. follow_redirects=True,
  47. )
  48. self.assertEqual(output.status_code, 404)
  49. self.assertIn(
  50. "This pagure instance isn&#39;t configured to support "
  51. "this feature",
  52. output.get_data(as_text=True),
  53. )
  54. self.assertEqual(os.listdir(self.archive_path), [])
  55. def test_project_invalid_conf(self):
  56. """ Test getting the archive when pagure is wrongly configured. """
  57. with mock.patch.dict(
  58. "pagure.config.config",
  59. {"ARCHIVE_FOLDER": os.path.join(self.path, "invalid")},
  60. ):
  61. output = self.app.get(
  62. "/somenamespace/test3/archive/tag1/test3-tag1.zip",
  63. follow_redirects=True,
  64. )
  65. self.assertEqual(output.status_code, 500)
  66. self.assertIn(
  67. "Incorrect configuration, please contact your admin",
  68. output.get_data(as_text=True),
  69. )
  70. self.assertEqual(os.listdir(self.archive_path), [])
  71. def test_project_invalid_format(self):
  72. """ Test getting the archive when the format provided is invalid. """
  73. with mock.patch.dict(
  74. "pagure.config.config",
  75. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  76. ):
  77. output = self.app.get(
  78. "/somenamespace/test3/archive/tag1/test3-tag1.unzip",
  79. follow_redirects=True,
  80. )
  81. self.assertEqual(output.status_code, 404)
  82. self.assertEqual(os.listdir(self.archive_path), [])
  83. def test_project_no_commit(self):
  84. """ Test getting the archive of an empty project. """
  85. with mock.patch.dict(
  86. "pagure.config.config",
  87. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  88. ):
  89. output = self.app.get(
  90. "/somenamespace/test3/archive/tag1/test3-tag1.zip",
  91. follow_redirects=True,
  92. )
  93. self.assertEqual(output.status_code, 404)
  94. self.assertIn(
  95. "<p>Invalid commit provided</p>", output.get_data(as_text=True)
  96. )
  97. self.assertEqual(os.listdir(self.archive_path), [])
  98. def test_project_no_tag(self):
  99. """ Test getting the archive of a non-empty project but without
  100. tags. """
  101. with mock.patch.dict(
  102. "pagure.config.config",
  103. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  104. ):
  105. output = self.app.get(
  106. "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
  107. )
  108. self.assertEqual(output.status_code, 404)
  109. self.assertIn(
  110. "<p>Invalid commit provided</p>", output.get_data(as_text=True)
  111. )
  112. self.assertEqual(os.listdir(self.archive_path), [])
  113. def test_project_no_tag(self):
  114. """ Test getting the archive of an empty project. """
  115. with mock.patch.dict(
  116. "pagure.config.config",
  117. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  118. ):
  119. output = self.app.get(
  120. "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
  121. )
  122. self.assertEqual(output.status_code, 404)
  123. self.assertIn(
  124. "<p>Invalid commit provided</p>", output.get_data(as_text=True)
  125. )
  126. self.assertEqual(os.listdir(self.archive_path), [])
  127. def test_project_w_tag_zip(self):
  128. """ Test getting the archive from a tag. """
  129. with mock.patch.dict(
  130. "pagure.config.config",
  131. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  132. ):
  133. output = self.app.get(
  134. "/test/archive/v1.0/test-v1.0.zip", follow_redirects=True
  135. )
  136. self.assertEqual(output.status_code, 200)
  137. self.assertEqual(os.listdir(self.archive_path), ["test"])
  138. self.assertEqual(
  139. os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
  140. )
  141. self.assertEqual(
  142. os.listdir(os.path.join(self.archive_path, "test", "tags")),
  143. ["v1.0"],
  144. )
  145. self.assertEqual(
  146. len(
  147. os.listdir(
  148. os.path.join(self.archive_path, "test", "tags", "v1.0")
  149. )
  150. ),
  151. 1,
  152. )
  153. files = os.listdir(
  154. os.path.join(self.archive_path, "test", "tags", "v1.0")
  155. )
  156. self.assertEqual(
  157. os.listdir(
  158. os.path.join(
  159. self.archive_path, "test", "tags", "v1.0", files[0]
  160. )
  161. ),
  162. ["test-v1.0.zip"],
  163. )
  164. def test_project_w_tag_tar(self):
  165. """ Test getting the archive from a tag. """
  166. with mock.patch.dict(
  167. "pagure.config.config",
  168. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  169. ):
  170. output = self.app.get(
  171. "/test/archive/v1.0/test-v1.0.tar", follow_redirects=True
  172. )
  173. self.assertEqual(output.status_code, 200)
  174. self.assertEqual(os.listdir(self.archive_path), ["test"])
  175. self.assertEqual(
  176. os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
  177. )
  178. self.assertEqual(
  179. os.listdir(os.path.join(self.archive_path, "test", "tags")),
  180. ["v1.0"],
  181. )
  182. self.assertEqual(
  183. len(
  184. os.listdir(
  185. os.path.join(self.archive_path, "test", "tags", "v1.0")
  186. )
  187. ),
  188. 1,
  189. )
  190. files = os.listdir(
  191. os.path.join(self.archive_path, "test", "tags", "v1.0")
  192. )
  193. self.assertEqual(
  194. os.listdir(
  195. os.path.join(
  196. self.archive_path, "test", "tags", "v1.0", files[0]
  197. )
  198. ),
  199. ["test-v1.0.tar"],
  200. )
  201. def test_project_w_tag_tar_gz(self):
  202. """ Test getting the archive from a tag. """
  203. with mock.patch.dict(
  204. "pagure.config.config",
  205. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  206. ):
  207. output = self.app.get(
  208. "/test/archive/v1.0/test-v1.0.tar.gz", follow_redirects=True
  209. )
  210. self.assertEqual(output.status_code, 200)
  211. self.assertEqual(os.listdir(self.archive_path), ["test"])
  212. self.assertEqual(
  213. os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
  214. )
  215. self.assertEqual(
  216. os.listdir(os.path.join(self.archive_path, "test", "tags")),
  217. ["v1.0"],
  218. )
  219. self.assertEqual(
  220. len(
  221. os.listdir(
  222. os.path.join(self.archive_path, "test", "tags", "v1.0")
  223. )
  224. ),
  225. 1,
  226. )
  227. files = os.listdir(
  228. os.path.join(self.archive_path, "test", "tags", "v1.0")
  229. )
  230. self.assertEqual(
  231. os.listdir(
  232. os.path.join(
  233. self.archive_path, "test", "tags", "v1.0", files[0]
  234. )
  235. ),
  236. ["test-v1.0.tar.gz"],
  237. )
  238. def test_project_w_commit_tar_gz(self):
  239. """ Test getting the archive from a commit. """
  240. repopath = os.path.join(self.path, "repos", "test.git")
  241. repo = pygit2.Repository(repopath)
  242. commit = repo.head.target.hex
  243. with mock.patch.dict(
  244. "pagure.config.config",
  245. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  246. ):
  247. output = self.app.get(
  248. "/test/archive/%s/test-v1.0.tar.gz" % commit,
  249. follow_redirects=True,
  250. )
  251. self.assertEqual(output.status_code, 200)
  252. self.assertEqual(os.listdir(self.archive_path), ["test"])
  253. self.assertEqual(
  254. os.listdir(os.path.join(self.archive_path, "test")), [commit]
  255. )
  256. self.assertEqual(
  257. os.listdir(os.path.join(self.archive_path, "test", commit)),
  258. ["test-v1.0.tar.gz"],
  259. )
  260. def test_project_w_commit_tar_gz_twice(self):
  261. """ Test getting the archive from a commit twice, so we hit the
  262. disk cache. """
  263. repopath = os.path.join(self.path, "repos", "test.git")
  264. repo = pygit2.Repository(repopath)
  265. commit = repo.head.target.hex
  266. with mock.patch.dict(
  267. "pagure.config.config",
  268. {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
  269. ):
  270. output = self.app.get(
  271. "/test/archive/%s/test-v1.0.tar.gz" % commit,
  272. follow_redirects=True,
  273. )
  274. self.assertEqual(output.status_code, 200)
  275. output = self.app.get(
  276. "/test/archive/%s/test-v1.0.tar.gz" % commit,
  277. follow_redirects=True,
  278. )
  279. self.assertEqual(output.status_code, 200)
  280. self.assertEqual(os.listdir(self.archive_path), ["test"])
  281. self.assertEqual(
  282. os.listdir(os.path.join(self.archive_path, "test")), [commit]
  283. )
  284. self.assertEqual(
  285. os.listdir(os.path.join(self.archive_path, "test", commit)),
  286. ["test-v1.0.tar.gz"],
  287. )
  288. if __name__ == "__main__":
  289. unittest.main(verbosity=2)