"""Functional tests for POST /action endpoint.""" import datetime import json from oto import response from conflict_manager.constants import database as db_consts from conflict_manager.constants import error from conflict_manager.constants import header from conflict_manager.logic import action as action_logic from conflict_manager.utils import api_utils def test_create_actions_success( client, mocker, get_conflicts_territories_mock, bulk_remove_territories_mock, bulk_create_fingerprint_rules_mock, get_rules_mock): """Expect OK response.""" conflict_ids_return_value = api_utils.make_pagination_response( [1, 2, 3, 4]) mocker.patch.object( action_logic.fact_conflict, 'get_conflict_ids_for_account_and_isrc', return_value=conflict_ids_return_value) mock_action_model = mocker.patch.object( action_logic.action_model, 'create', return_value=response.Response()) headers = { header.GRASS_ACCOUNT_TYPE: 'vendor', header.GRASS_ACCOUNT_ID: '123', header.CORRELATION_ID: 'test id', header.CONTENT_TYPE: 'application/json' } data = { 'territory_standard': 'ISO_3166_2106', 'isrc': 'TL1234567890', 'conflicting_owner': 'Ill Will', 'conflict_date': '2017-11-12', 'tuid': 123, 'release_action': { 'conflict_ids': [1, 2], 'reason': 'release test', 'additional_information': 'release test' }, 'assert_action': { 'conflict_ids': [3, 4], 'reason': 'assert test', 'additional_information': 'assert test' } } expected_date = datetime.datetime.utcnow().strftime( db_consts.SNOWFLAKE_DATE_FORMAT) expected_actions = [ { 'conflict_id': 1, 'action': 'release', 'action_date': expected_date, 'reason': 'release test', 'additional_information': 'release test', 'account_id': '123', 'account_type': 'vendor', 'es_id': '', 'tuid': 123 }, { 'conflict_id': 2, 'action': 'release', 'action_date': expected_date, 'reason': 'release test', 'additional_information': 'release test', 'account_id': '123', 'account_type': 'vendor', 'es_id': '', 'tuid': 123 }, { 'conflict_id': 3, 'action': 'assert', 'action_date': expected_date, 'reason': 'assert test', 'additional_information': 'assert test', 'account_id': '123', 'account_type': 'vendor', 'es_id': '', 'tuid': 123 }, { 'conflict_id': 4, 'action': 'assert', 'action_date': expected_date, 'reason': 'assert test', 'additional_information': 'assert test', 'account_id': '123', 'account_type': 'vendor', 'es_id': '', 'tuid': 123 }] result = client.post('action', headers=headers, data=json.dumps(data)) assert result.status_code == 200 mock_action_model.assert_called_with(expected_actions) get_conflicts_territories_mock.assert_called_with([1, 2]) bulk_remove_territories_mock.assert_called() bulk_create_fingerprint_rules_mock.assert_called() get_rules_mock.assert_called() def test_create_actions_validation_error(client, mocker): """Expect error response because of failed validation.""" conflict_ids_return_value = api_utils.make_pagination_response( [1, 2]) mocker.patch.object( action_logic.fact_conflict, 'get_conflict_ids_for_account_and_isrc', return_value=conflict_ids_return_value) headers = { header.GRASS_ACCOUNT_TYPE: 'vendor', header.GRASS_ACCOUNT_ID: '123', header.CORRELATION_ID: 'test id', header.CONTENT_TYPE: 'application/json' } data = { 'territory_standard': 'ISO_3166_2106', 'isrc': 'TL1234567890', 'tuid': 123, 'conflicting_owner': 'Ill Will', 'conflict_date': '2017-11-12', 'release_action': { 'conflict_ids': [1, 2], 'reason': 'release test', 'additional_information': 'release test' }, 'assert_action': { 'conflict_ids': [3, 4], 'reason': 'assert test', 'additional_information': 'assert test' } } result = client.post('action', headers=headers, data=json.dumps(data)) res_body = json.loads(result.data.decode()) assert result.status_code == 400 assert res_body['message'] == error.INVALID_TERRITORIES_MSG