aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_decorators.py
blob: 5a3915b8a5088de76de356738d08ba66faac035b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from schema import Schema
from werkzeug.datastructures import ImmutableMultiDict
from werkzeug.exceptions import BadRequest

from pysite.constants import ValidationTypes
from pysite.decorators import api_params
from tests import SiteTest


class DuckRequest:
    """A quacking request with the `args` parameter used in schema validation."""

    def __init__(self, args):
        self.args = args


class DecoratorTests(SiteTest):
    def test_decorator_api_json(self):
        """ Check the json validation decorator """
        SCHEMA = Schema([{"user_id": int, "role": int}])

        @api_params(schema=SCHEMA, validation_type=ValidationTypes.json)
        def try_json_type(data):
            return data

        with self.assertRaises(AttributeError):
            try_json_type("not json")

    def test_decorator_params(self):
        """ Check the params validation decorator """

        response = self.client.post('/testparams?test=params')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json, [{'test': 'params'}])

    def test_duplicate_params_with_dict_schema_raises_400(self):
        """Check that duplicate parameters with a dictionary schema return 400 Bad Request"""

        response = self.client.get('/testparams?segfault=yes&segfault=no')
        self.assert400(response)

    def test_single_params_with_dict_schema(self):
        """Single parameters with a dictionary schema and `allow_duplicate_keys=False` return 200"""

        response = self.client.get('/testparams?segfault=yes')
        self.assert200(response)
        self.assertEqual(response.json, {'segfault': 'yes'})