blob: 906cf126ef9549c229be3d2f7e6e61a1f52d5d72 [file] [log] [blame]
C.J. Collier37141e42020-02-13 13:49:49 -08001# Copyright 2016 Google LLC
Jon Wayne Parrott8713a712016-10-04 14:19:01 -07002#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Jon Wayne Parrott5824ad82016-10-06 09:27:44 -070015import datetime
Tres Seaver560cf1e2021-08-03 16:35:54 -040016import urllib
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070017
18import pytest
19
20from google.auth import _helpers
21
22
Jon Wayne Parrott6b21d752016-10-14 14:49:35 -070023class SourceClass(object):
24 def func(self): # pragma: NO COVER
25 """example docstring"""
Jon Wayne Parrott6b21d752016-10-14 14:49:35 -070026
27
28def test_copy_docstring_success():
29 def func(): # pragma: NO COVER
30 pass
31
32 _helpers.copy_docstring(SourceClass)(func)
33
34 assert func.__doc__ == SourceClass.func.__doc__
35
36
37def test_copy_docstring_conflict():
38 def func(): # pragma: NO COVER
39 """existing docstring"""
40 pass
41
42 with pytest.raises(ValueError):
43 _helpers.copy_docstring(SourceClass)(func)
44
45
46def test_copy_docstring_non_existing():
47 def func2(): # pragma: NO COVER
48 pass
49
50 with pytest.raises(AttributeError):
51 _helpers.copy_docstring(SourceClass)(func2)
52
53
Jon Wayne Parrott5824ad82016-10-06 09:27:44 -070054def test_utcnow():
55 assert isinstance(_helpers.utcnow(), datetime.datetime)
56
57
58def test_datetime_to_secs():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070059 assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0
60 assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200
Jon Wayne Parrott5824ad82016-10-06 09:27:44 -070061
62
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070063def test_to_bytes_with_bytes():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070064 value = b"bytes-val"
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070065 assert _helpers.to_bytes(value) == value
66
67
Tres Seaver560cf1e2021-08-03 16:35:54 -040068def test_to_bytes_with_text():
69 value = "string-val"
Bu Sun Kim9eec0912019-10-21 17:04:21 -070070 encoded_value = b"string-val"
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070071 assert _helpers.to_bytes(value) == encoded_value
72
73
74def test_to_bytes_with_nonstring_type():
75 with pytest.raises(ValueError):
76 _helpers.to_bytes(object())
77
78
Tres Seaver560cf1e2021-08-03 16:35:54 -040079def test_from_bytes_with_text():
80 value = "bytes-val"
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070081 assert _helpers.from_bytes(value) == value
82
83
84def test_from_bytes_with_bytes():
Bu Sun Kim9eec0912019-10-21 17:04:21 -070085 value = b"string-val"
Tres Seaver560cf1e2021-08-03 16:35:54 -040086 decoded_value = "string-val"
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070087 assert _helpers.from_bytes(value) == decoded_value
88
89
90def test_from_bytes_with_nonstring_type():
91 with pytest.raises(ValueError):
92 _helpers.from_bytes(object())
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -070093
94
95def _assert_query(url, expected):
96 parts = urllib.parse.urlsplit(url)
97 query = urllib.parse.parse_qs(parts.query)
98 assert query == expected
99
100
101def test_update_query_params_no_params():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700102 uri = "http://www.google.com"
103 updated = _helpers.update_query(uri, {"a": "b"})
104 assert updated == uri + "?a=b"
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -0700105
106
107def test_update_query_existing_params():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700108 uri = "http://www.google.com?x=y"
109 updated = _helpers.update_query(uri, {"a": "b", "c": "d&"})
110 _assert_query(updated, {"x": ["y"], "a": ["b"], "c": ["d&"]})
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -0700111
112
113def test_update_query_replace_param():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700114 base_uri = "http://www.google.com"
115 uri = base_uri + "?x=a"
116 updated = _helpers.update_query(uri, {"x": "b", "y": "c"})
117 _assert_query(updated, {"x": ["b"], "y": ["c"]})
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -0700118
119
120def test_update_query_remove_param():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700121 base_uri = "http://www.google.com"
122 uri = base_uri + "?x=a"
123 updated = _helpers.update_query(uri, {"y": "c"}, remove=["x"])
124 _assert_query(updated, {"y": ["c"]})
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700125
126
127def test_scopes_to_string():
128 cases = [
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700129 ("", ()),
130 ("", []),
131 ("", ("",)),
132 ("", [""]),
133 ("a", ("a",)),
134 ("b", ["b"]),
135 ("a b", ["a", "b"]),
136 ("a b", ("a", "b")),
137 ("a b", (s for s in ["a", "b"])),
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700138 ]
139 for expected, case in cases:
140 assert _helpers.scopes_to_string(case) == expected
141
142
143def test_string_to_scopes():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700144 cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])]
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700145
146 for case, expected in cases:
147 assert _helpers.string_to_scopes(case) == expected
Jon Wayne Parrott97eb8702016-11-17 09:43:16 -0800148
149
150def test_padded_urlsafe_b64decode():
151 cases = [
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700152 ("YQ==", b"a"),
153 ("YQ", b"a"),
154 ("YWE=", b"aa"),
155 ("YWE", b"aa"),
156 ("YWFhYQ==", b"aaaa"),
157 ("YWFhYQ", b"aaaa"),
158 ("YWFhYWE=", b"aaaaa"),
159 ("YWFhYWE", b"aaaaa"),
Jon Wayne Parrott97eb8702016-11-17 09:43:16 -0800160 ]
161
162 for case, expected in cases:
163 assert _helpers.padded_urlsafe_b64decode(case) == expected
Aditya Natrajae7e4f32019-02-15 00:02:10 -0500164
165
166def test_unpadded_urlsafe_b64encode():
Bu Sun Kim9eec0912019-10-21 17:04:21 -0700167 cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")]
Aditya Natrajae7e4f32019-02-15 00:02:10 -0500168
169 for case, expected in cases:
170 assert _helpers.unpadded_urlsafe_b64encode(case) == expected