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
Clement Verna 72ac1c
import unittest
Clement Verna 72ac1c
import sys
Clement Verna 72ac1c
import os
Clement Verna 72ac1c
import json
Clement Verna 72ac1c
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 """
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)
Clement Verna 72ac1c
        data = json.loads(output.data)
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)
Clement Verna 72ac1c
        data = json.loads(output.data)
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
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)
Clement Verna 72ac1c
        data = json.loads(output.data)
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Clement Verna ee0ae3
                "messages": [
Clement Verna 72ac1c
                    {"bugzilla": "No changes"},
Clement Verna 72ac1c
                    {"upstream": "Custom field upstream adjusted to True"},
Clement Verna 72ac1c
                ]
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Pierre-Yves Chibon b130e5
        self.session = pagure.lib.create_session(self.dbpath)
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)
Clement Verna 72ac1c
        data = json.loads(output.data)
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Clement Verna ee0ae3
                "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
Clement Verna 72ac1c
                ]
Clement Verna 72ac1c
            }
Clement Verna 72ac1c
        )
Clement Verna 72ac1c
Pierre-Yves Chibon b130e5
        self.session = pagure.lib.create_session(self.dbpath)
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)
Clement Verna 72ac1c
        data = json.loads(output.data)
Clement Verna 72ac1c
        self.assertDictEqual(
Clement Verna 72ac1c
            data,
Clement Verna 72ac1c
            {
Clement Verna ee0ae3
                "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)"},
Clement Verna 72ac1c
                ]
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)