• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2016 Google LLC
2#
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
15import datetime
16
17import pytest
18from six.moves import urllib
19
20from google.auth import _helpers
21
22
23class SourceClass(object):
24    def func(self):  # pragma: NO COVER
25        """example docstring"""
26
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
54def test_utcnow():
55    assert isinstance(_helpers.utcnow(), datetime.datetime)
56
57
58def test_datetime_to_secs():
59    assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0
60    assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200
61
62
63def test_to_bytes_with_bytes():
64    value = b"bytes-val"
65    assert _helpers.to_bytes(value) == value
66
67
68def test_to_bytes_with_unicode():
69    value = u"string-val"
70    encoded_value = b"string-val"
71    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
79def test_from_bytes_with_unicode():
80    value = u"bytes-val"
81    assert _helpers.from_bytes(value) == value
82
83
84def test_from_bytes_with_bytes():
85    value = b"string-val"
86    decoded_value = u"string-val"
87    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())
93
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():
102    uri = "http://www.google.com"
103    updated = _helpers.update_query(uri, {"a": "b"})
104    assert updated == uri + "?a=b"
105
106
107def test_update_query_existing_params():
108    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&"]})
111
112
113def test_update_query_replace_param():
114    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"]})
118
119
120def test_update_query_remove_param():
121    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"]})
125
126
127def test_scopes_to_string():
128    cases = [
129        ("", ()),
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"])),
138    ]
139    for expected, case in cases:
140        assert _helpers.scopes_to_string(case) == expected
141
142
143def test_string_to_scopes():
144    cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])]
145
146    for case, expected in cases:
147        assert _helpers.string_to_scopes(case) == expected
148
149
150def test_padded_urlsafe_b64decode():
151    cases = [
152        ("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"),
160    ]
161
162    for case, expected in cases:
163        assert _helpers.padded_urlsafe_b64decode(case) == expected
164
165
166def test_unpadded_urlsafe_b64encode():
167    cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")]
168
169    for case, expected in cases:
170        assert _helpers.unpadded_urlsafe_b64encode(case) == expected
171