-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathtest_utils.py
121 lines (79 loc) · 2.89 KB
/
test_utils.py
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
116
117
118
119
120
121
import pytest
from django.template.backends.django import Template
from django_unicorn.utils import (
create_template,
generate_checksum,
get_method_arguments,
is_non_string_sequence,
sanitize_html,
)
def test_generate_checksum_bytes(settings):
settings.SECRET_KEY = "asdf"
expected = "TfxFqcQL"
actual = generate_checksum(b'{"name": "test"}')
assert expected == actual
def test_generate_checksum_str(settings):
settings.SECRET_KEY = "asdf"
expected = "TfxFqcQL"
actual = generate_checksum('{"name": "test"}')
assert expected == actual
def test_generate_checksum_dict(settings):
settings.SECRET_KEY = "asdf"
# This is different than the above because `str(dict)` turns `{"name": "test"}` into `"{'name': 'test'}"`
expected = "JaV4PeA6"
actual = generate_checksum({"name": "test"})
assert expected == actual
def test_generate_checksum_invalid(settings):
settings.SECRET_KEY = "asdf"
with pytest.raises(TypeError) as e:
generate_checksum([])
assert e.exconly() == "TypeError: Invalid type: <class 'list'>"
def test_get_method_arguments():
def test_func(input_str):
return input_str
expected = [
"input_str",
]
actual = get_method_arguments(test_func)
assert actual == expected
def test_get_method_arguments_with_type_annotation():
def test_func(input_str: str):
return input_str
expected = [
"input_str",
]
actual = get_method_arguments(test_func)
assert actual == expected
def test_sanitize_html():
expected = '{"id":"abcd123","name":"text-inputs","key":"asdf","data":{"name":"World","testing_thing":"Whatever \\u003C/script\\u003E \\u003Cscript\\u003Ealert(\'uh oh\')\\u003C/script\\u003E"},"calls":[],"hash":"hjkl"}' # noqa: E501
data = '{"id":"abcd123","name":"text-inputs","key":"asdf","data":{"name":"World","testing_thing":"Whatever </script> <script>alert(\'uh oh\')</script>"},"calls":[],"hash":"hjkl"}' # noqa: E501
actual = sanitize_html(data)
assert actual == expected
def test_is_non_string_sequence_list():
assert is_non_string_sequence(
[
"",
]
)
def test_is_non_string_sequence_tuple():
assert is_non_string_sequence(("",))
def test_is_non_string_sequence_set():
assert is_non_string_sequence(
{
"",
}
)
def test_is_non_string_sequence_string():
assert not is_non_string_sequence("")
def test_is_non_string_sequence_bytes():
assert not is_non_string_sequence(b"")
def test_create_template_str():
actual = create_template("<div>test string template</div>")
assert actual
assert isinstance(actual, Template)
def test_create_template_callable():
def _get_template():
return "<div>test callable template</div>"
actual = create_template(_get_template)
assert actual
assert isinstance(actual, Template)