blob: 86cacefefe24f223b3af6edae17a0cd2b972c843 [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 Parrott5824ad82016-10-06 09:27:44 -070023def test_utcnow():
24 assert isinstance(_helpers.utcnow(), datetime.datetime)
25
26
27def test_datetime_to_secs():
28 assert _helpers.datetime_to_secs(
29 datetime.datetime(1970, 1, 1)) == 0
30 assert _helpers.datetime_to_secs(
31 datetime.datetime(1990, 5, 29)) == 643939200
32
33
Jon Wayne Parrott8713a712016-10-04 14:19:01 -070034def test_to_bytes_with_bytes():
35 value = b'bytes-val'
36 assert _helpers.to_bytes(value) == value
37
38
39def test_to_bytes_with_unicode():
40 value = u'string-val'
41 encoded_value = b'string-val'
42 assert _helpers.to_bytes(value) == encoded_value
43
44
45def test_to_bytes_with_nonstring_type():
46 with pytest.raises(ValueError):
47 _helpers.to_bytes(object())
48
49
50def test_from_bytes_with_unicode():
51 value = u'bytes-val'
52 assert _helpers.from_bytes(value) == value
53
54
55def test_from_bytes_with_bytes():
56 value = b'string-val'
57 decoded_value = u'string-val'
58 assert _helpers.from_bytes(value) == decoded_value
59
60
61def test_from_bytes_with_nonstring_type():
62 with pytest.raises(ValueError):
63 _helpers.from_bytes(object())
Jon Wayne Parrott64a9d6e2016-10-07 14:02:53 -070064
65
66def _assert_query(url, expected):
67 parts = urllib.parse.urlsplit(url)
68 query = urllib.parse.parse_qs(parts.query)
69 assert query == expected
70
71
72def test_update_query_params_no_params():
73 uri = 'http://www.google.com'
74 updated = _helpers.update_query(uri, {'a': 'b'})
75 assert updated == uri + '?a=b'
76
77
78def test_update_query_existing_params():
79 uri = 'http://www.google.com?x=y'
80 updated = _helpers.update_query(uri, {'a': 'b', 'c': 'd&'})
81 _assert_query(updated, {'x': ['y'], 'a': ['b'], 'c': ['d&']})
82
83
84def test_update_query_replace_param():
85 base_uri = 'http://www.google.com'
86 uri = base_uri + '?x=a'
87 updated = _helpers.update_query(uri, {'x': 'b', 'y': 'c'})
88 _assert_query(updated, {'x': ['b'], 'y': ['c']})
89
90
91def test_update_query_remove_param():
92 base_uri = 'http://www.google.com'
93 uri = base_uri + '?x=a'
94 updated = _helpers.update_query(uri, {'y': 'c'}, remove=['x'])
95 _assert_query(updated, {'y': ['c']})
Jon Wayne Parrott71ce2a02016-10-14 14:08:10 -070096
97
98def test_scopes_to_string():
99 cases = [
100 ('', ()),
101 ('', []),
102 ('', ('',)),
103 ('', ['', ]),
104 ('a', ('a',)),
105 ('b', ['b', ]),
106 ('a b', ['a', 'b']),
107 ('a b', ('a', 'b')),
108 ('a b', (s for s in ['a', 'b'])),
109 ]
110 for expected, case in cases:
111 assert _helpers.scopes_to_string(case) == expected
112
113
114def test_string_to_scopes():
115 cases = [
116 ('', []),
117 ('a', ['a']),
118 ('a b c d e f', ['a', 'b', 'c', 'd', 'e', 'f']),
119 ]
120
121 for case, expected in cases:
122 assert _helpers.string_to_scopes(case) == expected