"""Test handler utils.""" import codecs import copy from datetime import date from datetime import datetime import json import flask import pytest import application from deliveryhistory.constants import header from deliveryhistory.util import handler_util from deliveryhistory.validation import json_schema from deliveryhistory.validation import validators @pytest.fixture def validated_handler_fixture(): """Create a method with header validator added.""" @handler_util.validate_header(validators.HEADER_VALIDATOR) def test_method(): """Test method.""" return flask.Response('ok') return test_method @pytest.fixture def validated_body_handler_fixture(): """Create a method with body validator added.""" @handler_util.validate_body(validators.RECORDS_VALIDATOR) def test_method(): """Test method.""" return flask.Response('ok') return test_method def test_header_validation_success( header_fixture, context, validated_handler_fixture): """Test success response.""" with context, application.app.test_request_context(headers=header_fixture): response = validated_handler_fixture() assert response.status_code == 200 def test_header_validation_fail(context, validated_handler_fixture): """Test validation error response when Correlation-Id header missing.""" with context, application.app.test_request_context(): response = validated_handler_fixture() assert response.status_code == 400 data = json.loads(codecs.utf_8_decode(response.data)[0]) assert data['code'] == json_schema.VALIDATION_ERROR assert header.CORRELATION_ID in data['message'] def test_body_validation_success( body_fixture, validated_body_handler_fixture, context): """Test success response.""" with context, application.app.test_request_context( content_type='application/json', data=json.dumps(body_fixture)): response = validated_body_handler_fixture() assert response def test_body_validation_failure( body_fixture, validated_body_handler_fixture, context): """Test success response.""" mutated_body = copy.copy(body_fixture) mutated_body['items'][0].pop('isrc') with context, application.app.test_request_context( content_type='application/json', data=json.dumps(mutated_body)): response = validated_body_handler_fixture() assert response.status_code == 400 def test_pagination(): """Test pagination wrapper.""" test_records = [{'k': v} for v in range(30)] offset = 0 limit = 10 paginated = handler_util.add_pagination(test_records, offset, limit) assert len(paginated['items']) == limit assert len(test_records) == paginated['pagination']['total_records'] assert paginated['pagination']['offset'] == offset assert paginated['pagination']['limit'] == limit def test_json_datetime_encoder_date(): """Test DatetimeEncoder converts date objects to strings correctly.""" test_date = date.today() test_date_str = test_date.strftime( '"{0}"'.format(handler_util.DatetimeEncoder.DEFAULT_DATE_FORMAT)) json_string = json.dumps(test_date, cls=handler_util.DatetimeEncoder) assert json_string == test_date_str def test_json_datetime_encoder_datetime(): """Test DatetimeEncoder converts datetime objects to strings correctly.""" test_datetime = datetime.utcnow() test_datetime_str = test_datetime.strftime( '"{0}"'.format(handler_util.DatetimeEncoder.DEFAULT_DATETIME_FORMAT)) json_string = json.dumps(test_datetime, cls=handler_util.DatetimeEncoder) assert json_string == test_datetime_str