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 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon e8a127
import datetime
Pierre-Yves Chibon 961d71
import json
Pierre-Yves Chibon 1edad5
import unittest
Pierre-Yves Chibon 1edad5
import shutil
Pierre-Yves Chibon 1edad5
import sys
Pierre-Yves Chibon c1b474
import time
Pierre-Yves Chibon 1edad5
import os
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 961d71
import pygit2
Pierre-Yves Chibon a3de64
from mock import patch, MagicMock, Mock
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 930073
import pagure.lib.query
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 f523b9
    maxDiff = None
Pierre-Yves Chibon f523b9
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 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = list(
Pierre-Yves Chibon 73d120
            set(pagure.config.config["IP_ALLOWED_INTERNAL"] + [None])
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon b130e5
Pierre-Yves Chibon 73d120
        pagure.config.config["GIT_FOLDER"] = os.path.join(self.path, "repos")
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"IP_ALLOWED_INTERNAL": []})
Slavek Kabrda 984d0f
    def test_internal_access_only(self):
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/ssh/lookupkey/")
Slavek Kabrda 984d0f
        self.assertEqual(output.status_code, 403)
Slavek Kabrda 984d0f
Slavek Kabrda 984d0f
        # no internal IP addresses => will fail
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/ssh/lookupkey/")
Slavek Kabrda 984d0f
        self.assertEqual(output.status_code, 403)
Slavek Kabrda 984d0f
        # wrong token => will fail
Slavek Kabrda 984d0f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/pv/ssh/lookupkey/",
Pierre-Yves Chibon 73d120
            headers={"Authorization": "token doesntexist"},
Slavek Kabrda 984d0f
        )
Slavek Kabrda 984d0f
        self.assertEqual(output.status_code, 401)
Slavek Kabrda 984d0f
        # correct token => will work
Slavek Kabrda 984d0f
        pagure.lib.query.add_token_to_user(
Pierre-Yves Chibon 73d120
            self.session, None, ["internal_access"], "pingou"
Slavek Kabrda 984d0f
        )
Slavek Kabrda 984d0f
        token = pagure.lib.query.search_token(
Pierre-Yves Chibon 73d120
            self.session, acls=["internal_access"], user="pingou"
Slavek Kabrda 984d0f
        )
Slavek Kabrda 984d0f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/pv/ssh/lookupkey/",
Pierre-Yves Chibon 73d120
            headers={"Authorization": "token %s" % token[0].id},
Slavek Kabrda 984d0f
        )
Slavek Kabrda 984d0f
        self.assertEqual(output.status_code, 400)
Slavek Kabrda 984d0f
Pierre-Yves Chibon 73d120
    @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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 1edad5
            session=self.session,
Pierre-Yves Chibon 1edad5
            repo_from=repo,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 1edad5
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 1edad5
        )
Pierre-Yves Chibon 1edad5
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": "foo"}
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Wrong http request
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        data = {"objid": "foo", "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Invalid objid
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": request.uid, "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Valid objid, in-complete data for a comment
Pierre-Yves Chibon 73d120
        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 73d120
            "objid": request.uid,
Pierre-Yves Chibon 73d120
            "useremail": "foo@pingou.com",
Pierre-Yves Chibon 73d120
            "comment": "Looks good to me!",
Pierre-Yves Chibon 1edad5
        }
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 1edad5
        # Add comment
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/pull-request/comment/", data=data)
Pierre-Yves Chibon 1edad5
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"message": "Comment added"})
Pierre-Yves Chibon 1edad5
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Slavek Kabrda 984d0f
        # Check the @internal_access_only
Pierre-Yves Chibon 73d120
        before = pagure.config.config["IP_ALLOWED_INTERNAL"][:]
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = []
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
        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 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = before[:]
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
    @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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 21ea63
            session=self.session,
Pierre-Yves Chibon 21ea63
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 21ea63
        )
Pierre-Yves Chibon 21ea63
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": "foo"}
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Wrong http request
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        data = {"objid": "foo", "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Invalid objid
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": issue.uid, "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Valid objid, in-complete data for a comment
Pierre-Yves Chibon 73d120
        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 73d120
            "objid": issue.uid,
Pierre-Yves Chibon 73d120
            "useremail": "foo@pingou.com",
Pierre-Yves Chibon 73d120
            "comment": "Looks good to me!",
Pierre-Yves Chibon 0bf136
        }
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Add comment
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Pierre-Yves Chibon 0bf136
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"message": "Comment added"})
Pierre-Yves Chibon 0bf136
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Slavek Kabrda 984d0f
        # Check the @internal_access_only
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"].remove(None)
Pierre-Yves Chibon 73d120
        before = pagure.config.config["IP_ALLOWED_INTERNAL"][:]
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = []
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
        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 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = before[:]
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
    @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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon 0bf136
            session=self.session,
Pierre-Yves Chibon 0bf136
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this, really",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 0bf136
            private=True,
Pierre-Yves Chibon 0bf136
        )
Pierre-Yves Chibon 0bf136
        self.session.commit()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": "foo"}
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Wrong http request
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        data = {"objid": "foo", "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Invalid objid
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": issue.uid, "useremail": "foo@bar.com"}
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Valid objid, un-allowed user for this (private) ticket
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": issue.uid, "useremail": "foo@pingou.com"}
Pierre-Yves Chibon 0bf136
Pierre-Yves Chibon 0bf136
        # Valid objid, un-allowed user for this (private) ticket
Pierre-Yves Chibon 73d120
        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 73d120
            "objid": issue.uid,
Pierre-Yves Chibon 73d120
            "useremail": "foo@pingou.com",
Pierre-Yves Chibon 73d120
            "comment": "Looks good to me!",
Pierre-Yves Chibon 21ea63
        }
Pierre-Yves Chibon 21ea63
Pierre-Yves Chibon 21ea63
        # Add comment
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Pierre-Yves Chibon 21ea63
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"message": "Comment added"})
Pierre-Yves Chibon 21ea63
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Slavek Kabrda 984d0f
        # Check the @internal_access_only
Pierre-Yves Chibon 73d120
        before = pagure.config.config["IP_ALLOWED_INTERNAL"][:]
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = []
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
        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 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = before[:]
Pierre-Yves Chibon 450dd6
Pierre-Yves Chibon 73d120
    @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 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Vivek Anand d5809b
            session=self.session,
Vivek Anand d5809b
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue",
Pierre-Yves Chibon 73d120
            content="We should work on this, really",
Pierre-Yves Chibon 73d120
            user="pingou",
Vivek Anand d5809b
            private=True,
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg.title, "Test issue")
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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 = {
Pierre-Yves Chibon 73d120
            "objid": issue.uid,
Pierre-Yves Chibon 73d120
            "useremail": "foo@bar.com",
Pierre-Yves Chibon 73d120
            "comment": "Looks good to me!",
Vivek Anand d5809b
        }
Vivek Anand d5809b
Vivek Anand d5809b
        # Valid objid, un-allowed user for this (private) ticket
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="ticket",
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User added")
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.users]), ["foo"])
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.committers]), [])
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.admins]), [])
Pierre-Yves Chibon 15f25e
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="commit",
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User access updated")
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.users]), ["foo"])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted([u.username for u in repo.committers]), ["foo"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.admins]), [])
Pierre-Yves Chibon 15f25e
Vivek Anand d5809b
        # Add comment
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"message": "Comment added"})
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.add_user_to_project(
Vivek Anand d5809b
            self.session,
Vivek Anand d5809b
            project=repo,
Pierre-Yves Chibon 73d120
            new_user="foo",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 73d120
            access="admin",
Vivek Anand d5809b
        )
