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 | 5824ad8 | 2016-10-06 09:27:44 -0700 | [diff] [blame] | 23 | def test_utcnow(): |
| 24 | assert isinstance(_helpers.utcnow(), datetime.datetime) |
| 25 | |
| 26 | |
| 27 | def 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 Parrott | 8713a71 | 2016-10-04 14:19:01 -0700 | [diff] [blame] | 34 | def test_to_bytes_with_bytes(): |
| 35 | value = b'bytes-val' |
| 36 | assert _helpers.to_bytes(value) == value |
| 37 | |
| 38 | |
| 39 | def 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 | |
| 45 | def test_to_bytes_with_nonstring_type(): |
| 46 | with pytest.raises(ValueError): |
| 47 | _helpers.to_bytes(object()) |
| 48 | |
| 49 | |
| 50 | def test_from_bytes_with_unicode(): |
| 51 | value = u'bytes-val' |
| 52 | assert _helpers.from_bytes(value) == value |
| 53 | |
| 54 | |
| 55 | def 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 | |
| 61 | def test_from_bytes_with_nonstring_type(): |
| 62 | with pytest.raises(ValueError): |
| 63 | _helpers.from_bytes(object()) |
Jon Wayne Parrott | 64a9d6e | 2016-10-07 14:02:53 -0700 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def _assert_query(url, expected): |
| 67 | parts = urllib.parse.urlsplit(url) |
| 68 | query = urllib.parse.parse_qs(parts.query) |
| 69 | assert query == expected |
| 70 | |
| 71 | |
| 72 | def 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 | |
| 78 | def 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 | |
| 84 | def 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 | |
| 91 | def 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 Parrott | 71ce2a0 | 2016-10-14 14:08:10 -0700 | [diff] [blame^] | 96 | |
| 97 | |
| 98 | def 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 | |
| 114 | def 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 |