Blame tests/test_pagure_flask_internal.py

Pierre-Yves Chibon 1edad5
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
"""
Pierre-Yves Chibon 1edad5
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
 Authors:
Pierre-Yves Chibon 1edad5
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
"""
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 1edad5
import pkg_resources
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 961d71
import json
Pierre-Yves Chibon 1edad5
import unittest
Pierre-Yves Chibon 1edad5
import shutil
Pierre-Yves Chibon 1edad5
import sys
Pierre-Yves Chibon 1edad5
import os
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 961d71
import pygit2
Pierre-Yves Chibon 1edad5
from mock import patch
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 1edad5
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
import pagure.lib
Pierre-Yves Chibon 1edad5
import tests
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
class PagureFlaskInternaltests(tests.Modeltests):
Pierre-Yves Chibon 1edad5
    """ Tests for flask Internal controller of pagure """
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
    def setUp(self):
Pierre-Yves Chibon 1edad5
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 1edad5
        super(PagureFlaskInternaltests, self).setUp()
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 961d71
        pagure.APP.config['IP_ALLOWED_INTERNAL'] = list(set(
Pierre-Yves Chibon 961d71
            pagure.APP.config['IP_ALLOWED_INTERNAL'] + [None]))
Pierre-Yves Chibon 1edad5
        pagure.SESSION = self.session
Pierre-Yves Chibon 1edad5
        pagure.internal.SESSION = self.session
Pierre-Yves Chibon f4b1a9
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon 72ed6b
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon f4b1a9
Pierre-Yves Chibon 961d71
        pagure.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon 961d71
        pagure.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon 961d71
            tests.HERE, 'forks')
Pierre-Yves Chibon 1edad5
        pagure.APP.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon 961d71
        pagure.APP.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon 961d71
        pagure.APP.config['DOCS_FOLDER'] = None
Pierre-Yves Chibon 1edad5
        self.app = pagure.APP.test_client()
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 1edad5
    def test_pull_request_add_comment(self, send_email):
Pierre-Yves Chibon 1edad5
        """ Test the pull_request_add_comment function.  """
Pierre-Yves Chibon 1edad5
        send_email.return_value = True
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        tests.create_projects(self.session)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 1edad5
            session=self.session,
Pierre-Yves Chibon 1edad5
            repo_from=repo,
Pierre-Yves Chibon 1edad5
            branch_from='feature',
Pierre-Yves Chibon 1edad5
            repo_to=repo,
Pierre-Yves Chibon 1edad5
            branch_to='master',
Pierre-Yves Chibon 1edad5
            title='PR from the feature branch',
Pierre-Yves Chibon 1edad5
            user='pingou',
Pierre-Yves Chibon 1edad5
            requestfolder=None,
Pierre-Yves Chibon 1edad5
        )
