blob: 35c16d4f9aff41337838663f473487477dbd6ae4 [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"""
26 pass
27
28
29def test_copy_docstring_success():
30 def func(): # pragma: NO COVER
31 pass
32
33 _helpers.copy_docstring(SourceClass)(func)
34
35 assert func.__doc__ == SourceClass.func.__doc__
36
37
38def test_copy_docstring_conflict():
39 def func(): # pragma: NO COVER
40 """existing docstring"""
41 pass
42
43 with pytest.raises(ValueError):
44 _helpers.copy_docstring(SourceClass)(func)
45
46
47def test_copy_docstring_non_existing():
48 def func2(): # pragma: NO COVER
49 pass
50
51 with pytest.raises(AttributeError):
52 _helpers.copy_docstring(SourceClass)(func2)
53
54
Jon Wayne Parrott5824ad82016-10-06 09:27:44 -070055def test_utcnow():
56 assert isinstance(_helpers.utcnow(), datetime.datetime)
57
58
59def test_datetime_to_secs():
60 assert _helpers.datetime_to_secs(
61 datetime.datetime(1970, 1, 1)) == 0
62 assert _helpers.datetime_to_secs(
63 datetime.datetime(1990, 5, 29)) == 643939200
64
65
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070066def test_to_bytes_with_bytes():
67 value = b'bytes-val'
68 assert _helpers.to_bytes(value) == value
69
70
71def test_to_bytes_with_unicode():
72 value = u'string-val'
73 encoded_value = b'string-val'
74 assert _helpers.to_bytes(value) == encoded_value
75
76
77def test_to_bytes_with_nonstring_type():
78 with pytest.raises(ValueError):
79 _helpers.to_bytes(object())
80
81
82def test_from_bytes_with_unicode():
83 value = u'bytes-val'
84 assert _helpers.from_bytes(value) == value
85
86
87def test_from_bytes_with_bytes():
88 value = b'string-val'
89 decoded_value = u'string-val'
90 assert _helpers.from_bytes(value) == decoded_value
91
92
93def test_from_bytes_with_nonstring_type():
94 with pytest.raises(ValueError):
95 _helpers.from_bytes(object())
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -070096
97
98def _assert_query(url, expected):
99 parts = urllib.parse.urlsplit(url)
100 query = urllib.parse.parse_qs(parts.query)
101 assert query == expected
102
103
104def test_update_query_params_no_params():
105 uri = 'http://www.google.com'
106 updated = _helpers.update_query(uri, {'a': 'b'})
107 assert updated == uri + '?a=b'
108
109
110def test_update_query_existing_params():
111 uri = 'http://www.google.com?x=y'
112 updated = _helpers.update_query(uri, {'a': 'b', 'c': 'd&'})
113 _assert_query(updated, {'x': ['y'], 'a': ['b'], 'c': ['d&']})
114
115
116def test_update_query_replace_param():
117 base_uri = 'http://www.google.com'
118 uri = base_uri + '?x=a'
119 updated = _helpers.update_query(uri, {'x': 'b', 'y': 'c'})
120 _assert_query(updated, {'x': ['b'], 'y': ['c']})
121
122
123def test_update_query_remove_param():
124 base_uri = 'http://www.google.com'
125 uri = base_uri + '?x=a'
126 updated = _helpers.update_query(uri, {'y': 'c'}, remove=['x'])
127 _assert_query(updated, {'y': ['c']})
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -0700128
129
130def test_scopes_to_string():
131 cases = [
132 ('', ()),
133 ('', []),
134 ('', ('',)),
135 ('', ['', ]),
136 ('a', ('a',)),
137 ('b', ['b', ]),
138 ('a b', ['a', 'b']),
139 ('a b', ('a', 'b')),
140 ('a b', (s for s in ['a', 'b'])),
141 ]
142 for expected, case in cases:
143 assert _helpers.scopes_to_string(case) == expected
144
145
146def test_string_to_scopes():
147 cases = [
148 ('', []),
149 ('a', ['a']),
150 ('a b c d e f', ['a', 'b', 'c', 'd', 'e', 'f']),
151 ]
152
153 for case, expected in cases:
154 assert _helpers.string_to_scopes(case) == expected