blob: 79bdef3d7bfd49e48ee0f0f3c4426c8b46e917eb [file] [log] [blame]
Jon Wayne Parrott8713a712016-10-04 14:19:01 -07001# Copyright 2016 Google Inc.
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
Jon Wayne Parrott5824ad82016-10-06 09:27:44 -070015import datetime
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070016
17import pytest
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -070018from six.moves import urllib
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070019
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():
59 assert _helpers.datetime_to_secs(
60 datetime.datetime(1970, 1, 1)) == 0
61 assert _helpers.datetime_to_secs(
62 datetime.datetime(1990, 5, 29)) == 643939200
63
64
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070065def test_to_bytes_with_bytes():
66 value = b'bytes-val'
67 assert _helpers.to_bytes(value) == value
68
69
70def test_to_bytes_with_unicode():
71 value = u'string-val'
72 encoded_value = b'string-val'
73 assert _helpers.to_bytes(value) == encoded_value
74
75
76def test_to_bytes_with_nonstring_type():
77 with pytest.raises(ValueError):
78 _helpers.to_bytes(object())
79
80
81def test_from_bytes_with_unicode():
82 value = u'bytes-val'
83 assert _helpers.from_bytes(value) == value
84
85
86def test_from_bytes_with_bytes():
87 value = b'string-val'
88 decoded_value = u'string-val'
89 assert _helpers.from_bytes(value) == decoded_value
90
91
92def test_from_bytes_with_nonstring_type():
93 with pytest.raises(ValueError):
94 _helpers.from_bytes(object())
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -070095
96
97def _assert_query(url, expected):
98 parts = urllib.parse.urlsplit(url)
99 query = urllib.parse.parse_qs(parts.query)
100 assert query == expected
101
102
103def test_update_query_params_no_params():
104 uri = 'http://www.google.com'
105 updated = _helpers.update_query(uri, {'a': 'b'})
106 assert updated == uri + '?a=b'
107
108
109def test_update_query_existing_params():
110 uri = 'http://www.google.com?x=y'
111 updated = _helpers.update_query(uri, {'a': 'b', 'c': 'd&'})
112 _assert_query(updated, {'x': ['y'], 'a': ['b'], 'c': ['d&']})
113
114
115def test_update_query_replace_param():
116 base_uri = 'http://www.google.com'
117 uri = base_uri + '?x=a'
118 updated = _helpers.update_query(uri, {'x': 'b', 'y': 'c'})
119 _assert_query(updated, {'x': ['b'], 'y': ['c']})
120
121
122def test_update_query_remove_param():
123 base_uri = 'http://www.google.com'
124 uri = base_uri + '?x=a'
125 updated = _helpers.update_query(uri, {'y': 'c'}, remove=['x'])
126 _assert_query(updated, {'y': ['c']})
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700127
128
129def test_scopes_to_string():
130 cases = [
131 ('', ()),
132 ('', []),
133 ('', ('',)),
134 ('', ['', ]),
135 ('a', ('a',)),
136 ('b', ['b', ]),
137 ('a b', ['a', 'b']),
138 ('a b', ('a', 'b')),
139 ('a b', (s for s in ['a', 'b'])),
140 ]
141 for expected, case in cases:
142 assert _helpers.scopes_to_string(case) == expected
143
144
145def test_string_to_scopes():
146 cases = [
147 ('', []),
148 ('a', ['a']),
149 ('a b c d e f', ['a', 'b', 'c', 'd', 'e', 'f']),
150 ]
151
152 for case, expected in cases:
153 assert _helpers.string_to_scopes(case) == expected
Jon Wayne Parrott97eb8702016-11-17 09:43:16 -0800154
155
156def test_padded_urlsafe_b64decode():
157 cases = [
158 ('YQ==', b'a'),
159 ('YQ', b'a'),
160 ('YWE=', b'aa'),
161 ('YWE', b'aa'),
162 ('YWFhYQ==', b'aaaa'),
163 ('YWFhYQ', b'aaaa'),
164 ('YWFhYWE=', b'aaaaa'),
165 ('YWFhYWE', b'aaaaa'),
166 ]
167
168 for case, expected in cases:
169 assert _helpers.padded_urlsafe_b64decode(case) == expected
Aditya Natrajae7e4f32019-02-15 00:02:10 -0500170
171
172def test_unpadded_urlsafe_b64encode():
173 cases = [
174 (b'', b''),
175 (b'a', b'YQ'),
176 (b'aa', b'YWE'),
177 (b'aaa', b'YWFh'),
178 ]
179
180 for case, expected in cases:
181 assert _helpers.unpadded_urlsafe_b64encode(case) == expected