OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 Google Inc. All Rights Reserved. |
| 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 |
| 15 import copy |
| 16 import httmock |
| 17 import json |
| 18 import mock |
| 19 import os |
| 20 import sys |
| 21 import unittest |
| 22 |
| 23 from apitools.base.py import encoding |
| 24 from google.api.config import service_config |
| 25 from google.api.control import messages |
| 26 from oauth2client import client |
| 27 |
| 28 class ServiceConfigFetchTest(unittest.TestCase): |
| 29 |
| 30 _ACCESS_TOKEN = "test_access_token" |
| 31 |
| 32 _SERVICE_NAME = "test_service_name" |
| 33 _SERVICE_VERSION = "test_service_version" |
| 34 _SERVICE_CONFIG_JSON = { |
| 35 "name": _SERVICE_NAME, |
| 36 "id": _SERVICE_VERSION |
| 37 } |
| 38 |
| 39 _credentials = mock.MagicMock() |
| 40 _get_http_client = mock.MagicMock() |
| 41 |
| 42 def setUp(self): |
| 43 os.environ["ENDPOINTS_SERVICE_NAME"] = ServiceConfigFetchTest._SERVICE_NAME |
| 44 os.environ["ENDPOINTS_SERVICE_VERSION"] = ServiceConfigFetchTest._SERVICE_VE
RSION |
| 45 |
| 46 self._set_up_default_credential() |
| 47 |
| 48 def test_no_service_name(self): |
| 49 del os.environ["ENDPOINTS_SERVICE_NAME"] |
| 50 message = 'The "ENDPOINTS_SERVICE_NAME" environment variable is not set' |
| 51 with self.assertRaisesRegexp(ValueError, message): |
| 52 service_config.fetch_service_config() |
| 53 |
| 54 def test_no_service_version(self): |
| 55 del os.environ["ENDPOINTS_SERVICE_VERSION"] |
| 56 message = 'The "ENDPOINTS_SERVICE_VERSION" environment variable is not set' |
| 57 with self.assertRaisesRegexp(ValueError, message): |
| 58 service_config.fetch_service_config() |
| 59 |
| 60 @mock.patch("google.api.config.service_config.client.GoogleCredentials", |
| 61 _credentials) |
| 62 @mock.patch("google.api.config.service_config._get_http_client", _get_http_cli
ent) |
| 63 def test_fetch_service_config(self): |
| 64 mock_response = mock.MagicMock() |
| 65 mock_response.status = 200 |
| 66 mock_response.data = json.dumps(ServiceConfigFetchTest._SERVICE_CONFIG_JSON) |
| 67 mock_http_client = mock.MagicMock() |
| 68 mock_http_client.request.return_value = mock_response |
| 69 ServiceConfigFetchTest._get_http_client.return_value = mock_http_client |
| 70 |
| 71 service = encoding.JsonToMessage(messages.Service, |
| 72 json.dumps(self._SERVICE_CONFIG_JSON)) |
| 73 self.assertEqual(service, service_config.fetch_service_config()) |
| 74 |
| 75 template = service_config._SERVICE_MGMT_URL_TEMPLATE |
| 76 url = template.format(ServiceConfigFetchTest._SERVICE_NAME, |
| 77 ServiceConfigFetchTest._SERVICE_VERSION) |
| 78 headers={"Authorization": "Bearer " + ServiceConfigFetchTest._ACCESS_TOKEN} |
| 79 mock_http_client.request.assert_called_once_with("GET", url, |
| 80 headers=headers) |
| 81 |
| 82 @mock.patch("google.api.config.service_config.client.GoogleCredentials", |
| 83 _credentials) |
| 84 @mock.patch("google.api.config.service_config._get_http_client", _get_http_cli
ent) |
| 85 def test_fetch_service_config_failed(self): |
| 86 mock_response = mock.MagicMock() |
| 87 mock_response.status = 403 |
| 88 mock_http_client = mock.MagicMock() |
| 89 mock_http_client.request.return_value = mock_response |
| 90 ServiceConfigFetchTest._get_http_client.return_value = mock_http_client |
| 91 with self.assertRaisesRegexp(Exception, "status code 403"): |
| 92 service_config.fetch_service_config() |
| 93 |
| 94 @mock.patch("google.api.config.service_config.client.GoogleCredentials", |
| 95 _credentials) |
| 96 @mock.patch("google.api.config.service_config._get_http_client", _get_http_cli
ent) |
| 97 def test_fetch_service_config_with_wrong_service_name(self): |
| 98 mock_response = mock.MagicMock() |
| 99 mock_response.status = 200 |
| 100 config = copy.deepcopy(ServiceConfigFetchTest._SERVICE_CONFIG_JSON) |
| 101 config["name"] = "incorrect_service_name" |
| 102 mock_response.data = json.dumps(config) |
| 103 mock_http_client = mock.MagicMock() |
| 104 mock_http_client.request.return_value = mock_response |
| 105 ServiceConfigFetchTest._get_http_client.return_value = mock_http_client |
| 106 |
| 107 message = ("Unexpected service name in service config: " + |
| 108 config["name"]) |
| 109 with self.assertRaisesRegexp(ValueError, message): |
| 110 service_config.fetch_service_config() |
| 111 |
| 112 @mock.patch("google.api.config.service_config.client.GoogleCredentials", |
| 113 _credentials) |
| 114 @mock.patch("google.api.config.service_config._get_http_client", _get_http_cli
ent) |
| 115 def test_fetch_service_config_with_wrong_service_version(self): |
| 116 mock_response = mock.MagicMock() |
| 117 mock_response.status = 200 |
| 118 config = copy.deepcopy(ServiceConfigFetchTest._SERVICE_CONFIG_JSON) |
| 119 config["id"] = "incorrect_service_version" |
| 120 mock_response.data = json.dumps(config) |
| 121 mock_http_client = mock.MagicMock() |
| 122 mock_http_client.request.return_value = mock_response |
| 123 ServiceConfigFetchTest._get_http_client.return_value = mock_http_client |
| 124 |
| 125 message = ("Unexpected service version in service config: " + |
| 126 config["id"]) |
| 127 with self.assertRaisesRegexp(ValueError, message): |
| 128 service_config.fetch_service_config() |
| 129 |
| 130 def _set_up_default_credential(self): |
| 131 default_credential = mock.MagicMock() |
| 132 ServiceConfigFetchTest._credentials.get_application_default.return_value \ |
| 133 = default_credential |
| 134 default_credential.create_scoped.return_value = default_credential |
| 135 token = ServiceConfigFetchTest._ACCESS_TOKEN |
| 136 access_token = client.AccessTokenInfo(access_token=token, expires_in=None) |
| 137 default_credential.get_access_token.return_value = access_token |
OLD | NEW |