"""Functional tests for searching collaborator data.""" import json from unittest.mock import ANY from oto import status as response_code import pytest from tests.testutils import db, mock_auth fixture_data = [ { "id": 2, "name": "Another Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12346", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-523", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 4, "name": "Another Person", "vendor_id": 90210, "performance_rights": True, "subaccount_id": None, "participant_id": "12348", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 10, "name": "Another Person", "vendor_id": 120594, "performance_rights": True, "subaccount_id": None, "participant_id": "12348", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-323", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 1, "name": "Person", "vendor_id": 24601, "performance_rights": False, "subaccount_id": None, "participant_id": "12345", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-123", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 3, "name": "Person", "vendor_id": 90210, "performance_rights": True, "subaccount_id": None, "participant_id": "12347", "recipient_id": None, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": "Mixing Engineer", "internal_id": "ABC-423", "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 11, "name": "recipient again", "vendor_id": 6666, "subaccount_id": 123456, "participant_id": None, "performance_rights": True, "recipient_id": 4, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, { "id": 7, "name": "recipient", "vendor_id": 6666, "performance_rights": True, "subaccount_id": 123456, "participant_id": "10", "recipient_id": 1, "currency": "USD", "collaborator_type": "COLLABORATOR", "description": None, "internal_id": None, "created_date": ANY, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ] @db.test_schema_default_seed @pytest.mark.parametrize( "full_catalog_access, payload, expected_count, expected_items", [ (True, {"term": ""}, 0, []), (True, {"term": "Person"}, 5, [*fixture_data[0:-2]]), ( True, {"term": "Person", "offset": "0", "limit": "0"}, 5, [*fixture_data[0:-2]], ), (True, {"term": "Person", "offset": "0", "limit": "1"}, 5, [fixture_data[0]]), (True, {"term": "Person", "offset": "1", "limit": "1"}, 5, [fixture_data[1]]), ( True, {"term": "1", "offset": "0", "limit": "0"}, 3, [fixture_data[2], fixture_data[3], fixture_data[5]], ), ( False, {"term": "Person", "offset": "0", "limit": "0"}, 2, [fixture_data[0], fixture_data[3]], ), ( True, { "term": "recipient", "offset": "0", "limit": "0", "exactly_match_term": "true", }, 1, [fixture_data[6]], ), ], ) def test_search_collaborators( auth_client, mocker, mock_account, full_catalog_access, payload, expected_count, expected_items, ): """Testing collaborators search.""" vendor_id = "*" if full_catalog_access else mock_account.id mock_auth(mocker, vendor_id) result = auth_client.get( "/collaborators", query_string={**payload, "dimensions": "name,id"}, headers=[ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", "SettingsProfile"), ], ) result_data = json.loads(result.data.decode()) expected = { "items": expected_items, "pagination": {"type": "standard", "total_records": expected_count}, } assert result.status_code == response_code.OK assert result_data == expected @db.test_schema_default_seed def test_search_collaborators_no_access(auth_client, mocker, mock_account): """Testing collaborators search fails on invalid profile.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/collaborators", query_string={"term": "123", "dimensions": "name,id"}, headers=[ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", "BogusProfile"), ], ) assert result.status_code == response_code.FORBIDDEN