Vivek Anand d5809b
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "User access updated")
Pierre-Yves Chibon 15f25e
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.users]), ["foo"])
Pierre-Yves Chibon 15f25e
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted([u.username for u in repo.committers]), ["foo"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted([u.username for u in repo.admins]), ["foo"])
Pierre-Yves Chibon 15f25e
Vivek Anand d5809b
        # Add comment
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"message": "Comment added"})
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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
Slavek Kabrda 984d0f
        # Check the @internal_access_only
Pierre-Yves Chibon 73d120
        before = pagure.config.config["IP_ALLOWED_INTERNAL"][:]
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = []
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        output = self.app.put("/pv/ticket/comment/", data=data)
Vivek Anand d5809b
        self.assertEqual(output.status_code, 403)
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
        pagure.config.config["IP_ALLOWED_INTERNAL"] = before[:]
Vivek Anand d5809b
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 961d71
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 961d71
            author,
Pierre-Yves Chibon 961d71
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 961d71
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 961d71
            author,
Pierre-Yves Chibon 961d71
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 73d120
        second_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        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 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 961d71
            session=self.session,
Pierre-Yves Chibon 961d71
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 961d71
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 961d71
        )
Pierre-Yves Chibon 961d71
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": "blah"}
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
        # Missing CSRF
Pierre-Yves Chibon 73d120
        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 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 961d71
            # Missing request identifier
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            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 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 961d71
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 73d120
            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 73d120
                "code": "FFORWARD",
Pierre-Yves Chibon 73d120
                "message": "The pull-request can be merged and fast-forwarded",
Pierre-Yves Chibon 73d120
                "short_code": "Ok",
Pierre-Yves Chibon 961d71
            }
Pierre-Yves Chibon 961d71
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 961d71
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 961d71
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon bea61e
    def test_mergeable_request_pull_no_change(self, send_email):
Pierre-Yves Chibon bea61e
        """ Test the mergeable_request_pull endpoint when there are no
Pierre-Yves Chibon bea61e
        changes to merge.
Pierre-Yves Chibon bea61e
        """
Pierre-Yves Chibon bea61e
        send_email.return_value = True
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Create a git repo to play with
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon bea61e
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon bea61e
            author,
Pierre-Yves Chibon bea61e
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon bea61e
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon bea61e
            author,
Pierre-Yves Chibon bea61e
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon 73d120
        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 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon bea61e
            session=self.session,
Pierre-Yves Chibon bea61e
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon bea61e
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon bea61e
        )
Pierre-Yves Chibon bea61e
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"objid": "blah"}
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
        # Missing CSRF
Pierre-Yves Chibon 73d120
        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 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon bea61e
Pierre-Yves Chibon bea61e
            # Missing request identifier
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            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 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon bea61e
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon 73d120
            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 73d120
                "code": "NO_CHANGE",
Pierre-Yves Chibon 73d120
                "message": "Nothing to change, git is up to date",
Pierre-Yves Chibon 73d120
                "short_code": "No changes",
Pierre-Yves Chibon bea61e
            }
Pierre-Yves Chibon bea61e
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon bea61e
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 1edad5
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 6fe99a
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 6fe99a
            author,
Pierre-Yves Chibon 6fe99a
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 73d120
        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 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 6fe99a
            session=self.session,
Pierre-Yves Chibon 6fe99a
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 6fe99a
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 6fe99a
        )
Pierre-Yves Chibon 6fe99a
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 6fe99a
            # Missing request identifier
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            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 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 6fe99a
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 73d120
                "force": True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 705645
            exp = {
Pierre-Yves Chibon 73d120
                "code": "MERGE",
Pierre-Yves Chibon 73d120
                "message": "The pull-request can be merged with a merge commit",
Pierre-Yves Chibon 73d120
                "short_code": "With merge",
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # Asking a second time will trigger the cache
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 73d120
            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 73d120
                "code": "MERGE",
Pierre-Yves Chibon 73d120
                "message": "The pull-request can be merged with a merge commit",
Pierre-Yves Chibon 73d120
                "short_code": "With merge",
Pierre-Yves Chibon 6fe99a
            }
Pierre-Yves Chibon 6fe99a
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6fe99a
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 6fe99a
Pierre-Yves Chibon 73d120
    @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 73d120
        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 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 25d55d
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 25d55d
            author,
Pierre-Yves Chibon 25d55d
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 73d120
        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 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 25d55d
            session=self.session,
Pierre-Yves Chibon 25d55d
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 25d55d
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 25d55d
        )
Pierre-Yves Chibon 25d55d
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        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 73d120
        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 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 25d55d
            # Missing request identifier
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            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 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 25d55d
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 73d120
            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 73d120
                "code": "CONFLICTS",
Pierre-Yves Chibon 73d120
                "message": "The pull-request cannot be merged due to conflicts",
Pierre-Yves Chibon 73d120
                "short_code": "Conflicts",
Pierre-Yves Chibon 25d55d
            }
Pierre-Yves Chibon 25d55d
Aurélien Bompard 626417
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 25d55d
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 25d55d
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Slavek Kabrda 5759e6
    def test_mergeable_request_pull_merge_no_nonff_merges(self, send_email):
Slavek Kabrda 5759e6
        """ Test the mergeable_request_pull endpoint when the changes can
Slavek Kabrda 5759e6
        be merged with a merge commit, but project settings prohibit this.
Slavek Kabrda 5759e6
        """
Slavek Kabrda 5759e6
        send_email.return_value = True
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Create a git repo to play with
Slavek Kabrda 5759e6
Pierre-Yves Chibon 73d120
        origgitrepo = os.path.join(self.path, "repos", "test.git")
Slavek Kabrda 5759e6
        self.assertFalse(os.path.exists(origgitrepo))
Slavek Kabrda 5759e6
        os.makedirs(origgitrepo)
Slavek Kabrda 5759e6
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos_tmp", "test.git")
Slavek Kabrda 5759e6
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Slavek Kabrda 5759e6
        repo.index.write()
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Commits the files added
Slavek Kabrda 5759e6
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Slavek Kabrda 5759e6
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Slavek Kabrda 5759e6
            author,
Slavek Kabrda 5759e6
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Slavek Kabrda 5759e6
            # binary string representing the tree object ID
Slavek Kabrda 5759e6
            tree,
Slavek Kabrda 5759e6
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Slavek Kabrda 5759e6
        )
Slavek Kabrda 5759e6
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Slavek Kabrda 5759e6
        ori_remote = repo.remotes[0]
Slavek Kabrda 5759e6
        PagureRepo.push(ori_remote, refname)
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Slavek Kabrda 5759e6
        repo.index.write()
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Commits the files added
Slavek Kabrda 5759e6
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Slavek Kabrda 5759e6
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Slavek Kabrda 5759e6
            author,
Slavek Kabrda 5759e6
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "consider",
Slavek Kabrda 5759e6
            # binary string representing the tree object ID
Slavek Kabrda 5759e6
            tree,
Slavek Kabrda 5759e6
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Slavek Kabrda 5759e6
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/feature:refs/heads/feature"
Slavek Kabrda 5759e6
        ori_remote = repo.remotes[0]
Slavek Kabrda 5759e6
        PagureRepo.push(ori_remote, refname)
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Create another file in the master branch
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        repo.index.add(".gitignore")
Slavek Kabrda 5759e6
        repo.index.write()
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Commits the files added
Slavek Kabrda 5759e6
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Slavek Kabrda 5759e6
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Slavek Kabrda 5759e6
            author,
Slavek Kabrda 5759e6
            committer,
Pierre-Yves Chibon 73d120
            "Add .gitignore file for testing",
Slavek Kabrda 5759e6
            # binary string representing the tree object ID
Slavek Kabrda 5759e6
            tree,
