test_credentials_async.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 2024 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 pytest # type: ignore
  15. from google.auth import exceptions
  16. from google.auth.aio import credentials
  17. class CredentialsImpl(credentials.Credentials):
  18. pass
  19. def test_credentials_constructor():
  20. credentials = CredentialsImpl()
  21. assert not credentials.token
  22. @pytest.mark.asyncio
  23. async def test_before_request():
  24. credentials = CredentialsImpl()
  25. request = "water"
  26. headers = {}
  27. credentials.token = "orchid"
  28. # before_request should not affect the value of the token.
  29. await credentials.before_request(request, "http://example.com", "GET", headers)
  30. assert credentials.token == "orchid"
  31. assert headers["authorization"] == "Bearer orchid"
  32. assert "x-allowed-locations" not in headers
  33. request = "earth"
  34. headers = {}
  35. # Second call shouldn't affect token or headers.
  36. await credentials.before_request(request, "http://example.com", "GET", headers)
  37. assert credentials.token == "orchid"
  38. assert headers["authorization"] == "Bearer orchid"
  39. assert "x-allowed-locations" not in headers
  40. @pytest.mark.asyncio
  41. async def test_static_credentials_ctor():
  42. static_creds = credentials.StaticCredentials(token="orchid")
  43. assert static_creds.token == "orchid"
  44. @pytest.mark.asyncio
  45. async def test_static_credentials_apply_default():
  46. static_creds = credentials.StaticCredentials(token="earth")
  47. headers = {}
  48. await static_creds.apply(headers)
  49. assert headers["authorization"] == "Bearer earth"
  50. await static_creds.apply(headers, token="orchid")
  51. assert headers["authorization"] == "Bearer orchid"
  52. @pytest.mark.asyncio
  53. async def test_static_credentials_before_request():
  54. static_creds = credentials.StaticCredentials(token="orchid")
  55. request = "water"
  56. headers = {}
  57. # before_request should not affect the value of the token.
  58. await static_creds.before_request(request, "http://example.com", "GET", headers)
  59. assert static_creds.token == "orchid"
  60. assert headers["authorization"] == "Bearer orchid"
  61. assert "x-allowed-locations" not in headers
  62. request = "earth"
  63. headers = {}
  64. # Second call shouldn't affect token or headers.
  65. await static_creds.before_request(request, "http://example.com", "GET", headers)
  66. assert static_creds.token == "orchid"
  67. assert headers["authorization"] == "Bearer orchid"
  68. assert "x-allowed-locations" not in headers
  69. @pytest.mark.asyncio
  70. async def test_static_credentials_refresh():
  71. static_creds = credentials.StaticCredentials(token="orchid")
  72. request = "earth"
  73. with pytest.raises(exceptions.InvalidOperation) as exc:
  74. await static_creds.refresh(request)
  75. assert exc.match("Static credentials cannot be refreshed.")
  76. @pytest.mark.asyncio
  77. async def test_anonymous_credentials_ctor():
  78. anon = credentials.AnonymousCredentials()
  79. assert anon.token is None
  80. @pytest.mark.asyncio
  81. async def test_anonymous_credentials_refresh():
  82. anon = credentials.AnonymousCredentials()
  83. request = object()
  84. with pytest.raises(exceptions.InvalidOperation) as exc:
  85. await anon.refresh(request)
  86. assert exc.match("Anonymous credentials cannot be refreshed.")
  87. @pytest.mark.asyncio
  88. async def test_anonymous_credentials_apply_default():
  89. anon = credentials.AnonymousCredentials()
  90. headers = {}
  91. await anon.apply(headers)
  92. assert headers == {}
  93. with pytest.raises(ValueError):
  94. await anon.apply(headers, token="orchid")
  95. @pytest.mark.asyncio
  96. async def test_anonymous_credentials_before_request():
  97. anon = credentials.AnonymousCredentials()
  98. request = object()
  99. method = "GET"
  100. url = "https://example.com/api/endpoint"
  101. headers = {}
  102. await anon.before_request(request, method, url, headers)
  103. assert headers == {}