"""Functional tests for adding product_rejection activity.""" import json from unittest.mock import patch import pytest from owsresponse import response from notifications import handlers # noqa @pytest.mark.parametrize( 'data, expected_result', [ ( {'foo': 'bar'}, response.create_error_response( 'validation_error', { 'product_id': ['Missing data for required field.'], 'rejection_reasons': ['Missing data for required field.'], 'foo': ['Unknown field.'], }, ), ), ( {}, response.create_error_response( 'validation_error', { 'product_id': ['Missing data for required field.'], 'rejection_reasons': ['Missing data for required field.'], }, ), ), ( {'product_id': 134}, response.create_error_response( 'validation_error', {'rejection_reasons': ['Missing data for required field.']} ), ), ( {'product_id': 134, 'rejection_reasons': [{}]}, response.create_error_response( 'validation_error', { 'rejection_reasons': { '0': { 'comments': ['Missing data for required field.'], 'title': ['Missing data for required field.'], } } }, ), ), ], ) @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') @patch( 'notifications.logic.stream.add_product_rejection_activity', return_value=response.Response(status=201, message={'message': 'sent'}), ) def test_add_product_rejection_invalid( mock_logic, neo4j_exit, neo4j_enter, data, expected_result, fixture_client ): """Test adding an activity when an exception is raised.""" url = '/activity/product_rejection' result = fixture_client.post(url, data=json.dumps(data), content_type='application/json') response_data = json.loads(result.data.decode('utf-8')) assert result.status_code == expected_result.status assert response_data == expected_result.errors @pytest.mark.parametrize( 'data, expected_result', [ ( {'product_id': 134, 'rejection_reasons': [{'title': 'foo', 'comments': 'bar'}]}, response.Response(status=201, message={'message': 'sent'}), ), ], ) @patch('notifications.handlers.Neo4jSession.__enter__') @patch('notifications.handlers.Neo4jSession.__exit__') @patch( 'notifications.logic.stream.add_product_rejection_activity', return_value=response.Response(status=201, message={'message': 'sent'}), ) def test_add_product_rejection_success( mock_logic, neo4j_exit, neo4j_enter, data, expected_result, fixture_client ): """Test adding an activity when an exception is raised.""" url = '/activity/product_rejection' result = fixture_client.post(url, data=json.dumps(data), content_type='application/json') response_data = json.loads(result.data.decode('utf-8')) assert result.status_code == expected_result.status assert response_data == expected_result.message