Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 1 | # 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 Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 15 | import datetime |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 16 | |
| 17 | import pytest |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 18 | from six.moves import urllib |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 19 | |
| 20 | from google.auth import _helpers |
| 21 | |
| 22 | |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 23 | class SourceClass(object): |
| 24 | def func(self): # pragma: NO COVER |
| 25 | """example docstring""" |
Jon Wayne Parrott | 6b21d75 | 2016-10-14 14:49:35 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def 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 | |
| 37 | def 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 | |
| 46 | def 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 Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 54 | def test_utcnow(): |
| 55 | assert isinstance(_helpers.utcnow(), datetime.datetime) |
| 56 | |
| 57 | |
| 58 | def 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 Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 65 | def test_to_bytes_with_bytes(): |
| 66 | value = b'bytes-val' |
| 67 | assert _helpers.to_bytes(value) == value |
| 68 | |
| 69 | |
| 70 | def 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 | |
| 76 | def test_to_bytes_with_nonstring_type(): |
| 77 | with pytest.raises(ValueError): |
| 78 | _helpers.to_bytes(object()) |
| 79 | |
| 80 | |
| 81 | def test_from_bytes_with_unicode(): |
| 82 | value = u'bytes-val' |
| 83 | assert _helpers.from_bytes(value) == value |
| 84 | |
| 85 | |
| 86 | def 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 | |
| 92 | def test_from_bytes_with_nonstring_type(): |
| 93 | with pytest.raises(ValueError): |
| 94 | _helpers.from_bytes(object()) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def _assert_query(url, expected): |
| 98 | parts = urllib.parse.urlsplit(url) |
| 99 | query = urllib.parse.parse_qs(parts.query) |
| 100 | assert query == expected |
| 101 | |
| 102 | |
| 103 | def 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 | |
| 109 | def 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 | |
| 115 | def 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 | |
| 122 | def 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 Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 127 | |
| 128 | |
| 129 | def 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 | |
| 145 | def 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 Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 154 | |
| 155 | |
| 156 | def 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 Natraj | ae7e4f3 | 2019-02-15 00:02:10 -0500 | [diff] [blame^] | 170 | |
| 171 | |
| 172 | def 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 |