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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import datetime
import urllib
import pytest # type: ignore
from google.auth import _helpers
class SourceClass(object):
def func(self): # pragma: NO COVER
"""example docstring"""
def test_copy_docstring_success():
def func(): # pragma: NO COVER
pass
_helpers.copy_docstring(SourceClass)(func)
assert func.__doc__ == SourceClass.func.__doc__
def test_copy_docstring_conflict():
def func(): # pragma: NO COVER
"""existing docstring"""
pass
with pytest.raises(ValueError):
_helpers.copy_docstring(SourceClass)(func)
def test_copy_docstring_non_existing():
def func2(): # pragma: NO COVER
pass
with pytest.raises(AttributeError):
_helpers.copy_docstring(SourceClass)(func2)
def test_parse_content_type_plain():
assert _helpers.parse_content_type("text/html") == "text/html"
assert _helpers.parse_content_type("application/xml") == "application/xml"
assert _helpers.parse_content_type("application/json") == "application/json"
def test_parse_content_type_with_parameters():
content_type_html = "text/html; charset=UTF-8"
content_type_xml = "application/xml; charset=UTF-16; version=1.0"
content_type_json = "application/json; charset=UTF-8; indent=2"
assert _helpers.parse_content_type(content_type_html) == "text/html"
assert _helpers.parse_content_type(content_type_xml) == "application/xml"
assert _helpers.parse_content_type(content_type_json) == "application/json"
def test_parse_content_type_missing_or_broken():
content_type_foo = None
content_type_bar = ""
content_type_baz = "1234"
content_type_qux = " ; charset=UTF-8"
assert _helpers.parse_content_type(content_type_foo) == "text/plain"
assert _helpers.parse_content_type(content_type_bar) == "text/plain"
assert _helpers.parse_content_type(content_type_baz) == "text/plain"
assert _helpers.parse_content_type(content_type_qux) == "text/plain"
def test_utcnow():
assert isinstance(_helpers.utcnow(), datetime.datetime)
def test_datetime_to_secs():
assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0
assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200
def test_to_bytes_with_bytes():
value = b"bytes-val"
assert _helpers.to_bytes(value) == value
def test_to_bytes_with_unicode():
value = u"string-val"
encoded_value = b"string-val"
assert _helpers.to_bytes(value) == encoded_value
def test_to_bytes_with_nonstring_type():
with pytest.raises(ValueError):
_helpers.to_bytes(object())
def test_from_bytes_with_unicode():
value = u"bytes-val"
assert _helpers.from_bytes(value) == value
def test_from_bytes_with_bytes():
value = b"string-val"
decoded_value = u"string-val"
assert _helpers.from_bytes(value) == decoded_value
def test_from_bytes_with_nonstring_type():
with pytest.raises(ValueError):
_helpers.from_bytes(object())
def _assert_query(url, expected):
parts = urllib.parse.urlsplit(url)
query = urllib.parse.parse_qs(parts.query)
assert query == expected
def test_update_query_params_no_params():
uri = "http://www.google.com"
updated = _helpers.update_query(uri, {"a": "b"})
assert updated == uri + "?a=b"
def test_update_query_existing_params():
uri = "http://www.google.com?x=y"
updated = _helpers.update_query(uri, {"a": "b", "c": "d&"})
_assert_query(updated, {"x": ["y"], "a": ["b"], "c": ["d&"]})
def test_update_query_replace_param():
base_uri = "http://www.google.com"
uri = base_uri + "?x=a"
updated = _helpers.update_query(uri, {"x": "b", "y": "c"})
_assert_query(updated, {"x": ["b"], "y": ["c"]})
def test_update_query_remove_param():
base_uri = "http://www.google.com"
uri = base_uri + "?x=a"
updated = _helpers.update_query(uri, {"y": "c"}, remove=["x"])
_assert_query(updated, {"y": ["c"]})
def test_scopes_to_string():
cases = [
("", ()),
("", []),
("", ("",)),
("", [""]),
("a", ("a",)),
("b", ["b"]),
("a b", ["a", "b"]),
("a b", ("a", "b")),
("a b", (s for s in ["a", "b"])),
]
for expected, case in cases:
assert _helpers.scopes_to_string(case) == expected
def test_string_to_scopes():
cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])]
for case, expected in cases:
assert _helpers.string_to_scopes(case) == expected
def test_padded_urlsafe_b64decode():
cases = [
("YQ==", b"a"),
("YQ", b"a"),
("YWE=", b"aa"),
("YWE", b"aa"),
("YWFhYQ==", b"aaaa"),
("YWFhYQ", b"aaaa"),
("YWFhYWE=", b"aaaaa"),
("YWFhYWE", b"aaaaa"),
]
for case, expected in cases:
assert _helpers.padded_urlsafe_b64decode(case) == expected
def test_unpadded_urlsafe_b64encode():
cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")]
for case, expected in cases:
assert _helpers.unpadded_urlsafe_b64encode(case) == expected
|