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
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 1edad5
import os
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 961d71
import pygit2
Pierre-Yves Chibon 1edad5
from mock import patch
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 1edad5
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 1edad5
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)
Pierre-Yves Chibon 1edad5
        js_data = json.loads(output.data)
Pierre-Yves Chibon 1edad5
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon b130e5
        self.session = pagure.lib.create_session(self.dbpath)
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)
Pierre-Yves Chibon 0bf136
        js_data = json.loads(output.data)
Pierre-Yves Chibon 0bf136
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon b130e5
        self.session = pagure.lib.create_session(self.dbpath)
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)
Pierre-Yves Chibon 21ea63
        js_data = json.loads(output.data)
Pierre-Yves Chibon 21ea63
        self.assertDictEqual(js_data, {'message': 'Comment added'})
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon b130e5
        self.session = pagure.lib.create_session(self.dbpath)
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)
Vivek Anand d5809b
        js_data = json.loads(output.data)
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)
Vivek Anand d5809b
        js_data = json.loads(output.data)
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')
Pierre-Yves Chibon 961d71
            csrf_token = output.data.split(
Pierre-Yves Chibon 961d71
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            # Missing request identifier
Pierre-Yves Chibon 961d71
            data = {
Pierre-Yves Chibon 961d71
                'csrf_token': csrf_token,
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 961d71
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 961d71
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            # With all the desired information
Pierre-Yves Chibon 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
Pierre-Yves Chibon 961d71
            js_data = json.loads(output.data)
Pierre-Yves Chibon 961d71
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon bea61e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon bea61e
    def test_mergeable_request_pull_no_change(self, send_email):
Pierre-Yves Chibon bea61e
        """ Test the mergeable_request_pull endpoint when there are no
Pierre-Yves Chibon bea61e
        changes to merge.
Pierre-Yves Chibon bea61e
        """
Pierre-Yves Chibon bea61e
        send_email.return_value = True
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Create a git repo to play with
Pierre-Yves Chibon bea61e
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')
Pierre-Yves Chibon bea61e
            csrf_token = output.data.split(
Pierre-Yves Chibon bea61e
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            # Missing request identifier
Pierre-Yves Chibon bea61e
            data = {
Pierre-Yves Chibon bea61e
                'csrf_token': csrf_token,
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon bea61e
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon bea61e
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            # With all the desired information
Pierre-Yves Chibon 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
Pierre-Yves Chibon bea61e
            js_data = json.loads(output.data)
Pierre-Yves Chibon bea61e
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 6fe99a
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 6fe99a
    def test_mergeable_request_pull_merge(self, send_email):
Pierre-Yves Chibon 6fe99a
        """ Test the mergeable_request_pull endpoint when the changes can
Pierre-Yves Chibon 6fe99a
        be merged with a merge commit.
Pierre-Yves Chibon 6fe99a
        """
Pierre-Yves Chibon 6fe99a
        send_email.return_value = True
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
        # Create a git repo to play with
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 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')
Pierre-Yves Chibon 6fe99a
            csrf_token = output.data.split(
Pierre-Yves Chibon 6fe99a
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            # Missing request identifier
Pierre-Yves Chibon 6fe99a
            data = {
Pierre-Yves Chibon 6fe99a
                'csrf_token': csrf_token,
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 6fe99a
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            # With all the desired information
Pierre-Yves Chibon 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 6fe99a
            }
Pierre-Yves Chibon 6fe99a
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 6fe99a
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 6fe99a
            exp = {
Pierre-Yves Chibon 6fe99a
              "code": "MERGE",
Pierre-Yves Chibon 6fe99a
              "message": "The pull-request can be merged with a merge commit",
Pierre-Yves Chibon 6fe99a
              "short_code": "With merge"
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            js_data = json.loads(output.data)
Pierre-Yves Chibon 6fe99a
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 25d55d
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 4a0bcf
    def test_mergeable_request_pull_conflicts(self, send_email):
Pierre-Yves Chibon 4a0bcf
        """ Test the mergeable_request_pull endpoint when the changes cannot
Pierre-Yves Chibon 4a0bcf
        be merged due to conflicts.
Pierre-Yves Chibon 25d55d
        """
Pierre-Yves Chibon 25d55d
        send_email.return_value = True
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
        # Create a git repo to play with
Pierre-Yves Chibon 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')
Pierre-Yves Chibon 25d55d
            csrf_token = output.data.split(
Pierre-Yves Chibon 25d55d
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            # Missing request identifier
Pierre-Yves Chibon 25d55d
            data = {
Pierre-Yves Chibon 25d55d
                'csrf_token': csrf_token,
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 25d55d
            output = self.app.post('/pv/pull-request/merge', data=data)
Pierre-Yves Chibon 25d55d
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            # With all the desired information
Pierre-Yves Chibon 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
Pierre-Yves Chibon 25d55d
            js_data = json.loads(output.data)
Pierre-Yves Chibon 25d55d
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 9f395f
    def test_get_branches_of_commit(self):
Pierre-Yves Chibon 9f395f
        ''' Test the get_branches_of_commit from the internal API. '''
Pierre-Yves Chibon 9f395f
        tests.create_projects(self.session)
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)
Pierre-Yves Chibon 9f395f
            csrf_token = output.data.split(
Pierre-Yves Chibon 9f395f
                b'name="csrf_token" type="hidden" value="')[1].split(b'">')[0]
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # No CSRF token
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'fakerepo',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {u'code': u'ERROR', u'message': u'Invalid input submitted'}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Invalid repo
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'fakerepo',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': u'No repo found with the information provided'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Rigth repo, no commit
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'test',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {u'code': u'ERROR', u'message': u'No commit id submitted'}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Request is fine, but git repo doesn't exist
Pierre-Yves Chibon 72ed6b
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 72ed6b
            user_id=1,  # pingou
Pierre-Yves Chibon 72ed6b
            name='test20',
Pierre-Yves Chibon 72ed6b
            description='test project #20',
Pierre-Yves Chibon 72ed6b
            hook_token='aaabbbhhh',
Pierre-Yves Chibon 72ed6b
        )
Pierre-Yves Chibon 72ed6b
        self.session.add(item)
Pierre-Yves Chibon 72ed6b
        self.session.commit()
Pierre-Yves Chibon 72ed6b
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 72ed6b
            'repo': 'test20',
Pierre-Yves Chibon 9f395f
            'commit_id': 'foo',
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': u'No git repo found with the information provided'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Create a git repo to play with
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)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'ERROR',
Pierre-Yves Chibon 9f395f
                u'message': 'This commit could not be found in this repo'
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # All good
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 9f395f
            'repo': 'test',
Pierre-Yves Chibon 9f395f
            'commit_id': commit_hash,
Pierre-Yves Chibon 9f395f
            'csrf_token': csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 9f395f
        output = self.app.post('/pv/branches/commit/', data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9f395f
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 9f395f
                u'code': u'OK',
Pierre-Yves Chibon 9f395f
                u'branches': ['feature_branch'],
Pierre-Yves Chibon 9f395f
            }
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 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)
Pierre-Yves Chibon 17e9e2
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {u'code': u'ERROR', u'message': u'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)
Pierre-Yves Chibon 17e9e2
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Pierre-Yves Chibon 17e9e2
                u'code': u'ERROR',
Pierre-Yves Chibon 17e9e2
                u'message': u'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)
Pierre-Yves Chibon 17e9e2
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {u"branches": {}, u"code": u"OK", u"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)
Pierre-Yves Chibon 17e9e2
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Pierre-Yves Chibon 17e9e2
                u'code': u'ERROR',
Pierre-Yves Chibon 17e9e2
                u'message': u'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)
Pierre-Yves Chibon 17e9e2
        js_data = json.loads(output.data.decode('utf-8'))
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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon e8a127
            {u'code': u'ERROR', u'message': u'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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon e8a127
            {u'code': u'ERROR',
Pierre-Yves Chibon e8a127
             u'message': u'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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
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 e8a127
        js_data2 = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 20a73c
            js_data2 in [
Pierre-Yves Chibon 20a73c
                {u'results': u"reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 20a73c
                {u'results': u"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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
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 e8a127
        js_data2 = json.loads(output.data.decode('utf-8'))
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,
Pierre-Yves Chibon e8a127
            {u'results': [
Pierre-Yves Chibon e8a127
                2,
Pierre-Yves Chibon 609c2b
                [[2, [[u'Alice Author', u'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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon e8a127
            {u'code': u'ERROR', u'message': u'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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon e8a127
            {u'code': u'ERROR',
Pierre-Yves Chibon e8a127
             u'message': u'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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
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 e8a127
        js_data2 = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 20a73c
            js_data2 in [
Pierre-Yves Chibon 20a73c
                {u'results': u"reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 20a73c
                {u'results': u"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)
Pierre-Yves Chibon e8a127
        js_data = json.loads(output.data.decode('utf-8'))
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 e8a127
        js_data2 = json.loads(output.data.decode('utf-8'))
Pierre-Yves Chibon e8a127
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data2,
Pierre-Yves Chibon e8a127
            {u'results': [[str(today), 2]]}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 1edad5
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)