"""Integration tests for new-release-data.theorchard.io.""" import json from datetime import datetime, timedelta, timezone import pytest import requests import yaml from aws_requests_auth.aws_auth import AWSRequestsAuth from bravado_core.spec import Spec from bravado_core.validate import validate_object from yaml import Loader from tests.integration import config def _validate_against_swagger(spec_dict, response): """Validate the new release data json against the swagger spec. Args: spec_dict (dict): New release data spec. response (dict): New release data. """ swagger_spec = Spec.from_dict(spec_dict) object_spec = spec_dict["definitions"]["ReleasesResponse"] validate_object(swagger_spec, object_spec, response) @pytest.mark.skip( "Need to stage credentials that can access https://qa-new-release-data.theorchard.io/releases" ) def test_file_modified_date(): """Verify response been updated within the expected time interval, and verify response matches spec. """ auth = AWSRequestsAuth( aws_access_key=config.AWS_ACCESS_KEY_ID, aws_secret_access_key=config.AWS_SECRET_ACCESS_KEY, aws_host=config.HOST, aws_region="us-east-1", aws_service="execute-api", ) spec = requests.get(config.SPEC_URL, auth=auth) spec_dict = yaml.load(spec.content, Loader) response = requests.get(config.APPLICATION_URL, auth=auth) json_response = json.loads(response.content) _validate_against_swagger(spec_dict, json_response) json_date = json_response["last_modified"] last_modified_date = datetime.strptime(json_date, "%Y-%m-%dT%H:%M:%SZ") time_diff = datetime.now(timezone.utc) - last_modified_date assert time_diff <= timedelta(minutes=config.EXPECT_UPDATE_WITHIN_MINUTES), ( "Time difference was {}, maximum allowed is {} minutes.".format( time_diff, config.EXPECT_UPDATE_WITHIN_MINUTES ) )