123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- # -*- coding: utf-8 -*-
- """
- (c) 2018 - Copyright Red Hat Inc
- Authors:
- Clement Verna <cverna@tutanota.com>
- """
- from __future__ import unicode_literals, absolute_import
- import unittest
- import sys
- import os
- import time
- import mock
- import pygit2
- sys.path.insert(
- 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
- )
- import pagure.lib.git
- import pagure.lib.query
- import tests
- from tests.test_pagure_lib_git_get_tags_objects import add_repo_tag
- class PagureFlaskUiArchivesTest(tests.Modeltests):
- """ Tests checking the archiving mechanism. """
- def setUp(self):
- """ Set up the environnment, ran before every tests. """
- super(PagureFlaskUiArchivesTest, self).setUp()
- tests.create_projects(self.session)
- tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
- project = pagure.lib.query._get_project(self.session, "test")
- # test has both commits and tags
- repopath = os.path.join(self.path, "repos", "test.git")
- tests.add_readme_git_repo(repopath)
- repo = pygit2.Repository(repopath)
- add_repo_tag(self.path, repo, ["v1.0", "v1.1"], "test.git")
- # test2 has only commits
- tests.add_readme_git_repo(
- os.path.join(self.path, "repos", "test2.git")
- )
- # somenamespace/test3 has neither commits nor tags
- # Create the archive folder:
- self.archive_path = os.path.join(self.path, "archives")
- os.mkdir(self.archive_path)
- def test_project_no_conf(self):
- """ Test getting the archive when pagure isn't configured. """
- output = self.app.get(
- "/somenamespace/test3/archive/tag1/test3-tag1.zip",
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- self.assertIn(
- "This pagure instance isn't configured to support "
- "this feature",
- output.get_data(as_text=True),
- )
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_invalid_conf(self):
- """ Test getting the archive when pagure is wrongly configured. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "invalid")},
- ):
- output = self.app.get(
- "/somenamespace/test3/archive/tag1/test3-tag1.zip",
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 500)
- self.assertIn(
- "Incorrect configuration, please contact your admin",
- output.get_data(as_text=True),
- )
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_invalid_format(self):
- """ Test getting the archive when the format provided is invalid. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/somenamespace/test3/archive/tag1/test3-tag1.unzip",
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_no_commit(self):
- """ Test getting the archive of an empty project. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/somenamespace/test3/archive/tag1/test3-tag1.zip",
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 404)
- self.assertIn(
- "<p>Invalid commit provided</p>", output.get_data(as_text=True)
- )
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_no_tag(self):
- """ Test getting the archive of a non-empty project but without
- tags. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- self.assertIn(
- "<p>Invalid commit provided</p>", output.get_data(as_text=True)
- )
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_no_tag(self):
- """ Test getting the archive of an empty project. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test2/archive/tag1/test2-tag1.zip", follow_redirects=True
- )
- self.assertEqual(output.status_code, 404)
- self.assertIn(
- "<p>Invalid commit provided</p>", output.get_data(as_text=True)
- )
- self.assertEqual(os.listdir(self.archive_path), [])
- def test_project_w_tag_zip(self):
- """ Test getting the archive from a tag. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test/archive/v1.0/test-v1.0.zip", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertEqual(os.listdir(self.archive_path), ["test"])
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
- )
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test", "tags")),
- ["v1.0"],
- )
- self.assertEqual(
- len(
- os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- ),
- 1,
- )
- files = os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- self.assertEqual(
- os.listdir(
- os.path.join(
- self.archive_path, "test", "tags", "v1.0", files[0]
- )
- ),
- ["test-v1.0.zip"],
- )
- def test_project_w_tag_tar(self):
- """ Test getting the archive from a tag. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test/archive/v1.0/test-v1.0.tar", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertEqual(os.listdir(self.archive_path), ["test"])
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
- )
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test", "tags")),
- ["v1.0"],
- )
- self.assertEqual(
- len(
- os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- ),
- 1,
- )
- files = os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- self.assertEqual(
- os.listdir(
- os.path.join(
- self.archive_path, "test", "tags", "v1.0", files[0]
- )
- ),
- ["test-v1.0.tar"],
- )
- def test_project_w_tag_tar_gz(self):
- """ Test getting the archive from a tag. """
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test/archive/v1.0/test-v1.0.tar.gz", follow_redirects=True
- )
- self.assertEqual(output.status_code, 200)
- self.assertEqual(os.listdir(self.archive_path), ["test"])
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test")), ["tags"]
- )
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test", "tags")),
- ["v1.0"],
- )
- self.assertEqual(
- len(
- os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- ),
- 1,
- )
- files = os.listdir(
- os.path.join(self.archive_path, "test", "tags", "v1.0")
- )
- self.assertEqual(
- os.listdir(
- os.path.join(
- self.archive_path, "test", "tags", "v1.0", files[0]
- )
- ),
- ["test-v1.0.tar.gz"],
- )
- def test_project_w_commit_tar_gz(self):
- """ Test getting the archive from a commit. """
- repopath = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.Repository(repopath)
- commit = repo.head.target.hex
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test/archive/%s/test-v1.0.tar.gz" % commit,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- self.assertEqual(os.listdir(self.archive_path), ["test"])
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test")), [commit]
- )
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test", commit)),
- ["test-v1.0.tar.gz"],
- )
- def test_project_w_commit_tar_gz_twice(self):
- """ Test getting the archive from a commit twice, so we hit the
- disk cache. """
- repopath = os.path.join(self.path, "repos", "test.git")
- repo = pygit2.Repository(repopath)
- commit = repo.head.target.hex
- with mock.patch.dict(
- "pagure.config.config",
- {"ARCHIVE_FOLDER": os.path.join(self.path, "archives")},
- ):
- output = self.app.get(
- "/test/archive/%s/test-v1.0.tar.gz" % commit,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- output = self.app.get(
- "/test/archive/%s/test-v1.0.tar.gz" % commit,
- follow_redirects=True,
- )
- self.assertEqual(output.status_code, 200)
- self.assertEqual(os.listdir(self.archive_path), ["test"])
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test")), [commit]
- )
- self.assertEqual(
- os.listdir(os.path.join(self.archive_path, "test", commit)),
- ["test-v1.0.tar.gz"],
- )
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|