C.J. Collier | 37141e4 | 2020-02-13 13:49:49 -0800 | [diff] [blame] | 1 | # Copyright 2016 Google LLC |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 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 |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 16 | import urllib |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 17 | |
| 18 | import pytest |
| 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(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 59 | assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0 |
| 60 | assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200 |
Jon Wayne Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 61 | |
| 62 | |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 63 | def test_to_bytes_with_bytes(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 64 | value = b"bytes-val" |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 65 | assert _helpers.to_bytes(value) == value |
| 66 | |
| 67 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 68 | def test_to_bytes_with_text(): |
| 69 | value = "string-val" |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 70 | encoded_value = b"string-val" |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 71 | assert _helpers.to_bytes(value) == encoded_value |
| 72 | |
| 73 | |
| 74 | def test_to_bytes_with_nonstring_type(): |
| 75 | with pytest.raises(ValueError): |
| 76 | _helpers.to_bytes(object()) |
| 77 | |
| 78 | |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 79 | def test_from_bytes_with_text(): |
| 80 | value = "bytes-val" |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 81 | assert _helpers.from_bytes(value) == value |
| 82 | |
| 83 | |
| 84 | def test_from_bytes_with_bytes(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 85 | value = b"string-val" |
Tres Seaver | 560cf1e | 2021-08-03 16:35:54 -0400 | [diff] [blame] | 86 | decoded_value = "string-val" |
Jon Wayne Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 87 | assert _helpers.from_bytes(value) == decoded_value |
| 88 | |
| 89 | |
| 90 | def test_from_bytes_with_nonstring_type(): |
| 91 | with pytest.raises(ValueError): |
| 92 | _helpers.from_bytes(object()) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 93 | |
| 94 | |
| 95 | def _assert_query(url, expected): |
| 96 | parts = urllib.parse.urlsplit(url) |
| 97 | query = urllib.parse.parse_qs(parts.query) |
| 98 | assert query == expected |
| 99 | |
| 100 | |
| 101 | def test_update_query_params_no_params(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 102 | uri = "http://www.google.com" |
| 103 | updated = _helpers.update_query(uri, {"a": "b"}) |
| 104 | assert updated == uri + "?a=b" |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | def test_update_query_existing_params(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 108 | uri = "http://www.google.com?x=y" |
| 109 | updated = _helpers.update_query(uri, {"a": "b", "c": "d&"}) |
| 110 | _assert_query(updated, {"x": ["y"], "a": ["b"], "c": ["d&"]}) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 111 | |
| 112 | |
| 113 | def test_update_query_replace_param(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 114 | base_uri = "http://www.google.com" |
| 115 | uri = base_uri + "?x=a" |
| 116 | updated = _helpers.update_query(uri, {"x": "b", "y": "c"}) |
| 117 | _assert_query(updated, {"x": ["b"], "y": ["c"]}) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 118 | |
| 119 | |
| 120 | def test_update_query_remove_param(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 121 | base_uri = "http://www.google.com" |
| 122 | uri = base_uri + "?x=a" |
| 123 | updated = _helpers.update_query(uri, {"y": "c"}, remove=["x"]) |
| 124 | _assert_query(updated, {"y": ["c"]}) |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def test_scopes_to_string(): |
| 128 | cases = [ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 129 | ("", ()), |
| 130 | ("", []), |
| 131 | ("", ("",)), |
| 132 | ("", [""]), |
| 133 | ("a", ("a",)), |
| 134 | ("b", ["b"]), |
| 135 | ("a b", ["a", "b"]), |
| 136 | ("a b", ("a", "b")), |
| 137 | ("a b", (s for s in ["a", "b"])), |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 138 | ] |
| 139 | for expected, case in cases: |
| 140 | assert _helpers.scopes_to_string(case) == expected |
| 141 | |
| 142 | |
| 143 | def test_string_to_scopes(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 144 | cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])] |
Jon Wayne Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 145 | |
| 146 | for case, expected in cases: |
| 147 | assert _helpers.string_to_scopes(case) == expected |
Jon Wayne Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def test_padded_urlsafe_b64decode(): |
| 151 | cases = [ |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 152 | ("YQ==", b"a"), |
| 153 | ("YQ", b"a"), |
| 154 | ("YWE=", b"aa"), |
| 155 | ("YWE", b"aa"), |
| 156 | ("YWFhYQ==", b"aaaa"), |
| 157 | ("YWFhYQ", b"aaaa"), |
| 158 | ("YWFhYWE=", b"aaaaa"), |
| 159 | ("YWFhYWE", b"aaaaa"), |
Jon Wayne Parrott | 97eb870 | 2016-11-17 09:43:16 -0800 | [diff] [blame] | 160 | ] |
| 161 | |
| 162 | for case, expected in cases: |
| 163 | assert _helpers.padded_urlsafe_b64decode(case) == expected |
Aditya Natraj | ae7e4f3 | 2019-02-15 00:02:10 -0500 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def test_unpadded_urlsafe_b64encode(): |
Bu Sun Kim | 9eec091 | 2019-10-21 17:04:21 -0700 | [diff] [blame] | 167 | cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")] |
Aditya Natraj | ae7e4f3 | 2019-02-15 00:02:10 -0500 | [diff] [blame] | 168 | |
| 169 | for case, expected in cases: |
| 170 | assert _helpers.unpadded_urlsafe_b64encode(case) == expected |