Blame tests/test_pagure_flask_ui_archives.py

Pierre-Yves Chibon ded1fa
# -*- coding: utf-8 -*-
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
"""
Pierre-Yves Chibon ded1fa
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
 Authors:
Pierre-Yves Chibon ded1fa
   Clement Verna <cverna@tutanota.com></cverna@tutanota.com>
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
"""
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
import unittest
Pierre-Yves Chibon ded1fa
import sys
Pierre-Yves Chibon ded1fa
import os
Pierre-Yves Chibon ded1fa
import time
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
import mock
Pierre-Yves Chibon ded1fa
import pygit2
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon ded1fa
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
import pagure.lib.git
Pierre-Yves Chibon ded1fa
import pagure.lib.query
Pierre-Yves Chibon ded1fa
import tests
Pierre-Yves Chibon ded1fa
from tests.test_pagure_lib_git_get_tags_objects import add_repo_tag
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
class PagureFlaskUiArchivesTest(tests.Modeltests):
Pierre-Yves Chibon ded1fa
    """ Tests checking the archiving mechanism. """
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def setUp(self):
Pierre-Yves Chibon ded1fa
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon ded1fa
        super(PagureFlaskUiArchivesTest, self).setUp()
Pierre-Yves Chibon ded1fa
        tests.create_projects(self.session)
Pierre-Yves Chibon ded1fa
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon ded1fa
        project = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        # test has both commits and tags
Pierre-Yves Chibon ded1fa
        repopath = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon ded1fa
        tests.add_readme_git_repo(repopath)
Pierre-Yves Chibon ded1fa
        repo = pygit2.Repository(repopath)
Pierre-Yves Chibon ded1fa
        add_repo_tag(self.path, repo, ['v1.0', 'v1.1'], 'test.git')
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        # test2 has only commits
Pierre-Yves Chibon ded1fa
        tests.add_readme_git_repo(os.path.join(
Pierre-Yves Chibon ded1fa
            self.path, 'repos', 'test2.git'))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        # somenamespace/test3 has neither commits nor tags
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        # Create the archive folder:
Pierre-Yves Chibon ded1fa
        self.archive_path = os.path.join(self.path, 'archives')
