blob: d475fc4bcbd5e7f36cd51e187025787dd1d7cf50 [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']})