"""POST /image Schema Validation. What this is testing is essentially the business-level validations that we have codified in our API definition doc for the POST /image endpoint (RAML). This means required fields, types, and maxlengths submitted in the POST body. NOTE: This means that the API documentation is ALSO the machine-readable spec for the API. Changing field definitions will directly affect the business-level logic. For that reason, theses tests will break if the API contract (RAML) is changed. """ from images.validation import json_schema from images.validation.schema import json_validators def test_post_image_validate_success(): """Successful response is returned when the data is valid.""" valid_post_image_data = { 'filename': 'somefilename.png', 'id': 'somefilename.png'} response = json_schema.validate( data=valid_post_image_data, validator=json_validators.POST_IMAGE_VALIDATOR) assert response.status == 200 def test_post_image_validate_invalid_filename(): """Validation fails on invalid filename type.""" invalid_post_image_data = {'filename': 123, 'id': 'someid.jpg'} response = json_schema.validate( data=invalid_post_image_data, validator=json_validators.POST_IMAGE_VALIDATOR) error_message = response.errors.get('message') assert response.status == 400 assert 'filename' in error_message def test_post_image_validate_required_filename(): """Validation fails on missing filename.""" missing_filename_post_image_data = {'id': 'someid.jpg'} response = json_schema.validate( data=missing_filename_post_image_data, validator=json_validators.POST_IMAGE_VALIDATOR) error_message = response.errors.get('message') assert response.status == 400 assert 'filename' in error_message def test_post_image_validate_invalid_id(): """Validation fails on invalid id type.""" invalid_post_image_data = {'id': 123, 'filename': 'someid.jpg'} response = json_schema.validate( data=invalid_post_image_data, validator=json_validators.POST_IMAGE_VALIDATOR) error_message = response.errors.get('message') assert response.status == 400 assert 'id' in error_message def test_post_image_validate_required_id(): """Validation fails on missing id.""" missing_id_post_image_data = {'filename': 'someid.jpg'} response = json_schema.validate( data=missing_id_post_image_data, validator=json_validators.POST_IMAGE_VALIDATOR) error_message = response.errors.get('message') assert response.status == 400 assert 'id' in error_message