Blame tests/test_progit_flask_ui_fork.py

Pierre-Yves Chibon 9347b5
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
"""
Pierre-Yves Chibon 9347b5
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
 Authors:
Pierre-Yves Chibon 9347b5
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
"""
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 9347b5
import pkg_resources
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
import json
Pierre-Yves Chibon 9347b5
import unittest
Pierre-Yves Chibon 9347b5
import shutil
Pierre-Yves Chibon 9347b5
import sys
Pierre-Yves Chibon 9347b5
import tempfile
Pierre-Yves Chibon 9347b5
import os
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
import pygit2
Pierre-Yves Chibon 9347b5
from mock import patch
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 9347b5
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
import pagure.lib
Pierre-Yves Chibon 9347b5
import tests
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
class PagureFlaskForktests(tests.Modeltests):
Pierre-Yves Chibon 9347b5
    """ Tests for flask fork controller of pagure """
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
    def setUp(self):
Pierre-Yves Chibon 9347b5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 9347b5
        super(PagureFlaskForktests, self).setUp()
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 9347b5
        pagure.SESSION = self.session
Pierre-Yves Chibon 9347b5
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon 9347b5
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 9347b5
        pagure.ui.fork.SESSION = self.session
Pierre-Yves Chibon 9347b5
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon ae1f3a
        pagure.APP.config['GIT_FOLDER'] = os.path.join(tests.HERE, 'repos')
Pierre-Yves Chibon ae1f3a
        pagure.APP.config['FORK_FOLDER'] = os.path.join(tests.HERE, 'forks')
Pierre-Yves Chibon 9347b5
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 9347b5
            tests.HERE, 'tickets')
Pierre-Yves Chibon 9347b5
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 9347b5
            tests.HERE, 'docs')
Pierre-Yves Chibon 9347b5
        pagure.APP.config['REQUESTS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 9347b5
            tests.HERE, 'requests')
Pierre-Yves Chibon 9347b5
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 224b96
    def set_up_git_repo(
Pierre-Yves Chibon 224b96
            self, new_project=None, branch_from='feature', mtype='FF'):
Pierre-Yves Chibon 9347b5
        """ Set up the git repo and create the corresponding PullRequest
Pierre-Yves Chibon 9347b5
        object.
Pierre-Yves Chibon 9347b5
        """
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Create a git repo to play with
Pierre-Yves Chibon ae1f3a
        gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
Pierre-Yves Chibon 9347b5
        self.assertFalse(os.path.exists(gitrepo))
Pierre-Yves Chibon 9347b5
        os.makedirs(gitrepo)
Pierre-Yves Chibon 9347b5
        repo = pygit2.init_repository(gitrepo, bare=True)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
Pierre-Yves Chibon 9347b5
        clone_repo = pygit2.clone_repository(gitrepo, newpath)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Create a file in that git repo
