Blame tests/test_pagure_flask_api_issue_custom_fields.py

Clement Verna 72ac1c
"""
Clement Verna 72ac1c
 (c) 2017 - Copyright Red Hat Inc
Clement Verna 72ac1c
Clement Verna 72ac1c
 Authors:
Clement Verna 72ac1c
   Clement Verna <cverna@tutanota.com></cverna@tutanota.com>
Clement Verna 72ac1c
Clement Verna 72ac1c
"""
Clement Verna 72ac1c
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Clement Verna 72ac1c
import unittest
Clement Verna 72ac1c
import sys
Clement Verna 72ac1c
import os
Clement Verna 72ac1c
import json
Clement Verna 72ac1c
Pierre-Yves Chibon aa57b3
from mock import patch, MagicMock
Clement Verna 72ac1c
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
)
Clement Verna 72ac1c
Pierre-Yves Chibon 930073
import pagure.lib.query  # noqa: E402
Clement Verna 72ac1c
import tests  # noqa: E402
Clement Verna 72ac1c
Clement Verna 72ac1c
Clement Verna 72ac1c
class PagureFlaskApiCustomFieldIssuetests(tests.Modeltests):
Clement Verna 72ac1c
    """ Tests for the flask API of pagure for issue's custom fields """
Pierre-Yves Chibon aa57b3
Clement Verna 72ac1c
    def setUp(self):
Clement Verna 72ac1c
        """ Set up the environnment, ran before every tests. """
Clement Verna 72ac1c
        self.maxDiff = None
Clement Verna 72ac1c
        super(PagureFlaskApiCustomFieldIssuetests, self).setUp()
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        pagure.config.config["TICKETS_FOLDER"] = None
Clement Verna 72ac1c
Clement Verna 72ac1c
        tests.create_projects(self.session)
Pierre-Yves Chibon 73d120
        tests.create_projects_git(os.path.join(self.path, "tickets"))
Clement Verna 72ac1c
        tests.create_tokens(self.session)
Clement Verna 72ac1c
        tests.create_tokens_acl(self.session)
Clement Verna 72ac1c
Clement Verna 72ac1c
        # Create normal issue
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        pagure.lib.query.new_issue(
Clement Verna 72ac1c
            session=self.session,
Clement Verna 72ac1c
            repo=repo,
Pierre-Yves Chibon 73d120
            title="Test issue #1",
Pierre-Yves Chibon 73d120
            content="We should work on this",
Pierre-Yves Chibon 73d120
            user="pingou",
Clement Verna 72ac1c
            private=False,
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
        self.session.commit()
Clement Verna 72ac1c
Clement Verna 72ac1c
    def test_api_update_custom_field_bad_request(self):
Clement Verna 72ac1c
        """ Test the api_update_custom_field method of the flask api.
Clement Verna 72ac1c
        This test that a badly form request returns the correct error.
Clement Verna 72ac1c
        """
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Clement Verna 72ac1c
Clement Verna ee0ae3
        # Request is not formated correctly (EMPTY)
Clement Verna ee0ae3
        payload = {}
Clement Verna 72ac1c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
René Genz dadd94
                "error": "Invalid or incomplete input submitted",
Clement Verna ee0ae3
                "error_code": "EINVALIDREQ",
Pierre-Yves Chibon 73d120
            },
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Clement Verna 72ac1c
    def test_api_update_custom_field_wrong_field(self):
Clement Verna 72ac1c
        """ Test the api_update_custom_field method of the flask api.
Clement Verna 72ac1c
        This test that an invalid field retruns the correct error.
Clement Verna 72ac1c
        """
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Clement Verna 72ac1c
        # Project does not have this custom field
Pierre-Yves Chibon 73d120
        payload = {"foo": "bar"}
Clement Verna 72ac1c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.assertEqual(output.status_code, 400)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Clement Verna 72ac1c
                "error": "Invalid custom field submitted",
Clement Verna 72ac1c
                "error_code": "EINVALIDISSUEFIELD",
