Blame tests/test_pagure_flask_api_pr_flag.py

Pierre-Yves Chibon 3ffb2e
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
"""
Pierre-Yves Chibon 3ffb2e
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
 Authors:
Pierre-Yves Chibon 3ffb2e
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
"""
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard 626417
Pierre-Yves Chibon 3ffb2e
import unittest
Pierre-Yves Chibon 3ffb2e
import sys
Pierre-Yves Chibon 3ffb2e
import os
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
import json
Pierre-Yves Chibon 3ffb2e
from mock import patch, MagicMock
Pierre-Yves Chibon 3ffb2e
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 3ffb2e
Slavek Kabrda 45252f
import pagure.config  # noqa
Pierre-Yves Chibon 930073
import pagure.lib.query  # noqa
Pierre-Yves Chibon 3ffb2e
import tests  # noqa
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
class PagureFlaskApiPRFlagtests(tests.Modeltests):
Pierre-Yves Chibon 3ffb2e
    """ Tests for the flask API of pagure for flagging pull-requests """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    maxDiff = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 3ffb2e
    def setUp(self):
Pierre-Yves Chibon 3ffb2e
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3ffb2e
        super(PagureFlaskApiPRFlagtests, self).setUp()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        tests.create_projects(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # 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 3ffb2e
            session=self.session,
Pierre-Yves Chibon 3ffb2e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 3ffb2e
            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 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check flags 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 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_invalid_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR on an invalid project. """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_incorrect_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR on the wrong project. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid token, wrong project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        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 3ffb2e
Pierre-Yves Chibon 0bd61f
    def test_pr_disabled(self):
Pierre-Yves Chibon 0bd61f
        """ Test the flagging a PR when PRs are disabled. """
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 0bd61f
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 0bd61f
        repo.settings = settings
Pierre-Yves Chibon 0bd61f
        self.session.add(repo)
Pierre-Yves Chibon 0bd61f
        self.session.commit()
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 0bd61f
        # PRs disabled
Pierre-Yves Chibon 0bd61f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 0bd61f
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0bd61f
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0bd61f
        self.assertDictEqual(
Pierre-Yves Chibon 0bd61f
            data,
Pierre-Yves Chibon 0bd61f
            {
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 0bd61f
        )
Pierre-Yves Chibon 0bd61f
Pierre-Yves Chibon 3ffb2e
    def test_no_pr(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR when the PR doesn't exist. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No PR
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/10/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_input(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging an existing PR but with no data. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No input
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {
Pierre-Yves Chibon 73d120
                    "comment": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "url": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "username": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_comment(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging an existing PR but with incomplete data. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Incomplete request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # 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 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon df049b
    @patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.query.add_pull_request_flag",
Pierre-Yves Chibon 73d120
        MagicMock(side_effect=pagure.exceptions.PagureException("error")),
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon df049b
    def test_raise_exception(self):
Pierre-Yves Chibon df049b
        """ Test the flagging a PR when adding a flag raises an exception. """
Pierre-Yves Chibon df049b
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon df049b
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests running",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon df049b
        }
Pierre-Yves Chibon df049b
Pierre-Yves Chibon df049b
        # Adding a flag raises an exception
Pierre-Yves Chibon df049b
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", headers=headers, data=data
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon df049b
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon df049b
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"error": "error", "error_code": "ENOCODE"})
Pierre-Yves Chibon df049b
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 3ffb2e
    def test_flagging_a_pul_request_with_notification(self, mock_email):
Pierre-Yves Chibon 3ffb2e
        """ Test the flagging a PR. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Enable PR notifications
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 3ffb2e
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["notify_on_pull-request_flag"] = True
Pierre-Yves Chibon 3ffb2e
        repo.settings = settings
Pierre-Yves Chibon 3ffb2e
        self.session.add(repo)
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests running",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        pr_uid = data["flag"]["pull_request_uid"]
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests running",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "pending",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests running")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check the notification sent
Pierre-Yves Chibon 3ffb2e
        mock_email.assert_called_once_with(
Pierre-Yves Chibon 73d120
            "\nJenkins flagged the pull-request `test pull-request` "
Pierre-Yves Chibon 73d120
            "as pending: Tests running\n\n"
Pierre-Yves Chibon 73d120
            "http://localhost.localdomain/test/pull-request/1\n",
Pierre-Yves Chibon 73d120
            "PR #1 - Jenkins: pending",
Pierre-Yves Chibon 73d120
            "bar@pingou.com",
Pierre-Yves Chibon 9ef9df
            assignee=None,
Pierre-Yves Chibon 73d120
            in_reply_to="test-pull-request-" + pr_uid,
Pierre-Yves Chibon 73d120
            mail_id="test-pull-request-" + pr_uid + "-1",
Pierre-Yves Chibon 73d120
            project_name="test",
Pierre-Yves Chibon 73d120
            reporter="pingou",
Pierre-Yves Chibon 73d120
            user_from="Jenkins",
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_updating_flag(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the updating the flag of a PR. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests running",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests running",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "pending",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests running")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Update flag  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "comment": "Tests passed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 100,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "success",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag updated",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests passed")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_adding_two_flags(self):
Pierre-Yves Chibon 3ffb2e
        """ Test the adding two flags to a PR. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests passed",
Pierre-Yves Chibon 73d120
            "status": "success",
Pierre-Yves Chibon 73d120
            "percent": "100",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 100,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "success",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests passed")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests running again",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon 73d120
        self.assertNotEqual(data["uid"], "jenkins_build_pagure_100+seed")
Pierre-Yves Chibon 73d120
        data["uid"] = "jenkins_build_pagure_100+seed"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests running again",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "pending",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Two flag 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 3ffb2e
        self.assertEqual(len(request.flags), 2)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests running again")
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[1].comment, "Tests passed")
Pierre-Yves Chibon f523b9
        self.assertEqual(request.flags[1].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
    @patch.dict(
Pierre-Yves Chibon 73d120
        "pagure.config.config",
Pierre-Yves Chibon 73d120
        {
Pierre-Yves Chibon 73d120
            "FLAG_STATUSES_LABELS": {
Pierre-Yves Chibon 73d120
                "pend!": "label-info",
Pierre-Yves Chibon 73d120
                "succeed!": "label-success",
Pierre-Yves Chibon 73d120
                "fail!": "label-danger",
Pierre-Yves Chibon 73d120
                "what?": "label-warning",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
            "FLAG_PENDING": "pend!",
Pierre-Yves Chibon 73d120
            "FLAG_SUCCESS": "succeed!",
Pierre-Yves Chibon 73d120
            "FLAG_FAILURE": "fail!",
Pierre-Yves Chibon 73d120
        },
Pierre-Yves Chibon 73d120
    )
Slavek Kabrda 45252f
    def test_flagging_a_pull_request_while_having_custom_statuses(self):
Slavek Kabrda 45252f
        """ Test flagging a PR while having custom statuses. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and no percent => should use FLAG_PENDING
Slavek Kabrda 45252f
        send_data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "comment": "Tests running",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Slavek Kabrda 45252f
        }
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertEqual(data["flag"]["status"], "pend!")
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and 50 % => should use FLAG_SUCCESS
Pierre-Yves Chibon 73d120
        send_data["percent"] = 50
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertEqual(data["flag"]["status"], "succeed!")
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # No status and 0 % => should use FLAG_FAILURE
Pierre-Yves Chibon 73d120
        send_data["percent"] = 0
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertEqual(data["flag"]["status"], "fail!")
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # Explicitly set status
Pierre-Yves Chibon 73d120
        send_data["status"] = "what?"
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
        self.assertEqual(data["flag"]["status"], "what?")