Slavek Kabrda 5759e6
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Slavek Kabrda 5759e6
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Slavek Kabrda 5759e6
        ori_remote = repo.remotes[0]
Slavek Kabrda 5759e6
        PagureRepo.push(ori_remote, refname)
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Create a PR for these changes
Slavek Kabrda 5759e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Slavek Kabrda 5759e6
            session=self.session,
Slavek Kabrda 5759e6
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Slavek Kabrda 5759e6
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Slavek Kabrda 5759e6
        )
Pierre-Yves Chibon 73d120
        settings = {"disable_non_fast-forward_merges": True}
Slavek Kabrda 5759e6
        project.settings = settings
Slavek Kabrda 5759e6
        self.session.commit()
Slavek Kabrda 5759e6
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the feature branch")
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        # Check if the PR can be merged
Slavek Kabrda 5759e6
        data = {}
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Slavek Kabrda 5759e6
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
            # With all the desired information
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Slavek Kabrda 5759e6
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 73d120
                "force": True,
Slavek Kabrda 5759e6
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Slavek Kabrda 5759e6
            self.assertEqual(output.status_code, 200)
Slavek Kabrda 5759e6
            exp = {
Pierre-Yves Chibon 73d120
                "code": "NEEDSREBASE",
Pierre-Yves Chibon 73d120
                "message": "The pull-request must be rebased before merging",
Pierre-Yves Chibon 73d120
                "short_code": "Needs rebase",
Slavek Kabrda 5759e6
            }
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
            js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 5759e6
            self.assertDictEqual(js_data, exp)
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
            # Asking a second time will trigger the cache
Slavek Kabrda 5759e6
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Slavek Kabrda 5759e6
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Slavek Kabrda 5759e6
            self.assertEqual(output.status_code, 200)
Slavek Kabrda 5759e6
            exp = {
Pierre-Yves Chibon 73d120
                "code": "NEEDSREBASE",
Pierre-Yves Chibon 73d120
                "message": "The pull-request must be rebased before merging",
Pierre-Yves Chibon 73d120
                "short_code": "Needs rebase",
Slavek Kabrda 5759e6
            }
Slavek Kabrda 5759e6
Slavek Kabrda 5759e6
            js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 5759e6
            self.assertDictEqual(js_data, exp)
Slavek Kabrda 5759e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 6937b6
    def test_mergeable_request_pull_minimum_score(self, send_email):
Pierre-Yves Chibon 6937b6
        """ Test the mergeable_request_pull endpoint when the changes can
Pierre-Yves Chibon 6937b6
        be merged with a merge FF, but project settings enforces vote on
Pierre-Yves Chibon 6937b6
        the PR.
Pierre-Yves Chibon 6937b6
        """
Pierre-Yves Chibon 6937b6
        send_email.return_value = True
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Create a git repo to play with
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 73d120
        origgitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 6937b6
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon 6937b6
        os.makedirs(origgitrepo)
Pierre-Yves Chibon 6937b6
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos_tmp", "test.git")
Pierre-Yves Chibon 6937b6
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 6937b6
        repo.index.write()
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Commits the files added
Pierre-Yves Chibon 6937b6
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 6937b6
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 6937b6
            author,
Pierre-Yves Chibon 6937b6
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 6937b6
            # binary string representing the tree object ID
Pierre-Yves Chibon 6937b6
            tree,
Pierre-Yves Chibon 6937b6
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Pierre-Yves Chibon 6937b6
        )
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 6937b6
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 6937b6
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 6937b6
        repo.index.write()
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Commits the files added
Pierre-Yves Chibon 6937b6
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 6937b6
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 6937b6
            author,
Pierre-Yves Chibon 6937b6
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "consider",
Pierre-Yves Chibon 6937b6
            # binary string representing the tree object ID
Pierre-Yves Chibon 6937b6
            tree,
Pierre-Yves Chibon 6937b6
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 6937b6
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/feature:refs/heads/feature"
Pierre-Yves Chibon 6937b6
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 6937b6
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Create a PR for these changes
Pierre-Yves Chibon 6937b6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 6937b6
            session=self.session,
Pierre-Yves Chibon 6937b6
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 6937b6
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 6937b6
        )
Pierre-Yves Chibon 73d120
        settings = {"Minimum_score_to_merge_pull-request": 2}
Pierre-Yves Chibon 6937b6
        project.settings = settings
Pierre-Yves Chibon 6937b6
        self.session.commit()
Pierre-Yves Chibon 6937b6
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the feature branch")
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        # Check if the PR can be merged
Pierre-Yves Chibon 6937b6
        data = {}
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon 6937b6
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
            # With all the desired information
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 6937b6
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 73d120
                "force": True,
Pierre-Yves Chibon 6937b6
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 6937b6
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6937b6
            exp = {
Pierre-Yves Chibon 73d120
                "code": "CONFLICTS",
Pierre-Yves Chibon 73d120
                "message": "Pull-Request does not meet the minimal number "
Pierre-Yves Chibon 73d120
                "of review required: 0/2",
Pierre-Yves Chibon 6937b6
            }
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6937b6
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 6937b6
Fabien Boucher 4c3ab2
            # Verify we get a valid merge_status (not 'unknown')
Pierre-Yves Chibon 73d120
            pub_api_call = self.app.get("/api/0/test/pull-request/1")
Fabien Boucher 4c3ab2
            data = json.loads(pub_api_call.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertIn(data["cached_merge_status"], ("MERGE", "FFORWARD"))
Fabien Boucher 4c3ab2
Pierre-Yves Chibon 6937b6
            # Asking a second time will trigger the cache
Pierre-Yves Chibon 6937b6
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 6937b6
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 6937b6
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 6937b6
            exp = {
Pierre-Yves Chibon 73d120
                "code": "CONFLICTS",
Pierre-Yves Chibon 73d120
                "message": "Pull-Request does not meet the minimal number "
Pierre-Yves Chibon 73d120
                "of review required: 0/2",
Pierre-Yves Chibon 6937b6
            }
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 6937b6
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 6937b6
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 6937b6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 705645
    @patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.git.merge_pull_request",
Pierre-Yves Chibon 73d120
        MagicMock(side_effect=pagure.exceptions.PagureException("error")),
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 705645
    def test_mergeable_request_pull_merge_pagureerror(self):
Pierre-Yves Chibon 705645
        """ Test the mergeable_request_pull endpoint when the backend
Pierre-Yves Chibon 705645
        raises an GitError exception.
Pierre-Yves Chibon 705645
        """
Pierre-Yves Chibon 705645
        # Create a git repo to play with
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 73d120
        origgitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 705645
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon 705645
        os.makedirs(origgitrepo)
Pierre-Yves Chibon 705645
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos_tmp", "test.git")
Pierre-Yves Chibon 705645
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "consider",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/feature:refs/heads/feature"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create another file in the master branch
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        repo.index.add(".gitignore")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add .gitignore file for testing",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a PR for these changes
Pierre-Yves Chibon 705645
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 705645
            session=self.session,
Pierre-Yves Chibon 705645
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 705645
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        self.session.commit()
Pierre-Yves Chibon 705645
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the feature branch")
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Check if the PR can be merged
Pierre-Yves Chibon 705645
        data = {}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon 705645
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 705645
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # With all the desired information
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 73d120
                "force": True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 500)
Pierre-Yves Chibon 73d120
            exp = {"code": "CONFLICTS", "message": "error"}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 705645
    @patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.git.merge_pull_request",
Pierre-Yves Chibon 73d120
        MagicMock(side_effect=pygit2.GitError("git error")),
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 705645
    def test_mergeable_request_pull_merge_giterror(self):
Pierre-Yves Chibon 705645
        """ Test the mergeable_request_pull endpoint when the backend
Pierre-Yves Chibon 705645
        raises an GitError exception.
Pierre-Yves Chibon 705645
        """
