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""" |
| 26 | pass |
| 27 | |
| 28 | |
| 29 | def 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 | |
| 38 | def 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 | |
| 47 | def 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 Parrott | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 55 | def test_utcnow(): |
| 56 | assert isinstance(_helpers.utcnow(), datetime.datetime) |
| 57 | |
| 58 | |
| 59 | def 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 Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 66 | def test_to_bytes_with_bytes(): |
| 67 | value = b'bytes-val' |
| 68 | assert _helpers.to_bytes(value) == value |
| 69 | |
| 70 | |
| 71 | def 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 | |
| 77 | def test_to_bytes_with_nonstring_type(): |
| 78 | with pytest.raises(ValueError): |
| 79 | _helpers.to_bytes(object()) |
| 80 | |
| 81 | |
| 82 | def test_from_bytes_with_unicode(): |
| 83 | value = u'bytes-val' |
| 84 | assert _helpers.from_bytes(value) == value |
| 85 | |
| 86 | |
| 87 | def 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 | |
| 93 | def test_from_bytes_with_nonstring_type(): |
| 94 | with pytest.raises(ValueError): |
| 95 | _helpers.from_bytes(object()) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def _assert_query(url, expected): |
| 99 | parts = urllib.parse.urlsplit(url) |
| 100 | query = urllib.parse.parse_qs(parts.query) |
| 101 | assert query == expected |
| 102 | |
| 103 | |
| 104 | def 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 | |
| 110 | def 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 | |
| 116 | def 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 | |
| 123 | def 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 Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | def 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 | |
| 146 | def 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 |