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 15f25e
 (c) 2015-2017 - 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
Aurélien Bompard 626417
from __future__ import unicode_literals
Aurélien Bompard 626417
Pierre-Yves Chibon 1edad5
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 1edad5
import pkg_resources
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon e8a127
import datetime
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 c1b474
import time
Pierre-Yves Chibon 1edad5
import os
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 961d71
import pygit2
Pierre-Yves Chibon a3de64
from mock import patch, MagicMock, Mock
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
Farhaan Bukhsh 72e9db
import pagure
Pierre-Yves Chibon 1edad5
import pagure.lib
Pierre-Yves Chibon 1edad5
import tests
Pierre-Yves Chibon cd4dad
from pagure.lib.repo import PagureRepo
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 b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = list(set(
Pierre-Yves Chibon b130e5
            pagure.config.config['IP_ALLOWED_INTERNAL'] + [None]))
Pierre-Yves Chibon b130e5
Pierre-Yves Chibon b130e5
        pagure.config.config['GIT_FOLDER'] = os.path.join(
Patrick Uiterwijk 170974
            self.path, 'repos')
Pierre-Yves Chibon b130e5
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon b130e5
        pagure.config.config['TICKETS_FOLDER'] = None
Pierre-Yves Chibon b130e5
        pagure.config.config['DOCS_FOLDER'] = None
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 b130e5
        repo = pagure.lib.get_authorized_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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1edad5
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 1edad5
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_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 b130e5
        before = pagure.config.config['IP_ALLOWED_INTERNAL'][:]
Pierre-Yves Chibon b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = []
Pierre-Yves Chibon 450dd6
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 b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = before[:]
Pierre-Yves Chibon 450dd6
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 b130e5
        repo = pagure.lib.get_authorized_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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0bf136
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 0bf136
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_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 b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'].remove(None)
Pierre-Yves Chibon b130e5
        before = pagure.config.config['IP_ALLOWED_INTERNAL'][:]
Pierre-Yves Chibon b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = []
Pierre-Yves Chibon 450dd6
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 b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = before[:]
Pierre-Yves Chibon 450dd6
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 b130e5
        repo = pagure.lib.get_authorized_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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 21ea63
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 21ea63
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_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 b130e5
        before = pagure.config.config['IP_ALLOWED_INTERNAL'][:]
Pierre-Yves Chibon b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = []
Pierre-Yves Chibon 450dd6
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 b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = before[:]
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 961d71
    @patch('pagure.lib.notify.send_email')
Vivek Anand d5809b
    def test_private_ticket_add_comment_acl(self, send_email):
Vivek Anand d5809b
        """ Test the ticket_add_comment function on a private ticket.  """
Vivek Anand d5809b
        send_email.return_value = True
Vivek Anand d5809b
Vivek Anand d5809b
        tests.create_projects(self.session)
Vivek Anand d5809b
Vivek Anand d5809b
        # Create issues to play with
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        msg = pagure.lib.new_issue(
Vivek Anand d5809b
            session=self.session,
Vivek Anand d5809b
            repo=repo,
Vivek Anand d5809b
            title='Test issue',
Vivek Anand d5809b
            content='We should work on this, really',
Vivek Anand d5809b
            user='pingou',
Vivek Anand d5809b
            private=True,
Vivek Anand d5809b
            ticketfolder=None
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Vivek Anand d5809b
        self.assertEqual(msg.title, 'Test issue')
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        issue = repo.issues[0]
Vivek Anand d5809b
        self.assertEqual(len(issue.comments), 0)
Vivek Anand d5809b
Vivek Anand d5809b
        # Currently, he is just an average user,
Vivek Anand d5809b
        # He doesn't have any access in this repo
Vivek Anand d5809b
        data = {
Vivek Anand d5809b
            'objid': issue.uid,
Vivek Anand d5809b
            'useremail': 'foo@bar.com',
Vivek Anand d5809b
            'comment': 'Looks good to me!',
Vivek Anand d5809b
        }
Vivek Anand d5809b
Vivek Anand d5809b
        # Valid objid, un-allowed user for this (private) ticket
Vivek Anand d5809b
        output = self.app.put('/pv/ticket/comment/', data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        # Let's promote him to be a ticketer
Vivek Anand d5809b
        # He shoudn't be able to comment even then though
Vivek Anand d5809b
        msg = pagure.lib.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Vivek Anand d5809b
            new_user='foo',
Vivek Anand d5809b
            user='pingou',
Vivek Anand d5809b
            access='ticket'
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Vivek Anand d5809b
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.users]), ['foo'])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.committers]), [])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.admins]), [])
Pierre-Yves Chibon 15f25e
Vivek Anand d5809b
        output = self.app.put('/pv/ticket/comment/', data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        # Let's promote him to be a committer
Vivek Anand d5809b
        # He should be able to comment
Vivek Anand d5809b
        msg = pagure.lib.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Vivek Anand d5809b
            new_user='foo',
Vivek Anand d5809b
            user='pingou',
Vivek Anand d5809b
            access='commit'
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Vivek Anand d5809b
        self.assertEqual(msg, 'User access updated')
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.users]), ['foo'])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.committers]), ['foo'])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.admins]), [])
Pierre-Yves Chibon 15f25e
Vivek Anand d5809b
        # Add comment
Vivek Anand d5809b
        output = self.app.put('/pv/ticket/comment/', data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Vivek Anand d5809b
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        issue = repo.issues[0]
Vivek Anand d5809b
        self.assertEqual(len(issue.comments), 1)
Vivek Anand d5809b
Vivek Anand d5809b
        # Let's promote him to be a admin
Vivek Anand d5809b
        # He should be able to comment
Vivek Anand d5809b
        msg = pagure.lib.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Vivek Anand d5809b
            new_user='foo',
Vivek Anand d5809b
            user='pingou',
Vivek Anand d5809b
            access='admin'
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Vivek Anand d5809b
        self.assertEqual(msg, 'User access updated')
Pierre-Yves Chibon 15f25e
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.users]), ['foo'])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.committers]), ['foo'])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 15f25e
            sorted([u.username for u in repo.admins]), ['foo'])
Pierre-Yves Chibon 15f25e
Vivek Anand d5809b
        # Add comment