Pierre-Yves Chibon 73d120
            },
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Pierre-Yves Chibon aa57b3
    @patch(
Pierre-Yves Chibon 73d120
        "pagure.lib.query.set_custom_key_value",
Pierre-Yves Chibon 73d120
        MagicMock(side_effect=pagure.exceptions.PagureException("error")),
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon aa57b3
    def test_api_update_custom_field_raise_error(self):
Pierre-Yves Chibon aa57b3
        """ Test the api_update_custom_field method of the flask api.
Pierre-Yves Chibon aa57b3
        This test the successful requests scenarii.
Pierre-Yves Chibon aa57b3
        """
Pierre-Yves Chibon aa57b3
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Pierre-Yves Chibon aa57b3
Pierre-Yves Chibon aa57b3
        # Set some custom fields
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.set_custom_key_fields(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            repo,
Pierre-Yves Chibon 73d120
            ["bugzilla", "upstream", "reviewstatus"],
Pierre-Yves Chibon 73d120
            ["link", "boolean", "list"],
Pierre-Yves Chibon 73d120
            [
Pierre-Yves Chibon 73d120
                "unused data for non-list type",
Pierre-Yves Chibon 73d120
                "",
Pierre-Yves Chibon 73d120
                "ack",
Pierre-Yves Chibon 73d120
                "nack",
Pierre-Yves Chibon 73d120
                "needs review",
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            [None, None, None],
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon aa57b3
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "List of custom fields updated")
Pierre-Yves Chibon aa57b3
Pierre-Yves Chibon 73d120
        payload = {"bugzilla": "", "upstream": True}
Pierre-Yves Chibon aa57b3
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon aa57b3
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon aa57b3
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 73d120
        self.assertDictEqual(data, {"error": "error", "error_code": "ENOCODE"})
Pierre-Yves Chibon aa57b3
Clement Verna 72ac1c
    def test_api_update_custom_field(self):
Clement Verna 72ac1c
        """ Test the api_update_custom_field method of the flask api.
Clement Verna 72ac1c
        This test the successful requests scenarii.
Clement Verna 72ac1c
        """
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        headers = {"Authorization": "token aaabbbcccddd"}
Clement Verna 72ac1c
Clement Verna 72ac1c
        # Set some custom fields
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.set_custom_key_fields(
Pierre-Yves Chibon 73d120
            self.session,
Pierre-Yves Chibon 73d120
            repo,
Pierre-Yves Chibon 73d120
            ["bugzilla", "upstream", "reviewstatus"],
Pierre-Yves Chibon 73d120
            ["link", "boolean", "list"],
Pierre-Yves Chibon 73d120
            [
Pierre-Yves Chibon 73d120
                "unused data for non-list type",
Pierre-Yves Chibon 73d120
                "",
Pierre-Yves Chibon 73d120
                "ack",
Pierre-Yves Chibon 73d120
                "nack",
Pierre-Yves Chibon 73d120
                "needs review",
Pierre-Yves Chibon 73d120
            ],
Pierre-Yves Chibon 73d120
            [None, None, None],
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.session.commit()
Pierre-Yves Chibon 73d120
        self.assertEqual(msg, "List of custom fields updated")
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        payload = {"bugzilla": "", "upstream": True}
Clement Verna 72ac1c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data["messages"].sort(key=lambda d: list(d.keys())[0])
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Aurélien Bompard 626417
                "messages": [
Clement Verna 72ac1c
                    {"bugzilla": "No changes"},
Clement Verna 72ac1c
                    {"upstream": "Custom field upstream adjusted to True"},
Aurélien Bompard 626417
                ]
Pierre-Yves Chibon 73d120
            },
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Clement Verna 72ac1c
        self.assertEqual(len(issue.other_fields), 1)
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
        payload = {
Pierre-Yves Chibon 73d120
            "bugzilla": "https://bugzilla.redhat.com/1234",
Pierre-Yves Chibon 73d120
            "upstream": False,
Pierre-Yves Chibon 73d120
            "reviewstatus": "ack",
Pierre-Yves Chibon 73d120
        }
Clement Verna 72ac1c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data["messages"].sort(key=lambda d: list(d.keys())[0])
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Aurélien Bompard 626417
                "messages": [
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "bugzilla": "Custom field bugzilla adjusted to "
Pierre-Yves Chibon 73d120
                        "https://bugzilla.redhat.com/1234"
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "reviewstatus": "Custom field reviewstatus adjusted to ack"
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "upstream": "Custom field upstream adjusted to False (was: True)"
Pierre-Yves Chibon 73d120
                    },
Aurélien Bompard 626417
                ]
Pierre-Yves Chibon 73d120
            },
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.get_authorized_project(self.session, "test")
Pierre-Yves Chibon 930073
        issue = pagure.lib.query.search_issues(self.session, repo, issueid=1)
Clement Verna 72ac1c
        self.assertEqual(len(issue.other_fields), 3)
Clement Verna 72ac1c
Clement Verna 72ac1c
        # Reset the value
Pierre-Yves Chibon 73d120
        payload = {"bugzilla": "", "upstream": "", "reviewstatus": ""}
Clement Verna 72ac1c
        output = self.app.post(
Pierre-Yves Chibon 73d120
            "/api/0/test/issue/1/custom", headers=headers, data=payload
Pierre-Yves Chibon 73d120
        )
Clement Verna 72ac1c
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Aurélien Bompard 626417
        data["messages"].sort(key=lambda d: list(d.keys())[0])
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Aurélien Bompard 626417
                "messages": [
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "bugzilla": "Custom field bugzilla reset "
Pierre-Yves Chibon 73d120
                        "(from https://bugzilla.redhat.com/1234)"
Pierre-Yves Chibon 73d120
                    },
Pierre-Yves Chibon 73d120
                    {
Pierre-Yves Chibon 73d120
                        "reviewstatus": "Custom field reviewstatus reset (from ack)"
Pierre-Yves Chibon 73d120
                    },
Clement Verna ee0ae3
                    {"upstream": "Custom field upstream reset (from False)"},
Aurélien Bompard 626417
                ]
Pierre-Yves Chibon 73d120
            },
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Clement Verna 72ac1c
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Clement Verna 72ac1c
    unittest.main(verbosity=2)