Blame tests/test_pagure_flask_ui_pr_no_sources.py

Pierre-Yves Chibon ff9832
# -*- coding: utf-8 -*-
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
"""
Pierre-Yves Chibon 9071b6
 (c) 2015-2018 - Copyright Red Hat Inc
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
 Authors:
Pierre-Yves Chibon ff9832
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
"""
Pierre-Yves Chibon ff9832
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon ff9832
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon ff9832
import pkg_resources
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
import json
Pierre-Yves Chibon ff9832
import unittest
Pierre-Yves Chibon ff9832
import shutil
Pierre-Yves Chibon ff9832
import sys
Pierre-Yves Chibon ff9832
import tempfile
Pierre-Yves Chibon ff9832
import time
Pierre-Yves Chibon ff9832
import os
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
import pygit2
Pierre-Yves Chibon ff9832
from mock import patch, MagicMock
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon ff9832
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
import pagure
Pierre-Yves Chibon ff9832
import pagure.lib
Pierre-Yves Chibon ff9832
import tests
Pierre-Yves Chibon ff9832
from pagure.lib.repo import PagureRepo
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
class PagureFlaskPrNoSourcestests(tests.Modeltests):
Pierre-Yves Chibon ff9832
    """ Tests PR in pagure when the source is gone """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    maxDiff = None
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon ff9832
    @patch('pagure.lib.notify.fedmsg_publish', MagicMock(return_value=True))
Pierre-Yves Chibon ff9832
    def setUp(self):
Pierre-Yves Chibon ff9832
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon ff9832
        super(PagureFlaskPrNoSourcestests, self).setUp()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        tests.create_projects(self.session)