Pierre-Yves Chibon ded1fa
        os.mkdir(self.archive_path)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_no_conf(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive when pagure isn't configured. """
Pierre-Yves Chibon ded1fa
        output = self.app.get(
Pierre-Yves Chibon ded1fa
            '/somenamespace/test3/archive/tag1/test3-tag1.zip',
Pierre-Yves Chibon ded1fa
            follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ded1fa
        self.assertIn(
Pierre-Yves Chibon ded1fa
            "This pagure instance isn't configured to support "
Pierre-Yves Chibon ded1fa
            "this feature", output.get_data(as_text=True))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_invalid_conf(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive when pagure is wrongly configured. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'invalid')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/somenamespace/test3/archive/tag1/test3-tag1.zip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 500)
Pierre-Yves Chibon ded1fa
            self.assertIn(
Pierre-Yves Chibon ded1fa
                "Incorrect configuration, please contact your admin",
Pierre-Yves Chibon ded1fa
                output.get_data(as_text=True))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_invalid_format(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive when the format provided is invalid. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/somenamespace/test3/archive/tag1/test3-tag1.unzip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_no_commit(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive of an empty project. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/somenamespace/test3/archive/tag1/test3-tag1.zip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ded1fa
            self.assertIn(
Pierre-Yves Chibon ded1fa
                "

Invalid commit provided

",
Pierre-Yves Chibon ded1fa
                output.get_data(as_text=True))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_no_tag(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive of a non-empty project but without
Pierre-Yves Chibon ded1fa
        tags. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test2/archive/tag1/test2-tag1.zip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ded1fa
            self.assertIn(
Pierre-Yves Chibon ded1fa
                "

Invalid commit provided

",
Pierre-Yves Chibon ded1fa
                output.get_data(as_text=True))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_no_tag(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive of an empty project. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test2/archive/tag1/test2-tag1.zip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ded1fa
            self.assertIn(
Pierre-Yves Chibon ded1fa
                "

Invalid commit provided

",
Pierre-Yves Chibon ded1fa
                output.get_data(as_text=True))
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(os.listdir(self.archive_path), [])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_w_tag_zip(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive from a tag. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/v1.0/test-v1.0.zip',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(self.archive_path), ['test'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test')),
Pierre-Yves Chibon ded1fa
            ['tags'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test', 'tags')),
Pierre-Yves Chibon ded1fa
            ['v1.0'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            len(os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))),
Pierre-Yves Chibon ded1fa
            1)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        files = os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0', files[0])),
Pierre-Yves Chibon ded1fa
            ['test-v1.0.zip'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_w_tag_tar(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive from a tag. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/v1.0/test-v1.0.tar',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(self.archive_path), ['test'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test')),
Pierre-Yves Chibon ded1fa
            ['tags'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test', 'tags')),
Pierre-Yves Chibon ded1fa
            ['v1.0'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            len(os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))),
Pierre-Yves Chibon ded1fa
            1)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        files = os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0', files[0])),
Pierre-Yves Chibon ded1fa
            ['test-v1.0.tar'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_w_tag_tar_gz(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive from a tag. """
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/v1.0/test-v1.0.tar.gz',
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(self.archive_path), ['test'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test')),
Pierre-Yves Chibon ded1fa
            ['tags'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test', 'tags')),
Pierre-Yves Chibon ded1fa
            ['v1.0'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            len(os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))),
Pierre-Yves Chibon ded1fa
            1)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        files = os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0'))
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(
Pierre-Yves Chibon ded1fa
                self.archive_path, 'test', 'tags', 'v1.0', files[0])),
Pierre-Yves Chibon ded1fa
            ['test-v1.0.tar.gz'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_w_commit_tar_gz(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive from a commit. """
Pierre-Yves Chibon ded1fa
        repopath = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon ded1fa
        repo = pygit2.Repository(repopath)
Pierre-Yves Chibon ded1fa
        commit = repo.head.target.hex
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/%s/test-v1.0.tar.gz' % commit,
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(self.archive_path), ['test'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test')),
Pierre-Yves Chibon ded1fa
            [commit])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test', commit)),
Pierre-Yves Chibon ded1fa
            ['test-v1.0.tar.gz'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
    def test_project_w_commit_tar_gz_twice(self):
Pierre-Yves Chibon ded1fa
        """ Test getting the archive from a commit twice, so we hit the
Pierre-Yves Chibon ded1fa
        disk cache. """
Pierre-Yves Chibon ded1fa
        repopath = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon ded1fa
        repo = pygit2.Repository(repopath)
Pierre-Yves Chibon ded1fa
        commit = repo.head.target.hex
Pierre-Yves Chibon ded1fa
        with mock.patch.dict(
Pierre-Yves Chibon ded1fa
                'pagure.config.config',
Pierre-Yves Chibon ded1fa
                {'ARCHIVE_FOLDER': os.path.join(self.path, 'archives')}):
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/%s/test-v1.0.tar.gz' % commit,
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            output = self.app.get(
Pierre-Yves Chibon ded1fa
                '/test/archive/%s/test-v1.0.tar.gz' % commit,
Pierre-Yves Chibon ded1fa
                follow_redirects=True)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(self.archive_path), ['test'])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test')),
Pierre-Yves Chibon ded1fa
            [commit])
Pierre-Yves Chibon ded1fa
        self.assertEqual(
Pierre-Yves Chibon ded1fa
            os.listdir(os.path.join(self.archive_path, 'test', commit)),
Pierre-Yves Chibon ded1fa
            ['test-v1.0.tar.gz'])
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
Pierre-Yves Chibon ded1fa
if __name__ == '__main__':
Pierre-Yves Chibon ded1fa
    unittest.main(verbosity=2)