test_api_key.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright 2022 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 api_key
  16. def test_credentials_constructor():
  17. with pytest.raises(ValueError) as excinfo:
  18. api_key.Credentials("")
  19. assert excinfo.match(r"Token must be a non-empty API key string")
  20. def test_expired_and_valid():
  21. credentials = api_key.Credentials("api-key")
  22. assert credentials.valid
  23. assert credentials.token == "api-key"
  24. assert not credentials.expired
  25. credentials.refresh(None)
  26. assert credentials.valid
  27. assert credentials.token == "api-key"
  28. assert not credentials.expired
  29. def test_before_request():
  30. credentials = api_key.Credentials("api-key")
  31. headers = {}
  32. credentials.before_request(None, "http://example.com", "GET", headers)
  33. assert headers["x-goog-api-key"] == "api-key"