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
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
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
Clement Verna 72ac1c
sys.path.insert(0, os.path.join(os.path.dirname(
Clement Verna 72ac1c
    os.path.abspath(__file__)), '..'))
Clement Verna 72ac1c
Clement Verna 72ac1c
import pagure  # noqa: E402
Clement Verna 72ac1c
import pagure.lib  # 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 b130e5
        pagure.config.config['TICKETS_FOLDER'] = None
Clement Verna 72ac1c
Clement Verna 72ac1c
        tests.create_projects(self.session)
Clement Verna 72ac1c
        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 b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Clement Verna 72ac1c
        pagure.lib.new_issue(
Clement Verna 72ac1c
            session=self.session,
Clement Verna 72ac1c
            repo=repo,
Clement Verna 72ac1c
            title='Test issue #1',
Clement Verna 72ac1c
            content='We should work on this',
Clement Verna 72ac1c
            user='pingou',
Clement Verna 72ac1c
            ticketfolder=None,
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
Clement Verna 72ac1c
        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(
Clement Verna 72ac1c
            '/api/0/test/issue/1/custom', headers=headers, data=payload)
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",
Clement Verna 72ac1c
            }
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
Clement Verna 72ac1c
        headers = {'Authorization': 'token aaabbbcccddd'}
Clement Verna 72ac1c
        # Project does not have this custom field
Clement Verna ee0ae3
        payload = {'foo': 'bar'}
Clement Verna 72ac1c
        output = self.app.post(
Clement Verna 72ac1c
            '/api/0/test/issue/1/custom', headers=headers, data=payload)
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",
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Pierre-Yves Chibon aa57b3
    @patch(
Pierre-Yves Chibon aa57b3
        'pagure.lib.set_custom_key_value',
Pierre-Yves Chibon aa57b3
        MagicMock(side_effect=pagure.exceptions.PagureException('error')))
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 aa57b3
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon aa57b3
Pierre-Yves Chibon aa57b3
        # Set some custom fields
Pierre-Yves Chibon aa57b3
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon aa57b3
        msg = pagure.lib.set_custom_key_fields(
Pierre-Yves Chibon aa57b3
            self.session, repo,
Pierre-Yves Chibon aa57b3
            ['bugzilla', 'upstream', 'reviewstatus'],
Pierre-Yves Chibon aa57b3
            ['link', 'boolean', 'list'],
Pierre-Yves Chibon aa57b3
            ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
Pierre-Yves Chibon aa57b3
            [None, None, None])
Pierre-Yves Chibon aa57b3
        self.session.commit()
Pierre-Yves Chibon aa57b3
        self.assertEqual(msg, 'List of custom fields updated')
Pierre-Yves Chibon aa57b3
Pierre-Yves Chibon aa57b3
        payload = {'bugzilla': '', 'upstream': True}
Pierre-Yves Chibon aa57b3
        output = self.app.post(
Pierre-Yves Chibon aa57b3
            '/api/0/test/issue/1/custom', headers=headers, data=payload)
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 aa57b3
        self.assertDictEqual(
Pierre-Yves Chibon aa57b3
            data, {u'error': u'error', u'error_code': u'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
Clement Verna 72ac1c
        headers = {'Authorization': 'token aaabbbcccddd'}
Clement Verna 72ac1c
Clement Verna 72ac1c
        # Set some custom fields
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Clement Verna 72ac1c
        msg = pagure.lib.set_custom_key_fields(
Clement Verna 72ac1c
            self.session, repo,
Clement Verna 72ac1c
            ['bugzilla', 'upstream', 'reviewstatus'],
Clement Verna 72ac1c
            ['link', 'boolean', 'list'],
Clement Verna 72ac1c
            ['unused data for non-list type', '', 'ack', 'nack', 'needs review'],
Clement Verna 72ac1c
            [None, None, None])
Clement Verna 72ac1c
        self.session.commit()
Clement Verna 72ac1c
        self.assertEqual(msg, 'List of custom fields updated')
Clement Verna 72ac1c
Clement Verna ee0ae3
        payload = {'bugzilla': '', 'upstream': True}
Clement Verna 72ac1c
        output = self.app.post(
Clement Verna 72ac1c
            '/api/0/test/issue/1/custom', headers=headers, data=payload)
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
                ]
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Clement Verna 72ac1c
        issue = pagure.lib.search_issues(self.session, repo, issueid=1)
Clement Verna 72ac1c
        self.assertEqual(len(issue.other_fields), 1)
Clement Verna 72ac1c
Clement Verna ee0ae3
        payload = {'bugzilla': 'https://bugzilla.redhat.com/1234',
Clement Verna ee0ae3
                   'upstream': False,
Clement Verna ee0ae3
                   'reviewstatus': 'ack'}
Clement Verna 72ac1c
        output = self.app.post(
Clement Verna 72ac1c
            '/api/0/test/issue/1/custom', headers=headers,
Clement Verna 72ac1c
            data=payload)
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": "Custom field bugzilla adjusted to "
Clement Verna 72ac1c
                                 "https://bugzilla.redhat.com/1234"},
Clement Verna 72ac1c
                    {"reviewstatus": "Custom field reviewstatus adjusted to ack"},
Clement Verna ee0ae3
                    {"upstream": "Custom field upstream adjusted to False (was: True)"},
Clement Verna 72ac1c
Aurélien Bompard 626417
                ]
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon b130e5
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Clement Verna 72ac1c
        issue = pagure.lib.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
Clement Verna ee0ae3
        payload = {'bugzilla': '', 'upstream': '', 'reviewstatus': ''}
Clement Verna 72ac1c
        output = self.app.post(
Clement Verna 72ac1c
            '/api/0/test/issue/1/custom', headers=headers,
Clement Verna 72ac1c
            data=payload)
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": "Custom field bugzilla reset "
Clement Verna 72ac1c
                                 "(from https://bugzilla.redhat.com/1234)"},
Clement Verna 72ac1c
                    {"reviewstatus": "Custom field reviewstatus reset (from ack)"},
Clement Verna ee0ae3
                    {"upstream": "Custom field upstream reset (from False)"},
Aurélien Bompard 626417
                ]
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Clement Verna 72ac1c
Clement Verna 72ac1c
if __name__ == '__main__':
Clement Verna 72ac1c
    unittest.main(verbosity=2)