Vivek Anand d5809b
        output = self.app.put('/pv/ticket/comment/', data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Vivek Anand d5809b
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Vivek Anand d5809b
        issue = repo.issues[0]
Vivek Anand d5809b
        self.assertEqual(len(issue.comments), 2)
Vivek Anand d5809b
Vivek Anand d5809b
        # Check the @localonly
Pierre-Yves Chibon b130e5
        before = pagure.config.config['IP_ALLOWED_INTERNAL'][:]
Pierre-Yves Chibon b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = []
Vivek Anand d5809b
Vivek Anand d5809b
        output = self.app.put('/pv/ticket/comment/', data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon b130e5
        pagure.config.config['IP_ALLOWED_INTERNAL'] = before[:]
Vivek Anand d5809b
Vivek Anand d5809b
    @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 cd4dad
        origgitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon cd4dad
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon cd4dad
        os.makedirs(origgitrepo)
Pierre-Yves Chibon cd4dad
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon cd4dad
        os.makedirs(os.path.join(self.path, 'repos_tmp'))
Pierre-Yves Chibon cd4dad
        gitrepo = os.path.join(self.path, 'repos_tmp', 'test.git')
Pierre-Yves Chibon cd4dad
        repo = pygit2.clone_repository(origgitrepo, 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 cd4dad
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 cd4dad
        refname = 'refs/heads/feature:refs/heads/feature'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 b130e5
        project = pagure.lib.get_authorized_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 b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 961d71
            output = self.app.get('/test/adduser')
Aurélien Bompard 626417
            csrf_token = output.get_data(as_text=True).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 b130e5
            project = pagure.lib.get_authorized_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
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
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
Patrick Uiterwijk 170974
        gitrepo = os.path.join(self.path, 'repos', '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 b130e5
        project = pagure.lib.get_authorized_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 b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon bea61e
            output = self.app.get('/test/adduser')
Aurélien Bompard 626417
            csrf_token = output.get_data(as_text=True).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 b130e5
            project = pagure.lib.get_authorized_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
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
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 cd4dad
        origgitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon cd4dad
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon cd4dad
        os.makedirs(origgitrepo)
Pierre-Yves Chibon cd4dad
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon cd4dad
        os.makedirs(os.path.join(self.path, 'repos_tmp'))
Pierre-Yves Chibon cd4dad
        gitrepo = os.path.join(self.path, 'repos_tmp', 'test.git')
Pierre-Yves Chibon cd4dad
        repo = pygit2.clone_repository(origgitrepo, 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 cd4dad
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 cd4dad
        refname = 'refs/heads/feature:refs/heads/feature'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 cd4dad
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 b130e5
        project = pagure.lib.get_authorized_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 b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 6fe99a
            output = self.app.get('/test/adduser')
Aurélien Bompard 626417
            csrf_token = output.get_data(as_text=True).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 b130e5
            project = pagure.lib.get_authorized_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 705645
                'force': True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 705645
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 705645
            exp = {
Pierre-Yves Chibon 705645
              "code": "MERGE",
Pierre-Yves Chibon 705645
              "message": "The pull-request can be merged with a merge commit",
Pierre-Yves Chibon 705645
              "short_code": "With merge"
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # Asking a second time will trigger the cache
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 705645
                'csrf_token': csrf_token,
Pierre-Yves Chibon 705645
                '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
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
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 cd4dad
        origgitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon cd4dad
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon cd4dad
        os.makedirs(origgitrepo)
Pierre-Yves Chibon cd4dad
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon cd4dad
        os.makedirs(os.path.join(self.path, 'repos_tmp'))
Pierre-Yves Chibon cd4dad
        gitrepo = os.path.join(self.path, 'repos_tmp', 'test.git')
Pierre-Yves Chibon cd4dad
        repo = pygit2.clone_repository(origgitrepo, 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 cd4dad
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 cd4dad
        refname = 'refs/heads/feature:refs/heads/feature'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 cd4dad
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon cd4dad
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon cd4dad
        PagureRepo.push(ori_remote, refname)
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 b130e5
        project = pagure.lib.get_authorized_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 b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 25d55d
            output = self.app.get('/test/adduser')
Aurélien Bompard 626417
            csrf_token = output.get_data(as_text=True).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 b130e5
            project = pagure.lib.get_authorized_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
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 25d55d
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 705645
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 705645
    @patch(
Pierre-Yves Chibon 705645
        'pagure.lib.git.merge_pull_request',
Pierre-Yves Chibon 705645
        MagicMock(side_effect=pagure.exceptions.PagureException('error')))
Pierre-Yves Chibon 705645
    def test_mergeable_request_pull_merge_pagureerror(self):
Pierre-Yves Chibon 705645
        """ Test the mergeable_request_pull endpoint when the backend
Pierre-Yves Chibon 705645
        raises an GitError exception.
Pierre-Yves Chibon 705645
        """
Pierre-Yves Chibon 705645
        # Create a git repo to play with
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        origgitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 705645
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon 705645
        os.makedirs(origgitrepo)
Pierre-Yves Chibon 705645
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 705645
        os.makedirs(os.path.join(self.path, 'repos_tmp'))
Pierre-Yves Chibon 705645
        gitrepo = os.path.join(self.path, 'repos_tmp', 'test.git')
Pierre-Yves Chibon 705645
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a file in that git repo
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('foo\n bar')
Pierre-Yves Chibon 705645
        repo.index.add('sources')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add sources file for testing',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            []
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 705645
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Edit the sources file again
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 705645
        repo.index.add('sources')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 705645
            'consider',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            [first_commit.oid.hex]
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        refname = 'refs/heads/feature:refs/heads/feature'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create another file in the master branch
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('*~')
Pierre-Yves Chibon 705645
        repo.index.add('.gitignore')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add .gitignore file for testing',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            [first_commit.oid.hex]
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a PR for these changes
Pierre-Yves Chibon 705645
        tests.create_projects(self.session)
Pierre-Yves Chibon 705645
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 705645
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 705645
            session=self.session,
Pierre-Yves Chibon 705645
            repo_from=project,
Pierre-Yves Chibon 705645
            branch_from='feature',
Pierre-Yves Chibon 705645
            repo_to=project,
Pierre-Yves Chibon 705645
            branch_to='master',
Pierre-Yves Chibon 705645
            title='PR from the feature branch',
Pierre-Yves Chibon 705645
            user='pingou',
Pierre-Yves Chibon 705645
            requestfolder=None,
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        self.session.commit()
Pierre-Yves Chibon 705645
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 705645
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Check if the PR can be merged
Pierre-Yves Chibon 705645
        data = {}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        user = tests.FakeUser()
Pierre-Yves Chibon 705645
        user.username = 'pingou'
Pierre-Yves Chibon 705645
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 705645
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 705645
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # With all the desired information
Pierre-Yves Chibon 705645
            project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 705645
                'csrf_token': csrf_token,
Pierre-Yves Chibon 705645
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 705645
                'force': True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 705645
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 500)
Pierre-Yves Chibon 705645
            exp = {u'code': u'CONFLICTS', u'message': u'error'}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon 705645
    @patch(
Pierre-Yves Chibon 705645
        'pagure.lib.git.merge_pull_request',
Pierre-Yves Chibon 705645
        MagicMock(side_effect=pygit2.GitError('git error')))
Pierre-Yves Chibon 705645
    def test_mergeable_request_pull_merge_giterror(self):
Pierre-Yves Chibon 705645
        """ Test the mergeable_request_pull endpoint when the backend
Pierre-Yves Chibon 705645
        raises an GitError exception.
Pierre-Yves Chibon 705645
        """
Pierre-Yves Chibon 705645
        # Create a git repo to play with
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        origgitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 705645
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon 705645
        os.makedirs(origgitrepo)
Pierre-Yves Chibon 705645
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 705645
        os.makedirs(os.path.join(self.path, 'repos_tmp'))
Pierre-Yves Chibon 705645
        gitrepo = os.path.join(self.path, 'repos_tmp', 'test.git')
Pierre-Yves Chibon 705645
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a file in that git repo
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('foo\n bar')
Pierre-Yves Chibon 705645
        repo.index.add('sources')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add sources file for testing',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            []
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 705645
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Edit the sources file again
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 705645
        repo.index.add('sources')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 705645
            'consider',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            [first_commit.oid.hex]
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        refname = 'refs/heads/feature:refs/heads/feature'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create another file in the master branch
Pierre-Yves Chibon 705645
        with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 705645
            stream.write('*~')
Pierre-Yves Chibon 705645
        repo.index.add('.gitignore')
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 705645
        author = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 705645
        committer = pygit2.Signature(
Pierre-Yves Chibon 705645
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 705645
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 705645
            'Add .gitignore file for testing',
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 705645
            [first_commit.oid.hex]
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        refname = 'refs/heads/master:refs/heads/master'
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a PR for these changes
Pierre-Yves Chibon 705645
        tests.create_projects(self.session)
Pierre-Yves Chibon 705645
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 705645
        req = pagure.lib.new_pull_request(
Pierre-Yves Chibon 705645
            session=self.session,
Pierre-Yves Chibon 705645
            repo_from=project,
Pierre-Yves Chibon 705645
            branch_from='feature',
Pierre-Yves Chibon 705645
            repo_to=project,
Pierre-Yves Chibon 705645
            branch_to='master',
Pierre-Yves Chibon 705645
            title='PR from the feature branch',
Pierre-Yves Chibon 705645
            user='pingou',
Pierre-Yves Chibon 705645
            requestfolder=None,
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        self.session.commit()
Pierre-Yves Chibon 705645
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 705645
        self.assertEqual(req.title, 'PR from the feature branch')
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Check if the PR can be merged
Pierre-Yves Chibon 705645
        data = {}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        user = tests.FakeUser()
Pierre-Yves Chibon 705645
        user.username = 'pingou'
Pierre-Yves Chibon 705645
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 705645
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 705645
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # With all the desired information
Pierre-Yves Chibon 705645
            project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 705645
                'csrf_token': csrf_token,
Pierre-Yves Chibon 705645
                'requestid': project.requests[0].uid,
Pierre-Yves Chibon 705645
                'force': True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 705645
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 409)
Pierre-Yves Chibon 705645
            exp = {u'code': u'CONFLICTS', u'message': u'git error'}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
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)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        user = tests.FakeUser()
Pierre-Yves Chibon 9f395f
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 9f395f
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 0c0985
            self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
            csrf_token = output.get_data(as_text=True).split(
Aurélien Bompard 626417
                'name="csrf_token" type="hidden" value="')[1].split('">')[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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR', 'message': '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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Aurélien Bompard 626417
                'code': 'ERROR',
Aurélien Bompard 626417
                'message': '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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR', 'message': '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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Aurélien Bompard 626417
                'code': 'ERROR',
Aurélien Bompard 626417
                'message': '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
Patrick Uiterwijk 170974
        gitrepo = os.path.join(self.path, 'repos', '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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Aurélien Bompard 626417
                'code': 'ERROR',
Aurélien Bompard 626417
                '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)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Aurélien Bompard 626417
                'code': 'OK',
Aurélien Bompard 626417
                'branches': ['feature_branch'],
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Michael Watters d19655
    def test_get_branches_of_commit_with_unrelated_branches(self):
Michael Watters d19655
        ''' Test the get_branches_of_commit from the internal API. '''
Michael Watters d19655
        tests.create_projects(self.session)
Michael Watters d19655
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Michael Watters d19655
Michael Watters d19655
        user = tests.FakeUser(username='pingou')
Michael Watters d19655
        with tests.user_set(self.app.application, user):
Michael Watters d19655
            csrf_token = self.get_csrf()
Michael Watters d19655
Michael Watters d19655
        # Create a git repo to play with
Michael Watters d19655
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Michael Watters d19655
        self.assertTrue(os.path.exists(gitrepo))
Michael Watters d19655
        repo = pygit2.Repository(gitrepo)
Michael Watters d19655
Michael Watters d19655
        # Create a file in that git repo
Michael Watters d19655
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Michael Watters d19655
            stream.write('foo\n bar')
Michael Watters d19655
        repo.index.add('sources')
Michael Watters d19655
        repo.index.write()
Michael Watters d19655
Michael Watters d19655
        # Commits the files added
Michael Watters d19655
        tree = repo.index.write_tree()
Michael Watters d19655
        author = pygit2.Signature(
Michael Watters d19655
            'Alice Author', 'alice@authors.tld')
Michael Watters d19655
        committer = pygit2.Signature(
Michael Watters d19655
            'Cecil Committer', 'cecil@committers.tld')
Michael Watters d19655
        repo.create_commit(
Michael Watters d19655
            'refs/heads/master',  # the name of the reference to update
Michael Watters d19655
            author,
Michael Watters d19655
            committer,
Michael Watters d19655
            'Add sources file for testing',
Michael Watters d19655
            # binary string representing the tree object ID
Michael Watters d19655
            tree,
Michael Watters d19655
            # list of binary strings representing parents of the new commit
Michael Watters d19655
            []
Michael Watters d19655
        )
Michael Watters d19655
Michael Watters d19655
        first_commit = repo.revparse_single('HEAD')
Michael Watters d19655
Michael Watters d19655
        # Edit the sources file again
Michael Watters d19655
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Michael Watters d19655
            stream.write('foo\n bar\nbaz\n boose')
Michael Watters d19655
        repo.index.add('sources')
Michael Watters d19655
        repo.index.write()
Michael Watters d19655
Michael Watters d19655
        # Commits the files added, but unrelated with the first commit
Michael Watters d19655
        tree = repo.index.write_tree()
Michael Watters d19655
        author = pygit2.Signature(
Michael Watters d19655
            'Alice Author', 'alice@authors.tld')
Michael Watters d19655
        committer = pygit2.Signature(
Michael Watters d19655
            'Cecil Committer', 'cecil@committers.tld')
Michael Watters d19655
        commit = repo.create_commit(
Michael Watters d19655
            'refs/heads/feature',  # the name of the reference to update
Michael Watters d19655
            author,
Michael Watters d19655
            committer,
Michael Watters d19655
            'Add baz and boose to the sources\n\n There are more objects to '
Michael Watters d19655
            'consider',
Michael Watters d19655
            # binary string representing the tree object ID
Michael Watters d19655
            tree,
Michael Watters d19655
            # list of binary strings representing parents of the new commit
Michael Watters d19655
            []
Michael Watters d19655
        )
Michael Watters d19655
        commit_hash = commit.hex
Michael Watters d19655
Michael Watters d19655
        # All good
Michael Watters d19655
        data = {
Michael Watters d19655
            'repo': 'test',
Michael Watters d19655
            'commit_id': commit_hash,
Michael Watters d19655
            'csrf_token': csrf_token,
Michael Watters d19655
        }
Michael Watters d19655
        output = self.app.post('/pv/branches/commit/', data=data)
Michael Watters d19655
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Michael Watters d19655
        self.assertDictEqual(
Michael Watters d19655
            js_data,
Michael Watters d19655
            {
Michael Watters d19655
                u'code': u'OK',
Michael Watters d19655
                u'branches': ['feature'],
Michael Watters d19655
            }
Michael Watters d19655
        )
Michael Watters d19655
Pierre-Yves Chibon 17e9e2
    def test_get_branches_head(self):
Pierre-Yves Chibon 17e9e2
        ''' Test the get_branches_head from the internal API. '''
Pierre-Yves Chibon 17e9e2
        tests.create_projects(self.session)
Pierre-Yves Chibon 17e9e2
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        user = tests.FakeUser()
Pierre-Yves Chibon 17e9e2
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 17e9e2
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # No CSRF token
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 17e9e2
            'repo': 'fakerepo',
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 17e9e2
        output = self.app.post('/pv/branches/heads/', data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR', 'message': 'Invalid input submitted'}
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Invalid repo
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 17e9e2
            'repo': 'fakerepo',
Pierre-Yves Chibon 17e9e2
            'commit_id': 'foo',
Pierre-Yves Chibon 17e9e2
            'csrf_token': csrf_token,
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 17e9e2
        output = self.app.post('/pv/branches/heads/', data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Aurélien Bompard 626417
                'code': 'ERROR',
Aurélien Bompard 626417
                'message': 'No repo found with the information provided'
Pierre-Yves Chibon 17e9e2
            }
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Rigth repo, no commit
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 17e9e2
            'repo': 'test',
Pierre-Yves Chibon 17e9e2
            'csrf_token': csrf_token,
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        output = self.app.post('/pv/branches/heads/', data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Aurélien Bompard 626417
            {"branches": {}, "code": "OK", "heads": {}}
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Request is fine, but git repo doesn't exist
Pierre-Yves Chibon 17e9e2
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 17e9e2
            user_id=1,  # pingou
Pierre-Yves Chibon 17e9e2
            name='test20',
Pierre-Yves Chibon 17e9e2
            description='test project #20',
Pierre-Yves Chibon 17e9e2
            hook_token='aaabbbhhh',
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
        self.session.add(item)
Pierre-Yves Chibon 17e9e2
        self.session.commit()
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 17e9e2
            'repo': 'test20',
Pierre-Yves Chibon 17e9e2
            'csrf_token': csrf_token,
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 17e9e2
        output = self.app.post('/pv/branches/heads/', data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Aurélien Bompard 626417
                'code': 'ERROR',
Aurélien Bompard 626417
                'message': 'No git repo found with the information provided'
Pierre-Yves Chibon 17e9e2
            }
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Create a git repo to play with
Pierre-Yves Chibon 17e9e2
        gitrepo = os.path.join(self.path, 'repos', 'test.git')
Pierre-Yves Chibon 17e9e2
        self.assertTrue(os.path.exists(gitrepo))
Pierre-Yves Chibon 17e9e2
        repo = pygit2.Repository(gitrepo)
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Create a file in that git repo
Pierre-Yves Chibon 17e9e2
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 17e9e2
            stream.write('foo\n bar')
Pierre-Yves Chibon 17e9e2
        repo.index.add('sources')
Pierre-Yves Chibon 17e9e2
        repo.index.write()
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Commits the files added
Pierre-Yves Chibon 17e9e2
        tree = repo.index.write_tree()
Pierre-Yves Chibon 17e9e2
        author = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 17e9e2
        committer = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 17e9e2
        repo.create_commit(
Pierre-Yves Chibon 17e9e2
            'refs/heads/master',  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 17e9e2
            'Add sources file for testing',
Pierre-Yves Chibon 17e9e2
            # binary string representing the tree object ID
Pierre-Yves Chibon 17e9e2
            tree,
Pierre-Yves Chibon 17e9e2
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 17e9e2
            []
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        first_commit = repo.revparse_single('HEAD')
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Edit the sources file again
Pierre-Yves Chibon 17e9e2
        with open(os.path.join(gitrepo, 'sources'), 'w') as stream:
Pierre-Yves Chibon 17e9e2
            stream.write('foo\n bar\nbaz\n boose')
Pierre-Yves Chibon 17e9e2
        repo.index.add('sources')
Pierre-Yves Chibon 17e9e2
        repo.index.write()
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Commits the files added
Pierre-Yves Chibon 17e9e2
        tree = repo.index.write_tree()
Pierre-Yves Chibon 17e9e2
        author = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 17e9e2
        committer = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 17e9e2
        repo.create_commit(
Pierre-Yves Chibon 17e9e2
            'refs/heads/feature',  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 17e9e2
            'Add baz and boose to the sources\n\n There are more objects to '
Pierre-Yves Chibon 17e9e2
            'consider',
Pierre-Yves Chibon 17e9e2
            # binary string representing the tree object ID
Pierre-Yves Chibon 17e9e2
            tree,
Pierre-Yves Chibon 17e9e2
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 17e9e2
            [first_commit.oid.hex]
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Create another file in the master branch
Pierre-Yves Chibon 17e9e2
        with open(os.path.join(gitrepo, '.gitignore'), 'w') as stream:
Pierre-Yves Chibon 17e9e2
            stream.write('*~')
Pierre-Yves Chibon 17e9e2
        repo.index.add('.gitignore')
Pierre-Yves Chibon 17e9e2
        repo.index.write()
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Commits the files added
Pierre-Yves Chibon 17e9e2
        tree = repo.index.write_tree()
Pierre-Yves Chibon 17e9e2
        author = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Alice Author', 'alice@authors.tld')
Pierre-Yves Chibon 17e9e2
        committer = pygit2.Signature(
Pierre-Yves Chibon 17e9e2
            'Cecil Committer', 'cecil@committers.tld')
Pierre-Yves Chibon 17e9e2
        commit_hash = repo.create_commit(
Pierre-Yves Chibon 17e9e2
            'refs/heads/feature_branch',  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 17e9e2
            'Add .gitignore file for testing',
Pierre-Yves Chibon 17e9e2
            # binary string representing the tree object ID
Pierre-Yves Chibon 17e9e2
            tree,
Pierre-Yves Chibon 17e9e2
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 17e9e2
            [first_commit.oid.hex]
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # All good
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 17e9e2
            'repo': 'test',
Pierre-Yves Chibon 17e9e2
            'csrf_token': csrf_token,
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 17e9e2
        output = self.app.post('/pv/branches/heads/', data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        # We can't test the content since the commit hash will change all
Pierre-Yves Chibon 17e9e2
        # the time, so let's just check the structure
Pierre-Yves Chibon 17e9e2
        self.assertEqual(
Pierre-Yves Chibon 17e9e2
            sorted(js_data.keys()), ['branches', 'code', 'heads'])
Pierre-Yves Chibon 17e9e2
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon 17e9e2
        self.assertEqual(len(js_data['heads']), 3)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(len(js_data['branches']), 3)
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_no_token(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits from the internal API. '''
Pierre-Yves Chibon e8a127
        # No CSRF token
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'fakerepo',
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/authors', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR', 'message': 'Invalid input submitted'}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_invalid_repo(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits from the internal API. '''
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # Invalid repo
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'fakerepo',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/authors', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR',
Aurélien Bompard 626417
             'message': 'No repo found with the information provided'}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_empty_git(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits from the internal API. '''
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon e8a127
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # No content in git
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'test',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/authors', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon e8a127
            sorted(js_data.keys()),
Pierre-Yves Chibon e8a127
            ['code', 'message', 'task_id', 'url']
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['message'], 'Stats asked')
Pierre-Yves Chibon e8a127
        self.assertTrue(js_data['url'].startswith('/pv/task/'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        output = self.app.get(js_data['url'])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 20a73c
            js_data2 in [
Aurélien Bompard 626417
                {'results': "reference 'refs/heads/master' not found"},
Aurélien Bompard 626417
                {'results': "Reference 'refs/heads/master' not found"}
Pierre-Yves Chibon 20a73c
            ]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_git_populated(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits from the internal API. '''
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon e8a127
        tests.create_projects_git(
Pierre-Yves Chibon e8a127
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon e8a127
        tests.add_content_git_repo(
Pierre-Yves Chibon e8a127
            os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # Content in git
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'test',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/authors', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon e8a127
            sorted(js_data.keys()),
Pierre-Yves Chibon e8a127
            ['code', 'message', 'task_id', 'url']
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['message'], 'Stats asked')
Pierre-Yves Chibon e8a127
        self.assertTrue(js_data['url'].startswith('/pv/task/'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        output = self.app.get(js_data['url'])
Pierre-Yves Chibon c1b474
        while output.status_code == 418:
Pierre-Yves Chibon c1b474
            time.sleep(0.5)
Pierre-Yves Chibon c1b474
            output = self.app.get(js_data['url'])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertTrue(js_data2['results'][3] > 1509110062)
Pierre-Yves Chibon e8a127
        js_data2['results'][3] = 1509110062
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data2,
Aurélien Bompard 626417
            {'results': [
Pierre-Yves Chibon e8a127
                2,
Aurélien Bompard 626417
                [[2, [['Alice Author', 'alice@authors.tld']]]],
Pierre-Yves Chibon e8a127
                1,
Pierre-Yves Chibon e8a127
                1509110062
Pierre-Yves Chibon e8a127
            ]
Pierre-Yves Chibon e8a127
            }
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_no_token(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits_trend from the internal API. '''
Pierre-Yves Chibon e8a127
        # No CSRF token
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'fakerepo',
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/trend', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR', 'message': 'Invalid input submitted'}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_invalid_repo(self):
Pierre-Yves Chibon e8a127
        """ Test the get_stats_commits_trend from the internal API. """
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # Invalid repo
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'fakerepo',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/trend', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Aurélien Bompard 626417
            {'code': 'ERROR',
Aurélien Bompard 626417
             'message': 'No repo found with the information provided'}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_empty_git(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits_trend from the internal API. '''
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon e8a127
        tests.create_projects_git(os.path.join(self.path, 'repos'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # No content in git
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'test',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/trend', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon e8a127
            sorted(js_data.keys()),
Pierre-Yves Chibon e8a127
            ['code', 'message', 'task_id', 'url']
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['message'], 'Stats asked')
Pierre-Yves Chibon e8a127
        self.assertTrue(js_data['url'].startswith('/pv/task/'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        output = self.app.get(js_data['url'])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 20a73c
            js_data2 in [
Aurélien Bompard 626417
                {'results': "reference 'refs/heads/master' not found"},
Aurélien Bompard 626417
                {'results': "Reference 'refs/heads/master' not found"}
Pierre-Yves Chibon 20a73c
            ]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_git_populated(self):
Pierre-Yves Chibon e8a127
        ''' Test the get_stats_commits_trend from the internal API. '''
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon e8a127
        tests.create_projects_git(
Pierre-Yves Chibon e8a127
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon e8a127
        tests.add_content_git_repo(
Pierre-Yves Chibon e8a127
            os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon e8a127
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon e8a127
            csrf_token = self.get_csrf()
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        # Content in git
Pierre-Yves Chibon e8a127
        data = {
Pierre-Yves Chibon e8a127
            'repo': 'test',
Pierre-Yves Chibon e8a127
            'csrf_token': csrf_token,
Pierre-Yves Chibon e8a127
        }
Pierre-Yves Chibon e8a127
        output = self.app.post('/pv/stats/commits/trend', data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon e8a127
            sorted(js_data.keys()),
Pierre-Yves Chibon e8a127
            ['code', 'message', 'task_id', 'url']
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon e8a127
        self.assertEqual(js_data['message'], 'Stats asked')
Pierre-Yves Chibon e8a127
        self.assertTrue(js_data['url'].startswith('/pv/task/'))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        output = self.app.get(js_data['url'])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data2,
Aurélien Bompard 626417
            {'results': [[str(today), 2]]}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 29abd1
    def test_get_project_family_no_project(self):
Pierre-Yves Chibon 29abd1
        ''' Test the get_project_family from the internal API. '''
Pierre-Yves Chibon 29abd1
        output = self.app.post('/pv/test/family')
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_family_no_csrf(self):
Pierre-Yves Chibon 29abd1
        ''' Test the get_project_family from the internal API. '''
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 29abd1
        tests.create_projects_git(
Pierre-Yves Chibon 29abd1
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 29abd1
        tests.add_content_git_repo(
Pierre-Yves Chibon 29abd1
            os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        output = self.app.post('/pv/test/family')
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 29abd1
        self.assertEqual(
Pierre-Yves Chibon 29abd1
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'message']
Pierre-Yves Chibon 29abd1
        )
Aurélien Bompard 626417
        self.assertEqual(js_data['code'], 'ERROR')
Aurélien Bompard 626417
        self.assertEqual(js_data['message'], 'Invalid input submitted')
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_family(self):
Pierre-Yves Chibon 29abd1
        ''' Test the get_project_family from the internal API. '''
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 29abd1
        tests.create_projects_git(
Pierre-Yves Chibon 29abd1
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 29abd1
        tests.add_content_git_repo(
Pierre-Yves Chibon 29abd1
            os.path.join(self.path, 'repos', 'test.git'))
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        user = tests.FakeUser()
Pierre-Yves Chibon 29abd1
        user.username = 'pingou'
Pierre-Yves Chibon 29abd1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 29abd1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        data = {
Pierre-Yves Chibon 29abd1
            'csrf_token': csrf_token,
Pierre-Yves Chibon 29abd1
        }
Pierre-Yves Chibon 29abd1
        output = self.app.post('/pv/test/family', data=data)
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 29abd1
        self.assertEqual(
Pierre-Yves Chibon 29abd1
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'family']
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        self.assertEqual(js_data['code'], 'OK')
Aurélien Bompard 626417
        self.assertEqual(js_data['family'], ['test'])
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_larger_family(self):
Pierre-Yves Chibon 29abd1
        ''' Test the get_project_family from the internal API. '''
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 29abd1
        tests.create_projects_git(
Pierre-Yves Chibon 29abd1
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Create a 3rd user
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.User(
Pierre-Yves Chibon 29abd1
            user='ralph',
Pierre-Yves Chibon 29abd1
            fullname='Ralph bar',
Pierre-Yves Chibon 29abd1
            password='ralph_foo',
Pierre-Yves Chibon 29abd1
            default_email='ralph@bar.com',
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon 29abd1
            user_id=3,
Pierre-Yves Chibon 29abd1
            email='ralph@bar.com')
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        self.session.commit()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Create a couple of forks of the test project
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 29abd1
            user_id=2,  # foo
Pierre-Yves Chibon 29abd1
            name='test',
Pierre-Yves Chibon 29abd1
            is_fork=True,
Pierre-Yves Chibon 29abd1
            parent_id=1,  # test
Pierre-Yves Chibon 29abd1
            description='test project #1',
Pierre-Yves Chibon 29abd1
            hook_token='aaabbbcccddd',
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 29abd1
            user_id=3,  # Ralph
Pierre-Yves Chibon 29abd1
            name='test',
Pierre-Yves Chibon 29abd1
            is_fork=True,
Pierre-Yves Chibon 29abd1
            parent_id=1,  # test
Pierre-Yves Chibon 29abd1
            description='test project #1',
Pierre-Yves Chibon 29abd1
            hook_token='aaabbbccceee',
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        self.session.commit()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Get on with testing
Pierre-Yves Chibon 29abd1
        user = tests.FakeUser()
Pierre-Yves Chibon 29abd1
        user.username = 'pingou'
Pierre-Yves Chibon 29abd1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 29abd1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        data = {
Pierre-Yves Chibon 29abd1
            'csrf_token': csrf_token,
Pierre-Yves Chibon 29abd1
        }
Pierre-Yves Chibon 29abd1
        output = self.app.post('/pv/test/family', data=data)
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 29abd1
        self.assertEqual(
Pierre-Yves Chibon 29abd1
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'family']
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon 29abd1
        self.assertEqual(
Pierre-Yves Chibon 29abd1
            js_data['family'],
Aurélien Bompard 626417
            ['test', 'fork/foo/test', 'fork/ralph/test'])
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon ff1592
    def test_get_project_larger_family_pr_only(self):
Pierre-Yves Chibon ff1592
        ''' Test the get_project_family from the internal API. '''
Pierre-Yves Chibon ff1592
        tests.create_projects(self.session)
Pierre-Yves Chibon ff1592
        tests.create_projects_git(
Pierre-Yves Chibon ff1592
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Create a 3rd user
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.User(
Pierre-Yves Chibon ff1592
            user='ralph',
Pierre-Yves Chibon ff1592
            fullname='Ralph bar',
Pierre-Yves Chibon ff1592
            password='ralph_foo',
Pierre-Yves Chibon ff1592
            default_email='ralph@bar.com',
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.UserEmail(
Pierre-Yves Chibon ff1592
            user_id=3,
Pierre-Yves Chibon ff1592
            email='ralph@bar.com')
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
        self.session.commit()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Create a couple of forks of the test project
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff1592
            user_id=2,  # foo
Pierre-Yves Chibon ff1592
            name='test',
Pierre-Yves Chibon ff1592
            is_fork=True,
Pierre-Yves Chibon ff1592
            parent_id=1,  # test
Pierre-Yves Chibon ff1592
            description='test project #1',
Pierre-Yves Chibon ff1592
            hook_token='aaabbbcccddd',
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon ff1592
        # disable issues in this fork
Pierre-Yves Chibon ff1592
        default_repo_settings = item.settings
Pierre-Yves Chibon ff1592
        default_repo_settings['issue_tracker'] = False
Pierre-Yves Chibon ff1592
        item.settings = default_repo_settings
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff1592
            user_id=3,  # Ralph
Pierre-Yves Chibon ff1592
            name='test',
Pierre-Yves Chibon ff1592
            is_fork=True,
Pierre-Yves Chibon ff1592
            parent_id=1,  # test
Pierre-Yves Chibon ff1592
            description='test project #1',
Pierre-Yves Chibon ff1592
            hook_token='aaabbbccceee',
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon ff1592
        # disable PRs in this fork
Pierre-Yves Chibon ff1592
        default_repo_settings = item.settings
Pierre-Yves Chibon ff1592
        default_repo_settings['pull_requests'] = False
Pierre-Yves Chibon ff1592
        item.settings = default_repo_settings
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
        self.session.commit()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Get on with testing
Pierre-Yves Chibon ff1592
        user = tests.FakeUser()
Pierre-Yves Chibon ff1592
        user.username = 'pingou'
Pierre-Yves Chibon ff1592
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon ff1592
            csrf_token = self.get_csrf()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        data = {
Pierre-Yves Chibon ff1592
            'csrf_token': csrf_token,
Pierre-Yves Chibon ff1592
            'allows_pr': '1',
Pierre-Yves Chibon ff1592
        }
Pierre-Yves Chibon ff1592
        output = self.app.post('/pv/test/family', data=data)
Pierre-Yves Chibon ff1592
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff1592
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ff1592
        self.assertEqual(
Pierre-Yves Chibon ff1592
            sorted(js_data.keys()),
Pierre-Yves Chibon ff1592
            ['code', 'family']
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon ff1592
        self.assertEqual(
Pierre-Yves Chibon ff1592
            js_data['family'],
Pierre-Yves Chibon ff1592
            ['test', 'fork/foo/test'])
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        data = {
Pierre-Yves Chibon ff1592
            'csrf_token': csrf_token,
Pierre-Yves Chibon ff1592
            'allows_issues': '1',
Pierre-Yves Chibon ff1592
        }
Pierre-Yves Chibon ff1592
        output = self.app.post('/pv/test/family', data=data)
Pierre-Yves Chibon ff1592
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff1592
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ff1592
        self.assertEqual(
Pierre-Yves Chibon ff1592
            sorted(js_data.keys()),
Pierre-Yves Chibon ff1592
            ['code', 'family']
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon ff1592
        self.assertEqual(
Pierre-Yves Chibon ff1592
            js_data['family'],
Pierre-Yves Chibon ff1592
            ['test', 'fork/ralph/test'])
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ea4717
    def test_get_pull_request_ready_branch_no_csrf(self):
Pierre-Yves Chibon ea4717
        '''Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon ea4717
        on the main repository
Pierre-Yves Chibon ea4717
        '''
Pierre-Yves Chibon ea4717
        tests.create_projects(self.session)
Pierre-Yves Chibon ea4717
        tests.create_projects_git(
Pierre-Yves Chibon ea4717
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
        # Query branches on the main repo
Pierre-Yves Chibon ea4717
        data = {
Pierre-Yves Chibon ea4717
            'repo': 'test',
Pierre-Yves Chibon ea4717
        }
Pierre-Yves Chibon ea4717
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon ea4717
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon ea4717
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ea4717
        self.assertEqual(
Pierre-Yves Chibon ea4717
            sorted(js_data.keys()),
Pierre-Yves Chibon ea4717
            ['code', 'message']
Pierre-Yves Chibon ea4717
        )
Pierre-Yves Chibon ea4717
        self.assertEqual(js_data['code'], 'ERROR')
Pierre-Yves Chibon ea4717
        self.assertEqual(
Pierre-Yves Chibon ea4717
            js_data['message'],
Pierre-Yves Chibon ea4717
            'Invalid input submitted'
Pierre-Yves Chibon ea4717
        )
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
    def test_get_pull_request_ready_branch_no_repo(self):
Pierre-Yves Chibon ea4717
        '''Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon ea4717
        on the main repository
Pierre-Yves Chibon ea4717
        '''
Pierre-Yves Chibon ea4717
        with tests.user_set(self.app.application, tests.FakeUser()):
Pierre-Yves Chibon ea4717
            csrf_token = self.get_csrf()
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
        # Query branches on an invalid repo
Pierre-Yves Chibon ea4717
        data = {
Pierre-Yves Chibon ea4717
            'repo': 'test',
Pierre-Yves Chibon ea4717
            'namespace': 'fake',
Pierre-Yves Chibon ea4717
            'csrf_token': csrf_token,
Pierre-Yves Chibon ea4717
        }
Pierre-Yves Chibon ea4717
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon ea4717
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ea4717
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ea4717
        self.assertEqual(
Pierre-Yves Chibon ea4717
            sorted(js_data.keys()),
Pierre-Yves Chibon ea4717
            ['code', 'message']
Pierre-Yves Chibon ea4717
        )
Pierre-Yves Chibon ea4717
        self.assertEqual(js_data['code'], 'ERROR')
Pierre-Yves Chibon ea4717
        self.assertEqual(
Pierre-Yves Chibon ea4717
            js_data['message'],
Pierre-Yves Chibon ea4717
            'No repo found with the information provided'
Pierre-Yves Chibon ea4717
        )
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_main_repo_no_branch(self):
Pierre-Yves Chibon fbbe78
        '''Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon fbbe78
        on the main repository
Pierre-Yves Chibon fbbe78
        '''
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon fbbe78
        user.username = 'pingou'
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the main repo
Pierre-Yves Chibon fbbe78
        data = {
Pierre-Yves Chibon fbbe78
            'csrf_token': csrf_token,
Pierre-Yves Chibon fbbe78
            'repo': 'test',
Pierre-Yves Chibon fbbe78
        }
Pierre-Yves Chibon fbbe78
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'message']
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            js_data['message'],
Aurélien Bompard 626417
            {'branch_w_pr': {}, 'new_branch': {}})
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork(self):
Pierre-Yves Chibon fbbe78
        '''Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork
Pierre-Yves Chibon fbbe78
        '''
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git'),
Pierre-Yves Chibon fbbe78
            branch='feature')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon fbbe78
            name='test',
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon fbbe78
            description='test project #1',
Pierre-Yves Chibon fbbe78
            hook_token='aaabbbcccddd',
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon fbbe78
        user.username = 'pingou'
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon fbbe78
        data = {
Pierre-Yves Chibon fbbe78
            'csrf_token': csrf_token,
Pierre-Yves Chibon fbbe78
            'repo': 'test',
Pierre-Yves Chibon fbbe78
            'repouser': 'foo',
Pierre-Yves Chibon fbbe78
        }
Pierre-Yves Chibon fbbe78
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'message']
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['code'], 'OK')
Aurélien Bompard d90ed4
        self.assertListEqual(
Aurélien Bompard d90ed4
            sorted(js_data['message'].keys()),
Aurélien Bompard 626417
            ['branch_w_pr', 'new_branch'])
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['message']['branch_w_pr'], {})
Aurélien Bompard 626417
        self.assertListEqual(
Aurélien Bompard 626417
            list(js_data['message']['new_branch']), ['feature'])
Slavek Kabrda 07edc0
        self.assertEqual(len(js_data['message']['new_branch']['feature']['commits']), 2)
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            js_data['message']['new_branch']['feature']['target_branch'], 'master')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork_no_parent_no_pr(self):
Pierre-Yves Chibon fbbe78
        '''Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork that has no parent repo (deleted) and doesn't allow PR
Pierre-Yves Chibon fbbe78
        '''
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git'),
Pierre-Yves Chibon fbbe78
            branch='feature')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon fbbe78
            name='test',
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon fbbe78
            description='test project #1',
Pierre-Yves Chibon fbbe78
            hook_token='aaabbbcccddd',
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
        settings = item.settings
Pierre-Yves Chibon fbbe78
        settings['pull_requests'] = False
Pierre-Yves Chibon fbbe78
        item.settings = settings
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Delete the parent project
Pierre-Yves Chibon fbbe78
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fbbe78
        self.session.delete(project)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon fbbe78
        user.username = 'pingou'
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon fbbe78
        data = {
Pierre-Yves Chibon fbbe78
            'csrf_token': csrf_token,
Pierre-Yves Chibon fbbe78
            'repo': 'test',
Pierre-Yves Chibon fbbe78
            'repouser': 'foo',
Pierre-Yves Chibon fbbe78
        }
Pierre-Yves Chibon fbbe78
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'message']
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['code'], 'ERROR')
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            js_data['message'],
Pierre-Yves Chibon fbbe78
            'Pull-request have been disabled for this repo')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork_no_parent(self):
Pierre-Yves Chibon fbbe78
        '''Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork that has no parent repo (deleted).
Pierre-Yves Chibon fbbe78
        '''
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon fbbe78
            os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git'),
Pierre-Yves Chibon fbbe78
            branch='feature')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon fbbe78
            name='test',
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon fbbe78
            description='test project #1',
Pierre-Yves Chibon fbbe78
            hook_token='aaabbbcccddd',
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
        settings = item.settings
Pierre-Yves Chibon fbbe78
        settings['pull_requests'] = True
Pierre-Yves Chibon fbbe78
        item.settings = settings
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Delete the parent project
Pierre-Yves Chibon fbbe78
        project = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon fbbe78
        self.session.delete(project)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon fbbe78
        user.username = 'pingou'
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon fbbe78
        data = {
Pierre-Yves Chibon fbbe78
            'csrf_token': csrf_token,
Pierre-Yves Chibon fbbe78
            'repo': 'test',
Pierre-Yves Chibon fbbe78
            'repouser': 'foo',
Pierre-Yves Chibon fbbe78
        }
Pierre-Yves Chibon fbbe78
        output = self.app.post('/pv/pull-request/ready', data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon fbbe78
            sorted(js_data.keys()),
Aurélien Bompard 626417
            ['code', 'message']
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['code'], 'OK')
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Aurélien Bompard d90ed4
            sorted(js_data['message'].keys()),
Aurélien Bompard 626417
            ['branch_w_pr', 'new_branch'])
Pierre-Yves Chibon fbbe78
        self.assertEqual(js_data['message']['branch_w_pr'], {})
Aurélien Bompard 626417
        self.assertListEqual(
Aurélien Bompard 626417
            list(js_data['message']['new_branch']), ['feature'])
Slavek Kabrda 07edc0
        self.assertEqual(len(js_data['message']['new_branch']['feature']['commits']), 2)
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            js_data['message']['new_branch']['feature']['target_branch'], 'master')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
    def test_get_pull_request_ready_branch_matching_target_off(self):
Slavek Kabrda 07edc0
        '''Test the get_pull_request_ready_branch from the internal API on
Slavek Kabrda 07edc0
        a fork while PR_TARGET_MATCHING_BRANCH is False
Slavek Kabrda 07edc0
        '''
Slavek Kabrda 07edc0
        tests.create_projects(self.session)
Slavek Kabrda 07edc0
        # make sure that head is not unborn
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'test.git'),
Slavek Kabrda 07edc0
            branch='master')
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'test.git'),
Slavek Kabrda 07edc0
            branch='feature')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        tests.create_projects_git(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos'), bare=True)
Slavek Kabrda 07edc0
        tests.create_projects_git(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git'),
Slavek Kabrda 07edc0
            branch='feature')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Create foo's fork of the test project
Slavek Kabrda 07edc0
        item = pagure.lib.model.Project(
Slavek Kabrda 07edc0
            user_id=2,  # foo
Slavek Kabrda 07edc0
            name='test',
Slavek Kabrda 07edc0
            is_fork=True,
Slavek Kabrda 07edc0
            parent_id=1,  # test
Slavek Kabrda 07edc0
            description='test project #1',
Slavek Kabrda 07edc0
            hook_token='aaabbbcccddd',
Slavek Kabrda 07edc0
        )
Slavek Kabrda 07edc0
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Slavek Kabrda 07edc0
        self.session.add(item)
Slavek Kabrda 07edc0
        self.session.commit()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Get on with testing
Slavek Kabrda 07edc0
        user = tests.FakeUser()
Slavek Kabrda 07edc0
        user.username = 'pingou'
Slavek Kabrda 07edc0
        with tests.user_set(self.app.application, user):
Slavek Kabrda 07edc0
            csrf_token = self.get_csrf()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Query branches on the Ralph's fork
Slavek Kabrda 07edc0
        data = {
Slavek Kabrda 07edc0
            'csrf_token': csrf_token,
Slavek Kabrda 07edc0
            'repo': 'test',
Slavek Kabrda 07edc0
            'repouser': 'foo',
Slavek Kabrda 07edc0
        }
Slavek Kabrda 07edc0
        output = self.app.post('/pv/pull-request/ready', data=data)
Slavek Kabrda 07edc0
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 07edc0
        js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            sorted(js_data.keys()),
Slavek Kabrda 07edc0
            ['code', 'message']
Slavek Kabrda 07edc0
        )
Slavek Kabrda 07edc0
        self.assertEqual(js_data['code'], 'OK')
Slavek Kabrda 07edc0
        self.assertListEqual(
Slavek Kabrda 07edc0
            sorted(js_data['message'].keys()),
Slavek Kabrda 07edc0
            ['branch_w_pr', 'new_branch'])
Slavek Kabrda 07edc0
        self.assertEqual(js_data['message']['branch_w_pr'], {})
Slavek Kabrda 07edc0
        self.assertListEqual(
Slavek Kabrda 07edc0
            list(js_data['message']['new_branch']), ['feature'])
Slavek Kabrda 07edc0
        self.assertEqual(len(js_data['message']['new_branch']['feature']['commits']), 2)
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            js_data['message']['new_branch']['feature']['target_branch'], 'master')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
    @patch.dict('pagure.config.config', {'PR_TARGET_MATCHING_BRANCH': True})
Slavek Kabrda 07edc0
    def test_get_pull_request_ready_branch_matching_target_on(self):
Slavek Kabrda 07edc0
        '''Test the get_pull_request_ready_branch from the internal API on
Slavek Kabrda 07edc0
        a fork while PR_TARGET_MATCHING_BRANCH is True
Slavek Kabrda 07edc0
        '''
Slavek Kabrda 07edc0
        tests.create_projects(self.session)
Slavek Kabrda 07edc0
        # make sure that head is not unborn
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'test.git'),
Slavek Kabrda 07edc0
            branch='master')
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'test.git'),
Slavek Kabrda 07edc0
            branch='feature')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        tests.create_projects_git(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos'), bare=True)
Slavek Kabrda 07edc0
        tests.create_projects_git(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'forks', 'foo'), bare=True)
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Slavek Kabrda 07edc0
            os.path.join(self.path, 'repos', 'forks', 'foo', 'test.git'),
Slavek Kabrda 07edc0
            branch='feature')
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Create foo's fork of the test project
Slavek Kabrda 07edc0
        item = pagure.lib.model.Project(
Slavek Kabrda 07edc0
            user_id=2,  # foo
Slavek Kabrda 07edc0
            name='test',
Slavek Kabrda 07edc0
            is_fork=True,
Slavek Kabrda 07edc0
            parent_id=1,  # test
Slavek Kabrda 07edc0
            description='test project #1',
Slavek Kabrda 07edc0
            hook_token='aaabbbcccddd',
Slavek Kabrda 07edc0
        )
Slavek Kabrda 07edc0
        item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Slavek Kabrda 07edc0
        self.session.add(item)
Slavek Kabrda 07edc0
        self.session.commit()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Get on with testing
Slavek Kabrda 07edc0
        user = tests.FakeUser()
Slavek Kabrda 07edc0
        user.username = 'pingou'
Slavek Kabrda 07edc0
        with tests.user_set(self.app.application, user):
Slavek Kabrda 07edc0
            csrf_token = self.get_csrf()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Query branches on the Ralph's fork
Slavek Kabrda 07edc0
        data = {
Slavek Kabrda 07edc0
            'csrf_token': csrf_token,
Slavek Kabrda 07edc0
            'repo': 'test',
Slavek Kabrda 07edc0
            'repouser': 'foo',
Slavek Kabrda 07edc0
        }
Slavek Kabrda 07edc0
        output = self.app.post('/pv/pull-request/ready', data=data)
Slavek Kabrda 07edc0
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 07edc0
        js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            sorted(js_data.keys()),
Slavek Kabrda 07edc0
            ['code', 'message']
Slavek Kabrda 07edc0
        )
Slavek Kabrda 07edc0
        self.assertEqual(js_data['code'], 'OK')
Slavek Kabrda 07edc0
        self.assertListEqual(
Slavek Kabrda 07edc0
            sorted(js_data['message'].keys()),
Slavek Kabrda 07edc0
            ['branch_w_pr', 'new_branch'])
Slavek Kabrda 07edc0
        self.assertEqual(js_data['message']['branch_w_pr'], {})
Slavek Kabrda 07edc0
        self.assertListEqual(
Slavek Kabrda 07edc0
            list(js_data['message']['new_branch']), ['feature'])
Slavek Kabrda 07edc0
        self.assertEqual(len(js_data['message']['new_branch']['feature']['commits']), 1)
Slavek Kabrda 07edc0
        self.assertEqual(
Slavek Kabrda 07edc0
            js_data['message']['new_branch']['feature']['target_branch'], 'feature')
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon a3de64
    def test_task_info_task_running(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task isn't
Pierre-Yves Chibon a3de64
        ready.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon a3de64
        task.get = MagicMock(return_value='FAILED')
Pierre-Yves Chibon a3de64
        task.ready = MagicMock(return_value=False)
Pierre-Yves Chibon a3de64
        with patch('pagure.lib.tasks.get_result', MagicMock(return_value=task)):
Pierre-Yves Chibon a3de64
            output = self.app.get('/pv/task/2')
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 418)
Pierre-Yves Chibon a3de64
Pierre-Yves Chibon a3de64
    def test_task_info_task_passed(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task failed.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon a3de64
        task.get = MagicMock(return_value='PASSED')
Pierre-Yves Chibon a3de64
        with patch('pagure.lib.tasks.get_result', MagicMock(return_value=task)):
Pierre-Yves Chibon a3de64
            output = self.app.get('/pv/task/2')
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon a3de64
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon a3de64
            self.assertEqual(js_data, {u'results': u'PASSED'})
Pierre-Yves Chibon a3de64
Pierre-Yves Chibon a3de64
    def test_task_info_task_failed(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task failed.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon a3de64
        task.get = MagicMock(return_value=Exception('Random error'))
Pierre-Yves Chibon a3de64
        with patch('pagure.lib.tasks.get_result', MagicMock(return_value=task)):
Pierre-Yves Chibon a3de64
            output = self.app.get('/pv/task/2')
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon a3de64
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon a3de64
            self.assertEqual(js_data, {u'results': u'Random error'})
Pierre-Yves Chibon a3de64
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 1edad5
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)