File: test_resource.py

package info (click to toggle)
python-braintree 4.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,576 kB
  • sloc: python: 28,946; makefile: 9; sh: 8
file content (115 lines) | stat: -rw-r--r-- 3,374 bytes parent folder | download
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from tests.test_helper import *
from braintree.resource import Resource

class TestResource(unittest.TestCase):
    def test_verify_keys_allows_wildcard_keys(self):
        signature = [
            {"foo": [{"bar": ["__any_key__"]}]}
        ]
        params = {
            "foo[bar][lower]": "lowercase",
            "foo[bar][UPPER]": "uppercase",
            "foo[bar][123]": "numeric",
            "foo[bar][under_scores]": "underscores",
            "foo[bar][dash-es]": "dashes",
            "foo[bar][ABC-abc_123]": "all together"
        }
        Resource.verify_keys(params, signature)

    def test_verify_keys_escapes_brackets_in_signature(self):
        signature = [
            {"customer": [{"custom_fields": ["__any_key__"]}]}
        ]
        params = {
            "customer_id": "value",
        }
        with self.assertRaises(KeyError):
            Resource.verify_keys(params, signature)

    def test_verify_keys_works_with_array_param(self):
        signature = [
            {"customer": ["one", "two"]}
        ]
        params = {
            "customer": {
                "one": "foo"
            }
        }
        Resource.verify_keys(params, signature)

    def test_verify_keys_raises_on_bad_array_param(self):
        signature = [
            {"customer": ["one", "two"]}
        ]
        params = {
            "customer": {
                "invalid": "foo"
            }
        }
        with self.assertRaises(KeyError):
            Resource.verify_keys(params, signature)

    def test_verify_keys_works_with_arrays(self):
        signature = [
            {"add_ons": [{"update": ["existing_id", "quantity"]}]}
        ]
        params = {
            "add_ons": {
                "update": [
                    {
                        "existing_id": "foo",
                        "quantity": 10
                    }
                ]
            }
        }
        Resource.verify_keys(params, signature)

    def test_verify_keys_raises_with_invalid_param_in_arrays(self):
        signature = [
            {"add_ons": [{"update": ["existing_id", "quantity"]}]}
        ]
        params = {
            "add_ons": {
                "update": [
                    {
                        "invalid": "foo",
                        "quantity": 10
                    }
                ]
            }
        }
        with self.assertRaises(KeyError):
            Resource.verify_keys(params, signature)

    def test_verify_keys_allows_text(self):
        text_string = u"text_string"
        assert isinstance(text_string, TestHelper.text_type)

        signature = [
            {"customer": [{"custom_fields": [text_string]}]}
        ]
        params = {
            "customer": {
                "custom_fields": {
                    text_string : text_string
                }
            }
        }
        Resource.verify_keys(params, signature)

    def test_verify_keys_allows_raw_data(self):
        raw_string = str.encode("raw_string")
        assert isinstance(raw_string, TestHelper.raw_type)

        signature = [
            {"customer": [{"custom_fields": [raw_string]}]}
        ]
        params = {
            "customer": {
                "custom_fields": {
                    raw_string : raw_string
                }
            }
        }
        Resource.verify_keys(params, signature)