test_pagure_flask_ui_archives.py 10 KB

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