"""Functional tests for getting 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 @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, vendor_id, expected_result", [ ( 1, 24601, { "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", "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ( 4, 90210, { "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", "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ], ) def test_get_collaborator( auth_client, mocker, collaborator_id, vendor_id, expected_result ): """Test getting a single collaborator with CollaboratorProfile.""" mock_auth(mocker, vendor_id) result = auth_client.get(f"/collaborators/{collaborator_id}") result_data = json.loads(result.data.decode()) expected_result["created_date"] = result_data["created_date"] assert result.status_code == response_code.OK assert result_data == expected_result @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, vendor_id, expected_result", [ ( 1, 24601, { "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", "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ( 4, 90210, { "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", "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ), ], ) def test_get_collaborator_moneyhub( auth_client, mocker, collaborator_id, vendor_id, expected_result ): """Test getting a single collaborator with CollaboratorProfile.""" mock_auth(mocker, collaborator_id) result = auth_client.get( f"/collaborators/{collaborator_id}", headers=[ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", "MoneyhubProfile"), ], ) result_data = json.loads(result.data.decode()) expected_result["created_date"] = result_data["created_date"] assert result.status_code == response_code.OK assert result_data == expected_result @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, vendor_id, expected_result", [ ( 7, 6666, { "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, "dp_enabled_date": ANY, "dp_splits_agreed_date": ANY, }, ) ], ) def test_get_collaborator_with_recipient_success( auth_client, collaborator_id, vendor_id, expected_result, mocker ): """Test getting a single collaborator successfully.""" mock_auth(mocker, vendor_id) result = auth_client.get( f"/collaborators/{collaborator_id}", query_string={"has_recipient": True} ) result_data = json.loads(result.data.decode()) expected_result["created_date"] = result_data["created_date"] assert result.status_code == response_code.OK assert result_data == expected_result @db.test_schema_default_seed @pytest.mark.parametrize( "collaborator_id, vendor_id, status", [ (1337, 24601, response_code.NOT_FOUND), (1, 90210, response_code.FORBIDDEN), ], ) def test_get_collaborator_fail(auth_client, collaborator_id, vendor_id, status, mocker): """Test failing to get a single collaborator.""" mock_auth(mocker, vendor_id) result = auth_client.get(f"/collaborators/{collaborator_id}") assert result.status_code == status @db.test_schema_default_seed @pytest.mark.parametrize( "vendor_id, limit, offset, expected_items", [ ( 24601, 0, 0, { "items": [ { "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": 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, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( 24601, 1, 0, { "items": [ { "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, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( 24601, 0, 1, { "items": [ { "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, }, ], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( 24601, 0, 1337, { "items": [], "pagination": { "type": "standard", "total_records": 2, }, }, ), ( 55555, 0, 0, { "items": [], "pagination": { "type": "standard", "total_records": 0, }, }, ), ], ) def test_get_collaborators( auth_client, vendor_id, limit, offset, expected_items, mocker ): """Expect to get a list of collaborators.""" mock_auth(mocker, vendor_id) result = auth_client.get( "/collaborators", query_string={"limit": limit, "offset": offset, "vendor_id": vendor_id}, ) result_data = json.loads(result.data.decode()) assert result.status_code == response_code.OK assert result_data == expected_items @db.test_schema_default_seed @pytest.mark.parametrize( "term, dimensions, expected_items", [ ( "Another Person", "name", { "items": [ { "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, }, ], "pagination": { "type": "standard", "total_records": 1, }, }, ), ( "1", "name,id", { "items": [ { "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, }, ], "pagination": { "type": "standard", "total_records": 1, }, }, ), ( "Another Person", "id", { "items": [], "pagination": { "type": "standard", "total_records": 0, }, }, ), ], ) def test_get_collaborators_with_search( auth_client, mocker, mock_account, term, dimensions, expected_items ): """Expect to get a list of collaborators.""" mock_auth(mocker, mock_account.id) result = auth_client.get( "/collaborators", query_string={"term": term, "dimensions": dimensions}, ) result_data = json.loads(result.data.decode()) assert result.status_code == response_code.OK assert result_data == expected_items def test_get_collaborators_error(auth_client, mocker): """Expect to get a list of collaborators.""" vendor_id = 24601 result = auth_client.get( "/collaborators", query_string={"vendor_id": vendor_id}, headers=[ ("orchard-identity-id", "7ec441df-7b21-4a71-a9da"), ("orchard-profile-id", "90003"), ("orchard-profile-type", "BogusProfile"), ], ) assert result.status_code == response_code.FORBIDDEN @db.test_schema_default_seed def test_get_collaborators_dataloader(auth_client, mocker): """Getting multiple collaborators.""" vendor_id = 24601 mock_auth(mocker, vendor_id) request_data = {"collaborator_ids": ["1", "2"]} result = auth_client.post( "/collaborators/dataloader", data=json.dumps(request_data), content_type="application/json", ) result_data = json.loads(result.data.decode()) for item in result_data: item["data"]["created_date"] = "now" assert result.status_code == response_code.OK assert result_data == [ { "data": { "id": 1, "name": "Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12345", "performance_rights": False, "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, }, }, { "data": { "id": 2, "name": "Another Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12346", "performance_rights": False, "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, } }, ] @db.test_schema_default_seed def test_get_collaborators_dataloader_forbidden(auth_client, mocker): """Getting multiple collaborators including forbidden and non-existent.""" vendor_id = 24601 mock_auth(mocker, vendor_id) request_data = { "collaborator_ids": [ "2", # exists and we have access "3", # exists and we don't have access "13", # doesn't exist ] } result = auth_client.post( "/collaborators/dataloader", data=json.dumps(request_data), content_type="application/json", ) result_data = json.loads(result.data.decode()) for item in result_data: if item["data"] is not None: item["data"]["created_date"] = "now" assert result.status_code == response_code.OK assert result_data == [ { "data": { "id": 2, "name": "Another Person", "vendor_id": 24601, "subaccount_id": None, "participant_id": "12346", "performance_rights": False, "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, }, }, { "data": None, }, { "data": None, }, ]