Kashatlast2 commented on code in PR #7458: URL: https://github.com/apache/trafficcontrol/pull/7458#discussion_r1184023618
########## traffic_ops/testing/api_contract/v4/test_regions.py: ########## @@ -0,0 +1,97 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +"""API Contract Test Case for regions endpoint.""" +import logging +import pytest +import requests + +from trafficops.tosession import TOSession + +# Create and configure logger +logger = logging.getLogger() + +primitive = bool | int | float | str | None + [email protected]('request_template_data', ["regions"], indirect=True) +def test_region_contract( + to_session: TOSession, + request_template_data: list[dict[str, object] | list[object] | primitive], + response_template_data: list[dict[str, object] | list[object] | primitive], + region_post_data: dict[str, object] +) -> None: + """ + Test step to validate keys, values and data types from regions endpoint + response. + :param to_session: Fixture to get Traffic Ops session. + :param get_region_data: Fixture to get region data from a prerequisites file. + :param region_prereq: Fixture to get sample region data and actual region response. + """ + # validate region keys from regions get response + logger.info("Accessing /regions endpoint through Traffic ops session.") + + region = request_template_data[0] + if not isinstance(region, dict): + raise TypeError("malformed region in prerequisite data; not an object") + + region_name = region.get("name") + if not isinstance(region_name, str): + raise TypeError("malformed region in prerequisite data; 'name' not a string") + + region_get_response: tuple[ + dict[str, object] | list[dict[str, object] | list[object] | primitive] | primitive, + requests.Response + ] = to_session.get_regions(query_params={"name": region_name}) + try: + region_data = region_get_response[0] + if not isinstance(region_data, list): + raise TypeError("malformed API response; 'response' property not an array") + + first_region = region_data[0] + if not isinstance(first_region, dict): + raise TypeError("malformed API response; first region in response is not an object") + region_keys = set(first_region.keys()) + + logger.info("region Keys from regions endpoint response %s", region_keys) + response_template = response_template_data.get("region").get("properties") + # validate region values from prereq data in regions get response. + prereq_values = [ + region_post_data["name"], + region_post_data["division"], + region_post_data["divisionName"]] Review Comment: Resolved -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
