test__helpers.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright 2016 Google LLC
  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. import datetime
  15. import pytest
  16. from six.moves import urllib
  17. from google.auth import _helpers
  18. class SourceClass(object):
  19. def func(self): # pragma: NO COVER
  20. """example docstring"""
  21. def test_copy_docstring_success():
  22. def func(): # pragma: NO COVER
  23. pass
  24. _helpers.copy_docstring(SourceClass)(func)
  25. assert func.__doc__ == SourceClass.func.__doc__
  26. def test_copy_docstring_conflict():
  27. def func(): # pragma: NO COVER
  28. """existing docstring"""
  29. pass
  30. with pytest.raises(ValueError):
  31. _helpers.copy_docstring(SourceClass)(func)
  32. def test_copy_docstring_non_existing():
  33. def func2(): # pragma: NO COVER
  34. pass
  35. with pytest.raises(AttributeError):
  36. _helpers.copy_docstring(SourceClass)(func2)
  37. def test_utcnow():
  38. assert isinstance(_helpers.utcnow(), datetime.datetime)
  39. def test_datetime_to_secs():
  40. assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0
  41. assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200
  42. def test_to_bytes_with_bytes():
  43. value = b"bytes-val"
  44. assert _helpers.to_bytes(value) == value
  45. def test_to_bytes_with_unicode():
  46. value = u"string-val"
  47. encoded_value = b"string-val"
  48. assert _helpers.to_bytes(value) == encoded_value
  49. def test_to_bytes_with_nonstring_type():
  50. with pytest.raises(ValueError):
  51. _helpers.to_bytes(object())
  52. def test_from_bytes_with_unicode():
  53. value = u"bytes-val"
  54. assert _helpers.from_bytes(value) == value
  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. def test_from_bytes_with_nonstring_type():
  60. with pytest.raises(ValueError):
  61. _helpers.from_bytes(object())
  62. def _assert_query(url, expected):
  63. parts = urllib.parse.urlsplit(url)
  64. query = urllib.parse.parse_qs(parts.query)
  65. assert query == expected
  66. def test_update_query_params_no_params():
  67. uri = "http://www.google.com"
  68. updated = _helpers.update_query(uri, {"a": "b"})
  69. assert updated == uri + "?a=b"
  70. def test_update_query_existing_params():
  71. uri = "http://www.google.com?x=y"
  72. updated = _helpers.update_query(uri, {"a": "b", "c": "d&"})
  73. _assert_query(updated, {"x": ["y"], "a": ["b"], "c": ["d&"]})
  74. def test_update_query_replace_param():
  75. base_uri = "http://www.google.com"
  76. uri = base_uri + "?x=a"
  77. updated = _helpers.update_query(uri, {"x": "b", "y": "c"})
  78. _assert_query(updated, {"x": ["b"], "y": ["c"]})
  79. def test_update_query_remove_param():
  80. base_uri = "http://www.google.com"
  81. uri = base_uri + "?x=a"
  82. updated = _helpers.update_query(uri, {"y": "c"}, remove=["x"])
  83. _assert_query(updated, {"y": ["c"]})
  84. def test_scopes_to_string():
  85. cases = [
  86. ("", ()),
  87. ("", []),
  88. ("", ("",)),
  89. ("", [""]),
  90. ("a", ("a",)),
  91. ("b", ["b"]),
  92. ("a b", ["a", "b"]),
  93. ("a b", ("a", "b")),
  94. ("a b", (s for s in ["a", "b"])),
  95. ]
  96. for expected, case in cases:
  97. assert _helpers.scopes_to_string(case) == expected
  98. def test_string_to_scopes():
  99. cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])]
  100. for case, expected in cases:
  101. assert _helpers.string_to_scopes(case) == expected
  102. def test_padded_urlsafe_b64decode():
  103. cases = [
  104. ("YQ==", b"a"),
  105. ("YQ", b"a"),
  106. ("YWE=", b"aa"),
  107. ("YWE", b"aa"),
  108. ("YWFhYQ==", b"aaaa"),
  109. ("YWFhYQ", b"aaaa"),
  110. ("YWFhYWE=", b"aaaaa"),
  111. ("YWFhYWE", b"aaaaa"),
  112. ]
  113. for case, expected in cases:
  114. assert _helpers.padded_urlsafe_b64decode(case) == expected
  115. def test_unpadded_urlsafe_b64encode():
  116. cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")]
  117. for case, expected in cases:
  118. assert _helpers.unpadded_urlsafe_b64encode(case) == expected