Pierre-Yves Chibon 1edad5
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        request = repo.requests[0]
Pierre-Yves Chibon 1edad5
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 1edad5
        self.assertEqual(len(request.discussion), 0)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        data = {
Pierre-Yves Chibon 1edad5
            'objid': 'foo',
Pierre-Yves Chibon 1edad5
        }
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Wrong http request
Pierre-Yves Chibon 1edad5
        output = self.app.post('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 405)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Invalid request
Pierre-Yves Chibon 1edad5
        output = self.app.put('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        data = {
Pierre-Yves Chibon 1edad5
            'objid': 'foo',
Pierre-Yves Chibon 1edad5
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 1edad5
        }
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Invalid objid
Pierre-Yves Chibon 1edad5
        output = self.app.put('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        data = {
Pierre-Yves Chibon 1edad5
            'objid': request.uid,
Pierre-Yves Chibon 1edad5
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 1edad5
        }
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Valid objid, in-complete data for a comment
Pierre-Yves Chibon 1edad5
        output = self.app.put('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        data = {
Pierre-Yves Chibon 1edad5
            'objid': request.uid,
Pierre-Yves Chibon 1edad5
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 1edad5
            'comment': 'Looks good to me!',
Pierre-Yves Chibon 1edad5
        }
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Add comment
Pierre-Yves Chibon 1edad5
        output = self.app.put('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 1edad5
        js_data = json.loads(output.data)
Pierre-Yves Chibon 1edad5
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 1edad5
        request = repo.requests[0]
Pierre-Yves Chibon 1edad5
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 1edad5
        self.assertEqual(len(request.discussion), 1)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Check the @localonly
Pierre-Yves Chibon 1edad5
        pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None)
Pierre-Yves Chibon 1edad5
        output = self.app.put('/pv/pull-request/comment/', data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 21ea63
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 21ea63
    def test_ticket_add_comment(self, send_email):
Pierre-Yves Chibon 21ea63
        """ Test the ticket_add_comment function.  """
Pierre-Yves Chibon 21ea63
        send_email.return_value = True
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        tests.create_projects(self.session)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Create issues to play with
Pierre-Yves Chibon 21ea63
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 21ea63
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 21ea63
            session=self.session,
Pierre-Yves Chibon 21ea63
            repo=repo,
Pierre-Yves Chibon 21ea63
            title='Test issue',
Pierre-Yves Chibon 21ea63
            content='We should work on this',
Pierre-Yves Chibon 21ea63
            user='pingou',
Pierre-Yves Chibon 21ea63
            ticketfolder=None
Pierre-Yves Chibon 21ea63
        )
Pierre-Yves Chibon 21ea63
        self.session.commit()
Pierre-Yves Chibon 21ea63
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        issue = repo.issues[0]
Pierre-Yves Chibon 21ea63
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        data = {
Pierre-Yves Chibon 21ea63
            'objid': 'foo',
Pierre-Yves Chibon 21ea63
        }
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Wrong http request
Pierre-Yves Chibon 21ea63
        output = self.app.post('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 405)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Invalid request
Pierre-Yves Chibon 21ea63
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        data = {
Pierre-Yves Chibon 21ea63
            'objid': 'foo',
Pierre-Yves Chibon 21ea63
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 21ea63
        }
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Invalid objid
Pierre-Yves Chibon 21ea63
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        data = {
Pierre-Yves Chibon 21ea63
            'objid': issue.uid,
Pierre-Yves Chibon 21ea63
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 21ea63
        }
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Valid objid, in-complete data for a comment
Pierre-Yves Chibon 21ea63
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        data = {
Pierre-Yves Chibon 0bf136
            'objid': issue.uid,
Pierre-Yves Chibon 0bf136
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 0bf136
            'comment': 'Looks good to me!',
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Add comment
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0bf136
        js_data = json.loads(output.data)
Pierre-Yves Chibon 0bf136
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 0bf136
        issue = repo.issues[0]
Pierre-Yves Chibon 0bf136
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Check the @localonly
Pierre-Yves Chibon 0bf136
        pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None)
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0bf136
    def test_private_ticket_add_comment(self, send_email):
Pierre-Yves Chibon 0bf136
        """ Test the ticket_add_comment function on a private ticket.  """
Pierre-Yves Chibon 0bf136
        send_email.return_value = True
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        tests.create_projects(self.session)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Create issues to play with
Pierre-Yves Chibon 0bf136
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 0bf136
        msg = pagure.lib.new_issue(
Pierre-Yves Chibon 0bf136
            session=self.session,
Pierre-Yves Chibon 0bf136
            repo=repo,
Pierre-Yves Chibon 0bf136
            title='Test issue',
Pierre-Yves Chibon 0bf136
            content='We should work on this, really',
Pierre-Yves Chibon 0bf136
            user='pingou',
Pierre-Yves Chibon 0bf136
            private=True,
Pierre-Yves Chibon 0bf136
            ticketfolder=None
Pierre-Yves Chibon 0bf136
        )
Pierre-Yves Chibon 0bf136
        self.session.commit()
Pierre-Yves Chibon 0bf136
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        issue = repo.issues[0]
Pierre-Yves Chibon 0bf136
        self.assertEqual(len(issue.comments), 0)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        data = {
Pierre-Yves Chibon 0bf136
            'objid': 'foo',
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Wrong http request
Pierre-Yves Chibon 0bf136
        output = self.app.post('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 405)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Invalid request
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        data = {
Pierre-Yves Chibon 0bf136
            'objid': 'foo',
Pierre-Yves Chibon 0bf136
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Invalid objid
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        data = {
Pierre-Yves Chibon 0bf136
            'objid': issue.uid,
Pierre-Yves Chibon 0bf136
            'useremail': 'foo@bar.com',
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Valid objid, un-allowed user for this (private) ticket
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        data = {
Pierre-Yves Chibon 0bf136
            'objid': issue.uid,
Pierre-Yves Chibon 0bf136
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Valid objid, un-allowed user for this (private) ticket
Pierre-Yves Chibon 0bf136
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        data = {
Pierre-Yves Chibon 21ea63
            'objid': issue.uid,
Pierre-Yves Chibon 21ea63
            'useremail': 'foo@pingou.com',
Pierre-Yves Chibon 21ea63
            'comment': 'Looks good to me!',
Pierre-Yves Chibon 21ea63
        }
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Add comment
Pierre-Yves Chibon 21ea63
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 21ea63
        js_data = json.loads(output.data)
Pierre-Yves Chibon 21ea63
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 21ea63
        issue = repo.issues[0]
Pierre-Yves Chibon 21ea63
        self.assertEqual(len(issue.comments), 1)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Check the @localonly
Pierre-Yves Chibon 21ea63
        pagure.APP.config['IP_ALLOWED_INTERNAL'].remove(None)
Pierre-Yves Chibon 21ea63
        output = self.app.put('/pv/ticket/comment/', data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 961d71
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 961d71
    def test_mergeable_request_pull_FF(self, send_email):
Pierre-Yves Chibon 961d71
        """ Test the mergeable_request_pull endpoint with a fast-forward
Pierre-Yves Chibon 961d71
        merge.
Pierre-Yves Chibon 961d71
        """
Pierre-Yves Chibon 961d71
        send_email.return_value = True
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Create a git repo to play with
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        gitrepo = os.path.join(tests.HERE, 'test.git')
Pierre-Yves Chibon 961d71
        self.assertFalse(os.path.exists(gitrepo))
Pierre-Yves Chibon 961d71
        os.makedirs(gitrepo)
Pierre-Yves Chibon 961d71
        repo = pygit2.init_repository(gitrepo)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Create a file in that git repo
Pierre-Yves Chibon 961d71
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 961d71
            stream.write('foo\n bar')
Pierre-Yves Chibon 961d71
        repo.index.add('sources')
Pierre-Yves Chibon 961d71
        repo.index.write()
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Commits the files added
Pierre-Yves Chibon 961d71
        tree = repo.index.write_tree()
Pierre-Yves Chibon 961d71
        author = pygit2.Signature(
Pierre-Yves Chibon 961d71
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 961d71
        committer = pygit2.Signature(
Pierre-Yves Chibon 961d71
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 961d71
        repo.create_commit(
Pierre-Yves Chibon 961d71
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 961d71
            author,
Pierre-Yves Chibon 961d71
            committer,
Pierre-Yves Chibon 961d71
            'Add sources file for testing',
Pierre-Yves Chibon 961d71
            # binary string representing the tree object ID
Pierre-Yves Chibon 961d71
            tree,
Pierre-Yves Chibon 961d71
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 961d71
            []
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Edit the sources file again
Pierre-Yves Chibon 961d71
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 961d71
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 961d71
        repo.index.add('sources')
Pierre-Yves Chibon 961d71
        repo.index.write()
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Commits the files added
Pierre-Yves Chibon 961d71
        tree = repo.index.write_tree()
Pierre-Yves Chibon 961d71
        author = pygit2.Signature(
Pierre-Yves Chibon 961d71
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 961d71
        committer = pygit2.Signature(
Pierre-Yves Chibon 961d71
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 961d71
        repo.create_commit(
Pierre-Yves Chibon 961d71
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 961d71
            author,
Pierre-Yves Chibon 961d71
            committer,
Pierre-Yves Chibon 961d71
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 961d71
            'consider',
Pierre-Yves Chibon 961d71
            # binary string representing the tree object ID
Pierre-Yves Chibon 961d71
            tree,
Pierre-Yves Chibon 961d71
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 961d71
            [first_commit.oid.hex]
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        second_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Create a PR for these changes
Pierre-Yves Chibon 961d71
        tests.create_projects(self.session)
Pierre-Yves Chibon 961d71
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 961d71
            session=self.session,
Pierre-Yves Chibon 961d71
            repo_from=project,
Pierre-Yves Chibon 961d71
            branch_from='feature',
Pierre-Yves Chibon 961d71
            repo_to=project,
Pierre-Yves Chibon 961d71
            branch_to='master',
Pierre-Yves Chibon 961d71
            title='PR from the feature branch',
Pierre-Yves Chibon 961d71
            user='pingou',
Pierre-Yves Chibon 961d71
            requestfolder=None,
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Check if the PR can be merged
Pierre-Yves Chibon 961d71
        data = {
Pierre-Yves Chibon 961d71
            'objid': 'blah',
Pierre-Yves Chibon 961d71
        }
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Missing CSRF
Pierre-Yves Chibon 961d71
        output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 961d71
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        user = tests.FakeUser()
Pierre-Yves Chibon 961d71
        user.username = 'pingou'
Pierre-Yves Chibon 961d71
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 961d71
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 961d71
            csrf_token = output.data.split(
Pierre-Yves Chibon 961d71
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            # Missing request identifier
Pierre-Yves Chibon 961d71
            data = {
Pierre-Yves Chibon 961d71
                'csrf_token': csrf_token,
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 961d71
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 961d71
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            # With all the desired information
Pierre-Yves Chibon 961d71
            project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 961d71
            data = {
Pierre-Yves Chibon 961d71
                'csrf_token': csrf_token,
Pierre-Yves Chibon 961d71
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 961d71
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 961d71
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 961d71
            exp = {
Pierre-Yves Chibon 961d71
              "code": "FFORWARD",
Pierre-Yves Chibon 961d71
              "message": "The pull-request can be merged and fast-forwarded",
Pierre-Yves Chibon 961d71
              "short_code": "Ok"
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            js_data = json.loads(output.data)
Pierre-Yves Chibon 961d71
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon bea61e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon bea61e
    def test_mergeable_request_pull_no_change(self, send_email):
Pierre-Yves Chibon bea61e
        """ Test the mergeable_request_pull endpoint when there are no
Pierre-Yves Chibon bea61e
        changes to merge.
Pierre-Yves Chibon bea61e
        """
Pierre-Yves Chibon bea61e
        send_email.return_value = True
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Create a git repo to play with
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        gitrepo = os.path.join(tests.HERE, 'test.git')
Pierre-Yves Chibon bea61e
        self.assertFalse(os.path.exists(gitrepo))
Pierre-Yves Chibon bea61e
        os.makedirs(gitrepo)
Pierre-Yves Chibon bea61e
        repo = pygit2.init_repository(gitrepo)
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Create a file in that git repo
Pierre-Yves Chibon bea61e
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon bea61e
            stream.write('foo\n bar')
Pierre-Yves Chibon bea61e
        repo.index.add('sources')
Pierre-Yves Chibon bea61e
        repo.index.write()
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Commits the files added
Pierre-Yves Chibon bea61e
        tree = repo.index.write_tree()
Pierre-Yves Chibon bea61e
        author = pygit2.Signature(
Pierre-Yves Chibon bea61e
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon bea61e
        committer = pygit2.Signature(
Pierre-Yves Chibon bea61e
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon bea61e
        repo.create_commit(
Pierre-Yves Chibon bea61e
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon bea61e
            author,
Pierre-Yves Chibon bea61e
            committer,
Pierre-Yves Chibon bea61e
            'Add sources file for testing',
Pierre-Yves Chibon bea61e
            # binary string representing the tree object ID
Pierre-Yves Chibon bea61e
            tree,
Pierre-Yves Chibon bea61e
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon bea61e
            []
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Edit the sources file again
Pierre-Yves Chibon bea61e
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon bea61e
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon bea61e
        repo.index.add('sources')
Pierre-Yves Chibon bea61e
        repo.index.write()
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Commits the files added
Pierre-Yves Chibon bea61e
        tree = repo.index.write_tree()
Pierre-Yves Chibon bea61e
        author = pygit2.Signature(
Pierre-Yves Chibon bea61e
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon bea61e
        committer = pygit2.Signature(
Pierre-Yves Chibon bea61e
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon bea61e
        repo.create_commit(
Pierre-Yves Chibon bea61e
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon bea61e
            author,
Pierre-Yves Chibon bea61e
            committer,
Pierre-Yves Chibon bea61e
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon bea61e
            'consider',
Pierre-Yves Chibon bea61e
            # binary string representing the tree object ID
Pierre-Yves Chibon bea61e
            tree,
Pierre-Yves Chibon bea61e
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon bea61e
            [first_commit.oid.hex]
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        second_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Create a PR for these changes
Pierre-Yves Chibon bea61e
        tests.create_projects(self.session)
Pierre-Yves Chibon bea61e
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon bea61e
            session=self.session,
Pierre-Yves Chibon bea61e
            repo_from=project,
Pierre-Yves Chibon bea61e
            branch_from='master',
Pierre-Yves Chibon bea61e
            repo_to=project,
Pierre-Yves Chibon bea61e
            branch_to='master',
Pierre-Yves Chibon bea61e
            title='PR from the feature branch',
Pierre-Yves Chibon bea61e
            user='pingou',
Pierre-Yves Chibon bea61e
            requestfolder=None,
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Check if the PR can be merged
Pierre-Yves Chibon bea61e
        data = {
Pierre-Yves Chibon bea61e
            'objid': 'blah',
Pierre-Yves Chibon bea61e
        }
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Missing CSRF
Pierre-Yves Chibon bea61e
        output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon bea61e
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        user = tests.FakeUser()
Pierre-Yves Chibon bea61e
        user.username = 'pingou'
Pierre-Yves Chibon bea61e
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon bea61e
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon bea61e
            csrf_token = output.data.split(
Pierre-Yves Chibon bea61e
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            # Missing request identifier
Pierre-Yves Chibon bea61e
            data = {
Pierre-Yves Chibon bea61e
                'csrf_token': csrf_token,
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon bea61e
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon bea61e
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            # With all the desired information
Pierre-Yves Chibon bea61e
            project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon bea61e
            data = {
Pierre-Yves Chibon bea61e
                'csrf_token': csrf_token,
Pierre-Yves Chibon bea61e
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon bea61e
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon bea61e
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon bea61e
            exp = {
Pierre-Yves Chibon bea61e
              "code": "NO_CHANGE",
Pierre-Yves Chibon bea61e
              "message": "Nothing to change, git is up to date",
Pierre-Yves Chibon bea61e
              "short_code": "No changes"
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            js_data = json.loads(output.data)
Pierre-Yves Chibon bea61e
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 6fe99a
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 6fe99a
    def test_mergeable_request_pull_merge(self, send_email):
Pierre-Yves Chibon 6fe99a
        """ Test the mergeable_request_pull endpoint when the changes can
Pierre-Yves Chibon 6fe99a
        be merged with a merge commit.
Pierre-Yves Chibon 6fe99a
        """
Pierre-Yves Chibon 6fe99a
        send_email.return_value = True
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Create a git repo to play with
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        gitrepo = os.path.join(tests.HERE, 'test.git')
Pierre-Yves Chibon 6fe99a
        self.assertFalse(os.path.exists(gitrepo))
Pierre-Yves Chibon 6fe99a
        os.makedirs(gitrepo)
Pierre-Yves Chibon 6fe99a
        repo = pygit2.init_repository(gitrepo)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Create a file in that git repo
Pierre-Yves Chibon 6fe99a
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 6fe99a
            stream.write('foo\n bar')
Pierre-Yves Chibon 6fe99a
        repo.index.add('sources')
Pierre-Yves Chibon 6fe99a
        repo.index.write()
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Commits the files added
Pierre-Yves Chibon 6fe99a
        tree = repo.index.write_tree()
Pierre-Yves Chibon 6fe99a
        author = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 6fe99a
        committer = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 6fe99a
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 6fe99a
            'Add sources file for testing',
Pierre-Yves Chibon 6fe99a
            # binary string representing the tree object ID
Pierre-Yves Chibon 6fe99a
            tree,
Pierre-Yves Chibon 6fe99a
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 6fe99a
            []
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Edit the sources file again
Pierre-Yves Chibon 6fe99a
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 6fe99a
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 6fe99a
        repo.index.add('sources')
Pierre-Yves Chibon 6fe99a
        repo.index.write()
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Commits the files added
Pierre-Yves Chibon 6fe99a
        tree = repo.index.write_tree()
Pierre-Yves Chibon 6fe99a
        author = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 6fe99a
        committer = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 6fe99a
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 6fe99a
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 6fe99a
            'consider',
Pierre-Yves Chibon 6fe99a
            # binary string representing the tree object ID
Pierre-Yves Chibon 6fe99a
            tree,
Pierre-Yves Chibon 6fe99a
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 6fe99a
            [first_commit.oid.hex]
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Create another file in the master branch
Pierre-Yves Chibon 6fe99a
        with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 6fe99a
            stream.write('*~')
Pierre-Yves Chibon 6fe99a
        repo.index.add('.gitignore')
Pierre-Yves Chibon 6fe99a
        repo.index.write()
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Commits the files added
Pierre-Yves Chibon 6fe99a
        tree = repo.index.write_tree()
Pierre-Yves Chibon 6fe99a
        author = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 6fe99a
        committer = pygit2.Signature(
Pierre-Yves Chibon 6fe99a
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 6fe99a
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 6fe99a
            'Add .gitignore file for testing',
Pierre-Yves Chibon 6fe99a
            # binary string representing the tree object ID
Pierre-Yves Chibon 6fe99a
            tree,
Pierre-Yves Chibon 6fe99a
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 6fe99a
            [first_commit.oid.hex]
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Create a PR for these changes
Pierre-Yves Chibon 6fe99a
        tests.create_projects(self.session)
Pierre-Yves Chibon 6fe99a
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 6fe99a
            session=self.session,
Pierre-Yves Chibon 6fe99a
            repo_from=project,
Pierre-Yves Chibon 6fe99a
            branch_from='feature',
Pierre-Yves Chibon 6fe99a
            repo_to=project,
Pierre-Yves Chibon 6fe99a
            branch_to='master',
Pierre-Yves Chibon 6fe99a
            title='PR from the feature branch',
Pierre-Yves Chibon 6fe99a
            user='pingou',
Pierre-Yves Chibon 6fe99a
            requestfolder=None,
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Check if the PR can be merged
Pierre-Yves Chibon 6fe99a
        data = {}
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Missing CSRF
Pierre-Yves Chibon 6fe99a
        output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 6fe99a
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        user = tests.FakeUser()
Pierre-Yves Chibon 6fe99a
        user.username = 'pingou'
Pierre-Yves Chibon 6fe99a
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 6fe99a
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 6fe99a
            csrf_token = output.data.split(
Pierre-Yves Chibon 6fe99a
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            # Missing request identifier
Pierre-Yves Chibon 6fe99a
            data = {
Pierre-Yves Chibon 6fe99a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 6fe99a
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            # With all the desired information
Pierre-Yves Chibon 6fe99a
            project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 6fe99a
            data = {
Pierre-Yves Chibon 6fe99a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 6fe99a
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 6fe99a
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fe99a
            exp = {
Pierre-Yves Chibon 6fe99a
              "code": "MERGE",
Pierre-Yves Chibon 6fe99a
              "message": "The pull-request can be merged with a merge commit",
Pierre-Yves Chibon 6fe99a
              "short_code": "With merge"
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            js_data = json.loads(output.data)
Pierre-Yves Chibon 6fe99a
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 25d55d
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 4a0bcf
    def test_mergeable_request_pull_conflicts(self, send_email):
Pierre-Yves Chibon 4a0bcf
        """ Test the mergeable_request_pull endpoint when the changes cannot
Pierre-Yves Chibon 4a0bcf
        be merged due to conflicts.
Pierre-Yves Chibon 25d55d
        """
Pierre-Yves Chibon 25d55d
        send_email.return_value = True
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Create a git repo to play with
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        gitrepo = os.path.join(tests.HERE, 'test.git')
Pierre-Yves Chibon 25d55d
        self.assertFalse(os.path.exists(gitrepo))
Pierre-Yves Chibon 25d55d
        os.makedirs(gitrepo)
Pierre-Yves Chibon 25d55d
        repo = pygit2.init_repository(gitrepo)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Create a file in that git repo
Pierre-Yves Chibon 25d55d
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 25d55d
            stream.write('foo\n bar')
Pierre-Yves Chibon 25d55d
        repo.index.add('sources')
Pierre-Yves Chibon 25d55d
        repo.index.write()
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Commits the files added
Pierre-Yves Chibon 25d55d
        tree = repo.index.write_tree()
Pierre-Yves Chibon 25d55d
        author = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 25d55d
        committer = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 25d55d
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 25d55d
            'Add sources file for testing',
Pierre-Yves Chibon 25d55d
            # binary string representing the tree object ID
Pierre-Yves Chibon 25d55d
            tree,
Pierre-Yves Chibon 25d55d
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 25d55d
            []
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Edit the sources file again
Pierre-Yves Chibon 25d55d
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 25d55d
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 25d55d
        repo.index.add('sources')
Pierre-Yves Chibon 25d55d
        repo.index.write()
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Commits the files added
Pierre-Yves Chibon 25d55d
        tree = repo.index.write_tree()
Pierre-Yves Chibon 25d55d
        author = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 25d55d
        committer = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 25d55d
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 25d55d
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 25d55d
            'consider',
Pierre-Yves Chibon 25d55d
            # binary string representing the tree object ID
Pierre-Yves Chibon 25d55d
            tree,
Pierre-Yves Chibon 25d55d
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 25d55d
            [first_commit.oid.hex]
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Create another file in the master branch
Pierre-Yves Chibon 25d55d
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 25d55d
            stream.write('foo\n bar\nbaz\n')
Pierre-Yves Chibon 25d55d
        repo.index.add('sources')
Pierre-Yves Chibon 25d55d
        repo.index.write()
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Commits the files added
Pierre-Yves Chibon 25d55d
        tree = repo.index.write_tree()
Pierre-Yves Chibon 25d55d
        author = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 25d55d
        committer = pygit2.Signature(
Pierre-Yves Chibon 25d55d
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 25d55d
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 25d55d
            'Add .gitignore file for testing',
Pierre-Yves Chibon 25d55d
            # binary string representing the tree object ID
Pierre-Yves Chibon 25d55d
            tree,
Pierre-Yves Chibon 25d55d
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 25d55d
            [first_commit.oid.hex]
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Create a PR for these changes
Pierre-Yves Chibon 25d55d
        tests.create_projects(self.session)
Pierre-Yves Chibon 25d55d
        project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon f9c5f9
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 25d55d
            session=self.session,
Pierre-Yves Chibon 25d55d
            repo_from=project,
Pierre-Yves Chibon 25d55d
            branch_from='feature',
Pierre-Yves Chibon 25d55d
            repo_to=project,
Pierre-Yves Chibon 25d55d
            branch_to='master',
Pierre-Yves Chibon 25d55d
            title='PR from the feature branch',
Pierre-Yves Chibon 25d55d
            user='pingou',
Pierre-Yves Chibon 25d55d
            requestfolder=None,
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Check if the PR can be merged
Pierre-Yves Chibon 25d55d
        data = {}
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Missing CSRF
Pierre-Yves Chibon 25d55d
        output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 25d55d
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        user = tests.FakeUser()
Pierre-Yves Chibon 25d55d
        user.username = 'pingou'
Pierre-Yves Chibon 25d55d
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 25d55d
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 25d55d
            csrf_token = output.data.split(
Pierre-Yves Chibon 25d55d
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            # Missing request identifier
Pierre-Yves Chibon 25d55d
            data = {
Pierre-Yves Chibon 25d55d
                'csrf_token': csrf_token,
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 25d55d
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 25d55d
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            # With all the desired information
Pierre-Yves Chibon 25d55d
            project = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 25d55d
            data = {
Pierre-Yves Chibon 25d55d
                'csrf_token': csrf_token,
Pierre-Yves Chibon 25d55d
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 25d55d
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 25d55d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 25d55d
            exp = {
Pierre-Yves Chibon 25d55d
              "code": "CONFLICTS",
Pierre-Yves Chibon 25d55d
              "message": "The pull-request cannot be merged due to conflicts",
Pierre-Yves Chibon 25d55d
              "short_code": "Conflicts"
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            js_data = json.loads(output.data)
Pierre-Yves Chibon 25d55d
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 9f395f
    def test_get_branches_of_commit(self):
Pierre-Yves Chibon 9f395f
        ''' Test the get_branches_of_commit from the internal API. '''
Pierre-Yves Chibon 9f395f
        tests.create_projects(self.session)
Pierre-Yves Chibon 72ed6b
        tests.create_projects_git(tests.HERE)
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        user = tests.FakeUser()
Pierre-Yves Chibon 9f395f
        user.username = 'pingou'
Pierre-Yves Chibon 9f395f
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 9f395f
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 0c0985
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9f395f
            csrf_token = output.data.split(
Pierre-Yves Chibon 9f395f
                b'name="csrf_token" type="hidden" value="')[1].split(b'">')[0]
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # No CSRF token
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'fakerepo',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {u'code': u'ERROR', u'message': u'Invalid input submitted'}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Invalid repo
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'fakerepo',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': u'No repo found with the information provided'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Rigth repo, no commit
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'test',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {u'code': u'ERROR', u'message': u'No commit id submitted'}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Request is fine, but git repo doesn't exist
Pierre-Yves Chibon 72ed6b
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 72ed6b
            user_id=1,  # pingou
Pierre-Yves Chibon 72ed6b
            name='test20',
Pierre-Yves Chibon 72ed6b
            description='test project #20',
Pierre-Yves Chibon 72ed6b
            hook_token='aaabbbhhh',
Pierre-Yves Chibon 72ed6b
        )
Pierre-Yves Chibon 72ed6b
        self.session.add(item)
Pierre-Yves Chibon 72ed6b
        self.session.commit()
Pierre-Yves Chibon 72ed6b
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 72ed6b
            'repo': 'test20',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': u'No git repo found with the information provided'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Create a git repo to play with
Pierre-Yves Chibon 9f395f
        gitrepo = os.path.join(tests.HERE, 'test.git')
Pierre-Yves Chibon 72ed6b
        self.assertTrue(os.path.exists(gitrepo))
Pierre-Yves Chibon 72ed6b
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Create a file in that git repo
Pierre-Yves Chibon 9f395f
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 9f395f
            stream.write('foo\n bar')
Pierre-Yves Chibon 9f395f
        repo.index.add('sources')
Pierre-Yves Chibon 9f395f
        repo.index.write()
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Commits the files added
Pierre-Yves Chibon 9f395f
        tree = repo.index.write_tree()
Pierre-Yves Chibon 9f395f
        author = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 9f395f
        committer = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 9f395f
        repo.create_commit(
Pierre-Yves Chibon 9f395f
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 9f395f
            'Add sources file for testing',
Pierre-Yves Chibon 9f395f
            # binary string representing the tree object ID
Pierre-Yves Chibon 9f395f
            tree,
Pierre-Yves Chibon 9f395f
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 9f395f
            []
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Edit the sources file again
Pierre-Yves Chibon 9f395f
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 9f395f
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 9f395f
        repo.index.add('sources')
Pierre-Yves Chibon 9f395f
        repo.index.write()
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Commits the files added
Pierre-Yves Chibon 9f395f
        tree = repo.index.write_tree()
Pierre-Yves Chibon 9f395f
        author = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 9f395f
        committer = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 9f395f
        repo.create_commit(
Pierre-Yves Chibon 9f395f
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 9f395f
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 9f395f
            'consider',
Pierre-Yves Chibon 9f395f
            # binary string representing the tree object ID
Pierre-Yves Chibon 9f395f
            tree,
Pierre-Yves Chibon 9f395f
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 9f395f
            [first_commit.oid.hex]
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Create another file in the master branch
Pierre-Yves Chibon 9f395f
        with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 9f395f
            stream.write('*~')
Pierre-Yves Chibon 9f395f
        repo.index.add('.gitignore')
Pierre-Yves Chibon 9f395f
        repo.index.write()
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Commits the files added
Pierre-Yves Chibon 9f395f
        tree = repo.index.write_tree()
Pierre-Yves Chibon 9f395f
        author = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 9f395f
        committer = pygit2.Signature(
Pierre-Yves Chibon 9f395f
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 9f395f
        commit_hash = repo.create_commit(
Pierre-Yves Chibon 9f395f
            'refs/heads/feature_branch',  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 9f395f
            'Add .gitignore file for testing',
Pierre-Yves Chibon 9f395f
            # binary string representing the tree object ID
Pierre-Yves Chibon 9f395f
            tree,
Pierre-Yves Chibon 9f395f
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 9f395f
            [first_commit.oid.hex]
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # All good but the commit id
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'test',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': 'This commit could not be found in this repo'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # All good
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'test',
Pierre-Yves Chibon 9f395f
            'commit_id': commit_hash,
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'OK',
Pierre-Yves Chibon 9f395f
                u'branches': ['feature_branch'],
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 1edad5
if __name__ == '__main__':
Pierre-Yves Chibon 1edad5
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon 1edad5
        PagureFlaskInternaltests)
Pierre-Yves Chibon 1edad5
    unittest.TextTestRunner(verbosity=2).run(SUITE)