Slavek Kabrda 45252f
Slavek Kabrda 45252f
        # Explicitly set wrong status
Pierre-Yves Chibon 73d120
        send_data["status"] = "nooo....."
Slavek Kabrda 45252f
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=send_data, headers=headers
Pierre-Yves Chibon 73d120
        )
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Slavek Kabrda 45252f
        self.assertEqual(output.status_code, 400)
Slavek Kabrda 45252f
        self.assertDictEqual(
Slavek Kabrda 45252f
            data,
Slavek Kabrda 45252f
            {
Slavek Kabrda 45252f
                "error": "Invalid or incomplete input submitted",
Slavek Kabrda 45252f
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
            },
Slavek Kabrda 45252f
        )
Slavek Kabrda 45252f
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
class PagureFlaskApiPRFlagUserTokentests(tests.Modeltests):
Pierre-Yves Chibon 3ffb2e
    """ Tests for the flask API of pagure for flagging pull-requests using
Pierre-Yves Chibon 3ffb2e
    an user token (ie: not restricted to a specific project).
Pierre-Yves Chibon 3ffb2e
    """
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    maxDiff = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 3ffb2e
    def setUp(self):
Pierre-Yves Chibon 3ffb2e
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 3ffb2e
        super(PagureFlaskApiPRFlagUserTokentests, self).setUp()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        tests.create_projects(self.session)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens(self.session, project_id=None)
