test_metrics.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2014 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. import platform
  15. import mock
  16. from google.auth import metrics
  17. from google.auth import version
  18. def test_add_metric_header():
  19. headers = {}
  20. metrics.add_metric_header(headers, None)
  21. assert headers == {}
  22. headers = {"x-goog-api-client": "foo"}
  23. metrics.add_metric_header(headers, "bar")
  24. assert headers == {"x-goog-api-client": "foo bar"}
  25. headers = {}
  26. metrics.add_metric_header(headers, "bar")
  27. assert headers == {"x-goog-api-client": "bar"}
  28. @mock.patch.object(platform, "python_version", return_value="3.7")
  29. def test_versions(mock_python_version):
  30. version_save = version.__version__
  31. version.__version__ = "1.1"
  32. assert metrics.python_and_auth_lib_version() == "gl-python/3.7 auth/1.1"
  33. version.__version__ = version_save
  34. @mock.patch(
  35. "google.auth.metrics.python_and_auth_lib_version",
  36. return_value="gl-python/3.7 auth/1.1",
  37. )
  38. def test_metric_values(mock_python_and_auth_lib_version):
  39. assert (
  40. metrics.token_request_access_token_mds()
  41. == "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/mds"
  42. )
  43. assert (
  44. metrics.token_request_id_token_mds()
  45. == "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/mds"
  46. )
  47. assert (
  48. metrics.token_request_access_token_impersonate()
  49. == "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/imp"
  50. )
  51. assert (
  52. metrics.token_request_id_token_impersonate()
  53. == "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/imp"
  54. )
  55. assert (
  56. metrics.token_request_access_token_sa_assertion()
  57. == "gl-python/3.7 auth/1.1 auth-request-type/at cred-type/sa"
  58. )
  59. assert (
  60. metrics.token_request_id_token_sa_assertion()
  61. == "gl-python/3.7 auth/1.1 auth-request-type/it cred-type/sa"
  62. )
  63. assert metrics.token_request_user() == "gl-python/3.7 auth/1.1 cred-type/u"
  64. assert metrics.mds_ping() == "gl-python/3.7 auth/1.1 auth-request-type/mds"
  65. assert metrics.reauth_start() == "gl-python/3.7 auth/1.1 auth-request-type/re-start"
  66. assert (
  67. metrics.reauth_continue() == "gl-python/3.7 auth/1.1 auth-request-type/re-cont"
  68. )
  69. @mock.patch(
  70. "google.auth.metrics.python_and_auth_lib_version",
  71. return_value="gl-python/3.7 auth/1.1",
  72. )
  73. def test_byoid_metric_header(mock_python_and_auth_lib_version):
  74. metrics_options = {}
  75. assert (
  76. metrics.byoid_metrics_header(metrics_options)
  77. == "gl-python/3.7 auth/1.1 google-byoid-sdk"
  78. )
  79. metrics_options["testKey"] = "testValue"
  80. assert (
  81. metrics.byoid_metrics_header(metrics_options)
  82. == "gl-python/3.7 auth/1.1 google-byoid-sdk testKey/testValue"
  83. )