Pierre-Yves Chibon 9347b5
        with open(os.path.join(newpath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 9347b5
            stream.write('foo\n bar')
Pierre-Yves Chibon 9347b5
        clone_repo.index.add('sources')
Pierre-Yves Chibon 9347b5
        clone_repo.index.write()
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Commits the files added
Pierre-Yves Chibon 9347b5
        tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 9347b5
        author = pygit2.Signature(
Pierre-Yves Chibon 9347b5
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 9347b5
        committer = pygit2.Signature(
Pierre-Yves Chibon 9347b5
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 9347b5
        clone_repo.create_commit(
Pierre-Yves Chibon 9347b5
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 9347b5
            author,
Pierre-Yves Chibon 9347b5
            committer,
Pierre-Yves Chibon 9347b5
            'Add sources file for testing',
Pierre-Yves Chibon 9347b5
            # binary string representing the tree object ID
Pierre-Yves Chibon 9347b5
            tree,
Pierre-Yves Chibon 9347b5
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 9347b5
            []
Pierre-Yves Chibon 9347b5
        )
Pierre-Yves Chibon 9347b5
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 9347b5
        ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 9347b5
        ori_remote.push(refname)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 224b96
        if mtype == 'merge':
Pierre-Yves Chibon 224b96
            with open(os.path.join(newpath, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 224b96
                stream.write('*~')
Pierre-Yves Chibon 224b96
            clone_repo.index.add('.gitignore')
Pierre-Yves Chibon 224b96
            clone_repo.index.write()
Pierre-Yves Chibon 224b96
Pierre-Yves Chibon 224b96
            # Commits the files added
Pierre-Yves Chibon 224b96
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 224b96
            author = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 224b96
            committer = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 224b96
            clone_repo.create_commit(
Pierre-Yves Chibon 224b96
                'refs/heads/master',
Pierre-Yves Chibon 224b96
                author,
Pierre-Yves Chibon 224b96
                committer,
Pierre-Yves Chibon 224b96
                'Add .gitignore file for testing',
Pierre-Yves Chibon 224b96
                # binary string representing the tree object ID
Pierre-Yves Chibon 224b96
                tree,
Pierre-Yves Chibon 224b96
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 224b96
                [first_commit.oid.hex]
Pierre-Yves Chibon 224b96
            )
Pierre-Yves Chibon 224b96
            refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 224b96
            ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 224b96
            ori_remote.push(refname)
Pierre-Yves Chibon 224b96
Pierre-Yves Chibon 224b96
        if mtype == 'conflicts':
Pierre-Yves Chibon 224b96
            with open(os.path.join(newpath, 'sources'), 'w') as stream:
Pierre-Yves Chibon 224b96
                stream.write('foo\n bar\nbaz')
Pierre-Yves Chibon 224b96
            clone_repo.index.add('sources')
Pierre-Yves Chibon 224b96
            clone_repo.index.write()
Pierre-Yves Chibon 224b96
Pierre-Yves Chibon 224b96
            # Commits the files added
Pierre-Yves Chibon 224b96
            tree = clone_repo.index.write_tree()
Pierre-Yves Chibon 224b96
            author = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 224b96
            committer = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 224b96
            clone_repo.create_commit(
Pierre-Yves Chibon 224b96
                'refs/heads/master',
Pierre-Yves Chibon 224b96
                author,
Pierre-Yves Chibon 224b96
                committer,
Pierre-Yves Chibon 224b96
                'Add sources conflicting',
Pierre-Yves Chibon 224b96
                # binary string representing the tree object ID
Pierre-Yves Chibon 224b96
                tree,
Pierre-Yves Chibon 224b96
                # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 224b96
                [first_commit.oid.hex]
Pierre-Yves Chibon 224b96
            )
Pierre-Yves Chibon 224b96
            refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 224b96
            ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 224b96
            ori_remote.push(refname)
Pierre-Yves Chibon 224b96
Pierre-Yves Chibon 9347b5
        # Set the second repo
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        new_gitrepo = newpath
Pierre-Yves Chibon 9347b5
        if new_project:
Pierre-Yves Chibon 9347b5
            # Create a new git repo to play with
Pierre-Yves Chibon 9347b5
            new_gitrepo = new_project.path
Pierre-Yves Chibon 9347b5
            if not os.path.exists(new_gitrepo):
Pierre-Yves Chibon 9347b5
                os.makedirs(new_gitrepo)
Pierre-Yves Chibon 9347b5
                new_repo = pygit2.clone_repository(gitrepo, new_gitrepo)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        repo = pygit2.Repository(new_gitrepo)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 224b96
        if mtype != 'nochanges':
Pierre-Yves Chibon 224b96
            # Edit the sources file again
Pierre-Yves Chibon 224b96
            with open(os.path.join(new_gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 224b96
                stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 224b96
            repo.index.add('sources')
Pierre-Yves Chibon 224b96
            repo.index.write()
Pierre-Yves Chibon 224b96
Pierre-Yves Chibon 224b96
            # Commits the files added
Pierre-Yves Chibon 224b96
            tree = repo.index.write_tree()
Pierre-Yves Chibon 224b96
            author = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 224b96
            committer = pygit2.Signature(
Pierre-Yves Chibon 224b96
                'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 224b96
            repo.create_commit(
Pierre-Yves Chibon 224b96
                'refs/heads/%s' % branch_from,
Pierre-Yves Chibon 224b96
                author,
Pierre-Yves Chibon 224b96
                committer,
Pierre-Yves Chibon 224b96
                'A commit on branch %s' % branch_from,
Pierre-Yves Chibon 224b96
                tree,
Pierre-Yves Chibon 224b96
                [first_commit.oid.hex]
Pierre-Yves Chibon 224b96
            )
Pierre-Yves Chibon 224b96
            refname = 'refs/heads/%s:refs/heads/%s' % (branch_from, branch_from)
Pierre-Yves Chibon 224b96
            ori_remote = clone_repo.remotes[0]
Pierre-Yves Chibon 224b96
            ori_remote.push(refname)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Create a PR for these changes
Pierre-Yves Chibon 9347b5
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 9347b5
        msg = pagure.lib.new_pull_request(
Pierre-Yves Chibon 9347b5
            session=self.session,
Pierre-Yves Chibon 9347b5
            repo_from=project,
Pierre-Yves Chibon 9347b5
            branch_from=branch_from,
Pierre-Yves Chibon 9347b5
            repo_to=project,
Pierre-Yves Chibon 9347b5
            branch_to='master',
Pierre-Yves Chibon 9347b5
            title='PR from the %s branch' % branch_from,
Pierre-Yves Chibon 9347b5
            user='pingou',
Pierre-Yves Chibon 9347b5
            requestfolder=None,
Pierre-Yves Chibon 9347b5
        )
Pierre-Yves Chibon 9347b5
        self.session.commit()
Pierre-Yves Chibon 9347b5
        self.assertEqual(msg, 'Request created')
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 9347b5
    def test_request_pull(self, send_email):
Pierre-Yves Chibon 9347b5
        """ Test the request_pull endpoint. """
Pierre-Yves Chibon 9347b5
        send_email.return_value = True
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        tests.create_projects(self.session)
Pierre-Yves Chibon 9347b5
        tests.create_projects_git(
Pierre-Yves Chibon 9347b5
            os.path.join(tests.HERE, 'requests'), bare=True)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Non-existant project
Pierre-Yves Chibon 9347b5
        output = self.app.get('/foobar/pull-request/1')
Pierre-Yves Chibon 9347b5
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # Project has no PR
Pierre-Yves Chibon 9347b5
        output = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon 9347b5
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        self.set_up_git_repo(new_project=None, branch_from='feature')
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 9347b5
        self.assertEqual(len(project.requests), 1)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 9347b5
        # View the pull-request
Pierre-Yves Chibon 9347b5
        output = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon 9347b5
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9347b5
        self.assertIn(
Pierre-Yves Chibon 9347b5
            '<title>Pull request #1 - test - Pagure</title>', output.data)
Pierre-Yves Chibon 9347b5
        self.assertIn(
Pierre-Yves Chibon 9347b5
            'title="View file as of 2a552b">View', output.data)
Pierre-Yves Chibon 9347b5
Pierre-Yves Chibon 502b7a
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 502b7a
    def test_merge_request_pull_FF(self, send_email):
Pierre-Yves Chibon 8911da
        """ Test the merge_request_pull endpoint with a FF PR. """
Pierre-Yves Chibon 502b7a
        send_email.return_value = True
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
        self.test_request_pull()
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
        user = tests.FakeUser()
Pierre-Yves Chibon 502b7a
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 502b7a
            output = self.app.get('/test/pull-request/1')
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            csrf_token = output.data.split(
Pierre-Yves Chibon 502b7a
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # No CSRF
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/test/pull-request/1/merge', data={}, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 502b7a
            self.assertIn(
Pierre-Yves Chibon 502b7a
                '<title>Pull request #1 - test - Pagure</title>', output.data)
Pierre-Yves Chibon 502b7a
            self.assertIn(
Pierre-Yves Chibon 502b7a
                'title="View file as of 2a552b">View', output.data)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Wrong project
Pierre-Yves Chibon 502b7a
            data = {
Pierre-Yves Chibon 502b7a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 502b7a
            }
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/foobar/pull-request/100/merge', data=data, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Wrong project
Pierre-Yves Chibon 502b7a
            data = {
Pierre-Yves Chibon 502b7a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 502b7a
            }
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/test/pull-request/1/merge', data=data, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
        user.username = 'pingou'
Pierre-Yves Chibon 502b7a
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Wrong request id
Pierre-Yves Chibon 502b7a
            data = {
Pierre-Yves Chibon 502b7a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 502b7a
            }
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/test/pull-request/100/merge', data=data, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Project w/o pull-request
Pierre-Yves Chibon 502b7a
            repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 502b7a
            settings = repo.settings
Pierre-Yves Chibon 502b7a
            settings['pull_requests'] = False
Pierre-Yves Chibon 502b7a
            repo.settings = settings
Pierre-Yves Chibon 502b7a
            self.session.add(repo)
Pierre-Yves Chibon 502b7a
            self.session.commit()
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Pull-request disabled
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/test/pull-request/1/merge', data=data, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            # Project w pull-request but only assignee can merge
Pierre-Yves Chibon 502b7a
            settings['pull_requests'] = True
Pierre-Yves Chibon 502b7a
            settings['Only_assignee_can_merge_pull-request'] = True
Pierre-Yves Chibon 502b7a
            repo.settings = settings
Pierre-Yves Chibon 502b7a
            self.session.add(repo)
Pierre-Yves Chibon 502b7a
            self.session.commit()
Pierre-Yves Chibon 502b7a
Pierre-Yves Chibon 502b7a
            output = self.app.post(
Pierre-Yves Chibon 502b7a
                '/test/pull-request/1/merge', data=data, follow_redirects=True)
Pierre-Yves Chibon 502b7a
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 502b7a
            self.assertIn(
Pierre-Yves Chibon 502b7a
                '<title>Pull request #1 - test - Pagure</title>',
Pierre-Yves Chibon 502b7a
                output.data)
Pierre-Yves Chibon 502b7a
            self.assertIn(
Pierre-Yves Chibon 502b7a
                '
  • This request must be assigned to be merged
  • ',
    Pierre-Yves Chibon 502b7a
                    output.data)
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 502b7a
                # PR assigned but not to this user
    Pierre-Yves Chibon 502b7a
                repo = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon 502b7a
                req = repo.requests[0]
    Pierre-Yves Chibon 502b7a
                req.assignee_id = 2
    Pierre-Yves Chibon 502b7a
                self.session.add(req)
    Pierre-Yves Chibon 502b7a
                self.session.commit()
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 502b7a
                output = self.app.post(
    Pierre-Yves Chibon 502b7a
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon 502b7a
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '<title>Pull request #1 - test - Pagure</title>',
    Pierre-Yves Chibon 502b7a
                    output.data)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '
  • Only the assignee can merge this review
  • ',
    Pierre-Yves Chibon 502b7a
                    output.data)
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 502b7a
                # Project w/ minimal PR score
    Pierre-Yves Chibon 502b7a
                settings['Only_assignee_can_merge_pull-request'] = False
    Pierre-Yves Chibon 502b7a
                settings['Minimum_score_to_merge_pull-request'] = 2
    Pierre-Yves Chibon 502b7a
                repo.settings = settings
    Pierre-Yves Chibon 502b7a
                self.session.add(repo)
    Pierre-Yves Chibon 502b7a
                self.session.commit()
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 502b7a
                output = self.app.post(
    Pierre-Yves Chibon 502b7a
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon 502b7a
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '<title>Pull request #1 - test - Pagure</title>',
    Pierre-Yves Chibon 502b7a
                    output.data)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '
  • This request does not have the minimum '
  • Pierre-Yves Chibon 502b7a
                    'review score necessary to be merged', output.data)
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 502b7a
                # Merge
    Pierre-Yves Chibon 502b7a
                settings['Minimum_score_to_merge_pull-request'] = -1
    Pierre-Yves Chibon 502b7a
                repo.settings = settings
    Pierre-Yves Chibon 502b7a
                self.session.add(repo)
    Pierre-Yves Chibon 502b7a
                self.session.commit()
    Pierre-Yves Chibon 502b7a
                output = self.app.post(
    Pierre-Yves Chibon 502b7a
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon 502b7a
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '<title>Overview - test - Pagure</title>', output.data)
    Pierre-Yves Chibon 502b7a
                self.assertIn(
    Pierre-Yves Chibon 502b7a
                    '
  • Changes merged!
  • ', output.data)
    Pierre-Yves Chibon 502b7a
    Pierre-Yves Chibon 4ce7b3
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 4ce7b3
        def test_merge_request_pull_merge(self, send_email):
    Pierre-Yves Chibon 4ce7b3
            """ Test the merge_request_pull endpoint with a merge PR. """
    Pierre-Yves Chibon 4ce7b3
            send_email.return_value = True
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon 4ce7b3
            tests.create_projects(self.session)
    Pierre-Yves Chibon 4ce7b3
            tests.create_projects_git(
    Pierre-Yves Chibon 4ce7b3
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon 4ce7b3
            self.set_up_git_repo(
    Pierre-Yves Chibon 4ce7b3
                new_project=None, branch_from='feature', mtype='merge')
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon 4ce7b3
            user = tests.FakeUser()
    Pierre-Yves Chibon 4ce7b3
            user.username = 'pingou'
    Pierre-Yves Chibon 4ce7b3
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 4ce7b3
                output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon 4ce7b3
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon 4ce7b3
                csrf_token = output.data.split(
    Pierre-Yves Chibon 4ce7b3
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon 4ce7b3
                data = {
    Pierre-Yves Chibon 4ce7b3
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon 4ce7b3
                }
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon 4ce7b3
                # Merge
    Pierre-Yves Chibon 4ce7b3
                output = self.app.post(
    Pierre-Yves Chibon 4ce7b3
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon 4ce7b3
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 4ce7b3
                self.assertIn(
    Pierre-Yves Chibon 4ce7b3
                    '<title>Overview - test - Pagure</title>', output.data)
    Pierre-Yves Chibon 4ce7b3
                self.assertIn(
    Pierre-Yves Chibon 4ce7b3
                    '
  • Changes merged!
  • ', output.data)
    Pierre-Yves Chibon 4ce7b3
    Pierre-Yves Chibon fb36e7
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon fb36e7
        def test_merge_request_pull_conflicts(self, send_email):
    Pierre-Yves Chibon fb36e7
            """ Test the merge_request_pull endpoint with a conflicting PR. """
    Pierre-Yves Chibon fb36e7
            send_email.return_value = True
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon fb36e7
            tests.create_projects(self.session)
    Pierre-Yves Chibon fb36e7
            tests.create_projects_git(
    Pierre-Yves Chibon fb36e7
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon fb36e7
            self.set_up_git_repo(
    Pierre-Yves Chibon fb36e7
                new_project=None, branch_from='feature', mtype='conflicts')
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon fb36e7
            user = tests.FakeUser()
    Pierre-Yves Chibon fb36e7
            user.username = 'pingou'
    Pierre-Yves Chibon fb36e7
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon fb36e7
                output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon fb36e7
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon fb36e7
                csrf_token = output.data.split(
    Pierre-Yves Chibon fb36e7
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon fb36e7
                data = {
    Pierre-Yves Chibon fb36e7
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon fb36e7
                }
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon fb36e7
                # Merge conflicts
    Pierre-Yves Chibon fb36e7
                output = self.app.post(
    Pierre-Yves Chibon fb36e7
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon fb36e7
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon fb36e7
                self.assertIn(
    Pierre-Yves Chibon fb36e7
                    '<title>Pull request #1 - test - Pagure</title>',
    Pierre-Yves Chibon fb36e7
                    output.data)
    Pierre-Yves Chibon fb36e7
                self.assertIn(
    Pierre-Yves Chibon fb36e7
                    '
  • Merge conflicts!
  • ', output.data)
    Pierre-Yves Chibon fb36e7
    Pierre-Yves Chibon 51d667
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 51d667
        def test_merge_request_pull_nochange(self, send_email):
    Pierre-Yves Chibon 51d667
            """ Test the merge_request_pull endpoint. """
    Pierre-Yves Chibon 51d667
            send_email.return_value = True
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 51d667
            tests.create_projects(self.session)
    Pierre-Yves Chibon 51d667
            tests.create_projects_git(
    Pierre-Yves Chibon 51d667
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon 51d667
            self.set_up_git_repo(
    Pierre-Yves Chibon 51d667
                new_project=None, branch_from='master', mtype='nochanges')
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 51d667
            user = tests.FakeUser()
    Pierre-Yves Chibon 51d667
            user.username = 'pingou'
    Pierre-Yves Chibon 51d667
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 51d667
                output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon 51d667
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 51d667
                csrf_token = output.data.split(
    Pierre-Yves Chibon 51d667
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 51d667
                data = {
    Pierre-Yves Chibon 51d667
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon 51d667
                }
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 51d667
                # Nothing to merge
    Pierre-Yves Chibon 51d667
                output = self.app.post(
    Pierre-Yves Chibon 51d667
                    '/test/pull-request/1/merge', data=data, follow_redirects=True)
    Pierre-Yves Chibon 51d667
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 51d667
                self.assertIn(
    Pierre-Yves Chibon 51d667
                    '<title>Pull request #1 - test - Pagure</title>', output.data)
    Pierre-Yves Chibon 51d667
                self.assertIn(
    Pierre-Yves Chibon 51d667
                    '
  • Nothing to do, changes were already '
  • Pierre-Yves Chibon 51d667
                    'merged', output.data)
    Pierre-Yves Chibon 51d667
    Pierre-Yves Chibon 21924a
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 21924a
        def test_request_pull_close(self, send_email):
    Pierre-Yves Chibon 21924a
            """ Test the request_pull endpoint with a closed PR. """
    Pierre-Yves Chibon 21924a
            send_email.return_value = True
    Pierre-Yves Chibon 21924a
    Pierre-Yves Chibon 21924a
            self.test_merge_request_pull_FF()
    Pierre-Yves Chibon 21924a
    Pierre-Yves Chibon 21924a
            output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon 21924a
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 21924a
            self.assertIn(
    Pierre-Yves Chibon 21924a
                '<title>Pull request #1 - test - Pagure</title>', output.data)
    Pierre-Yves Chibon 21924a
            self.assertIn(
    Pierre-Yves Chibon 21924a
                'Merged', output.data)
    Pierre-Yves Chibon 21924a
            self.assertIn(
    Pierre-Yves Chibon 21924a
                'title="View file as of 2a552b">View', output.data)
    Pierre-Yves Chibon 21924a
    Pierre-Yves Chibon c19e35
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon c19e35
        def test_request_pull_disabled(self, send_email):
    Pierre-Yves Chibon c19e35
            """ Test the request_pull endpoint with PR disabled. """
    Pierre-Yves Chibon c19e35
            send_email.return_value = True
    Pierre-Yves Chibon c19e35
    Pierre-Yves Chibon c19e35
            tests.create_projects(self.session)
    Pierre-Yves Chibon c19e35
            tests.create_projects_git(
    Pierre-Yves Chibon c19e35
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon c19e35
            self.set_up_git_repo(new_project=None, branch_from='feature')
    Pierre-Yves Chibon c19e35
    Pierre-Yves Chibon c19e35
            # Project w/o pull-request
    Pierre-Yves Chibon c19e35
            repo = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon c19e35
            settings = repo.settings
    Pierre-Yves Chibon c19e35
            settings['pull_requests'] = False
    Pierre-Yves Chibon c19e35
            repo.settings = settings
    Pierre-Yves Chibon c19e35
            self.session.add(repo)
    Pierre-Yves Chibon c19e35
            self.session.commit()
    Pierre-Yves Chibon c19e35
    Pierre-Yves Chibon c19e35
            output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon c19e35
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon c19e35
    Pierre-Yves Chibon cbbc1c
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon cbbc1c
        def test_request_pull_empty_repo(self, send_email):
    Pierre-Yves Chibon ee247e
            """ Test the request_pull endpoint against an empty repo. """
    Pierre-Yves Chibon cbbc1c
            send_email.return_value = True
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            tests.create_projects(self.session)
    Pierre-Yves Chibon cbbc1c
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon cbbc1c
                user_id=2,  # foo
    Pierre-Yves Chibon cbbc1c
                name='test',
    Pierre-Yves Chibon cbbc1c
                description='test project #1',
    Pierre-Yves Chibon cbbc1c
                hook_token='aaabbb',
    Pierre-Yves Chibon cbbc1c
                parent_id=1,
    Pierre-Yves Chibon cbbc1c
            )
    Pierre-Yves Chibon cbbc1c
            self.session.add(item)
    Pierre-Yves Chibon cbbc1c
            self.session.commit()
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            tests.create_projects_git(
    Pierre-Yves Chibon cbbc1c
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon cbbc1c
            tests.create_projects_git(
    Pierre-Yves Chibon cbbc1c
                os.path.join(tests.HERE, 'forks', 'foo'), bare=True)
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            # Create a git repo to play with
    Pierre-Yves Chibon cbbc1c
            gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
    Pierre-Yves Chibon cbbc1c
            self.assertFalse(os.path.exists(gitrepo))
    Pierre-Yves Chibon cbbc1c
            os.makedirs(gitrepo)
    Pierre-Yves Chibon cbbc1c
            repo = pygit2.init_repository(gitrepo, bare=True)
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            # Create a fork of this repo
    Pierre-Yves Chibon cbbc1c
            newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
    Pierre-Yves Chibon cbbc1c
            gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git')
    Pierre-Yves Chibon cbbc1c
            new_repo = pygit2.clone_repository(gitrepo, newpath)
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            # Edit the sources file again
    Pierre-Yves Chibon cbbc1c
            with open(os.path.join(newpath, 'sources'), 'w') as stream:
    Pierre-Yves Chibon cbbc1c
                stream.write('foo\n bar\nbaz\n boose')
    Pierre-Yves Chibon cbbc1c
            new_repo.index.add('sources')
    Pierre-Yves Chibon cbbc1c
            new_repo.index.write()
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            # Commits the files added
    Pierre-Yves Chibon cbbc1c
            tree = new_repo.index.write_tree()
    Pierre-Yves Chibon cbbc1c
            author = pygit2.Signature(
    Pierre-Yves Chibon cbbc1c
                'Alice Author', 'alice@authors.tld')
    Pierre-Yves Chibon cbbc1c
            committer = pygit2.Signature(
    Pierre-Yves Chibon cbbc1c
                'Cecil Committer', 'cecil@committers.tld')
    Pierre-Yves Chibon cbbc1c
            new_repo.create_commit(
    Pierre-Yves Chibon cbbc1c
                'refs/heads/feature',
    Pierre-Yves Chibon cbbc1c
                author,
    Pierre-Yves Chibon cbbc1c
                committer,
    Pierre-Yves Chibon cbbc1c
                'A commit on branch feature',
    Pierre-Yves Chibon cbbc1c
                tree,
    Pierre-Yves Chibon cbbc1c
                []
    Pierre-Yves Chibon cbbc1c
            )
    Pierre-Yves Chibon cbbc1c
            refname = 'refs/heads/feature:refs/heads/feature'
    Pierre-Yves Chibon cbbc1c
            ori_remote = new_repo.remotes[0]
    Pierre-Yves Chibon cbbc1c
            ori_remote.push(refname)
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            # Create a PR for these changes
    Pierre-Yves Chibon cbbc1c
            project = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon cbbc1c
            msg = pagure.lib.new_pull_request(
    Pierre-Yves Chibon cbbc1c
                session=self.session,
    Pierre-Yves Chibon cbbc1c
                repo_from=item,
    Pierre-Yves Chibon cbbc1c
                branch_from='feature',
    Pierre-Yves Chibon cbbc1c
                repo_to=project,
    Pierre-Yves Chibon cbbc1c
                branch_to='master',
    Pierre-Yves Chibon cbbc1c
                title='PR from the feature branch',
    Pierre-Yves Chibon cbbc1c
                user='pingou',
    Pierre-Yves Chibon cbbc1c
                requestfolder=None,
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            )
    Pierre-Yves Chibon cbbc1c
            self.session.commit()
    Pierre-Yves Chibon cbbc1c
            self.assertEqual(msg, 'Request created')
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon cbbc1c
            output = self.app.get('/test/pull-request/1')
    Pierre-Yves Chibon cbbc1c
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon cbbc1c
            self.assertIn(
    Pierre-Yves Chibon cbbc1c
                '<title>Pull request #1 - test - Pagure</title>', output.data)
    Pierre-Yves Chibon cbbc1c
            self.assertTrue(output.data.count(''), 1)
    Pierre-Yves Chibon cbbc1c
    Pierre-Yves Chibon 981ad4
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 981ad4
        def test_request_pull_empty_fork(self, send_email):
    Pierre-Yves Chibon 981ad4
            """ Test the request_pull endpoint from an empty fork. """
    Pierre-Yves Chibon 981ad4
            send_email.return_value = True
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            tests.create_projects(self.session)
    Pierre-Yves Chibon 981ad4
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon 981ad4
                user_id=2,  # foo
    Pierre-Yves Chibon 981ad4
                name='test',
    Pierre-Yves Chibon 981ad4
                description='test project #1',
    Pierre-Yves Chibon 981ad4
                hook_token='aaabbb',
    Pierre-Yves Chibon 981ad4
                parent_id=1,
    Pierre-Yves Chibon 981ad4
            )
    Pierre-Yves Chibon 981ad4
            self.session.add(item)
    Pierre-Yves Chibon 981ad4
            self.session.commit()
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            tests.create_projects_git(
    Pierre-Yves Chibon 981ad4
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon 981ad4
            tests.create_projects_git(
    Pierre-Yves Chibon 981ad4
                os.path.join(tests.HERE, 'forks', 'foo'), bare=True)
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            # Create a git repo to play with
    Pierre-Yves Chibon 981ad4
            gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
    Pierre-Yves Chibon 981ad4
            self.assertFalse(os.path.exists(gitrepo))
    Pierre-Yves Chibon 981ad4
            os.makedirs(gitrepo)
    Pierre-Yves Chibon 981ad4
            repo = pygit2.init_repository(gitrepo, bare=True)
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            # Create a fork of this repo
    Pierre-Yves Chibon 981ad4
            newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
    Pierre-Yves Chibon 981ad4
            gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git')
    Pierre-Yves Chibon 981ad4
            new_repo = pygit2.clone_repository(gitrepo, newpath)
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            # Create a PR for these "changes" (there are none, both repos are
    Pierre-Yves Chibon 981ad4
            # empty)
    Pierre-Yves Chibon 981ad4
            project = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon 981ad4
            msg = pagure.lib.new_pull_request(
    Pierre-Yves Chibon 981ad4
                session=self.session,
    Pierre-Yves Chibon 981ad4
                repo_from=item,
    Pierre-Yves Chibon 981ad4
                branch_from='feature',
    Pierre-Yves Chibon 981ad4
                repo_to=project,
    Pierre-Yves Chibon 981ad4
                branch_to='master',
    Pierre-Yves Chibon 981ad4
                title='PR from the feature branch',
    Pierre-Yves Chibon 981ad4
                user='pingou',
    Pierre-Yves Chibon 981ad4
                requestfolder=None,
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            )
    Pierre-Yves Chibon 981ad4
            self.session.commit()
    Pierre-Yves Chibon 981ad4
            self.assertEqual(msg, 'Request created')
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon 981ad4
            output = self.app.get('/test/pull-request/1', follow_redirects=True)
    Pierre-Yves Chibon 981ad4
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 981ad4
            self.assertIn(
    Pierre-Yves Chibon 981ad4
                '<title>Overview - test - Pagure</title>', output.data)
    Pierre-Yves Chibon 981ad4
            self.assertIn(
    Pierre-Yves Chibon 981ad4
                '
  • Fork is empty, there are no commits to '
  • Pierre-Yves Chibon 981ad4
                'request pulling', output.data)
    Pierre-Yves Chibon 981ad4
    Pierre-Yves Chibon a2d4e0
        def test_request_pulls(self):
    Pierre-Yves Chibon a2d4e0
            """ Test the request_pulls endpoint. """
    Pierre-Yves Chibon a2d4e0
            # No such project
    Pierre-Yves Chibon a2d4e0
            output = self.app.get('/test/pull-requests')
    Pierre-Yves Chibon a2d4e0
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            tests.create_projects(self.session)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            output = self.app.get('/test/pull-requests')
    Pierre-Yves Chibon a2d4e0
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('Pull-requests (0)', output.data)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('(0 Closed)', output.data)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            self.set_up_git_repo(new_project=None, branch_from='feature')
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            output = self.app.get('/test/pull-requests')
    Pierre-Yves Chibon a2d4e0
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('Pull-requests (1)', output.data)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('(0 Closed)', output.data)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            output = self.app.get('/test/pull-requests?status=closed')
    Pierre-Yves Chibon a2d4e0
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('Closed Pull-requests (0)', output.data)
    Pierre-Yves Chibon a2d4e0
            self.assertIn('(1 Open)', output.data)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            # Project w/o pull-request
    Pierre-Yves Chibon a2d4e0
            repo = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon a2d4e0
            settings = repo.settings
    Pierre-Yves Chibon a2d4e0
            settings['pull_requests'] = False
    Pierre-Yves Chibon a2d4e0
            repo.settings = settings
    Pierre-Yves Chibon a2d4e0
            self.session.add(repo)
    Pierre-Yves Chibon a2d4e0
            self.session.commit()
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon a2d4e0
            output = self.app.get('/test/pull-requests')
    Pierre-Yves Chibon a2d4e0
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon a2d4e0
    Pierre-Yves Chibon d61bcb
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon d61bcb
        def test_request_pull_patch(self, send_email):
    Pierre-Yves Chibon d61bcb
            """ Test the request_pull_patch endpoint. """
    Pierre-Yves Chibon d61bcb
            send_email.return_value = True
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            output = self.app.get('/test/pull-request/1.patch')
    Pierre-Yves Chibon d61bcb
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            tests.create_projects(self.session)
    Pierre-Yves Chibon d61bcb
            tests.create_projects_git(
    Pierre-Yves Chibon d61bcb
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon d61bcb
            self.set_up_git_repo(
    Pierre-Yves Chibon d61bcb
                new_project=None, branch_from='feature', mtype='merge')
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            output = self.app.get('/test/pull-request/100.patch')
    Pierre-Yves Chibon d61bcb
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            output = self.app.get('/test/pull-request/1.patch')
    Pierre-Yves Chibon d61bcb
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            npatch = []
    Pierre-Yves Chibon d61bcb
            for row in output.data.split('\n'):
    Pierre-Yves Chibon d61bcb
                if row.startswith('Date:'):
    Pierre-Yves Chibon d61bcb
                    continue
    Pierre-Yves Chibon d61bcb
                if row.startswith('From '):
    Pierre-Yves Chibon d61bcb
                    row = row.split(' ', 2)[2]
    Pierre-Yves Chibon d61bcb
                npatch.append(row)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            exp = """Mon Sep 17 00:00:00 2001
    Pierre-Yves Chibon d61bcb
    From: Alice Author <alice@authors.tld></alice@authors.tld>
    Pierre-Yves Chibon d61bcb
    Subject: A commit on branch feature
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
    ---
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
    diff --git a/.gitignore b/.gitignore
    Pierre-Yves Chibon d61bcb
    new file mode 100644
    Pierre-Yves Chibon d61bcb
    index 0000000..e4e5f6c
    Pierre-Yves Chibon d61bcb
    --- /dev/null
    Pierre-Yves Chibon d61bcb
    +++ b/.gitignore
    Pierre-Yves Chibon d61bcb
    @@ -0,0 +1 @@
    Pierre-Yves Chibon d61bcb
    +*~
    Pierre-Yves Chibon d61bcb
    \ No newline at end of file
    Pierre-Yves Chibon d61bcb
    diff --git a/sources b/sources
    Pierre-Yves Chibon d61bcb
    index 9f44358..2a552bb 100644
    Pierre-Yves Chibon d61bcb
    --- a/sources
    Pierre-Yves Chibon d61bcb
    +++ b/sources
    Pierre-Yves Chibon d61bcb
    @@ -1,2 +1,4 @@
    Pierre-Yves Chibon d61bcb
     foo
    Pierre-Yves Chibon d61bcb
    - bar
    Pierre-Yves Chibon d61bcb
    \ No newline at end of file
    Pierre-Yves Chibon d61bcb
    + bar
    Pierre-Yves Chibon d61bcb
    +baz
    Pierre-Yves Chibon d61bcb
    + boose
    Pierre-Yves Chibon d61bcb
    \ No newline at end of file
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
    """
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            patch = '\n'.join(npatch)
    Pierre-Yves Chibon d61bcb
            #print patch
    Pierre-Yves Chibon d61bcb
            self.assertEqual(patch, exp)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            # Project w/o pull-request
    Pierre-Yves Chibon d61bcb
            repo = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon d61bcb
            settings = repo.settings
    Pierre-Yves Chibon d61bcb
            settings['pull_requests'] = False
    Pierre-Yves Chibon d61bcb
            repo.settings = settings
    Pierre-Yves Chibon d61bcb
            self.session.add(repo)
    Pierre-Yves Chibon d61bcb
            self.session.commit()
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon d61bcb
            output = self.app.get('/test/pull-request/1.patch')
    Pierre-Yves Chibon d61bcb
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon d61bcb
    Pierre-Yves Chibon b54ac4
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon f75e13
        def test_request_pull_patch_close(self, send_email):
    Pierre-Yves Chibon f75e13
            """ Test the request_pull_patch endpoint with a closed PR. """
    Pierre-Yves Chibon f75e13
            send_email.return_value = True
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
            self.test_merge_request_pull_FF()
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
            output = self.app.get('/test/pull-request/1.patch')
    Pierre-Yves Chibon f75e13
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
            npatch = []
    Pierre-Yves Chibon f75e13
            for row in output.data.split('\n'):
    Pierre-Yves Chibon f75e13
                if row.startswith('Date:'):
    Pierre-Yves Chibon f75e13
                    continue
    Pierre-Yves Chibon f75e13
                if row.startswith('From '):
    Pierre-Yves Chibon f75e13
                    row = row.split(' ', 2)[2]
    Pierre-Yves Chibon f75e13
                npatch.append(row)
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
            exp = """Mon Sep 17 00:00:00 2001
    Pierre-Yves Chibon f75e13
    From: Alice Author <alice@authors.tld></alice@authors.tld>
    Pierre-Yves Chibon f75e13
    Subject: A commit on branch feature
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
    ---
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
    diff --git a/sources b/sources
    Pierre-Yves Chibon f75e13
    index 9f44358..2a552bb 100644
    Pierre-Yves Chibon f75e13
    --- a/sources
    Pierre-Yves Chibon f75e13
    +++ b/sources
    Pierre-Yves Chibon f75e13
    @@ -1,2 +1,4 @@
    Pierre-Yves Chibon f75e13
     foo
    Pierre-Yves Chibon f75e13
    - bar
    Pierre-Yves Chibon f75e13
    \ No newline at end of file
    Pierre-Yves Chibon f75e13
    + bar
    Pierre-Yves Chibon f75e13
    +baz
    Pierre-Yves Chibon f75e13
    + boose
    Pierre-Yves Chibon f75e13
    \ No newline at end of file
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
    """
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
            patch = '\n'.join(npatch)
    Pierre-Yves Chibon f75e13
            #print patch
    Pierre-Yves Chibon f75e13
            self.assertEqual(patch, exp)
    Pierre-Yves Chibon f75e13
    Pierre-Yves Chibon f75e13
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 300a00
        def test_request_pull_patch_empty_repo(self, send_email):
    Pierre-Yves Chibon 300a00
            """ Test the request_pull_patch endpoint against an empty repo. """
    Pierre-Yves Chibon 300a00
            send_email.return_value = True
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            tests.create_projects(self.session)
    Pierre-Yves Chibon 300a00
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon 300a00
                user_id=2,  # foo
    Pierre-Yves Chibon 300a00
                name='test',
    Pierre-Yves Chibon 300a00
                description='test project #1',
    Pierre-Yves Chibon 300a00
                hook_token='aaabbb',
    Pierre-Yves Chibon 300a00
                parent_id=1,
    Pierre-Yves Chibon 300a00
            )
    Pierre-Yves Chibon 300a00
            self.session.add(item)
    Pierre-Yves Chibon 300a00
            self.session.commit()
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            tests.create_projects_git(
    Pierre-Yves Chibon 300a00
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon 300a00
            tests.create_projects_git(
    Pierre-Yves Chibon 300a00
                os.path.join(tests.HERE, 'forks', 'foo'), bare=True)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            # Create a git repo to play with
    Pierre-Yves Chibon 300a00
            gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
    Pierre-Yves Chibon 300a00
            self.assertFalse(os.path.exists(gitrepo))
    Pierre-Yves Chibon 300a00
            os.makedirs(gitrepo)
    Pierre-Yves Chibon 300a00
            repo = pygit2.init_repository(gitrepo, bare=True)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            # Create a fork of this repo
    Pierre-Yves Chibon 300a00
            newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
    Pierre-Yves Chibon 300a00
            gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git')
    Pierre-Yves Chibon 300a00
            new_repo = pygit2.clone_repository(gitrepo, newpath)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            # Edit the sources file again
    Pierre-Yves Chibon 300a00
            with open(os.path.join(newpath, 'sources'), 'w') as stream:
    Pierre-Yves Chibon 300a00
                stream.write('foo\n bar\nbaz\n boose')
    Pierre-Yves Chibon 300a00
            new_repo.index.add('sources')
    Pierre-Yves Chibon 300a00
            new_repo.index.write()
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            # Commits the files added
    Pierre-Yves Chibon 300a00
            tree = new_repo.index.write_tree()
    Pierre-Yves Chibon 300a00
            author = pygit2.Signature(
    Pierre-Yves Chibon 300a00
                'Alice Author', 'alice@authors.tld')
    Pierre-Yves Chibon 300a00
            committer = pygit2.Signature(
    Pierre-Yves Chibon 300a00
                'Cecil Committer', 'cecil@committers.tld')
    Pierre-Yves Chibon 300a00
            new_repo.create_commit(
    Pierre-Yves Chibon 300a00
                'refs/heads/feature',
    Pierre-Yves Chibon 300a00
                author,
    Pierre-Yves Chibon 300a00
                committer,
    Pierre-Yves Chibon 300a00
                'A commit on branch feature',
    Pierre-Yves Chibon 300a00
                tree,
    Pierre-Yves Chibon 300a00
                []
    Pierre-Yves Chibon 300a00
            )
    Pierre-Yves Chibon 300a00
            refname = 'refs/heads/feature:refs/heads/feature'
    Pierre-Yves Chibon 300a00
            ori_remote = new_repo.remotes[0]
    Pierre-Yves Chibon 300a00
            ori_remote.push(refname)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            # Create a PR for these "changes" (there are none, both repos are
    Pierre-Yves Chibon 300a00
            # empty)
    Pierre-Yves Chibon 300a00
            project = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon 300a00
            msg = pagure.lib.new_pull_request(
    Pierre-Yves Chibon 300a00
                session=self.session,
    Pierre-Yves Chibon 300a00
                repo_from=item,
    Pierre-Yves Chibon 300a00
                branch_from='feature',
    Pierre-Yves Chibon 300a00
                repo_to=project,
    Pierre-Yves Chibon 300a00
                branch_to='master',
    Pierre-Yves Chibon 300a00
                title='PR from the feature branch',
    Pierre-Yves Chibon 300a00
                user='pingou',
    Pierre-Yves Chibon 300a00
                requestfolder=None,
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            )
    Pierre-Yves Chibon 300a00
            self.session.commit()
    Pierre-Yves Chibon 300a00
            self.assertEqual(msg, 'Request created')
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            output = self.app.get('/test/pull-request/1.patch', follow_redirects=True)
    Pierre-Yves Chibon 300a00
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            npatch = []
    Pierre-Yves Chibon 300a00
            for row in output.data.split('\n'):
    Pierre-Yves Chibon 300a00
                if row.startswith('Date:'):
    Pierre-Yves Chibon 300a00
                    continue
    Pierre-Yves Chibon 300a00
                if row.startswith('From '):
    Pierre-Yves Chibon 300a00
                    row = row.split(' ', 2)[2]
    Pierre-Yves Chibon 300a00
                npatch.append(row)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            exp = """Mon Sep 17 00:00:00 2001
    Pierre-Yves Chibon 300a00
    From: Alice Author <alice@authors.tld></alice@authors.tld>
    Pierre-Yves Chibon 300a00
    Subject: A commit on branch feature
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
    ---
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
    diff --git a/sources b/sources
    Pierre-Yves Chibon 300a00
    new file mode 100644
    Pierre-Yves Chibon 300a00
    index 0000000..2a552bb
    Pierre-Yves Chibon 300a00
    --- /dev/null
    Pierre-Yves Chibon 300a00
    +++ b/sources
    Pierre-Yves Chibon 300a00
    @@ -0,0 +1,4 @@
    Pierre-Yves Chibon 300a00
    +foo
    Pierre-Yves Chibon 300a00
    + bar
    Pierre-Yves Chibon 300a00
    +baz
    Pierre-Yves Chibon 300a00
    + boose
    Pierre-Yves Chibon 300a00
    \ No newline at end of file
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
    """
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
            patch = '\n'.join(npatch)
    Pierre-Yves Chibon 300a00
            #print patch
    Pierre-Yves Chibon 300a00
            self.assertEqual(patch, exp)
    Pierre-Yves Chibon 300a00
    Pierre-Yves Chibon 300a00
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon b54ac4
        def test_request_pull_patch_empty_fork(self, send_email):
    Pierre-Yves Chibon b54ac4
            """ Test the request_pull_patch endpoint from an empty fork. """
    Pierre-Yves Chibon b54ac4
            send_email.return_value = True
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            tests.create_projects(self.session)
    Pierre-Yves Chibon b54ac4
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon b54ac4
                user_id=2,  # foo
    Pierre-Yves Chibon b54ac4
                name='test',
    Pierre-Yves Chibon b54ac4
                description='test project #1',
    Pierre-Yves Chibon b54ac4
                hook_token='aaabbb',
    Pierre-Yves Chibon b54ac4
                parent_id=1,
    Pierre-Yves Chibon b54ac4
            )
    Pierre-Yves Chibon b54ac4
            self.session.add(item)
    Pierre-Yves Chibon b54ac4
            self.session.commit()
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            tests.create_projects_git(
    Pierre-Yves Chibon b54ac4
                os.path.join(tests.HERE, 'requests'), bare=True)
    Pierre-Yves Chibon b54ac4
            tests.create_projects_git(
    Pierre-Yves Chibon b54ac4
                os.path.join(tests.HERE, 'forks', 'foo'), bare=True)
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            # Create a git repo to play with
    Pierre-Yves Chibon b54ac4
            gitrepo = os.path.join(tests.HERE, 'repos', 'test.git')
    Pierre-Yves Chibon b54ac4
            self.assertFalse(os.path.exists(gitrepo))
    Pierre-Yves Chibon b54ac4
            os.makedirs(gitrepo)
    Pierre-Yves Chibon b54ac4
            repo = pygit2.init_repository(gitrepo, bare=True)
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            # Create a fork of this repo
    Pierre-Yves Chibon b54ac4
            newpath = tempfile.mkdtemp(prefix='pagure-fork-test')
    Pierre-Yves Chibon b54ac4
            gitrepo = os.path.join(tests.HERE, 'forks', 'foo', 'test.git')
    Pierre-Yves Chibon b54ac4
            new_repo = pygit2.clone_repository(gitrepo, newpath)
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            # Create a PR for these "changes" (there are none, both repos are
    Pierre-Yves Chibon b54ac4
            # empty)
    Pierre-Yves Chibon b54ac4
            project = pagure.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon b54ac4
            msg = pagure.lib.new_pull_request(
    Pierre-Yves Chibon b54ac4
                session=self.session,
    Pierre-Yves Chibon b54ac4
                repo_from=item,
    Pierre-Yves Chibon b54ac4
                branch_from='feature',
    Pierre-Yves Chibon b54ac4
                repo_to=project,
    Pierre-Yves Chibon b54ac4
                branch_to='master',
    Pierre-Yves Chibon b54ac4
                title='PR from the feature branch',
    Pierre-Yves Chibon b54ac4
                user='pingou',
    Pierre-Yves Chibon b54ac4
                requestfolder=None,
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            )
    Pierre-Yves Chibon b54ac4
            self.session.commit()
    Pierre-Yves Chibon b54ac4
            self.assertEqual(msg, 'Request created')
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon b54ac4
            output = self.app.get('/test/pull-request/1.patch', follow_redirects=True)
    Pierre-Yves Chibon b54ac4
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon b54ac4
            self.assertIn(
    Pierre-Yves Chibon b54ac4
                '<title>Overview - test - Pagure</title>', output.data)
    Pierre-Yves Chibon b54ac4
            self.assertIn(
    Pierre-Yves Chibon b54ac4
                '
  • Fork is empty, there are no commits to '
  • Pierre-Yves Chibon b54ac4
                'request pulling', output.data)
    Pierre-Yves Chibon b54ac4
    Pierre-Yves Chibon 9347b5
    Pierre-Yves Chibon 9347b5
    if __name__ == '__main__':
    Pierre-Yves Chibon 9347b5
        SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskForktests)
    Pierre-Yves Chibon 9347b5
        unittest.TextTestRunner(verbosity=2).run(SUITE)