Pierre-Yves Chibon 705645
        # Create a git repo to play with
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 73d120
        origgitrepo = os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 705645
        self.assertFalse(os.path.exists(origgitrepo))
Pierre-Yves Chibon 705645
        os.makedirs(origgitrepo)
Pierre-Yves Chibon 705645
        orig_repo = pygit2.init_repository(origgitrepo, bare=True)
Pierre-Yves Chibon 73d120
        os.makedirs(os.path.join(self.path, "repos_tmp"))
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos_tmp", "test.git")
Pierre-Yves Chibon 705645
        repo = pygit2.clone_repository(origgitrepo, gitrepo)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "consider",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/feature:refs/heads/feature"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create another file in the master branch
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        repo.index.add(".gitignore")
Pierre-Yves Chibon 705645
        repo.index.write()
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Commits the files added
Pierre-Yves Chibon 705645
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 705645
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 705645
            author,
Pierre-Yves Chibon 705645
            committer,
Pierre-Yves Chibon 73d120
            "Add .gitignore file for testing",
Pierre-Yves Chibon 705645
            # binary string representing the tree object ID
Pierre-Yves Chibon 705645
            tree,
Pierre-Yves Chibon 705645
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 73d120
        refname = "refs/heads/master:refs/heads/master"
Pierre-Yves Chibon 705645
        ori_remote = repo.remotes[0]
Pierre-Yves Chibon 705645
        PagureRepo.push(ori_remote, refname)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Create a PR for these changes
Pierre-Yves Chibon 705645
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 705645
            session=self.session,
Pierre-Yves Chibon 705645
            repo_from=project,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 705645
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="PR from the feature branch",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 705645
        )
Pierre-Yves Chibon 705645
        self.session.commit()
Pierre-Yves Chibon 705645
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "PR from the feature branch")
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        # Check if the PR can be merged
Pierre-Yves Chibon 705645
        data = {}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon 705645
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 705645
            csrf_token = self.get_csrf(output=output)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            # With all the desired information
Pierre-Yves Chibon 73d120
            project = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
                self.session, "test"
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 705645
            data = {
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 73d120
                "requestid": project.requests[0].uid,
Pierre-Yves Chibon 73d120
                "force": True,
Pierre-Yves Chibon 705645
            }
Pierre-Yves Chibon 73d120
            output = self.app.post("/pv/pull-request/merge", data=data)
Pierre-Yves Chibon 705645
            self.assertEqual(output.status_code, 409)
Pierre-Yves Chibon 73d120
            exp = {"code": "CONFLICTS", "message": "git error"}
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 705645
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 705645
            self.assertDictEqual(js_data, exp)
Pierre-Yves Chibon 705645
Pierre-Yves Chibon 9f395f
    def test_get_branches_of_commit(self):
Pierre-Yves Chibon 73d120
        """ Test the get_branches_of_commit from the internal API. """
Pierre-Yves Chibon 9f395f
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/test/adduser")
Pierre-Yves Chibon 0c0985
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # No CSRF token
Pierre-Yves Chibon 73d120
        data = {"repo": "fakerepo", "commit_id": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "ERROR", "message": "Invalid input submitted"}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Invalid repo
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 73d120
            "repo": "fakerepo",
Pierre-Yves Chibon 73d120
            "commit_id": "foo",
Pierre-Yves Chibon 73d120
            "csrf_token": csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Rigth repo, no commit
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "ERROR", "message": "No commit id submitted"}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Request is fine, but git repo doesn't exist
Pierre-Yves Chibon 72ed6b
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 72ed6b
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test20",
Pierre-Yves Chibon 73d120
            description="test project #20",
Pierre-Yves Chibon 73d120
            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 73d120
        data = {"repo": "test20", "commit_id": "foo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No git repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 9f395f
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 9f395f
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [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 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 9f395f
        commit_hash = repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature_branch",  # the name of the reference to update
Pierre-Yves Chibon 9f395f
            author,
Pierre-Yves Chibon 9f395f
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [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 73d120
        data = {"repo": "test", "commit_id": "foo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 9f395f
            js_data,
Pierre-Yves Chibon 9f395f
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "This commit could not be found in this repo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Pierre-Yves Chibon 9f395f
        # All good
Pierre-Yves Chibon 9f395f
        data = {
Pierre-Yves Chibon 73d120
            "repo": "test",
Pierre-Yves Chibon 73d120
            "commit_id": commit_hash,
Pierre-Yves Chibon 73d120
            "csrf_token": csrf_token,
Pierre-Yves Chibon 9f395f
        }
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Pierre-Yves Chibon 9f395f
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9f395f
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "OK", "branches": ["feature_branch"]}
Pierre-Yves Chibon 9f395f
        )
Pierre-Yves Chibon 9f395f
Michael Watters d19655
    def test_get_branches_of_commit_with_unrelated_branches(self):
Pierre-Yves Chibon 73d120
        """ Test the get_branches_of_commit from the internal API. """
Michael Watters d19655
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Michael Watters d19655
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="pingou")
Michael Watters d19655
        with tests.user_set(self.app.application, user):
Michael Watters d19655
            csrf_token = self.get_csrf()
Michael Watters d19655
Michael Watters d19655
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        gitrepo = os.path.join(self.path, "repos", "test.git")
Michael Watters d19655
        self.assertTrue(os.path.exists(gitrepo))
Michael Watters d19655
        repo = pygit2.Repository(gitrepo)
Michael Watters d19655
Michael Watters d19655
        # Create a file in that git repo
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Michael Watters d19655
        repo.index.write()
Michael Watters d19655
Michael Watters d19655
        # Commits the files added
Michael Watters d19655
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Michael Watters d19655
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Michael Watters d19655
            author,
Michael Watters d19655
            committer,
Pierre-Yves Chibon 73d120
            "Add sources file for testing",
Michael Watters d19655
            # binary string representing the tree object ID
Michael Watters d19655
            tree,
Michael Watters d19655
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Michael Watters d19655
        )
Michael Watters d19655
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Michael Watters d19655
Michael Watters d19655
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        repo.index.add("sources")
Michael Watters d19655
        repo.index.write()
Michael Watters d19655
Michael Watters d19655
        # Commits the files added, but unrelated with the first commit
Michael Watters d19655
        tree = repo.index.write_tree()
Pierre-Yves Chibon 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Michael Watters d19655
        commit = repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Michael Watters d19655
            author,
Michael Watters d19655
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "consider",
Michael Watters d19655
            # binary string representing the tree object ID
Michael Watters d19655
            tree,
Michael Watters d19655
            # list of binary strings representing parents of the new commit
Pierre-Yves Chibon 73d120
            [],
Michael Watters d19655
        )
Michael Watters d19655
        commit_hash = commit.hex
Michael Watters d19655
Michael Watters d19655
        # All good
Michael Watters d19655
        data = {
Pierre-Yves Chibon 73d120
            "repo": "test",
Pierre-Yves Chibon 73d120
            "commit_id": commit_hash,
Pierre-Yves Chibon 73d120
            "csrf_token": csrf_token,
Michael Watters d19655
        }
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/commit/", data=data)
Michael Watters d19655
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data, {"code": "OK", "branches": ["feature"]})
Michael Watters d19655
Pierre-Yves Chibon 17e9e2
    def test_get_branches_head(self):
Pierre-Yves Chibon 73d120
        """ Test the get_branches_head from the internal API. """
