test__helpers.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 urllib
  16. import pytest # type: ignore
  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_parse_content_type_plain():
  38. assert _helpers.parse_content_type("text/html") == "text/html"
  39. assert _helpers.parse_content_type("application/xml") == "application/xml"
  40. assert _helpers.parse_content_type("application/json") == "application/json"
  41. def test_parse_content_type_with_parameters():
  42. content_type_html = "text/html; charset=UTF-8"
  43. content_type_xml = "application/xml; charset=UTF-16; version=1.0"
  44. content_type_json = "application/json; charset=UTF-8; indent=2"
  45. assert _helpers.parse_content_type(content_type_html) == "text/html"
  46. assert _helpers.parse_content_type(content_type_xml) == "application/xml"
  47. assert _helpers.parse_content_type(content_type_json) == "application/json"
  48. def test_parse_content_type_missing_or_broken():
  49. content_type_foo = None
  50. content_type_bar = ""
  51. content_type_baz = "1234"
  52. content_type_qux = " ; charset=UTF-8"
  53. assert _helpers.parse_content_type(content_type_foo) == "text/plain"
  54. assert _helpers.parse_content_type(content_type_bar) == "text/plain"
  55. assert _helpers.parse_content_type(content_type_baz) == "text/plain"
  56. assert _helpers.parse_content_type(content_type_qux) == "text/plain"
  57. def test_utcnow():
  58. assert isinstance(_helpers.utcnow(), datetime.datetime)
  59. def test_datetime_to_secs():
  60. assert _helpers.datetime_to_secs(datetime.datetime(1970, 1, 1)) == 0
  61. assert _helpers.datetime_to_secs(datetime.datetime(1990, 5, 29)) == 643939200
  62. def test_to_bytes_with_bytes():
  63. value = b"bytes-val"
  64. assert _helpers.to_bytes(value) == value
  65. def test_to_bytes_with_unicode():
  66. value = u"string-val"
  67. encoded_value = b"string-val"
  68. assert _helpers.to_bytes(value) == encoded_value
  69. def test_to_bytes_with_nonstring_type():
  70. with pytest.raises(ValueError):
  71. _helpers.to_bytes(object())
  72. def test_from_bytes_with_unicode():
  73. value = u"bytes-val"
  74. assert _helpers.from_bytes(value) == value
  75. def test_from_bytes_with_bytes():
  76. value = b"string-val"
  77. decoded_value = u"string-val"
  78. assert _helpers.from_bytes(value) == decoded_value
  79. def test_from_bytes_with_nonstring_type():
  80. with pytest.raises(ValueError):
  81. _helpers.from_bytes(object())
  82. def _assert_query(url, expected):
  83. parts = urllib.parse.urlsplit(url)
  84. query = urllib.parse.parse_qs(parts.query)
  85. assert query == expected
  86. def test_update_query_params_no_params():
  87. uri = "http://www.google.com"
  88. updated = _helpers.update_query(uri, {"a": "b"})
  89. assert updated == uri + "?a=b"
  90. def test_update_query_existing_params():
  91. uri = "http://www.google.com?x=y"
  92. updated = _helpers.update_query(uri, {"a": "b", "c": "d&"})
  93. _assert_query(updated, {"x": ["y"], "a": ["b"], "c": ["d&"]})
  94. def test_update_query_replace_param():
  95. base_uri = "http://www.google.com"
  96. uri = base_uri + "?x=a"
  97. updated = _helpers.update_query(uri, {"x": "b", "y": "c"})
  98. _assert_query(updated, {"x": ["b"], "y": ["c"]})
  99. def test_update_query_remove_param():
  100. base_uri = "http://www.google.com"
  101. uri = base_uri + "?x=a"
  102. updated = _helpers.update_query(uri, {"y": "c"}, remove=["x"])
  103. _assert_query(updated, {"y": ["c"]})
  104. def test_scopes_to_string():
  105. cases = [
  106. ("", ()),
  107. ("", []),
  108. ("", ("",)),
  109. ("", [""]),
  110. ("a", ("a",)),
  111. ("b", ["b"]),
  112. ("a b", ["a", "b"]),
  113. ("a b", ("a", "b")),
  114. ("a b", (s for s in ["a", "b"])),
  115. ]
  116. for expected, case in cases:
  117. assert _helpers.scopes_to_string(case) == expected
  118. def test_string_to_scopes():
  119. cases = [("", []), ("a", ["a"]), ("a b c d e f", ["a", "b", "c", "d", "e", "f"])]
  120. for case, expected in cases:
  121. assert _helpers.string_to_scopes(case) == expected
  122. def test_padded_urlsafe_b64decode():
  123. cases = [
  124. ("YQ==", b"a"),
  125. ("YQ", b"a"),
  126. ("YWE=", b"aa"),
  127. ("YWE", b"aa"),
  128. ("YWFhYQ==", b"aaaa"),
  129. ("YWFhYQ", b"aaaa"),
  130. ("YWFhYWE=", b"aaaaa"),
  131. ("YWFhYWE", b"aaaaa"),
  132. ]
  133. for case, expected in cases:
  134. assert _helpers.padded_urlsafe_b64decode(case) == expected
  135. def test_unpadded_urlsafe_b64encode():
  136. cases = [(b"", b""), (b"a", b"YQ"), (b"aa", b"YWE"), (b"aaa", b"YWFh")]
  137. for case, expected in cases:
  138. assert _helpers.unpadded_urlsafe_b64encode(case) == expected