"""Tests for handlers.""" import json from oto import response import pytest from ows_product_physical.constant import field from ows_product_physical.constant import header from ows_product_physical.logic import product_physical from ows_product_physical.validation import validation def test_handler_put_product_by_id_validation_success( feature_engine, mocker, client, valid_put_header, valid_put_product_data): """Test `PUT /product/{productId}` handler with successful validation.""" mocker.spy(validation, 'validate') mocker.patch.object( product_physical, 'update', return_value=response.Response( message={'product_id': 1234}), autospec=True) result = client.put( '/product/1234', headers=valid_put_header, data=json.dumps(valid_put_product_data)) assert validation.validate.called assert result.status_code == 200 assert product_physical.update.called def test_handler_put_product_by_id_header_validation_error( mocker, client, valid_put_header, valid_put_product_data, feature_engine): """Test `PUT /product/{productId}` handler fails on invalid headers.""" invalid_put_headers = valid_put_header invalid_put_headers.pop(header.CONTENT_TYPE) mocker.spy(validation, 'validate') mocker.spy(product_physical, 'update') result = client.put( '/product/1234', headers=invalid_put_headers, data=json.dumps(valid_put_product_data)) assert validation.validate.called assert result.status_code == 400 assert not product_physical.update.called @pytest.mark.parametrize( ('field_to_modify', 'modified_value'), [ (field.UPC, None), ('FIELD_THAT_DOES_NOT_EXIST', 'applesauce') ]) def test_handler_put_product_by_id_body_validation_error( feature_engine, mocker, client, valid_put_header, valid_put_product_data, field_to_modify, modified_value): """Test `PUT /product/{productId}` handler fails on invalid body.""" invalid_put_product_data = valid_put_product_data invalid_put_product_data[field_to_modify] = modified_value mocker.spy(validation, 'validate') mocker.spy(product_physical, 'update') result = client.put( '/product/1234', headers=valid_put_header, data=json.dumps(invalid_put_product_data)) assert validation.validate.called assert result.status_code == 400 assert not product_physical.update.called def test_handler_put_product_product_code_validation_error( feature_engine, mocker, client, fixture_response_created, valid_put_header, valid_put_product_data): """Test PUT /product/{productId} product code validation failure.""" mutated_product = valid_put_product_data mutated_product.update({field.PRODUCT_CODE: '123456789ABCDEFG'}) mocker.spy(product_physical, 'update') result = client.put( '/product/1234', headers=valid_put_header, data=json.dumps(mutated_product)) assert result.status_code == 400 assert not product_physical.update.called def test_handler_put_product_by_id_header_error( client, valid_put_header, feature_engine): """Test `PUT /product/{productId}` handler fails on invalid headers.""" invalid_header = valid_put_header invalid_header['Grass-Account-Type'] = 'applesauce' result = client.put('/product/1234', headers=invalid_header) assert result.status_code == 400 def test_handler_product_put_accepts_display_upc( mocker, client, feature_engine, valid_put_header): """Test that display_upc is accepted.""" mocker.patch.object( product_physical, 'update', return_value=response.Response({}), autospec=True) result = client.put('/product/1234', headers=valid_put_header, data=json.dumps({'display_upc': '1234567890123'})) assert result.status_code == 200