Pierre-Yves Chibon 17e9e2
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "fakerepo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/heads/", data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "ERROR", "message": "Invalid input submitted"}
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Invalid repo
Pierre-Yves Chibon 17e9e2
        data = {
Pierre-Yves Chibon 73d120
            "repo": "fakerepo",
Pierre-Yves Chibon 73d120
            "commit_id": "foo",
Pierre-Yves Chibon 73d120
            "csrf_token": csrf_token,
Pierre-Yves Chibon 17e9e2
        }
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/heads/", data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Rigth repo, no commit
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/heads/", data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"branches": {}, "code": "OK", "heads": {}}
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Request is fine, but git repo doesn't exist
Pierre-Yves Chibon 17e9e2
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 17e9e2
            user_id=1,  # pingou
Pierre-Yves Chibon 73d120
            name="test20",
Pierre-Yves Chibon 73d120
            description="test project #20",
Pierre-Yves Chibon 73d120
            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 73d120
        data = {"repo": "test20", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/heads/", data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        self.assertDictEqual(
Pierre-Yves Chibon 17e9e2
            js_data,
Pierre-Yves Chibon 17e9e2
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No git repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Create a git repo to play with
Pierre-Yves Chibon 73d120
        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 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 17e9e2
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/master",  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [],
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 73d120
        first_commit = repo.revparse_single("HEAD")
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # Edit the sources file again
Pierre-Yves Chibon 73d120
        with open(os.path.join(gitrepo, "sources"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("foo\n bar\nbaz\n boose")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 17e9e2
        repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature",  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 73d120
            "Add baz and boose to the sources\n\n There are more objects to "
Pierre-Yves Chibon 73d120
            "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 73d120
            [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 73d120
        with open(os.path.join(gitrepo, ".gitignore"), "w") as stream:
Pierre-Yves Chibon 73d120
            stream.write("*~")
Pierre-Yves Chibon 73d120
        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 73d120
        author = pygit2.Signature("Alice Author", "alice@authors.tld")
Pierre-Yves Chibon 73d120
        committer = pygit2.Signature("Cecil Committer", "cecil@committers.tld")
Pierre-Yves Chibon 17e9e2
        commit_hash = repo.create_commit(
Pierre-Yves Chibon 73d120
            "refs/heads/feature_branch",  # the name of the reference to update
Pierre-Yves Chibon 17e9e2
            author,
Pierre-Yves Chibon 17e9e2
            committer,
Pierre-Yves Chibon 73d120
            "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 73d120
            [first_commit.oid.hex],
Pierre-Yves Chibon 17e9e2
        )
Pierre-Yves Chibon 17e9e2
Pierre-Yves Chibon 17e9e2
        # All good
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/branches/heads/", data=data)
Pierre-Yves Chibon 17e9e2
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 17e9e2
        # We can't test the content since the commit hash will change all
Pierre-Yves Chibon 17e9e2
        # the time, so let's just check the structure
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["branches", "code", "heads"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(len(js_data["heads"]), 3)
Pierre-Yves Chibon 73d120
        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 73d120
        """ Test the get_stats_commits from the internal API. """
Pierre-Yves Chibon e8a127
        # No CSRF token
Pierre-Yves Chibon 73d120
        data = {"repo": "fakerepo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/authors", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "ERROR", "message": "Invalid input submitted"}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_invalid_repo(self):
Pierre-Yves Chibon 73d120
        """ Test the get_stats_commits from the internal API. """
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "fakerepo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/authors", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_empty_git(self):
Pierre-Yves Chibon 73d120
        """ Test the get_stats_commits from the internal API. """
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/authors", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(js_data.keys()), ["code", "message", "task_id", "url"]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Stats asked")
Pierre-Yves Chibon 73d120
        self.assertTrue(js_data["url"].startswith("/pv/task/"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 73d120
        output = self.app.get(js_data["url"])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 73d120
            js_data2
Pierre-Yves Chibon 73d120
            in [
Pierre-Yves Chibon 73d120
                {"results": "reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 73d120
                {"results": "Reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 20a73c
            ]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_git_populated(self):
Pierre-Yves Chibon 73d120
        """ Test the get_stats_commits from the internal API. """
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon e8a127
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/authors", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(js_data.keys()), ["code", "message", "task_id", "url"]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Stats asked")
Pierre-Yves Chibon 73d120
        self.assertTrue(js_data["url"].startswith("/pv/task/"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 73d120
        output = self.app.get(js_data["url"])
Pierre-Yves Chibon c1b474
        while output.status_code == 418:
Pierre-Yves Chibon 73d120
            output = self.app.get(js_data["url"])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertTrue(js_data2["results"][3] > 1509110062)
Pierre-Yves Chibon 73d120
        js_data2["results"][3] = 1509110062
Pierre-Yves Chibon 409376
        self.assertTrue(
Pierre-Yves Chibon 73d120
            js_data2
Pierre-Yves Chibon 73d120
            in [
Pierre-Yves Chibon 409376
                {
Pierre-Yves Chibon 73d120
                    "results": [
Pierre-Yves Chibon 409376
                        2,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            [
Pierre-Yves Chibon 73d120
                                2,
Pierre-Yves Chibon 73d120
                                [
Pierre-Yves Chibon 73d120
                                    [
Pierre-Yves Chibon 73d120
                                        "Alice Author",
Pierre-Yves Chibon 73d120
                                        "alice@authors.tld",
Pierre-Yves Chibon 73d120
                                        "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                        "96c52c78570ffc4cfefcdadf5f8e77aeebcb11e07225df11bbf2fce381cdb8bd"
Pierre-Yves Chibon 73d120
                                        "?s=32&d=retro",
Pierre-Yves Chibon 73d120
                                    ]
Pierre-Yves Chibon 73d120
                                ],
Pierre-Yves Chibon 73d120
                            ]
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 409376
                        1,
Pierre-Yves Chibon 73d120
                        1509110062,
Pierre-Yves Chibon 409376
                    ]
Pierre-Yves Chibon 409376
                },
Pierre-Yves Chibon 409376
                {
Pierre-Yves Chibon 73d120
                    "results": [
Pierre-Yves Chibon 409376
                        2,
Pierre-Yves Chibon 73d120
                        [
Pierre-Yves Chibon 73d120
                            [
Pierre-Yves Chibon 73d120
                                2,
Pierre-Yves Chibon 73d120
                                [
Pierre-Yves Chibon 73d120
                                    [
Pierre-Yves Chibon 73d120
                                        "Alice Author",
Pierre-Yves Chibon 73d120
                                        "alice@authors.tld",
Pierre-Yves Chibon 73d120
                                        "https://seccdn.libravatar.org/avatar/"
Pierre-Yves Chibon 73d120
                                        "96c52c78570ffc4cfefcdadf5f8e77aeebcb11e07225df11bbf2fce381cdb8bd"
Pierre-Yves Chibon 73d120
                                        "?d=retro&s=32",
Pierre-Yves Chibon 73d120
                                    ]
Pierre-Yves Chibon 73d120
                                ],
Pierre-Yves Chibon 73d120
                            ]
Pierre-Yves Chibon 73d120
                        ],
Pierre-Yves Chibon 409376
                        1,
Pierre-Yves Chibon 73d120
                        1509110062,
Pierre-Yves Chibon 409376
                    ]
Pierre-Yves Chibon 73d120
                },
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 73d120
        """ Test the get_stats_commits_trend from the internal API. """
Pierre-Yves Chibon e8a127
        # No CSRF token
Pierre-Yves Chibon 73d120
        data = {"repo": "fakerepo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/trend", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            js_data, {"code": "ERROR", "message": "Invalid input submitted"}
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_invalid_repo(self):
Pierre-Yves Chibon e8a127
        """ Test the get_stats_commits_trend from the internal API. """
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "fakerepo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/trend", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertDictEqual(
Pierre-Yves Chibon e8a127
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "code": "ERROR",
Pierre-Yves Chibon 73d120
                "message": "No repo found with the information provided",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_empty_git(self):
Pierre-Yves Chibon 73d120
        """ Test the get_stats_commits_trend from the internal API. """
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/trend", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(js_data.keys()), ["code", "message", "task_id", "url"]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Stats asked")
Pierre-Yves Chibon 73d120
        self.assertTrue(js_data["url"].startswith("/pv/task/"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 73d120
        output = self.app.get(js_data["url"])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 20a73c
        self.assertTrue(
Pierre-Yves Chibon 73d120
            js_data2
Pierre-Yves Chibon 73d120
            in [
Pierre-Yves Chibon 73d120
                {"results": "reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 73d120
                {"results": "Reference 'refs/heads/master' not found"},
Pierre-Yves Chibon 20a73c
            ]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
    def test_get_stats_commits_trend_git_populated(self):
Pierre-Yves Chibon 73d120
        """ Test the get_stats_commits_trend from the internal API. """
Pierre-Yves Chibon e8a127
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon e8a127
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon e8a127
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        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 73d120
        data = {"repo": "test", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/stats/commits/trend", data=data)
Pierre-Yves Chibon e8a127
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        self.assertEqual(
Pierre-Yves Chibon 73d120
            sorted(js_data.keys()), ["code", "message", "task_id", "url"]
Pierre-Yves Chibon e8a127
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Stats asked")
Pierre-Yves Chibon 73d120
        self.assertTrue(js_data["url"].startswith("/pv/task/"))
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 73d120
        output = self.app.get(js_data["url"])
Aurélien Bompard 626417
        js_data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e8a127
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 73d120
        self.assertDictEqual(js_data2, {"results": [[str(today), 2]]})
Pierre-Yves Chibon e8a127
Pierre-Yves Chibon 29abd1
    def test_get_project_family_no_project(self):
Pierre-Yves Chibon 73d120
        """ Test the get_project_family from the internal API. """
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family")
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_family_no_csrf(self):
Pierre-Yves Chibon 73d120
        """ Test the get_project_family from the internal API. """
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 29abd1
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family")
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "message"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "ERROR")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Invalid input submitted")
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_family(self):
Pierre-Yves Chibon 73d120
        """ Test the get_project_family from the internal API. """
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 29abd1
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon 29abd1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 29abd1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family", data=data)
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "family"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["family"], ["test"])
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
    def test_get_project_larger_family(self):
Pierre-Yves Chibon 73d120
        """ Test the get_project_family from the internal API. """
Pierre-Yves Chibon 29abd1
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Create a 3rd user
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="ralph",
Pierre-Yves Chibon 73d120
            fullname="Ralph bar",
Pierre-Yves Chibon 73d120
            password="ralph_foo",
Pierre-Yves Chibon 73d120
            default_email="ralph@bar.com",
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        self.session.commit()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Create a couple of forks of the test project
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 29abd1
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 29abd1
            is_fork=True,
Pierre-Yves Chibon 29abd1
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        item = pagure.lib.model.Project(
Pierre-Yves Chibon 29abd1
            user_id=3,  # Ralph
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon 29abd1
            is_fork=True,
Pierre-Yves Chibon 29abd1
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccceee",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon 29abd1
        self.session.add(item)
Pierre-Yves Chibon 29abd1
        self.session.commit()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 29abd1
        # Get on with testing
Pierre-Yves Chibon 29abd1
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon 29abd1
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 29abd1
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family", data=data)
Pierre-Yves Chibon 29abd1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "family"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 29abd1
        self.assertEqual(
Pierre-Yves Chibon 73d120
            js_data["family"], ["test", "fork/foo/test", "fork/ralph/test"]
Pierre-Yves Chibon 29abd1
        )
Pierre-Yves Chibon 29abd1
Pierre-Yves Chibon ff1592
    def test_get_project_larger_family_pr_only(self):
Pierre-Yves Chibon 73d120
        """ Test the get_project_family from the internal API. """
Pierre-Yves Chibon ff1592
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Create a 3rd user
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="ralph",
Pierre-Yves Chibon 73d120
            fullname="Ralph bar",
Pierre-Yves Chibon 73d120
            password="ralph_foo",
Pierre-Yves Chibon 73d120
            default_email="ralph@bar.com",
Pierre-Yves Chibon ff1592
        )
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="ralph@bar.com")
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
        self.session.commit()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Create a couple of forks of the test project
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff1592
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon ff1592
            is_fork=True,
Pierre-Yves Chibon ff1592
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon ff1592
        # disable issues in this fork
Pierre-Yves Chibon ff1592
        default_repo_settings = item.settings
Pierre-Yves Chibon 73d120
        default_repo_settings["issue_tracker"] = False
Pierre-Yves Chibon ff1592
        item.settings = default_repo_settings
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        item = pagure.lib.model.Project(
Pierre-Yves Chibon ff1592
            user_id=3,  # Ralph
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon ff1592
            is_fork=True,
Pierre-Yves Chibon ff1592
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbccceee",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon ff1592
        # disable PRs in this fork
Pierre-Yves Chibon ff1592
        default_repo_settings = item.settings
Pierre-Yves Chibon 73d120
        default_repo_settings["pull_requests"] = False
Pierre-Yves Chibon ff1592
        item.settings = default_repo_settings
Pierre-Yves Chibon ff1592
        self.session.add(item)
Pierre-Yves Chibon ff1592
        self.session.commit()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ff1592
        # Get on with testing
Pierre-Yves Chibon ff1592
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon ff1592
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon ff1592
            csrf_token = self.get_csrf()
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "allows_pr": "1"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family", data=data)
Pierre-Yves Chibon ff1592
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff1592
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "family"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["family"], ["test", "fork/foo/test"])
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "allows_issues": "1"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/test/family", data=data)
Pierre-Yves Chibon ff1592
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon ff1592
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "family"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["family"], ["test", "fork/ralph/test"])
Pierre-Yves Chibon ff1592
Pierre-Yves Chibon ea4717
    def test_get_pull_request_ready_branch_no_csrf(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon ea4717
        on the main repository
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon ea4717
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
        # Query branches on the main repo
Pierre-Yves Chibon 73d120
        data = {"repo": "test"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon ea4717
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon ea4717
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "message"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "ERROR")
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["message"], "Invalid input submitted")
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
    def test_get_pull_request_ready_branch_no_repo(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon ea4717
        on the main repository
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon ea4717
        with tests.user_set(self.app.application, tests.FakeUser()):
Pierre-Yves Chibon ea4717
            csrf_token = self.get_csrf()
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon ea4717
        # Query branches on an invalid repo
Pierre-Yves Chibon 73d120
        data = {"repo": "test", "namespace": "fake", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon ea4717
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon ea4717
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "message"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "ERROR")
Pierre-Yves Chibon ea4717
        self.assertEqual(
Pierre-Yves Chibon 73d120
            js_data["message"], "No repo found with the information provided"
Pierre-Yves Chibon ea4717
        )
Pierre-Yves Chibon ea4717
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_main_repo_no_branch(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API
Pierre-Yves Chibon fbbe78
        on the main repository
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the main repo
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "task"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test", "repouser": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "task"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        output = self.app.get("/pv/task/" + js_data["task"])
Pierre-Yves Chibon f523b9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f523b9
        js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 07edc0
        self.assertEqual(
Pierre-Yves Chibon f523b9
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "results": {
Pierre-Yves Chibon 73d120
                    "branch_w_pr": {},
Pierre-Yves Chibon 73d120
                    "new_branch": {
Pierre-Yves Chibon 73d120
                        "feature": {"commits": 2, "target_branch": "master"}
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f523b9
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork_no_parent_no_pr(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork that has no parent repo (deleted) and doesn't allow PR
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
        settings = item.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon fbbe78
        item.settings = settings
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Delete the parent project
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fbbe78
        self.session.delete(project)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test", "repouser": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "message"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "ERROR")
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon 73d120
            js_data["message"], "Pull-request have been disabled for this repo"
Pierre-Yves Chibon fbbe78
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
    def test_get_pull_request_ready_branch_on_fork_no_parent(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API on
Pierre-Yves Chibon fbbe78
        a fork that has no parent repo (deleted).
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon fbbe78
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon fbbe78
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Create foo's fork of the test project
Pierre-Yves Chibon fbbe78
        item = pagure.lib.model.Project(
Pierre-Yves Chibon fbbe78
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Pierre-Yves Chibon fbbe78
            is_fork=True,
Pierre-Yves Chibon fbbe78
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
        settings = item.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = True
Pierre-Yves Chibon fbbe78
        item.settings = settings
Pierre-Yves Chibon fbbe78
        self.session.add(item)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Delete the parent project
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fbbe78
        self.session.delete(project)
Pierre-Yves Chibon fbbe78
        self.session.commit()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Get on with testing
Pierre-Yves Chibon fbbe78
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon fbbe78
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fbbe78
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon fbbe78
        # Query branches on the Ralph's fork
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test", "repouser": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Pierre-Yves Chibon fbbe78
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "task"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        output = self.app.get("/pv/task/" + js_data["task"])
Pierre-Yves Chibon f523b9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f523b9
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fbbe78
        self.assertEqual(
Pierre-Yves Chibon f523b9
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "results": {
Pierre-Yves Chibon 73d120
                    "branch_w_pr": {},
Pierre-Yves Chibon 73d120
                    "new_branch": {
Pierre-Yves Chibon 73d120
                        "feature": {"commits": 2, "target_branch": "master"}
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f523b9
        )
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
    def test_get_pull_request_ready_branch_matching_target_off(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API on
Slavek Kabrda 07edc0
        a fork while PR_TARGET_MATCHING_BRANCH is False
Pierre-Yves Chibon 73d120
        """
Slavek Kabrda 07edc0
        tests.create_projects(self.session)
Slavek Kabrda 07edc0
        # make sure that head is not unborn
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="master"
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="feature"
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Slavek Kabrda 07edc0
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Create foo's fork of the test project
Slavek Kabrda 07edc0
        item = pagure.lib.model.Project(
Slavek Kabrda 07edc0
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Slavek Kabrda 07edc0
            is_fork=True,
Slavek Kabrda 07edc0
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Slavek Kabrda 07edc0
        self.session.add(item)
Slavek Kabrda 07edc0
        self.session.commit()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Get on with testing
Slavek Kabrda 07edc0
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Slavek Kabrda 07edc0
        with tests.user_set(self.app.application, user):
Slavek Kabrda 07edc0
            csrf_token = self.get_csrf()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Query branches on the Ralph's fork
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test", "repouser": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Slavek Kabrda 07edc0
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 07edc0
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "task"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        output = self.app.get("/pv/task/" + js_data["task"])
Pierre-Yves Chibon f523b9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f523b9
        js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 07edc0
        self.assertEqual(
Pierre-Yves Chibon f523b9
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "results": {
Pierre-Yves Chibon 73d120
                    "branch_w_pr": {},
Pierre-Yves Chibon 73d120
                    "new_branch": {
Pierre-Yves Chibon 73d120
                        "feature": {"commits": 2, "target_branch": "master"}
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f523b9
        )
Slavek Kabrda 07edc0
Pierre-Yves Chibon 73d120
    @patch.dict("pagure.config.config", {"PR_TARGET_MATCHING_BRANCH": True})
Slavek Kabrda 07edc0
    def test_get_pull_request_ready_branch_matching_target_on(self):
Pierre-Yves Chibon 73d120
        """Test the get_pull_request_ready_branch from the internal API on
Slavek Kabrda 07edc0
        a fork while PR_TARGET_MATCHING_BRANCH is True
Pierre-Yves Chibon 73d120
        """
Slavek Kabrda 07edc0
        tests.create_projects(self.session)
Slavek Kabrda 07edc0
        # make sure that head is not unborn
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="master"
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="feature"
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Slavek Kabrda 07edc0
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo"), bare=True
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "foo", "test.git"),
Pierre-Yves Chibon 6fdafd
            append="testing from foo's fork",
Pierre-Yves Chibon 73d120
            branch="feature",
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Create foo's fork of the test project
Slavek Kabrda 07edc0
        item = pagure.lib.model.Project(
Slavek Kabrda 07edc0
            user_id=2,  # foo
Pierre-Yves Chibon 73d120
            name="test",
Slavek Kabrda 07edc0
            is_fork=True,
Slavek Kabrda 07edc0
            parent_id=1,  # test
Pierre-Yves Chibon 73d120
            description="test project #1",
Pierre-Yves Chibon 73d120
            hook_token="aaabbbcccddd",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        item.close_status = [
Pierre-Yves Chibon 73d120
            "Invalid",
Pierre-Yves Chibon 73d120
            "Insufficient data",
Pierre-Yves Chibon 73d120
            "Fixed",
Pierre-Yves Chibon 73d120
            "Duplicate",
Pierre-Yves Chibon 73d120
        ]
Slavek Kabrda 07edc0
        self.session.add(item)
Slavek Kabrda 07edc0
        self.session.commit()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Get on with testing
Slavek Kabrda 07edc0
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Slavek Kabrda 07edc0
        with tests.user_set(self.app.application, user):
Slavek Kabrda 07edc0
            csrf_token = self.get_csrf()
Slavek Kabrda 07edc0
Slavek Kabrda 07edc0
        # Query branches on the Ralph's fork
Pierre-Yves Chibon 73d120
        data = {"csrf_token": csrf_token, "repo": "test", "repouser": "foo"}
Pierre-Yves Chibon 73d120
        output = self.app.post("/pv/pull-request/ready", data=data)
Slavek Kabrda 07edc0
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 07edc0
        js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(sorted(js_data.keys()), ["code", "task"])
Pierre-Yves Chibon 73d120
        self.assertEqual(js_data["code"], "OK")
Pierre-Yves Chibon 73d120
        output = self.app.get("/pv/task/" + js_data["task"])
Pierre-Yves Chibon f523b9
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f523b9
        js_data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 07edc0
        self.assertEqual(
Pierre-Yves Chibon f523b9
            js_data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "results": {
Pierre-Yves Chibon 73d120
                    "branch_w_pr": {},
Pierre-Yves Chibon 73d120
                    "new_branch": {
Pierre-Yves Chibon 73d120
                        "feature": {"commits": 1, "target_branch": "feature"}
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f523b9
        )
Pierre-Yves Chibon fbbe78
Pierre-Yves Chibon a3de64
    def test_task_info_task_running(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task isn't
Pierre-Yves Chibon a3de64
        ready.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon 73d120
        task.get = MagicMock(return_value="FAILED")
Pierre-Yves Chibon a3de64
        task.ready = MagicMock(return_value=False)
Pierre-Yves Chibon 73d120
        with patch(
Pierre-Yves Chibon 73d120
            "pagure.lib.tasks.get_result", MagicMock(return_value=task)
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pv/task/2")
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 418)
Pierre-Yves Chibon a3de64
Pierre-Yves Chibon a3de64
    def test_task_info_task_passed(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task failed.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon 73d120
        task.get = MagicMock(return_value="PASSED")
Pierre-Yves Chibon 73d120
        with patch(
Pierre-Yves Chibon 73d120
            "pagure.lib.tasks.get_result", MagicMock(return_value=task)
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pv/task/2")
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon a3de64
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertEqual(js_data, {"results": "PASSED"})
Pierre-Yves Chibon a3de64
Pierre-Yves Chibon a3de64
    def test_task_info_task_failed(self):
Pierre-Yves Chibon a3de64
        """ Test the task_info internal API endpoint when the task failed.
Pierre-Yves Chibon a3de64
        """
Pierre-Yves Chibon a3de64
        task = MagicMock()
Pierre-Yves Chibon 73d120
        task.get = MagicMock(return_value=Exception("Random error"))
Pierre-Yves Chibon 73d120
        with patch(
Pierre-Yves Chibon 73d120
            "pagure.lib.tasks.get_result", MagicMock(return_value=task)
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 73d120
            output = self.app.get("/pv/task/2")
Pierre-Yves Chibon a3de64
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon a3de64
            js_data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertEqual(js_data, {"results": "Random error"})
Pierre-Yves Chibon a3de64
Patrick Uiterwijk 9b237b
    def test_lookup_ssh_key(self):
Patrick Uiterwijk 9b237b
        """ Test the mergeable_request_pull endpoint when the backend
Patrick Uiterwijk 9b237b
        raises an GitError exception.
Patrick Uiterwijk 9b237b
        """
Patrick Uiterwijk 9b237b
        tests.create_projects(self.session)
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        project_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIC4zmifEL8TLLZUZnjAuVL8495DAkpAAM2eBhwHwawBm"
Pierre-Yves Chibon 73d120
        project_key_fp = "SHA256:ZSUQAqpPDWi90Fs6Ow8Epc8F3qiKVfU+H5ssvo7jiI0"
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        pingou = pagure.lib.query.get_user(self.session, "pingou")
Pierre-Yves Chibon 73d120
        user_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDTsfdTcXw4rlU1aQwOTbLOXqossLwpPIk27S/G17kUz"
Pierre-Yves Chibon 73d120
        user_key_fp = "SHA256:jUJHzrq2Ct6Ubf7Y9rnB6tGnbHM9dMVsveyfPojm+i0"
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 930073
        pagure.lib.query.add_sshkey_to_project_or_user(
Patrick Uiterwijk 9b237b
            self.session,
Patrick Uiterwijk 9b237b
            user_key,
Patrick Uiterwijk 9b237b
            pushaccess=True,
Patrick Uiterwijk 9b237b
            creator=pingou,
Patrick Uiterwijk 9b237b
            user=pingou,
Patrick Uiterwijk 9b237b
        )
Pierre-Yves Chibon 930073
        pagure.lib.query.add_sshkey_to_project_or_user(
Patrick Uiterwijk 9b237b
            self.session,
Patrick Uiterwijk 9b237b
            project_key,
Patrick Uiterwijk 9b237b
            pushaccess=True,
Patrick Uiterwijk 9b237b
            creator=pingou,
Patrick Uiterwijk 9b237b
            project=repo,
Patrick Uiterwijk 9b237b
        )
Patrick Uiterwijk 9b237b
        self.session.commit()
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        url = "/pv/ssh/lookupkey/"
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        output = self.app.post(url, data={"search_key": "asdf"})
Patrick Uiterwijk 9b237b
        self.assertEqual(output.status_code, 200)
Patrick Uiterwijk 9b237b
        result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(result["found"], False)
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        output = self.app.post(url, data={"search_key": user_key_fp})
Patrick Uiterwijk 9b237b
        self.assertEqual(output.status_code, 200)
Patrick Uiterwijk 9b237b
        result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(result["found"], True)
Pierre-Yves Chibon 73d120
        self.assertEqual(result["username"], "pingou")
Pierre-Yves Chibon 73d120
        self.assertEqual(result["public_key"], user_key)
Patrick Uiterwijk 9b237b
Patrick Uiterwijk 9b237b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            url, data={"search_key": user_key_fp, "username": "pingou"}
Patrick Uiterwijk 9b237b
        )
Patrick Uiterwijk 9b237b
        self.assertEqual(output.status_code, 200)
Patrick Uiterwijk 9b237b
        result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(result["found"], True)
Pierre-Yves Chibon 73d120
        self.assertEqual(result["username"], "pingou")
Pierre-Yves Chibon 73d120
        self.assertEqual(result["public_key"], user_key)
Patrick Uiterwijk 9b237b
Patrick Uiterwijk 9b237b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            url, data={"search_key": user_key_fp, "username": "foo"}
Patrick Uiterwijk 9b237b
        )
Patrick Uiterwijk 9b237b
        self.assertEqual(output.status_code, 200)
Patrick Uiterwijk 9b237b
        result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(result["found"], False)
Patrick Uiterwijk 9b237b
Pierre-Yves Chibon 73d120
        output = self.app.post(url, data={"search_key": project_key_fp})
Patrick Uiterwijk 9b237b
        self.assertEqual(output.status_code, 200)
Patrick Uiterwijk 9b237b
        result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(result["found"], True)
Pierre-Yves Chibon 73d120
        self.assertEqual(result["username"], "deploykey_test_2")
Pierre-Yves Chibon 73d120
        self.assertEqual(result["public_key"], project_key)
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch.dict(
Pierre-Yves Chibon 73d120
        "pagure.config.config",
Pierre-Yves Chibon 73d120
        {"REPOSPANNER_REGIONS": {"region": {"repo_prefix": "prefix"}}},
Pierre-Yves Chibon 73d120
    )
Patrick Uiterwijk d75948
    def test_check_ssh_access(self):
Patrick Uiterwijk d75948
        """ Test the SSH access check endpoint. """
Patrick Uiterwijk d75948
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        self.session.query(pagure.lib.model.Project).filter(
Pierre-Yves Chibon 73d120
            pagure.lib.model.Project.name == "test2"
Pierre-Yves Chibon 73d120
        ).update({pagure.lib.model.Project.repospanner_region: "region"})
Slavek Kabrda b6a383
        self.session.commit()
Patrick Uiterwijk d75948
Pierre-Yves Chibon 73d120
        url = "/pv/ssh/checkaccess/"
Pierre-Yves Chibon 73d120
Patrick Uiterwijk d75948
        def runtest(project, username, access):
Patrick Uiterwijk d75948
            output = self.app.post(
Pierre-Yves Chibon 73d120
                url, data={"gitdir": project, "username": username}
Patrick Uiterwijk d75948
            )
Patrick Uiterwijk d75948
            self.assertEqual(output.status_code, 200)
Patrick Uiterwijk d75948
            result = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
            self.assertEqual(result["access"], access)
Patrick Uiterwijk d75948
            return result
Patrick Uiterwijk d75948
Pierre-Yves Chibon 73d120
        runtest("project.git", "pingou", False)
Pierre-Yves Chibon 73d120
        i1 = runtest("test.git", "pingou", True)
Pierre-Yves Chibon 73d120
        i2 = runtest("test.git", "foo", True)
Pierre-Yves Chibon 73d120
        i3 = runtest("tickets/test.git", "pingou", True)
Pierre-Yves Chibon 73d120
        i4 = runtest("test2.git", "pingou", True)
Pierre-Yves Chibon 73d120
        runtest("tickets/test.git", "foo", False)
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["reponame"], "test.git")
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["repospanner_reponame"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["repotype"], "main")
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["region"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["project_name"], "test")
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["project_user"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i1["project_namespace"], None)
Patrick Uiterwijk d75948
        self.assertEqual(i1, i2)
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["reponame"], "tickets/test.git")
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["repospanner_reponame"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["repotype"], "tickets")
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["region"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["project_name"], "test")
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["project_user"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i3["project_namespace"], None)
Pierre-Yves Chibon 73d120
        self.assertEqual(i4["repospanner_reponame"], "prefix/main/test2")
Pierre-Yves Chibon 73d120
        self.assertEqual(i4["region"], "region")
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)