Pierre-Yves Chibon 3ffb2e
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # 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 3ffb2e
            session=self.session,
Pierre-Yves Chibon 3ffb2e
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 3ffb2e
            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 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
        self.session.commit()
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Check flags 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 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_pr(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging a non-existing PR. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/foo/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_pr_other_project(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging a non-existing PR on a different project. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
        # Valid token, wrong project
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test2/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 404)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_input(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but without submitting any data. """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # No input
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 73d120
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {
Pierre-Yves Chibon 73d120
                    "comment": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "url": ["This field is required."],
Pierre-Yves Chibon 73d120
                    "username": ["This field is required."],
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_no_comment(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but without all the required info.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Incomplete request
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"comment": ["This field is required."]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # 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 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_invalid_status(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR but with an invalid status.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "status": "failed",
Pierre-Yves Chibon 73d120
            "comment": "Failed to run the tests",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Invalid status submitted
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 3ffb2e
                "error": "Invalid or incomplete input submitted",
Pierre-Yves Chibon 3ffb2e
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
                "errors": {"status": ["Not a valid choice"]},
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # 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 3ffb2e
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email")
Pierre-Yves Chibon 3ffb2e
    def test_flag_pr_no_status(self, mock_email):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR without providing a status.
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        Also check that no notifications have been sent.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 0,
Pierre-Yves Chibon 73d120
            "comment": "Tests failed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests failed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 0,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "failure",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests failed")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 0)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # no notifications sent
Pierre-Yves Chibon 3ffb2e
        mock_email.assert_not_called()
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
    def test_editing_flag(self):
Pierre-Yves Chibon 3ffb2e
        """ Test flagging an existing PR without providing a status.
Pierre-Yves Chibon 3ffb2e
        """
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "status": "failure",
Pierre-Yves Chibon 73d120
            "comment": "Tests failed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Valid request  -  w/o providing the status
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests failed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": None,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "failure",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag added",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # One flag 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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests failed")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, None)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Update flag
Pierre-Yves Chibon 3ffb2e
        data = {
Pierre-Yves Chibon 73d120
            "username": "Jenkins",
Pierre-Yves Chibon 73d120
            "percent": 100,
Pierre-Yves Chibon 73d120
            "comment": "Tests passed",
Pierre-Yves Chibon 73d120
            "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
            "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
            "status": "success",
Pierre-Yves Chibon 3ffb2e
        }
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/pull-request/1/flag", data=data, headers=headers
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flag"]["date_created"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["date_updated"] = "1510742565"
Pierre-Yves Chibon 73d120
        data["flag"]["pull_request_uid"] = "62b49f00d489452994de5010565fab81"
Pierre-Yves Chibon e2c468
        data["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 3ffb2e
        self.assertDictEqual(
Pierre-Yves Chibon 3ffb2e
            data,
Pierre-Yves Chibon 3ffb2e
            {
Pierre-Yves Chibon 73d120
                "flag": {
Pierre-Yves Chibon 73d120
                    "comment": "Tests passed",
Pierre-Yves Chibon 73d120
                    "date_created": "1510742565",
Pierre-Yves Chibon 73d120
                    "date_updated": "1510742565",
Pierre-Yves Chibon 73d120
                    "percent": 100,
Pierre-Yves Chibon 73d120
                    "pull_request_uid": "62b49f00d489452994de5010565fab81",
Pierre-Yves Chibon 73d120
                    "status": "success",
Pierre-Yves Chibon 73d120
                    "url": "http://jenkins.cloud.fedoraproject.org/",
Pierre-Yves Chibon 73d120
                    "user": {
Pierre-Yves Chibon 73d120
                        "default_email": "bar@pingou.com",
Pierre-Yves Chibon 73d120
                        "emails": ["bar@pingou.com", "foo@pingou.com"],
Pierre-Yves Chibon 73d120
                        "fullname": "PY C",
Pierre-Yves Chibon 73d120
                        "name": "pingou",
Pierre-Yves Chibon 3ffb2e
                    },
Pierre-Yves Chibon 73d120
                    "username": "Jenkins",
Pierre-Yves Chibon 73d120
                },
Pierre-Yves Chibon 73d120
                "message": "Flag updated",
Pierre-Yves Chibon 73d120
                "uid": "jenkins_build_pagure_100+seed",
Pierre-Yves Chibon 73d120
                "avatar_url": "https://seccdn.libravatar.org/avatar/...",
Pierre-Yves Chibon 73d120
                "user": "pingou",
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 3ffb2e
        )
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
        # Still only one flag
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 3ffb2e
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].comment, "Tests passed")
Pierre-Yves Chibon 3ffb2e
        self.assertEqual(request.flags[0].percent, 100)
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 3ffb2e
Pierre-Yves Chibon 0e98b0
class PagureFlaskApiGetPRFlagtests(tests.Modeltests):
Pierre-Yves Chibon 0e98b0
    """ Tests for the flask API of pagure for retrieving pull-requests flags
Pierre-Yves Chibon 0e98b0
    """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    maxDiff = None
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 73d120
    @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
Pierre-Yves Chibon 0e98b0
    def setUp(self):
Pierre-Yves Chibon 0e98b0
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 0e98b0
        super(PagureFlaskApiGetPRFlagtests, self).setUp()
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 73d120
        pagure.config.config["REQUESTS_FOLDER"] = None
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        tests.create_projects(self.session)
Pierre-Yves Chibon 0e98b0
        tests.create_tokens(self.session)
Pierre-Yves Chibon 0e98b0
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # 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 0e98b0
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 0e98b0
            session=self.session,
Pierre-Yves Chibon 0e98b0
            repo_from=forked_repo,
Pierre-Yves Chibon 73d120
            branch_from="master",
Pierre-Yves Chibon 0e98b0
            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 0e98b0
        )
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(req.title, "test pull-request")
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # Check flags before
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
        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 0e98b0
        self.assertEqual(len(request.flags), 0)
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_invalid_project(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR on an invalid project. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # Invalid project
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/foo/pull-request/1/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0e98b0
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Project not found", "error_code": "ENOPROJECT"}
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_pr_disabled(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR when PRs are disabled. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 0e98b0
        settings = repo.settings
Pierre-Yves Chibon 73d120
        settings["pull_requests"] = False
Pierre-Yves Chibon 0e98b0
        repo.settings = settings
Pierre-Yves Chibon 0e98b0
        self.session.add(repo)
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # PRs disabled
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0e98b0
        self.assertDictEqual(
Pierre-Yves Chibon 0e98b0
            data,
Pierre-Yves Chibon 0e98b0
            {
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 0e98b0
        )
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_no_pr(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR when the PR doesn't exist. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # No PR
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/10/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0e98b0
        self.assertDictEqual(
Pierre-Yves Chibon 73d120
            data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_no_flag(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR when the PR has no flags. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # No flag
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"flags": []})
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_get_flag(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR when the PR has one flag. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # Add a flag to the PR
Pierre-Yves Chibon 0e98b0
        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 0e98b0
        msg = pagure.lib.query.add_pull_request_flag(
Pierre-Yves Chibon 0e98b0
            session=self.session,
Pierre-Yves Chibon 0e98b0
            request=request,
Pierre-Yves Chibon 0e98b0
            username="jenkins",
Pierre-Yves Chibon 0e98b0
            percent=None,
Pierre-Yves Chibon 0e98b0
            comment="Build passes",
Pierre-Yves Chibon 73d120
            status="success",
Pierre-Yves Chibon 0e98b0
            url="http://jenkins.cloud.fedoraproject.org",
Pierre-Yves Chibon 0e98b0
            uid="jenkins_build_pagure_34",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            token="aaabbbcccddd",
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        self.assertEqual(len(request.flags), 1)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].token_id, "aaabbbcccddd")
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # 1 flag
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flags"][0]["date_created"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][0]["date_updated"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][0][
Pierre-Yves Chibon 73d120
            "pull_request_uid"
Pierre-Yves Chibon 73d120
        ] = "72a61033c2fc464aa9ef514c057aa62c"
Pierre-Yves Chibon 0e98b0
        self.assertDictEqual(
Pierre-Yves Chibon 0e98b0
            data,
Pierre-Yves Chibon 0e98b0
            {
Pierre-Yves Chibon 73d120
                "flags": [
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "comment": "Build passes",
Pierre-Yves Chibon 73d120
                        "date_created": "1541413645",
Pierre-Yves Chibon 73d120
                        "date_updated": "1541413645",
Pierre-Yves Chibon 73d120
                        "percent": None,
Pierre-Yves Chibon 73d120
                        "pull_request_uid": "72a61033c2fc464aa9ef514c057aa62c",
Pierre-Yves Chibon 73d120
                        "status": "success",
Pierre-Yves Chibon 73d120
                        "url": "http://jenkins.cloud.fedoraproject.org",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                        "username": "jenkins",
Pierre-Yves Chibon 73d120
                    }
Pierre-Yves Chibon 73d120
                ]
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
    def test_get_flags(self):
Pierre-Yves Chibon 0e98b0
        """ Test the retrieving the flags of a PR when the PR has one flag. """
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # Add two flags to the PR
Pierre-Yves Chibon 0e98b0
        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 0e98b0
        msg = pagure.lib.query.add_pull_request_flag(
Pierre-Yves Chibon 0e98b0
            session=self.session,
Pierre-Yves Chibon 0e98b0
            request=request,
Pierre-Yves Chibon 0e98b0
            username="jenkins",
Pierre-Yves Chibon 0e98b0
            percent=None,
Pierre-Yves Chibon 0e98b0
            comment="Build passes",
Pierre-Yves Chibon 73d120
            status="success",
Pierre-Yves Chibon 0e98b0
            url="http://jenkins.cloud.fedoraproject.org",
Pierre-Yves Chibon 0e98b0
            uid="jenkins_build_pagure_34",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            token="aaabbbcccddd",
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, ("Flag added", "jenkins_build_pagure_34"))
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        msg = pagure.lib.query.add_pull_request_flag(
Pierre-Yves Chibon 0e98b0
            session=self.session,
Pierre-Yves Chibon 0e98b0
            request=request,
Pierre-Yves Chibon 0e98b0
            username="travis",
Pierre-Yves Chibon 0e98b0
            percent=None,
Pierre-Yves Chibon 0e98b0
            comment="Build pending",
Pierre-Yves Chibon 73d120
            status="pending",
Pierre-Yves Chibon 0e98b0
            url="http://travis.io",
Pierre-Yves Chibon 0e98b0
            uid="travis_build_pagure_34",
Pierre-Yves Chibon 73d120
            user="foo",
Pierre-Yves Chibon 73d120
            token="aaabbbcccddd",
Pierre-Yves Chibon 0e98b0
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, ("Flag added", "travis_build_pagure_34"))
Pierre-Yves Chibon 0e98b0
        self.session.commit()
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        self.assertEqual(len(request.flags), 2)
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[1].token_id, "aaabbbcccddd")
Pierre-Yves Chibon 73d120
        self.assertEqual(request.flags[0].token_id, "aaabbbcccddd")
Pierre-Yves Chibon 0e98b0
Pierre-Yves Chibon 0e98b0
        # 1 flag
Pierre-Yves Chibon 73d120
        output = self.app.get("/api/0/test/pull-request/1/flag")
Pierre-Yves Chibon 0e98b0
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0e98b0
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        data["flags"][0]["date_created"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][0]["date_updated"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][0][
Pierre-Yves Chibon 73d120
            "pull_request_uid"
Pierre-Yves Chibon 73d120
        ] = "72a61033c2fc464aa9ef514c057aa62c"
Pierre-Yves Chibon 73d120
        data["flags"][1]["date_created"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][1]["date_updated"] = "1541413645"
Pierre-Yves Chibon 73d120
        data["flags"][1][
Pierre-Yves Chibon 73d120
            "pull_request_uid"
Pierre-Yves Chibon 73d120
        ] = "72a61033c2fc464aa9ef514c057aa62c"
Pierre-Yves Chibon 0e98b0
        self.assertDictEqual(
Pierre-Yves Chibon 0e98b0
            data,
Pierre-Yves Chibon 0e98b0
            {
Pierre-Yves Chibon 73d120
                "flags": [
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "comment": "Build pending",
Pierre-Yves Chibon 73d120
                        "date_created": "1541413645",
Pierre-Yves Chibon 73d120
                        "date_updated": "1541413645",
Pierre-Yves Chibon 73d120
                        "percent": None,
Pierre-Yves Chibon 73d120
                        "pull_request_uid": "72a61033c2fc464aa9ef514c057aa62c",
Pierre-Yves Chibon 73d120
                        "status": "pending",
Pierre-Yves Chibon 73d120
                        "url": "http://travis.io",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                        "username": "travis",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "comment": "Build passes",
Pierre-Yves Chibon 73d120
                        "date_created": "1541413645",
Pierre-Yves Chibon 73d120
                        "date_updated": "1541413645",
Pierre-Yves Chibon 73d120
                        "percent": None,
Pierre-Yves Chibon 73d120
                        "pull_request_uid": "72a61033c2fc464aa9ef514c057aa62c",
Pierre-Yves Chibon 73d120
                        "status": "success",
Pierre-Yves Chibon 73d120
                        "url": "http://jenkins.cloud.fedoraproject.org",
Pierre-Yves Chibon 73d120
                        "user": {"fullname": "foo bar", "name": "foo"},
Pierre-Yves Chibon 73d120
                        "username": "jenkins",
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                ]
Pierre-Yves Chibon 73d120
            },
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 3ffb2e
    unittest.main(verbosity=2)