Blame tests/test_pagure_flask_api_fork.py

Pierre-Yves Chibon c71c2d
# -*- coding: utf-8 -*-
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
"""
Pierre-Yves Chibon c71c2d
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
 Authors:
Pierre-Yves Chibon c71c2d
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
"""
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon c71c2d
import datetime
Pierre-Yves Chibon c71c2d
import unittest
Pierre-Yves Chibon c71c2d
import shutil
Pierre-Yves Chibon c71c2d
import sys
Pierre-Yves Chibon c71c2d
import os
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
import json
Pierre-Yves Chibon 9811e6
from mock import patch, MagicMock
Pierre-Yves Chibon c71c2d
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 c71c2d
Pierre-Yves Chibon 930073
import pagure.lib.query
Lenka Segura 3d9cf6
import pagure.default_config
Pierre-Yves Chibon c71c2d
import tests
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
class PagureFlaskApiForktests(tests.Modeltests):
Pierre-Yves Chibon c71c2d
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c27d61
    maxDiff = None
Pierre-Yves Chibon c27d61
Pierre-Yves Chibon c71c2d
    def setUp(self):
Pierre-Yves Chibon c71c2d
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon c71c2d
        super(PagureFlaskApiForktests, self).setUp()
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_pr_disabled(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api when PR
Pierre-Yves Chibon fe796e
        are disabled. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon fe796e
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon fe796e
        repo.settings = settings
Pierre-Yves Chibon fe796e
        self.session.add(repo)
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests")
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data,
Pierre-Yves Chibon fe796e
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_pr_closed(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api to list
Pierre-Yves Chibon fe796e
        the closed PRs. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=closed")
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon fe796e
            data,
Pierre-Yves Chibon fe796e
            {
Pierre-Yves Chibon 73d120
                "args": {
Pierre-Yves Chibon 73d120
                    "assignee": None,
Pierre-Yves Chibon 73d120
                    "author": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "status": "closed",
Karsten Hopp 566852
                },
Pierre-Yves Chibon 73d120
                "pagination": {
Pierre-Yves Chibon 73d120
                    "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                    "next": None,
Pierre-Yves Chibon 73d120
                    "page": 1,
Pierre-Yves Chibon 73d120
                    "pages": 0,
Pierre-Yves Chibon 73d120
                    "per_page": 20,
Pierre-Yves Chibon 73d120
                    "prev": None,
Karsten Hopp 566852
                },
Pierre-Yves Chibon 73d120
                "requests": [],
Pierre-Yves Chibon 73d120
                "total_requests": 0,
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Close the PR and try again
Pierre-Yves Chibon 930073
        pagure.lib.query.close_pull_request(
Pierre-Yves Chibon 73d120
            self.session, request=req, user="pingou", merged=False
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=closed")
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            ["args", "pagination", "requests", "total_requests"],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data["args"],
Karsten Hopp 566852
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "author": None,
Pierre-Yves Chibon 73d120
                "page": 1,
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "status": "closed",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_requests"], 1)
Pierre-Yves Chibon fe796e
Lenka Segura 96f2fe
        # Create two closed pull-requests
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 96f2fe
        pagure.lib.query.new_pull_request(
Lenka Segura 96f2fe
            session=self.session,
Lenka Segura 96f2fe
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Lenka Segura 96f2fe
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="closed pullrequest by user foo on repo test",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            status="Closed",
Lenka Segura 96f2fe
        )
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 96f2fe
        pagure.lib.query.new_pull_request(
Lenka Segura 96f2fe
            session=self.session,
Lenka Segura 96f2fe
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Lenka Segura 96f2fe
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="closed pullrequest by user pingou on repo test",
Pierre-Yves Chibon 73d120
            user="pingou",
Lenka Segura 96f2fe
            status="Closed",
Lenka Segura 96f2fe
        )
Lenka Segura 96f2fe
        self.session.commit()
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 96f2fe
        pagure.lib.query.new_pull_request(
Lenka Segura 96f2fe
            session=self.session,
Lenka Segura 96f2fe
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Lenka Segura 96f2fe
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="merged pullrequest by user pingou on repo test",
Pierre-Yves Chibon 73d120
            user="pingou",
Lenka Segura 96f2fe
            status="Merged",
Lenka Segura 96f2fe
        )
Lenka Segura 96f2fe
        self.session.commit()
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 96f2fe
        pagure.lib.query.new_pull_request(
Lenka Segura 96f2fe
            session=self.session,
Lenka Segura 96f2fe
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Lenka Segura 96f2fe
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="merged pullrequest by user foo on repo test",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            status="Merged",
Lenka Segura 96f2fe
        )
Lenka Segura 96f2fe
        self.session.commit()
Lenka Segura 96f2fe
Lenka Segura 96f2fe
        # Test the API view of closed pull-requests
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=closed")
Lenka Segura 96f2fe
        self.assertEqual(output.status_code, 200)
Lenka Segura 96f2fe
        data = json.loads(output.get_data(as_text=True))
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        self.assertEqual(len(data["requests"]), 3)
Lenka Segura 96f2fe
        self.assertEqual(
Lenka Segura 96f2fe
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            ["args", "pagination", "requests", "total_requests"],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        for req in data["requests"]:
Pierre-Yves Chibon 73d120
            self.assertEqual(req["status"], "Closed")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["args"]["status"], "closed")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["args"]["page"], 1)
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_requests"], 3)
Lenka Segura 96f2fe
Lenka Segura 96f2fe
        # Test the API view of merged pull-requests
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=merged")
Lenka Segura 96f2fe
        self.assertEqual(output.status_code, 200)
Lenka Segura 96f2fe
        data = json.loads(output.get_data(as_text=True))
Lenka Segura 96f2fe
Pierre-Yves Chibon 73d120
        self.assertEqual(len(data["requests"]), 2)
Lenka Segura 96f2fe
        self.assertEqual(
Lenka Segura 96f2fe
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            ["args", "pagination", "requests", "total_requests"],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        for req in data["requests"]:
Pierre-Yves Chibon 73d120
            self.assertEqual(req["status"], "Merged")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["args"]["status"], "merged")
Pierre-Yves Chibon 73d120
        self.assertEqual(data["args"]["page"], 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_requests"], 2)
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon fe796e
    def test_api_pull_request_views_all_pr(self):
Pierre-Yves Chibon fe796e
        """ Test the api_pull_request_views method of the flask api to list
Pierre-Yves Chibon fe796e
        all PRs. """
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        tests.create_projects(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens(self.session)
Pierre-Yves Chibon fe796e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon fe796e
            session=self.session,
Pierre-Yves Chibon fe796e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon fe796e
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon fe796e
        self.session.commit()
Pierre-Yves Chibon fe796e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=all")
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            ["args", "pagination", "requests", "total_requests"],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data["args"],
Karsten Hopp 566852
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "author": None,
Pierre-Yves Chibon 73d120
                "page": 1,
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "status": "all",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_requests"], 1)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon fe796e
        # Close the PR and try again
Pierre-Yves Chibon 930073
        pagure.lib.query.close_pull_request(
Pierre-Yves Chibon 73d120
            self.session, request=req, user="pingou", merged=False
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests?status=all")
Pierre-Yves Chibon fe796e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fe796e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon fe796e
        self.assertEqual(
Pierre-Yves Chibon dd03b1
            sorted(data.keys()),
Pierre-Yves Chibon 73d120
            ["args", "pagination", "requests", "total_requests"],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon fe796e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data["args"],
Karsten Hopp 566852
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "author": None,
Pierre-Yves Chibon 73d120
                "page": 1,
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "status": "all",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon fe796e
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(data["total_requests"], 1)
Pierre-Yves Chibon fe796e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_views(self, send_email):
Pierre-Yves Chibon ad85b9
        """ Test the api_pull_request_views method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon 7cc2d4
Pierre-Yves Chibon ad85b9
        tests.create_projects(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_tokens(self.session)
Pierre-Yves Chibon ad85b9
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon ad85b9
            session=self.session,
Pierre-Yves Chibon ad85b9
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon ad85b9
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon ad85b9
        )
Pierre-Yves Chibon ad85b9
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Invalid repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/pull-requests")
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon ad85b9
        )
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # List pull-requests
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests")
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["requests"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["requests"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data["pagination"][k] = "http://localhost..."
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "args": {
Pierre-Yves Chibon 316f15
                "assignee": None,
Pierre-Yves Chibon 316f15
                "author": None,
Karsten Hopp 566852
                "page": 1,
Karsten Hopp 566852
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "status": True,
Matt Prahl 6bf79a
            },
Pierre-Yves Chibon 73d120
            "pagination": {
Pierre-Yves Chibon 73d120
                "first": "http://localhost...",
Pierre-Yves Chibon 73d120
                "last": "http://localhost...",
Pierre-Yves Chibon 73d120
                "next": None,
Pierre-Yves Chibon 73d120
                "page": 1,
Pierre-Yves Chibon 73d120
                "pages": 1,
Pierre-Yves Chibon 73d120
                "per_page": 20,
Pierre-Yves Chibon 73d120
                "prev": None,
Karsten Hopp 566852
            },
Pierre-Yves Chibon 73d120
            "requests": [
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "assignee": None,
Pierre-Yves Chibon 73d120
                    "branch": "master",
Pierre-Yves Chibon 73d120
                    "branch_from": "master",
Pierre-Yves Chibon 73d120
                    "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                    "closed_at": None,
Pierre-Yves Chibon 73d120
                    "closed_by": None,
Pierre-Yves Chibon 73d120
                    "comments": [],
Pierre-Yves Chibon 73d120
                    "commit_start": None,
Pierre-Yves Chibon 73d120
                    "commit_stop": None,
Pierre-Yves Chibon ad85b9
                    "date_created": "1431414800",
Pierre-Yves Chibon ad85b9
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "initial_comment": None,
Pierre-Yves Chibon 73d120
                    "last_updated": "1431414800",
Pierre-Yves Chibon 73d120
                    "project": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "access_users": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "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 73d120
                        "custom_keys": [],
Pierre-Yves Chibon 73d120
                        "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                        "date_modified": "1431414800",
Pierre-Yves Chibon 73d120
                        "description": "test project #1",
Pierre-Yves Chibon 73d120
                        "fullname": "test",
Pierre-Yves Chibon 73d120
                        "url_path": "test",
Pierre-Yves Chibon 73d120
                        "id": 1,
Pierre-Yves Chibon 73d120
                        "milestones": {},
Pierre-Yves Chibon 73d120
                        "name": "test",
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "parent": None,
Pierre-Yves Chibon 73d120
                        "priorities": {},
Pierre-Yves Chibon 73d120
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6bf79a
                    },
Pierre-Yves Chibon 73d120
                    "remote_git": None,
Pierre-Yves Chibon 73d120
                    "repo_from": {
Pierre-Yves Chibon 73d120
                        "access_groups": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "access_users": {
Pierre-Yves Chibon 73d120
                            "admin": [],
Pierre-Yves Chibon 73d120
                            "commit": [],
Pierre-Yves Chibon 73d120
                            "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                            "ticket": [],
Pierre-Yves Chibon 73d120
                        },
Pierre-Yves Chibon 73d120
                        "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 73d120
                        "custom_keys": [],
Pierre-Yves Chibon 73d120
                        "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                        "date_modified": "1431414800",
Pierre-Yves Chibon 73d120
                        "description": "test project #1",
Pierre-Yves Chibon 73d120
                        "fullname": "test",
Pierre-Yves Chibon 73d120
                        "url_path": "test",
Pierre-Yves Chibon 73d120
                        "id": 1,
Pierre-Yves Chibon 73d120
                        "milestones": {},
Pierre-Yves Chibon 73d120
                        "name": "test",
Pierre-Yves Chibon 73d120
                        "namespace": None,
Pierre-Yves Chibon 73d120
                        "parent": None,
Pierre-Yves Chibon 73d120
                        "priorities": {},
Pierre-Yves Chibon 73d120
                        "tags": [],
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "status": "Open",
Pierre-Yves Chibon a30916
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "threshold_reached": None,
Pierre-Yves Chibon 73d120
                    "title": "test pull-request",
Pierre-Yves Chibon 73d120
                    "uid": "1431414800",
Pierre-Yves Chibon 73d120
                    "updated_on": "1431414800",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon ad85b9
                }
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            "total_requests": 1,
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon ad85b9
        # Access Pull-Request authenticated
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-requests", headers=headers)
Pierre-Yves Chibon ad85b9
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data2["requests"][0]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["requests"][0]["last_updated"] = "1431414800"
Pierre-Yves Chibon 73d120
        for k in ["first", "last"]:
Pierre-Yves Chibon 73d120
            self.assertIsNotNone(data["pagination"][k])
Pierre-Yves Chibon 73d120
            data2["pagination"][k] = "http://localhost..."
Pierre-Yves Chibon ad85b9
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon ad85b9
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_view_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_view method of the flask api. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_view(self, send_email):
Pierre-Yves Chibon c71c2d
        """ Test the api_pull_request_view method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon c71c2d
        tests.create_projects(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_tokens(self.session)
Pierre-Yves Chibon c71c2d
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c71c2d
            session=self.session,
Pierre-Yves Chibon c71c2d
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon c71c2d
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Invalid repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/pull-request/1")
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Invalid issue for this repo
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test2/pull-request/1")
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon c71c2d
        )
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Valid issue
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1431414800"
Matt Prahl 6bf79a
        expected_data = {
Matt Prahl 6bf79a
            "assignee": None,
Matt Prahl 6bf79a
            "branch": "master",
Matt Prahl 6bf79a
            "branch_from": "master",
Pierre-Yves Chibon f21734
            "cached_merge_status": "unknown",
Matt Prahl 6bf79a
            "closed_at": None,
Matt Prahl 6bf79a
            "closed_by": None,
Matt Prahl 6bf79a
            "comments": [],
Matt Prahl 6bf79a
            "commit_start": None,
Matt Prahl 6bf79a
            "commit_stop": None,
Matt Prahl 6bf79a
            "date_created": "1431414800",
Matt Prahl 6bf79a
            "id": 1,
Matt Prahl 6bf79a
            "initial_comment": None,
Matt Prahl 6bf79a
            "last_updated": "1431414800",
Matt Prahl 6bf79a
            "project": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
                "access_users": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Matt Prahl 6bf79a
                },
Pierre-Yves Chibon f254bf
                "close_status": [
Matt Prahl 6bf79a
                    "Invalid",
Matt Prahl 6bf79a
                    "Insufficient data",
Matt Prahl 6bf79a
                    "Fixed",
Pierre-Yves Chibon 73d120
                    "Duplicate",
Pierre-Yves Chibon f254bf
                ],
Pierre-Yves Chibon 022564
                "custom_keys": [],
Pierre-Yves Chibon c71c2d
                "date_created": "1431414800",
Clement Verna cd6e30
                "date_modified": "1431414800",
Pierre-Yves Chibon c71c2d
                "description": "test project #1",
Pierre-Yves Chibon c9e619
                "fullname": "test",
Pierre-Yves Chibon 469e24
                "url_path": "test",
Pierre-Yves Chibon c71c2d
                "id": 1,
Pierre-Yves Chibon f254bf
                "milestones": {},
Pierre-Yves Chibon c71c2d
                "name": "test",
Pierre-Yves Chibon 733a9d
                "namespace": None,
Pierre-Yves Chibon c71c2d
                "parent": None,
Pierre-Yves Chibon 14bde2
                "priorities": {},
Pierre-Yves Chibon a30916
                "tags": [],
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "remote_git": None,
Matt Prahl 6bf79a
            "repo_from": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Matt Prahl 6bf79a
                "access_users": {
Matt Prahl 6bf79a
                    "admin": [],
Matt Prahl 6bf79a
                    "commit": [],
Matt Prahl 6bf79a
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Pierre-Yves Chibon 73d120
                },
Matt Prahl 6bf79a
                "close_status": [
Matt Prahl 6bf79a
                    "Invalid",
Matt Prahl 6bf79a
                    "Insufficient data",
Matt Prahl 6bf79a
                    "Fixed",
Pierre-Yves Chibon 73d120
                    "Duplicate",
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
                "custom_keys": [],
Pierre-Yves Chibon 73d120
                "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                "date_modified": "1431414800",
Pierre-Yves Chibon 73d120
                "description": "test project #1",
Pierre-Yves Chibon 73d120
                "fullname": "test",
Pierre-Yves Chibon 73d120
                "url_path": "test",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "milestones": {},
Pierre-Yves Chibon 73d120
                "name": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "parent": None,
Pierre-Yves Chibon 73d120
                "priorities": {},
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6bf79a
            },
Matt Prahl 6bf79a
            "status": "Open",
Pierre-Yves Chibon 31a638
            "tags": [],
Pierre-Yves Chibon 2233c1
            "threshold_reached": None,
Matt Prahl 6bf79a
            "title": "test pull-request",
Matt Prahl 6bf79a
            "uid": "1431414800",
Matt Prahl 6bf79a
            "updated_on": "1431414800",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Matt Prahl 6bf79a
        }
Matt Prahl 6bf79a
        self.assertDictEqual(data, expected_data)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon c71c2d
        # Access Pull-Request authenticated
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1", headers=headers)
Pierre-Yves Chibon c71c2d
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["uid"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["last_updated"] = "1431414800"
Pierre-Yves Chibon c71c2d
        self.assertDictEqual(data, data2)
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Slavek Kabrda 1a6e21
    def test_api_pull_request_by_uid_view(self, send_email):
Slavek Kabrda 1a6e21
        """ Test the api_pull_request_by_uid_view method of the flask api. """
Slavek Kabrda 1a6e21
        send_email.return_value = True
Slavek Kabrda 1a6e21
        tests.create_projects(self.session)
Slavek Kabrda 1a6e21
        tests.create_tokens(self.session)
Slavek Kabrda 1a6e21
        tests.create_tokens_acl(self.session)
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Slavek Kabrda 1a6e21
            session=self.session,
Slavek Kabrda 1a6e21
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Slavek Kabrda 1a6e21
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Slavek Kabrda 1a6e21
        )
Slavek Kabrda 1a6e21
        self.session.commit()
Slavek Kabrda 1a6e21
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Slavek Kabrda 1a6e21
        uid = req.uid
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Invalid request
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/pull-requests/{}".format(uid + "aaa"))
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 404)
Slavek Kabrda 1a6e21
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 1a6e21
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Slavek Kabrda 1a6e21
        )
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Valid issue
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/pull-requests/{}".format(uid))
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 1a6e21
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1431414800"
Slavek Kabrda 1a6e21
        expected_data = {
Slavek Kabrda 1a6e21
            "assignee": None,
Slavek Kabrda 1a6e21
            "branch": "master",
Slavek Kabrda 1a6e21
            "branch_from": "master",
Slavek Kabrda 1a6e21
            "cached_merge_status": "unknown",
Slavek Kabrda 1a6e21
            "closed_at": None,
Slavek Kabrda 1a6e21
            "closed_by": None,
Slavek Kabrda 1a6e21
            "comments": [],
Slavek Kabrda 1a6e21
            "commit_start": None,
Slavek Kabrda 1a6e21
            "commit_stop": None,
Slavek Kabrda 1a6e21
            "date_created": "1431414800",
Slavek Kabrda 1a6e21
            "id": 1,
Slavek Kabrda 1a6e21
            "initial_comment": None,
Slavek Kabrda 1a6e21
            "last_updated": "1431414800",
Slavek Kabrda 1a6e21
            "project": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Slavek Kabrda 1a6e21
                "access_users": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Slavek Kabrda 1a6e21
                },
Slavek Kabrda 1a6e21
                "close_status": [
Slavek Kabrda 1a6e21
                    "Invalid",
Slavek Kabrda 1a6e21
                    "Insufficient data",
Slavek Kabrda 1a6e21
                    "Fixed",
Pierre-Yves Chibon 73d120
                    "Duplicate",
Slavek Kabrda 1a6e21
                ],
Slavek Kabrda 1a6e21
                "custom_keys": [],
Slavek Kabrda 1a6e21
                "date_created": "1431414800",
Slavek Kabrda 1a6e21
                "date_modified": "1431414800",
Slavek Kabrda 1a6e21
                "description": "test project #1",
Slavek Kabrda 1a6e21
                "fullname": "test",
Slavek Kabrda 1a6e21
                "url_path": "test",
Slavek Kabrda 1a6e21
                "id": 1,
Slavek Kabrda 1a6e21
                "milestones": {},
Slavek Kabrda 1a6e21
                "name": "test",
Slavek Kabrda 1a6e21
                "namespace": None,
Slavek Kabrda 1a6e21
                "parent": None,
Slavek Kabrda 1a6e21
                "priorities": {},
Slavek Kabrda 1a6e21
                "tags": [],
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Slavek Kabrda 1a6e21
            },
Slavek Kabrda 1a6e21
            "remote_git": None,
Slavek Kabrda 1a6e21
            "repo_from": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Slavek Kabrda 1a6e21
                "access_users": {
Slavek Kabrda 1a6e21
                    "admin": [],
Slavek Kabrda 1a6e21
                    "commit": [],
Slavek Kabrda 1a6e21
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Pierre-Yves Chibon 73d120
                },
Slavek Kabrda 1a6e21
                "close_status": [
Slavek Kabrda 1a6e21
                    "Invalid",
Slavek Kabrda 1a6e21
                    "Insufficient data",
Slavek Kabrda 1a6e21
                    "Fixed",
Pierre-Yves Chibon 73d120
                    "Duplicate",
Pierre-Yves Chibon 73d120
                ],
Pierre-Yves Chibon 73d120
                "custom_keys": [],
Pierre-Yves Chibon 73d120
                "date_created": "1431414800",
Pierre-Yves Chibon 73d120
                "date_modified": "1431414800",
Pierre-Yves Chibon 73d120
                "description": "test project #1",
Pierre-Yves Chibon 73d120
                "fullname": "test",
Pierre-Yves Chibon 73d120
                "url_path": "test",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "milestones": {},
Pierre-Yves Chibon 73d120
                "name": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "parent": None,
Pierre-Yves Chibon 73d120
                "priorities": {},
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Slavek Kabrda 1a6e21
            },
Slavek Kabrda 1a6e21
            "status": "Open",
Pierre-Yves Chibon 31a638
            "tags": [],
Pierre-Yves Chibon 2233c1
            "threshold_reached": None,
Slavek Kabrda 1a6e21
            "title": "test pull-request",
Slavek Kabrda 1a6e21
            "uid": uid,
Slavek Kabrda 1a6e21
            "updated_on": "1431414800",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Slavek Kabrda 1a6e21
        }
Slavek Kabrda 1a6e21
        self.assertDictEqual(data, expected_data)
Slavek Kabrda 1a6e21
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Slavek Kabrda 1a6e21
Slavek Kabrda 1a6e21
        # Access Pull-Request authenticated
Pierre-Yves Chibon 73d120
        output = self.app.get(
Pierre-Yves Chibon 73d120
            "/api/0/pull-requests/{}".format(uid), headers=headers
Pierre-Yves Chibon 73d120
        )
Slavek Kabrda 1a6e21
        self.assertEqual(output.status_code, 200)
Slavek Kabrda 1a6e21
        data2 = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["project"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["project"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["repo_from"]["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["repo_from"]["date_modified"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["date_created"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["updated_on"] = "1431414800"
Pierre-Yves Chibon 73d120
        data2["last_updated"] = "1431414800"
Slavek Kabrda 1a6e21
        self.assertDictEqual(data, data2)
Slavek Kabrda 1a6e21
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_close_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_close method of the flask api. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 7cc2d4
    def test_api_pull_request_close(self, send_email):
Pierre-Yves Chibon 209c7e
        """ Test the api_pull_request_close method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon 7cc2d4
Pierre-Yves Chibon 209c7e
        tests.create_projects(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 209c7e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 209c7e
            session=self.session,
Pierre-Yves Chibon 209c7e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 209c7e
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Invalid project
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 209c7e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 209c7e
        )
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 209c7e
        # Valid token, wrong project
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon f12b4a
        # Invalid PR
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/2/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Create a token for foo for this project
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
Pierre-Yves Chibon f12b4a
            user_id=2,
Pierre-Yves Chibon f12b4a
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to close PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon b11d54
        for acl in acls:
Pierre-Yves Chibon 73d120
            if acl.name == "pull_request_close":
Pierre-Yves Chibon b11d54
                break
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 73d120
            token_id="foobar_token", acl_id=acl.id
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foobar_token"}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # User not admin
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon f12b4a
            {
Pierre-Yves Chibon 73d120
                "error": "You are not allowed to merge/close pull-request "
Pierre-Yves Chibon 73d120
                "for this project",
Pierre-Yves Chibon 73d120
                "error_code": "ENOPRCLOSE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 209c7e
        # Close PR
Pierre-Yves Chibon 209c7e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/close", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 209c7e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Pull-request closed!"})
Pierre-Yves Chibon 209c7e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_pr_disabled(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when PR
Pierre-Yves Chibon 417805
        are disabled. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_only_assigned(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        only assignee can merge the PR and the PR isn't assigned. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Only_assignee_can_merge_pull-request"] = True
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "This request must be assigned to be merged",
Pierre-Yves Chibon 73d120
                "error_code": "ENOTASSIGNED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_only_assigned_not_assignee(
Pierre-Yves Chibon 73d120
        self, send_email
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        only assignee can merge the PR and the PR isn't assigned to the
Pierre-Yves Chibon 417805
        user asking to merge. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 73d120
        req.assignee = pagure.lib.query.search_user(self.session, "foo")
Pierre-Yves Chibon 417805
        self.session.add(req)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Only_assignee_can_merge_pull-request"] = True
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Julen Landa Alustiza a51825
                "error": "Only the assignee can merge this request",
Pierre-Yves Chibon 73d120
                "error_code": "ENOTASSIGNEE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_pull_request_merge_minimal_score(self, send_email):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_merge method of the flask api when
Pierre-Yves Chibon 417805
        a PR requires a certain minimal score to be merged. """
Pierre-Yves Chibon 417805
        send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Minimum_score_to_merge_pull-request"] = 2
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "This request does not have the minimum review "
Pierre-Yves Chibon 73d120
                "score necessary to be merged",
Pierre-Yves Chibon 73d120
                "error_code": "EPRSCORE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Patrick Uiterwijk 02a7cc
    def test_api_pull_request_merge(self, send_email):
Pierre-Yves Chibon a9c964
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 7cc2d4
        send_email.return_value = True
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon a9c964
        tests.create_tokens(self.session)
Pierre-Yves Chibon a9c964
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon a9c964
            session=self.session,
Pierre-Yves Chibon a9c964
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon a9c964
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Invalid project
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon a9c964
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon a9c964
        )
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon a9c964
        # Valid token, wrong project
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon f12b4a
        # Invalid PR
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/2/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Create a token for foo for this project
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
Pierre-Yves Chibon f12b4a
            user_id=2,
Pierre-Yves Chibon f12b4a
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon c02e60
Pierre-Yves Chibon c02e60
        # Allow the token to merge PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon b11d54
        for acl in acls:
Pierre-Yves Chibon 73d120
            if acl.name == "pull_request_merge":
Pierre-Yves Chibon b11d54
                break
Pierre-Yves Chibon f12b4a
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 73d120
            token_id="foobar_token", acl_id=acl.id
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
        self.session.add(item)
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foobar_token"}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # User not admin
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon dea6ca
            data,
Pierre-Yves Chibon dea6ca
            {
Pierre-Yves Chibon 73d120
                "error": "You are not allowed to merge/close pull-request "
Pierre-Yves Chibon 73d120
                "for this project",
Pierre-Yves Chibon 73d120
                "error_code": "ENOPRCLOSE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Merge PR
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Changes merged!"})
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 457b8e
    def test_api_pull_request_merge_conflicting(self, send_email):
Pierre-Yves Chibon 457b8e
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 457b8e
        send_email.return_value = True
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        tests.create_projects(self.session)
Pierre-Yves Chibon 457b8e
        tests.add_content_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git")
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        # Fork
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 457b8e
        task = pagure.lib.query.fork_project(
Pierre-Yves Chibon 73d120
            session=self.session, user="pingou", repo=project
Pierre-Yves Chibon 457b8e
        )
Pierre-Yves Chibon 457b8e
        self.session.commit()
Pierre-Yves Chibon 457b8e
        self.assertEqual(
Pierre-Yves Chibon 457b8e
            task.get(),
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "endpoint": "ui_ns.view_repo",
Pierre-Yves Chibon 73d120
                "repo": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "username": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        # Add content to the fork
Pierre-Yves Chibon 457b8e
        tests.add_content_to_git(
Pierre-Yves Chibon 457b8e
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
Pierre-Yves Chibon 73d120
            filename="foobar",
Pierre-Yves Chibon 73d120
            content="content from the fork",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        # Add content to the main repo, so they conflict
Pierre-Yves Chibon 457b8e
        tests.add_content_to_git(
Pierre-Yves Chibon 457b8e
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            filename="foobar",
Pierre-Yves Chibon 73d120
            content="content from the main repo",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 73d120
        project = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 457b8e
        fork = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test", user="pingou"
Pierre-Yves Chibon 457b8e
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 457b8e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        # Create the pull-request to close
Pierre-Yves Chibon 457b8e
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 457b8e
            session=self.session,
Pierre-Yves Chibon 457b8e
            repo_from=fork,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 457b8e
            repo_to=project,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 457b8e
        )
Pierre-Yves Chibon 457b8e
        self.session.commit()
Pierre-Yves Chibon 457b8e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 457b8e
        # Merge PR
Pierre-Yves Chibon 457b8e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 457b8e
        self.assertEqual(output.status_code, 409)
Pierre-Yves Chibon 457b8e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 457b8e
        self.assertDictEqual(
Pierre-Yves Chibon 457b8e
            data,
Pierre-Yves Chibon 457b8e
            {
Pierre-Yves Chibon 73d120
                "error": "This pull-request conflicts and thus cannot be merged",
Pierre-Yves Chibon 73d120
                "error_code": "EPRCONFLICTS",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 457b8e
        )
Pierre-Yves Chibon 457b8e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Patrick Uiterwijk 02a7cc
    def test_api_pull_request_merge_user_token(self, send_email):
Pierre-Yves Chibon dea6ca
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon dea6ca
        send_email.return_value = True
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon dea6ca
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon dea6ca
            session=self.session,
Pierre-Yves Chibon dea6ca
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon dea6ca
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Invalid project
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Valid token, invalid PR
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Valid token, invalid PR - other project
Pierre-Yves Chibon dea6ca
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/2/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon dea6ca
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon dea6ca
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Create a token for foo for this project
Pierre-Yves Chibon dea6ca
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="foobar_token",
Pierre-Yves Chibon dea6ca
            user_id=2,
Pierre-Yves Chibon dea6ca
            project_id=1,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.add(item)
Pierre-Yves Chibon dea6ca
        self.session.commit()
Pierre-Yves Chibon dea6ca
Pierre-Yves Chibon dea6ca
        # Allow the token to merge PR
Pierre-Yves Chibon 930073
        acls = pagure.lib.query.get_acls(self.session)
Pierre-Yves Chibon dea6ca
        acl = None
Pierre-Yves Chibon dea6ca
        for acl in acls:
Pierre-Yves Chibon 73d120
            if acl.name == "pull_request_merge":
Pierre-Yves Chibon dea6ca
                break
Pierre-Yves Chibon dea6ca
        item = pagure.lib.model.TokenAcl(
Pierre-Yves Chibon 73d120
            token_id="foobar_token", acl_id=acl.id
Pierre-Yves Chibon dea6ca
        )
Pierre-Yves Chibon dea6ca
        self.session.add(item)
Pierre-Yves Chibon f12b4a
        self.session.commit()
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foobar_token"}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # User not admin
Pierre-Yves Chibon f12b4a
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f12b4a
        self.assertEqual(output.status_code, 403)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f12b4a
        self.assertDictEqual(
Pierre-Yves Chibon f12b4a
            data,
Pierre-Yves Chibon f12b4a
            {
Pierre-Yves Chibon 73d120
                "error": "You are not allowed to merge/close pull-request "
Pierre-Yves Chibon 73d120
                "for this project",
Pierre-Yves Chibon 73d120
                "error_code": "ENOPRCLOSE",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon f12b4a
        )
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon f12b4a
Pierre-Yves Chibon f12b4a
        # Merge PR
Pierre-Yves Chibon a9c964
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/merge", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon a9c964
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Changes merged!"})
Pierre-Yves Chibon a9c964
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 4eb5ad
    def test_api_pull_request_add_comment(self, mockemail):
Pierre-Yves Chibon 9ba77b
        """ Test the api_pull_request_add_comment method of the flask api. """
Pierre-Yves Chibon 4eb5ad
        mockemail.return_value = True
Pierre-Yves Chibon 4eb5ad
Pierre-Yves Chibon 9ba77b
        tests.create_projects(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9ba77b
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Invalid project
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Valid token, wrong project
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # No input
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 9ba77b
            session=self.session,
Pierre-Yves Chibon 9ba77b
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 9ba77b
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
        self.session.commit()
Pierre-Yves Chibon f9c5f9
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Incomplete request
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9ba77b
        self.assertDictEqual(
Pierre-Yves Chibon 9ba77b
            data,
Pierre-Yves Chibon 9ba77b
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9ba77b
        )
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # Valid request
Pierre-Yves Chibon 9ba77b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 9ba77b
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9ba77b
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 9ba77b
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 417805
    def test_api_pull_request_add_comment_wrong_user(self):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_add_comment method of the flask api
Pierre-Yves Chibon 417805
        when the user is not found in the DB. """
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Valid request
Pierre-Yves Chibon 73d120
        with patch(
Pierre-Yves Chibon 73d120
            "pagure.lib.query.add_pull_request_comment",
Pierre-Yves Chibon 73d120
            side_effect=pagure.exceptions.PagureException("error"),
Pierre-Yves Chibon 73d120
        ):
Pierre-Yves Chibon 417805
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test/pull-request/1/comment",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                headers=headers,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 417805
            self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 417805
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
            self.assertDictEqual(
Pierre-Yves Chibon 73d120
                data, {"error": "error", "error_code": "ENOCODE"}
Pierre-Yves Chibon 417805
            )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 417805
    def test_api_pull_request_add_comment_pr_disabled(self):
Pierre-Yves Chibon 417805
        """ Test the api_pull_request_add_comment method of the flask api
Pierre-Yves Chibon 417805
        when PRs are disabled. """
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Valid request
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # no comment added
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 1dfd94
    def test_api_pull_request_add_comment_user_token(self, mockemail):
Pierre-Yves Chibon 1dfd94
        """ Test the api_pull_request_add_comment method of the flask api. """
Pierre-Yves Chibon 1dfd94
        mockemail.return_value = True
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        tests.create_projects(self.session)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 1dfd94
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Invalid project
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid token, invalid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid token, invalid request in another project
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Create a pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 73d120
        forked_repo = pagure.lib.query.get_authorized_project(
Pierre-Yves Chibon 73d120
            self.session, "test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 1dfd94
            session=self.session,
Pierre-Yves Chibon 1dfd94
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 1dfd94
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
        self.session.commit()
Pierre-Yves Chibon 1dfd94
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"title": "test issue"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Incomplete request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 1dfd94
        self.assertDictEqual(
Pierre-Yves Chibon 1dfd94
            data,
Pierre-Yves Chibon 1dfd94
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 1dfd94
        )
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # No change
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting question"}
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # Valid request
Pierre-Yves Chibon 1dfd94
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 1dfd94
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 1dfd94
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 1dfd94
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 417805
    def test_api_subscribe_pull_request_pr_disabled(self, p_send_email):
Pierre-Yves Chibon 417805
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 417805
        p_send_email.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 417805
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 417805
        repo.settings = settings
Pierre-Yves Chibon 417805
        self.session.add(repo)
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Invalid project
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 73d120
    def test_api_subscribe_pull_request_invalid_token(
Pierre-Yves Chibon 73d120
        self, p_send_email, p_ugt
Pierre-Yves Chibon 73d120
    ):
Pierre-Yves Chibon 417805
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 417805
        p_send_email.return_value = True
Pierre-Yves Chibon 417805
        p_ugt.return_value = True
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon 417805
        self.session.add(item)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        tests.create_projects(self.session)
Pierre-Yves Chibon 417805
        tests.create_tokens(self.session, user_id=3, project_id=2)
Pierre-Yves Chibon 417805
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Create pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 417805
            session=self.session,
Pierre-Yves Chibon 417805
            repo_from=repo,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 417805
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
        self.session.commit()
Pierre-Yves Chibon 417805
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        # Check subscribtion before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 417805
        data = {}
Pierre-Yves Chibon 417805
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 417805
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 417805
        self.assertDictEqual(
Pierre-Yves Chibon 417805
            data,
Pierre-Yves Chibon 417805
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Julen Landa Alustiza d477d2
                "http://localhost.localdomain/settings#nav-api-tab to get or "
Pierre-Yves Chibon 73d120
                "renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 417805
        )
Pierre-Yves Chibon 417805
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 4f5301
    def test_api_subscribe_pull_request(self, p_send_email, p_ugt):
Pierre-Yves Chibon 4f5301
        """ Test the api_subscribe_pull_request method of the flask api. """
Pierre-Yves Chibon 4f5301
        p_send_email.return_value = True
Pierre-Yves Chibon 4f5301
        p_ugt.return_value = True
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon 4f5301
        self.session.add(item)
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        self.session.commit()
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        tests.create_projects(self.session)
Pierre-Yves Chibon 4f5301
        tests.create_tokens(self.session, user_id=3)
Pierre-Yves Chibon 4f5301
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Invalid project
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/subscribe", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Valid token, wrong project
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/subscribe", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 417805
        self.assertEqual(output.status_code, 401)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertEqual(
Pierre-Yves Chibon 73d120
            pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # No input
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Create pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 4f5301
            session=self.session,
Pierre-Yves Chibon 4f5301
            repo_from=repo,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 4f5301
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
        self.session.commit()
Pierre-Yves Chibon 4f5301
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Check subscribtion before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Unsubscribe - no changes
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "You are no longer watching this pull-request",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "bar",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "You are no longer watching this pull-request",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "bar",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # No change
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Subscribe
Pierre-Yves Chibon 73d120
        data = {"status": True}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "You are now watching this pull-request",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "bar",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Subscribe - no changes
Pierre-Yves Chibon 73d120
        data = {"status": True}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "You are now watching this pull-request",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "bar",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou", "bar"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 4f5301
        # Unsubscribe
Pierre-Yves Chibon 4f5301
        data = {}
Pierre-Yves Chibon 4f5301
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/subscribe", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 4f5301
        self.assertDictEqual(
Pierre-Yves Chibon 4f5301
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "message": "You are no longer watching this pull-request",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "bar",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 4f5301
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 4f5301
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git")
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 335a73
    def test_api_subscribe_pull_request_logged_in(self, p_send_email, p_ugt):
Pierre-Yves Chibon 335a73
        """ Test the api_subscribe_pull_request method of the flask api
Pierre-Yves Chibon 335a73
        when the user is logged in via the UI. """
Pierre-Yves Chibon 335a73
        p_send_email.return_value = True
Pierre-Yves Chibon 335a73
        p_ugt.return_value = True
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        item = pagure.lib.model.User(
Pierre-Yves Chibon 73d120
            user="bar",
Pierre-Yves Chibon 73d120
            fullname="bar foo",
Pierre-Yves Chibon 73d120
            password="foo",
Pierre-Yves Chibon 73d120
            default_email="bar@bar.com",
Pierre-Yves Chibon 335a73
        )
Pierre-Yves Chibon 335a73
        self.session.add(item)
Pierre-Yves Chibon 73d120
        item = pagure.lib.model.UserEmail(user_id=3, email="bar@bar.com")
Pierre-Yves Chibon 335a73
        self.session.add(item)
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        self.session.commit()
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        tests.create_projects(self.session)
Pierre-Yves Chibon 335a73
        tests.create_tokens(self.session, user_id=3)
Pierre-Yves Chibon 335a73
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Create pull-request
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 335a73
            session=self.session,
Pierre-Yves Chibon 335a73
            repo_from=repo,
Pierre-Yves Chibon 73d120
            branch_from="feature",
Pierre-Yves Chibon 335a73
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon 335a73
        )
Pierre-Yves Chibon 335a73
        self.session.commit()
Pierre-Yves Chibon 335a73
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Check subscribtion before
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 335a73
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Subscribe
Pierre-Yves Chibon 73d120
        user = tests.FakeUser(username="foo")
Pierre-Yves Chibon 335a73
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            data = {"status": True}
Pierre-Yves Chibon 335a73
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/api/0/test/pull-request/1/subscribe", data=data
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 335a73
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 335a73
            data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon e2c468
            data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 335a73
            self.assertDictEqual(
Pierre-Yves Chibon 335a73
                data,
Pierre-Yves Chibon 73d120
                {
Pierre-Yves Chibon 73d120
                    "message": "You are now watching this pull-request",
Pierre-Yves Chibon 73d120
                    "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                    "user": "foo",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 335a73
            )
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 335a73
        # Check subscribtions after
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 73d120
            self.session, project_id=1, requestid=1
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 335a73
        self.assertEqual(
Pierre-Yves Chibon 930073
            pagure.lib.query.get_watch_list(self.session, request),
Pierre-Yves Chibon 73d120
            set(["pingou", "foo"]),
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 335a73
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_project(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not the project doesn't exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foobar/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_title(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not title is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"title": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_branch_to(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not branch to is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"branch_to": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_missing_branch_from(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        not branch from is submitted.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"branch_from": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_pr_disabled(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the parent repo disabled pull-requests.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Pull-Request have been deactivated for this project",
Pierre-Yves Chibon 73d120
                "error_code": "EPULLREQUESTSDISABLED",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_signed_pr(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the parent repo enforces signed-off pull-requests.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Enforce_signed-off_commits_in_pull-request"] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "This repo enforces that all commits are signed "
Pierre-Yves Chibon 73d120
                "off by their author.",
Pierre-Yves Chibon 73d120
                "error_code": "ENOSIGNEDOFF",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_branch_from(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the branch from does not exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Enforce_signed-off_commits_in_pull-request"] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "foobarbaz",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": "Branch foobarbaz does not exist",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 625a97
    def test_api_pull_request_open_invalid_token(self):
Pierre-Yves Chibon 625a97
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 625a97
        queried with an invalid token.
Pierre-Yves Chibon 625a97
        """
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 625a97
        tests.create_tokens(self.session)
Pierre-Yves Chibon 625a97
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 625a97
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "foobarbaz",
Pierre-Yves Chibon 625a97
        }
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 625a97
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 625a97
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 625a97
        self.assertDictEqual(
Pierre-Yves Chibon 625a97
            data,
Pierre-Yves Chibon 625a97
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or expired token. Please visit "
Julen Landa Alustiza d477d2
                "http://localhost.localdomain/settings#nav-api-tab to get or "
Pierre-Yves Chibon 73d120
                "renew your API token.",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDTOK",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 625a97
        )
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 625a97
    def test_api_pull_request_open_invalid_access(self):
Pierre-Yves Chibon 625a97
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 625a97
        the user opening the PR doesn't have commit access.
Pierre-Yves Chibon 625a97
        """
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 625a97
        tests.create_tokens(self.session, user_id=2)
Pierre-Yves Chibon 625a97
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 625a97
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "foobarbaz",
Pierre-Yves Chibon 625a97
        }
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 625a97
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 625a97
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon 625a97
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 625a97
        self.assertDictEqual(
Pierre-Yves Chibon 625a97
            data,
Pierre-Yves Chibon 625a97
            {
Pierre-Yves Chibon 73d120
                "error": "You do not have sufficient permissions to "
Pierre-Yves Chibon 73d120
                "perform this action",
Pierre-Yves Chibon 73d120
                "error_code": "ENOTHIGHENOUGH",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 625a97
        )
Pierre-Yves Chibon 625a97
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 9811e6
    def test_api_pull_request_open_invalid_branch_to(self):
Pierre-Yves Chibon 9811e6
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 9811e6
        the branch to does not exist.
Pierre-Yves Chibon 9811e6
        """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        # Check the behavior if the project disabled the issue tracker
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 9811e6
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Enforce_signed-off_commits_in_pull-request"] = True
Pierre-Yves Chibon 9811e6
        repo.settings = settings
Pierre-Yves Chibon 9811e6
        self.session.add(repo)
Pierre-Yves Chibon 9811e6
        self.session.commit()
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "foobarbaz",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": "Branch foobarbaz could not be found in the "
Pierre-Yves Chibon 73d120
                "target repo",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Lenka Segura 3d9cf6
    def test_api_pull_request_open_project_token_different_project(self):
Lenka Segura 3d9cf6
        """Test the api_pull_request_create method with the project token
Lenka Segura 3d9cf6
        of a different project - fails"""
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
        tests.create_tokens(self.session, project_id=2)
Lenka Segura 3d9cf6
        tests.create_tokens_acl(self.session)
Lenka Segura 3d9cf6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token foo_token"}
Lenka Segura 3d9cf6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test of PR",
Pierre-Yves Chibon 73d120
            "inicial comment": "Some readme adjustment",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Lenka Segura 3d9cf6
        }
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
        self.assertEqual(output.status_code, 401)
Lenka Segura 3d9cf6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Lenka Segura 3d9cf6
    def test_api_pull_request_open_user_token_invalid_acls(self):
Lenka Segura 3d9cf6
        """Test the api_pull_request_create method with the user token, but with
Lenka Segura 3d9cf6
        no acls for opening pull request - fails"""
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 73d120
        for acl in (
Pierre-Yves Chibon 73d120
            "create_project",
Pierre-Yves Chibon 73d120
            "fork_project",
Pierre-Yves Chibon 73d120
            "modify_project",
Pierre-Yves Chibon 73d120
            "update_watch_status",
Pierre-Yves Chibon 73d120
        ):
Lenka Segura 3d9cf6
            tests.create_tokens_acl(self.session, acl_name=acl)
Lenka Segura 3d9cf6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Lenka Segura 3d9cf6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test of PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Some readme adjustment",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 73d120
        }
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
        self.assertEqual(output.status_code, 401)
Lenka Segura 3d9cf6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Lenka Segura 3d9cf6
    def test_api_pull_request_open_from_branch_to_origin(self):
Lenka Segura 3d9cf6
        """Test the api_pull_request_create method from a fork to a master,
Lenka Segura 3d9cf6
       with project token of a origin with all the acls"""
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects(
Pierre-Yves Chibon 73d120
            self.session, is_fork=True, hook_token_suffix="foo"
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
        project_query = self.session.query(pagure.lib.model.Project)
Pierre-Yves Chibon 73d120
        for project in project_query.filter_by(name="test").all():
Lenka Segura 3d9cf6
            if project.parent_id == None:
Lenka Segura 3d9cf6
                parent = project
Lenka Segura 3d9cf6
            else:
Lenka Segura 3d9cf6
                child = project
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
Pierre-Yves Chibon 73d120
            branch="branch",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "forks", "pingou", "test.git"),
Pierre-Yves Chibon 73d120
            branch="branch",
Pierre-Yves Chibon 73d120
        )
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        # Create tokens
Lenka Segura 3d9cf6
        parent_token = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="iamparenttoken",
Lenka Segura 3d9cf6
            user_id=parent.user_id,
Lenka Segura 3d9cf6
            project_id=parent.id,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Lenka Segura 3d9cf6
        )
Lenka Segura 3d9cf6
        self.session.add(parent_token)
Lenka Segura 3d9cf6
Lenka Segura 3d9cf6
        fork_token = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="iamforktoken",
Lenka Segura 3d9cf6
            user_id=child.user_id,
Lenka Segura 3d9cf6
            project_id=child.id,
Pierre-Yves Chibon 73d120
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Lenka Segura 3d9cf6
        )
Lenka Segura 3d9cf6
        self.session.add(fork_token)
Lenka Segura 3d9cf6
        self.session.commit()
Lenka Segura 3d9cf6
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session, token_id="iamparenttoken")
Lenka Segura 3d9cf6
        for acl in pagure.default_config.CROSS_PROJECT_ACLS:
Pierre-Yves Chibon 73d120
            tests.create_tokens_acl(
Pierre-Yves Chibon 73d120
                self.session, token_id="iamforktoken", acl_name=acl
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token iamforktoken"}
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "war of tomatoes",
Pierre-Yves Chibon 73d120
            "initial_comment": "the manifest",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "branch",
Pierre-Yves Chibon 6fa4b3
            "repo_from": "test",
Pierre-Yves Chibon 6fa4b3
            "repo_from_username": "pingou",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 6fa4b3
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 9811e6
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    def test_api_pull_request_open(self):
Pierre-Yves Chibon 73d120
        """ Test the api_pull_request_create method of the flask api. """
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        tests.create_tokens(self.session)
Pierre-Yves Chibon 9811e6
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 9811e6
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 9811e6
        }
Pierre-Yves Chibon 9811e6
Pierre-Yves Chibon 9811e6
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 9811e6
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["uid"] = "e8b68df8711648deac67c3afed15a798"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1516348115"
Pierre-Yves Chibon 9811e6
        self.assertDictEqual(
Pierre-Yves Chibon 9811e6
            data,
Pierre-Yves Chibon 9811e6
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "branch": "master",
Pierre-Yves Chibon 73d120
                "branch_from": "test",
Pierre-Yves Chibon 73d120
                "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                "closed_at": None,
Pierre-Yves Chibon 73d120
                "closed_by": None,
Pierre-Yves Chibon 73d120
                "comments": [],
Pierre-Yves Chibon 73d120
                "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
                "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
                "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
                "last_updated": "1516348115",
Pierre-Yves Chibon 73d120
                "project": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                    "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "remote_git": None,
Pierre-Yves Chibon 73d120
                "repo_from": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                    "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "Open",
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "threshold_reached": None,
Pierre-Yves Chibon 73d120
                "title": "Test PR",
Pierre-Yves Chibon 73d120
                "uid": "e8b68df8711648deac67c3afed15a798",
Pierre-Yves Chibon 73d120
                "updated_on": "1516348115",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    def test_api_pull_request_open_missing_initial_comment(self):
Pierre-Yves Chibon 73d120
        """ Test the api_pull_request_create method of the flask api when
Pierre-Yves Chibon 73d120
        not initial comment is submitted.
Pierre-Yves Chibon 73d120
        """
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.create_tokens(self.session)
Pierre-Yves Chibon 73d120
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 73d120
        }
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["uid"] = "e8b68df8711648deac67c3afed15a798"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1516348115"
Pierre-Yves Chibon 73d120
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data,
Pierre-Yves Chibon 73d120
            {
Pierre-Yves Chibon 73d120
                "assignee": None,
Pierre-Yves Chibon 73d120
                "branch": "master",
Pierre-Yves Chibon 73d120
                "branch_from": "test",
Pierre-Yves Chibon 73d120
                "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
                "closed_at": None,
Pierre-Yves Chibon 73d120
                "closed_by": None,
Pierre-Yves Chibon 73d120
                "comments": [],
Pierre-Yves Chibon 73d120
                "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
                "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
                "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "initial_comment": None,
Pierre-Yves Chibon 73d120
                "last_updated": "1516348115",
Pierre-Yves Chibon 73d120
                "project": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                    "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "remote_git": None,
Pierre-Yves Chibon 73d120
                "repo_from": {
Pierre-Yves Chibon 73d120
                    "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                    "access_users": {
Pierre-Yves Chibon 73d120
                        "admin": [],
Pierre-Yves Chibon 73d120
                        "commit": [],
Pierre-Yves Chibon 73d120
                        "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                        "ticket": [],
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "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 73d120
                    "custom_keys": [],
Pierre-Yves Chibon 73d120
                    "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                    "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                    "description": "test project #1",
Pierre-Yves Chibon 73d120
                    "fullname": "test",
Pierre-Yves Chibon 73d120
                    "id": 1,
Pierre-Yves Chibon 73d120
                    "milestones": {},
Pierre-Yves Chibon 73d120
                    "name": "test",
Pierre-Yves Chibon 73d120
                    "namespace": None,
Pierre-Yves Chibon 73d120
                    "parent": None,
Pierre-Yves Chibon 73d120
                    "priorities": {},
Pierre-Yves Chibon 73d120
                    "tags": [],
Pierre-Yves Chibon 73d120
                    "url_path": "test",
Pierre-Yves Chibon 73d120
                    "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "status": "Open",
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "threshold_reached": None,
Pierre-Yves Chibon 73d120
                "title": "Test PR",
Pierre-Yves Chibon 73d120
                "uid": "e8b68df8711648deac67c3afed15a798",
Pierre-Yves Chibon 73d120
                "updated_on": "1516348115",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
class PagureFlaskApiForkPRDiffStatstests(tests.Modeltests):
Pierre-Yves Chibon 73d120
    """ Tests for the flask API of pagure for the diff stats endpoint of PRs
Pierre-Yves Chibon 73d120
    """
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    maxDiff = None
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
    def setUp(self):
Pierre-Yves Chibon 73d120
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 73d120
        super(PagureFlaskApiForkPRDiffStatstests, self).setUp()
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), ncommits=5
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon c67f41
        # Create the pull-request to close
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon c67f41
            session=self.session,
Pierre-Yves Chibon c67f41
            repo_from=repo,
Pierre-Yves Chibon 73d120
            branch_from="test",
Pierre-Yves Chibon c67f41
            repo_to=repo,
Pierre-Yves Chibon 73d120
            branch_to="master",
Pierre-Yves Chibon 73d120
            title="test pull-request",
Pierre-Yves Chibon 73d120
            user="pingou",
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
        self.session.commit()
Pierre-Yves Chibon c67f41
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_no_repo(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/invalid/pull-request/404/diffstats")
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_no_pr(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/404/diffstats")
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_modified(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/diffstats")
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {
Pierre-Yves Chibon 73d120
                "sources": {
Pierre-Yves Chibon 73d120
                    "lines_added": 10,
Pierre-Yves Chibon 73d120
                    "lines_removed": 0,
Pierre-Yves Chibon f6a6e0
                    "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
Pierre-Yves Chibon f6a6e0
                    "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
Pierre-Yves Chibon 73d120
                    "old_path": "sources",
Pierre-Yves Chibon 73d120
                    "status": "M",
Pierre-Yves Chibon c67f41
                }
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_added_mofidied(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon c67f41
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), ncommits=5
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c67f41
        tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            readme_name="README.md",
Pierre-Yves Chibon 73d120
            branch="test",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/diffstats")
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertTrue(
Pierre-Yves Chibon 73d120
            data
Pierre-Yves Chibon 73d120
            in [
Pierre-Yves Chibon c67f41
                {
Pierre-Yves Chibon 73d120
                    "README.md": {
Pierre-Yves Chibon 73d120
                        "lines_added": 5,
Pierre-Yves Chibon 73d120
                        "lines_removed": 0,
Pierre-Yves Chibon 73d120
                        "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
Pierre-Yves Chibon 73d120
                        "old_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 73d120
                        "old_path": "README.md",
Pierre-Yves Chibon 73d120
                        "status": "A",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "sources": {
Pierre-Yves Chibon 73d120
                        "lines_added": 5,
Pierre-Yves Chibon 73d120
                        "lines_removed": 0,
Pierre-Yves Chibon 73d120
                        "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
Pierre-Yves Chibon 73d120
                        "old_id": "293500070b9dfc6ab66e31383f8f7fccf6a95fe2",
Pierre-Yves Chibon 73d120
                        "old_path": "sources",
Pierre-Yves Chibon 73d120
                        "status": "M",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon c67f41
                },
Pierre-Yves Chibon c67f41
                {
Pierre-Yves Chibon 73d120
                    "README.md": {
Pierre-Yves Chibon 73d120
                        "lines_added": 5,
Pierre-Yves Chibon 73d120
                        "lines_removed": 0,
Pierre-Yves Chibon 73d120
                        "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
Pierre-Yves Chibon 73d120
                        "old_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 73d120
                        "old_path": "README.md",
Pierre-Yves Chibon 73d120
                        "status": "A",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    "sources": {
Pierre-Yves Chibon 73d120
                        "lines_added": 10,
Pierre-Yves Chibon 73d120
                        "lines_removed": 0,
Pierre-Yves Chibon 73d120
                        "new_id": "540916fbd3d825d14cc0c0b2397606fda69379ce",
Pierre-Yves Chibon 73d120
                        "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
Pierre-Yves Chibon 73d120
                        "old_path": "sources",
Pierre-Yves Chibon 73d120
                        "status": "M",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon c67f41
            ]
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon c67f41
    def test_api_pull_request_diffstats_file_modified_deleted(self):
Pierre-Yves Chibon c67f41
        """ Test the api_pull_request_merge method of the flask api. """
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
        pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon c67f41
        tests.add_readme_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            readme_name="README.md",
Pierre-Yves Chibon 73d120
            branch="test",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c67f41
        tests.remove_file_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"),
Pierre-Yves Chibon 73d120
            filename="sources",
Pierre-Yves Chibon 73d120
            branch="test",
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon c67f41
        self.assertEqual(len(repo.requests), 1)
Pierre-Yves Chibon c67f41
        pagure.lib.tasks.update_pull_request(repo.requests[0].uid)
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/diffstats")
Pierre-Yves Chibon c67f41
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon c67f41
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c67f41
        self.assertEqual(
Pierre-Yves Chibon c67f41
            data,
Pierre-Yves Chibon c67f41
            {
Pierre-Yves Chibon 73d120
                "README.md": {
Pierre-Yves Chibon 73d120
                    "lines_added": 5,
Pierre-Yves Chibon 73d120
                    "lines_removed": 0,
Pierre-Yves Chibon 73d120
                    "new_id": "bd913ea153650b94f33f53e5164c36a28b761bf4",
Pierre-Yves Chibon 73d120
                    "old_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 73d120
                    "old_path": "README.md",
Pierre-Yves Chibon 73d120
                    "status": "A",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "sources": {
Pierre-Yves Chibon 73d120
                    "lines_added": 0,
Pierre-Yves Chibon 73d120
                    "lines_removed": 5,
Pierre-Yves Chibon 73d120
                    "new_id": "0000000000000000000000000000000000000000",
Pierre-Yves Chibon 73d120
                    "old_id": "265f133a7c94ede4cb183dd808219c5bf9e08f87",
Pierre-Yves Chibon 73d120
                    "old_path": "sources",
Pierre-Yves Chibon 73d120
                    "status": "D",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon c67f41
        )
Pierre-Yves Chibon c67f41
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 2233c1
class PagureApiThresholdReachedTests(tests.Modeltests):
Pierre-Yves Chibon 2233c1
    """ Test the behavior of the threshold_reached value returned by the API.
Pierre-Yves Chibon 2233c1
    """
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 2233c1
    maxDiff = None
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def _clean_data(self, data):
Pierre-Yves Chibon 73d120
        data["project"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["project"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["repo_from"]["date_modified"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["uid"] = "e8b68df8711648deac67c3afed15a798"
Pierre-Yves Chibon 73d120
        data["commit_start"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["commit_stop"] = "114f1b468a5f05e635fcb6394273f3f907386eab"
Pierre-Yves Chibon 73d120
        data["date_created"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["last_updated"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["updated_on"] = "1516348115"
Pierre-Yves Chibon 73d120
        data["comments"] = []  # Let's not check the comments
Pierre-Yves Chibon 2233c1
        return data
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 2233c1
    def setUp(self):
Pierre-Yves Chibon 2233c1
        """ Set up the environment for the tests. """
Pierre-Yves Chibon 2233c1
        super(PagureApiThresholdReachedTests, self).setUp()
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "requests"), bare=True
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        tests.add_readme_git_repo(os.path.join(self.path, "repos", "test.git"))
Pierre-Yves Chibon 73d120
        tests.add_commit_git_repo(
Pierre-Yves Chibon 73d120
            os.path.join(self.path, "repos", "test.git"), branch="test"
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        tests.create_tokens(self.session)
Pierre-Yves Chibon 2233c1
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        # Add a token for user `foo`
Pierre-Yves Chibon 2233c1
        item = pagure.lib.model.Token(
Pierre-Yves Chibon 73d120
            id="aaabbbcccddd_foo",
Pierre-Yves Chibon 2233c1
            user_id=2,
Pierre-Yves Chibon 2233c1
            project_id=1,
Pierre-Yves Chibon 2233c1
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon 73d120
            + datetime.timedelta(days=30),
Pierre-Yves Chibon 2233c1
        )
Pierre-Yves Chibon 2233c1
        self.session.add(item)
Pierre-Yves Chibon 2233c1
        self.session.commit()
Pierre-Yves Chibon 2233c1
        tests.create_tokens_acl(self.session, token_id="aaabbbcccddd_foo")
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        # Add a minimal required score:
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query._get_project(self.session, "test")
Pierre-Yves Chibon 2233c1
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["Minimum_score_to_merge_pull-request"] = 2
Pierre-Yves Chibon 2233c1
        repo.settings = settings
Pierre-Yves Chibon 2233c1
        self.session.add(repo)
Pierre-Yves Chibon 2233c1
        self.session.commit()
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 2233c1
        data = {
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "branch_to": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 2233c1
        }
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/new", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        self.expected_data = {
Pierre-Yves Chibon 73d120
            "assignee": None,
Pierre-Yves Chibon 73d120
            "branch": "master",
Pierre-Yves Chibon 73d120
            "branch_from": "test",
Pierre-Yves Chibon 73d120
            "cached_merge_status": "unknown",
Pierre-Yves Chibon 73d120
            "closed_at": None,
Pierre-Yves Chibon 73d120
            "closed_by": None,
Pierre-Yves Chibon 73d120
            "comments": [],
Pierre-Yves Chibon 73d120
            "commit_start": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
            "commit_stop": "114f1b468a5f05e635fcb6394273f3f907386eab",
Pierre-Yves Chibon 73d120
            "date_created": "1516348115",
Pierre-Yves Chibon 73d120
            "id": 1,
Pierre-Yves Chibon 73d120
            "initial_comment": "Nothing much, the changes speak for themselves",
Pierre-Yves Chibon 73d120
            "last_updated": "1516348115",
Pierre-Yves Chibon 73d120
            "project": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                "access_users": {
Pierre-Yves Chibon 73d120
                    "admin": [],
Pierre-Yves Chibon 73d120
                    "commit": [],
Pierre-Yves Chibon 73d120
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "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 73d120
                "custom_keys": [],
Pierre-Yves Chibon 73d120
                "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                "description": "test project #1",
Pierre-Yves Chibon 73d120
                "fullname": "test",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "milestones": {},
Pierre-Yves Chibon 73d120
                "name": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "parent": None,
Pierre-Yves Chibon 73d120
                "priorities": {},
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "url_path": "test",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "remote_git": None,
Pierre-Yves Chibon 73d120
            "repo_from": {
Pierre-Yves Chibon 73d120
                "access_groups": {"admin": [], "commit": [], "ticket": []},
Pierre-Yves Chibon 73d120
                "access_users": {
Pierre-Yves Chibon 73d120
                    "admin": [],
Pierre-Yves Chibon 73d120
                    "commit": [],
Pierre-Yves Chibon 73d120
                    "owner": ["pingou"],
Pierre-Yves Chibon 73d120
                    "ticket": [],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "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 73d120
                "custom_keys": [],
Pierre-Yves Chibon 73d120
                "date_created": "1516348115",
Pierre-Yves Chibon 73d120
                "date_modified": "1516348115",
Pierre-Yves Chibon 73d120
                "description": "test project #1",
Pierre-Yves Chibon 73d120
                "fullname": "test",
Pierre-Yves Chibon 73d120
                "id": 1,
Pierre-Yves Chibon 73d120
                "milestones": {},
Pierre-Yves Chibon 73d120
                "name": "test",
Pierre-Yves Chibon 73d120
                "namespace": None,
Pierre-Yves Chibon 73d120
                "parent": None,
Pierre-Yves Chibon 73d120
                "priorities": {},
Pierre-Yves Chibon 73d120
                "tags": [],
Pierre-Yves Chibon 73d120
                "url_path": "test",
Pierre-Yves Chibon 73d120
                "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "status": "Open",
Pierre-Yves Chibon 31a638
            "tags": [],
Pierre-Yves Chibon 73d120
            "threshold_reached": None,
Pierre-Yves Chibon 73d120
            "title": "Test PR",
Pierre-Yves Chibon 73d120
            "uid": "e8b68df8711648deac67c3afed15a798",
Pierre-Yves Chibon 73d120
            "updated_on": "1516348115",
Pierre-Yves Chibon 73d120
            "user": {"fullname": "PY C", "name": "pingou"},
Pierre-Yves Chibon 2233c1
        }
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_no_comments(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has no comments.
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        # Check the PR with 0 comment:
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = False
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_one_comments(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has one comment.
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
        # Check the PR with 1 comment:
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting solution :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = False
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_two_comments_one_person(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has two comments
Pierre-Yves Chibon 2233c1
        but from the same person.
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
        # Add two comments from the same user:
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting solution :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "Indeed it is :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = False
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_two_comments_two_persons(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has two comments
Pierre-Yves Chibon 2233c1
        from two different persons.
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
        # Add two comments from two users:
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting solution :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd_foo"}
Pierre-Yves Chibon 73d120
        data = {"comment": "Indeed it is :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 73d120
        data["comments"] = []  # Let's not check the comments
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = True
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_three_comments_two_persons_changed_to_no(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has three
Pierre-Yves Chibon 2233c1
        comments from two person among which one changed their mind from
Pierre-Yves Chibon 2233c1
        +1 to -1.
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
        # Add three comments from two users:
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting solution :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd_foo"}
Pierre-Yves Chibon 73d120
        data = {"comment": "Indeed it is :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
        data = {
Pierre-Yves Chibon 73d120
            "comment": "Nevermind the bug is elsewhere in fact :thumbsdown:"
Pierre-Yves Chibon 2233c1
        }
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 73d120
        data["comments"] = []  # Let's not check the comments
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = False
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 2233c1
    def test_api_pull_request_three_comments_two_persons_changed_to_yes(self):
Pierre-Yves Chibon 2233c1
        """ Check the value of threshold_reach when the PR has three
Pierre-Yves Chibon 2233c1
        comments from two person among which one changed their mind from
Pierre-Yves Chibon 2233c1
        -1 to +1
Pierre-Yves Chibon 2233c1
        """
Pierre-Yves Chibon 2233c1
        # Add three comments from two users:
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 73d120
        data = {"comment": "This is a very interesting solution :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd_foo"}
Pierre-Yves Chibon 73d120
        data = {"comment": "I think the bug is elsewhere :thumbsdown:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        data = {"comment": "Nevermind it is here :thumbsup:"}
Pierre-Yves Chibon 2233c1
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/comment", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"message": "Comment added"})
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1")
Pierre-Yves Chibon 2233c1
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 2233c1
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 2233c1
        data = self._clean_data(data)
Pierre-Yves Chibon 73d120
        data["comments"] = []  # Let's not check the comments
Pierre-Yves Chibon 2233c1
        self.expected_data["threshold_reached"] = True
Pierre-Yves Chibon 2233c1
        self.assertDictEqual(data, self.expected_data)
Pierre-Yves Chibon 2233c1
Pierre-Yves Chibon c71c2d
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)