Pierre-Yves Chibon ff9832
        tests.create_projects_git(
Pierre-Yves Chibon ff9832
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Create foo's fork of pingou's test project
Pierre-Yves Chibon ff9832
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff9832
            user_id=2,  # foo
Pierre-Yves Chibon ff9832
            name='test',
Pierre-Yves Chibon ff9832
            description='test project #1',
Pierre-Yves Chibon ff9832
            hook_token='aaabbb',
Pierre-Yves Chibon ff9832
            is_fork=True,
Pierre-Yves Chibon ff9832
            parent_id=1,
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
        self.session.add(item)
Pierre-Yves Chibon ff9832
        self.session.commit()
Pierre-Yves Chibon ff9832
        # Create the fork's git repo
Pierre-Yves Chibon ff9832
        repo_path = os.path.join(self.path, 'repos', item.path)
Pierre-Yves Chibon ff9832
        pygit2.init_repository(repo_path, bare=True)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon b130e5
        fork = pagure.lib.get_authorized_project(
Pierre-Yves Chibon ff9832
            self.session, 'test', user='foo')
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        self.set_up_git_repo(repo=project, fork=fork)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Ensure things got setup straight
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 0fe9c9
        # wait for the worker to process the task
Pierre-Yves Chibon 0fe9c9
        path = os.path.join(
Pierre-Yves Chibon 0fe9c9
            self.path, 'repos', 'test.git',
Pierre-Yves Chibon 0fe9c9
            'refs', 'pull', '1', 'head')
Pierre-Yves Chibon 0fe9c9
        cnt = 0
Pierre-Yves Chibon 0fe9c9
        while not os.path.exists(path):
Pierre-Yves Chibon 0fe9c9
            time.sleep(0.1)
Pierre-Yves Chibon 0fe9c9
            cnt += 1
Pierre-Yves Chibon 0fe9c9
            if cnt == 100:
Pierre-Yves Chibon 0fe9c9
                # We're about 10 seconds in, let's bail
Pierre-Yves Chibon 0fe9c9
                raise Exception('Sorry, worker took too long')
Pierre-Yves Chibon 0fe9c9
Pierre-Yves Chibon ff9832
    def set_up_git_repo(self, repo, fork, branch_from='feature'):
Pierre-Yves Chibon ff9832
        """ Set up the git repo and create the corresponding PullRequest
Pierre-Yves Chibon ff9832
        object.
Pierre-Yves Chibon ff9832
        """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Clone the main repo
Pierre-Yves Chibon ff9832
        gitrepo = os.path.join(self.path, 'repos', repo.path)
Pierre-Yves Chibon ff9832
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon ff9832
        repopath = os.path.join(newpath, 'test')
Pierre-Yves Chibon ff9832
        clone_repo = pygit2.clone_repository(gitrepo, repopath)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Create a file in that git repo
Pierre-Yves Chibon ff9832
        with open(os.path.join(repopath, 'sources'), 'w') as stream:
Pierre-Yves Chibon ff9832
            stream.write('foo\n bar')
Pierre-Yves Chibon ff9832
        clone_repo.index.add('sources')
Pierre-Yves Chibon ff9832
        clone_repo.index.write()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Commits the files added
Pierre-Yves Chibon ff9832
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon ff9832
        author = pygit2.Signature(
Pierre-Yves Chibon ff9832
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon ff9832
        committer = pygit2.Signature(
Pierre-Yves Chibon ff9832
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon ff9832
        clone_repo.create_commit(
Pierre-Yves Chibon ff9832
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon ff9832
            author,
Pierre-Yves Chibon ff9832
            committer,
Pierre-Yves Chibon ff9832
            'Add sources file for testing',
Pierre-Yves Chibon ff9832
            # binary string representing the tree object ID
Pierre-Yves Chibon ff9832
            tree,
Pierre-Yves Chibon ff9832
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon ff9832
            []
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon ff9832
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon ff9832
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        first_commit = clone_repo.revparse_single('HEAD')
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Set the second repo
Pierre-Yves Chibon ff9832
        repopath = os.path.join(self.path, 'repos', fork.path)
Pierre-Yves Chibon ff9832
        new_gitrepo = os.path.join(newpath, 'fork_test')
Pierre-Yves Chibon ff9832
        clone_repo = pygit2.clone_repository(repopath, new_gitrepo)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Add the main project as remote repo
Pierre-Yves Chibon ff9832
        upstream_path = os.path.join(self.path, 'repos', repo.path)
Pierre-Yves Chibon ff9832
        remote = clone_repo.create_remote('upstream', upstream_path)
Pierre-Yves Chibon ff9832
        remote.fetch()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Edit the sources file again
Pierre-Yves Chibon ff9832
        with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon ff9832
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon ff9832
        clone_repo.index.add('sources')
Pierre-Yves Chibon ff9832
        clone_repo.index.write()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Commits the files added
Pierre-Yves Chibon ff9832
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon ff9832
        author = pygit2.Signature(
Pierre-Yves Chibon ff9832
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon ff9832
        committer = pygit2.Signature(
Pierre-Yves Chibon ff9832
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon ff9832
        clone_repo.create_commit(
Pierre-Yves Chibon ff9832
            'refs/heads/%s' % branch_from,
Pierre-Yves Chibon ff9832
            author,
Pierre-Yves Chibon ff9832
            committer,
Pierre-Yves Chibon ff9832
            'A commit on branch %s' % branch_from,
Pierre-Yves Chibon ff9832
            tree,
Pierre-Yves Chibon ff9832
            [first_commit.oid.hex]
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
        refname = 'refs/heads/%s' % (branch_from)
Pierre-Yves Chibon ff9832
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon ff9832
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Create a PR for these changes
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon ff9832
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon ff9832
            session=self.session,
Pierre-Yves Chibon ff9832
            repo_from=fork,
Pierre-Yves Chibon ff9832
            branch_from=branch_from,
Pierre-Yves Chibon ff9832
            repo_to=project,
Pierre-Yves Chibon ff9832
            branch_to='master',
Pierre-Yves Chibon ff9832
            title='PR from the %s branch' % branch_from,
Pierre-Yves Chibon ff9832
            user='pingou',
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
        self.session.commit()
Pierre-Yves Chibon ff9832
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon ff9832
        self.assertEqual(req.title, 'PR from the %s branch' % branch_from)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        shutil.rmtree(newpath)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_request_pull_reference(self):
Pierre-Yves Chibon ff9832
        """ Test if there is a reference created for a new PR. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon ff9832
            list(repo.listall_references()),
Pierre-Yves Chibon ff9832
            ['refs/heads/master', 'refs/pull/1/head']
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_request_pull_fork_reference(self):
Pierre-Yves Chibon ff9832
        """ Test if there the references created on the fork. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon ff9832
            self.session, 'test', user='foo')
Pierre-Yves Chibon ff9832
        self.assertEqual(len(project.requests), 0)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        gitrepo = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon ff9832
            list(repo.listall_references()),
Pierre-Yves Chibon ff9832
            ['refs/heads/feature']
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
    def test_accessing_pr_fork_deleted(self):
Pierre-Yves Chibon ff9832
        """ Test accessing the PR if the fork has been deleted. """
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete fork on disk
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon ff9832
            self.session, 'test', user='foo')
Pierre-Yves Chibon ff9832
        repo_path = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon ff9832
        self.assertTrue(os.path.exists(repo_path))
Pierre-Yves Chibon ff9832
        shutil.rmtree(repo_path)
Pierre-Yves Chibon ff9832
        self.assertFalse(os.path.exists(repo_path))
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete fork in the DB
Pierre-Yves Chibon ff9832
        self.session.delete(project)
Pierre-Yves Chibon ff9832
        self.session.commit()
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # View the pull-request
Pierre-Yves Chibon ff9832
        output2 = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon ff9832
        self.assertEqual(output2.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 5da9ba
    def test_accessing_pr_patch_fork_deleted(self):
Pierre-Yves Chibon 5da9ba
        """ Test accessing the PR's patch if the fork has been deleted. """
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete fork on disk
Pierre-Yves Chibon 5da9ba
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 5da9ba
            self.session, 'test', user='foo')
Pierre-Yves Chibon 5da9ba
        repo_path = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon 5da9ba
        self.assertTrue(os.path.exists(repo_path))
Pierre-Yves Chibon 5da9ba
        shutil.rmtree(repo_path)
Pierre-Yves Chibon 5da9ba
        self.assertFalse(os.path.exists(repo_path))
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete fork in the DB
Pierre-Yves Chibon 5da9ba
        self.session.delete(project)
Pierre-Yves Chibon 5da9ba
        self.session.commit()
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # View the pull-request
Aurélien Bompard 626417
        output = self.app.get('/test/pull-request/1.patch')
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
        self.assertIn(
Pierre-Yves Chibon 5da9ba
            '--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@',
Aurélien Bompard 626417
            output.get_data(as_text=True))
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon ff9832
    def test_accessing_pr_branch_deleted(self):
Pierre-Yves Chibon ff9832
        """ Test accessing the PR if branch it originates from has been
Pierre-Yves Chibon ff9832
        deleted. """
Pierre-Yves Chibon b130e5
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon ff9832
            self.session, 'test', user='foo')
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Check the branches before
Pierre-Yves Chibon ff9832
        gitrepo = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon ff9832
            list(repo.listall_references()),
Pierre-Yves Chibon ff9832
            ['refs/heads/feature']
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Delete branch of the fork
Pierre-Yves Chibon ff9832
        user = tests.FakeUser(username='foo')
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon ff9832
            output = self.app.post(
Pierre-Yves Chibon ff9832
                '/fork/foo/test/b/feature/delete', follow_redirects=True)
Pierre-Yves Chibon ff9832
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # Check the branches after
Pierre-Yves Chibon ff9832
        gitrepo = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon ff9832
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon ff9832
        self.assertEqual(
Pierre-Yves Chibon ff9832
            list(repo.listall_references()),
Pierre-Yves Chibon ff9832
            []
Pierre-Yves Chibon ff9832
        )
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
        # View the pull-request
Pierre-Yves Chibon ff9832
        output2 = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon ff9832
        self.assertEqual(output2.status_code, 200)
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon 5da9ba
    def test_accessing_pr_patch_branch_deleted(self):
Pierre-Yves Chibon 5da9ba
        """ Test accessing the PR's patch if branch it originates from has
Pierre-Yves Chibon 5da9ba
        been deleted. """
Pierre-Yves Chibon 5da9ba
        project = pagure.lib.get_authorized_project(
Pierre-Yves Chibon 5da9ba
            self.session, 'test', user='foo')
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Check the branches before
Pierre-Yves Chibon 5da9ba
        gitrepo = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon 5da9ba
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 5da9ba
        self.assertEqual(
Pierre-Yves Chibon 5da9ba
            list(repo.listall_references()),
Pierre-Yves Chibon 5da9ba
            ['refs/heads/feature']
Pierre-Yves Chibon 5da9ba
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Delete branch of the fork
Pierre-Yves Chibon 5da9ba
        user = tests.FakeUser(username='foo')
Pierre-Yves Chibon 5da9ba
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 5da9ba
            output = self.app.post(
Pierre-Yves Chibon 5da9ba
                '/fork/foo/test/b/feature/delete', follow_redirects=True)
Pierre-Yves Chibon 5da9ba
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # Check the branches after
Pierre-Yves Chibon 5da9ba
        gitrepo = os.path.join(self.path, 'repos', project.path)
Pierre-Yves Chibon 5da9ba
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 5da9ba
        self.assertEqual(
Pierre-Yves Chibon 5da9ba
            list(repo.listall_references()),
Pierre-Yves Chibon 5da9ba
            []
Pierre-Yves Chibon 5da9ba
        )
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon 5da9ba
        # View the pull-request
Aurélien Bompard 626417
        output = self.app.get('/test/pull-request/1.patch')
Aurélien Bompard 626417
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5da9ba
        self.assertIn(
Pierre-Yves Chibon 5da9ba
            '--- a/sources\n+++ b/sources\n@@ -1,2 +1,4 @@',
Aurélien Bompard 626417
            output.get_data(as_text=True))
Pierre-Yves Chibon 5da9ba
Pierre-Yves Chibon ff9832
Pierre-Yves Chibon ff9832
if __name__ == '__main__':
Pierre-Yves Chibon ff9832
    unittest.main(verbosity=2)