"""Test Permissions model operations.""" from unittest.mock import call from unittest.mock import patch import pytest from sound_recordings.cypher import permissions as cypher from sound_recordings.models import permissions @pytest.mark.parametrize( 'resource', [ ('vendor'), ('subaccount') ] ) @patch('sound_recordings.models.permissions.get_session') def test_has_profile_access_account(mock_neo4j_get_session, resource): """Test query for profile access on account.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_results = mock_neo4j_session.run.return_value mock_neo4j_results.single.return_value = { 'has_access': True } profile = ('ContentProfile', 123) resource_ids = [456] resource_type = resource.title() result = permissions.has_profile_access( profile, resource_ids, resource_type ) assert mock_neo4j_session.run.called assert mock_neo4j_session.run.call_args_list == [ call( getattr(cypher, f'{resource.upper()}_ACCESS'), profile_id=123, profile_type='ContentProfile', resource_ids=[456] ) ] assert result @patch('sound_recordings.models.permissions.get_session') def test_has_profile_access_track(mock_neo4j_get_session): """Test query for profile access on track.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_results = mock_neo4j_session.run.return_value mock_neo4j_results.single.return_value = { 'has_access': True } profile = ('ContentProfile', 123) resource_ids = [456] resource_type = 'Track' result = permissions.has_profile_access( profile, resource_ids, resource_type ) assert mock_neo4j_session.run.called assert mock_neo4j_session.run.call_args_list == [ call( cypher.TRACK_ACCESS, profile_id=123, profile_type='ContentProfile', resource_ids=[456] ) ] assert result @patch('sound_recordings.models.permissions.get_session') def test_has_profile_access_non_existing_profile(mock_neo4j_get_session): """Test query for non existing profile.""" mock_neo4j_session = mock_neo4j_get_session.return_value mock_neo4j_results = mock_neo4j_session.run.return_value mock_neo4j_results.single.return_value = None profile = ('ContentProfile', 123) resource_ids = [456] resource_type = 'Track' result = permissions.has_profile_access( profile, resource_ids, resource_type ) assert mock_neo4j_session.run.called assert mock_neo4j_session.run.call_args_list == [ call( cypher.TRACK_ACCESS, profile_id=123, profile_type='ContentProfile', resource_ids=[456] ) ] assert not result @patch('sound_recordings.models.permissions.get_session') def test_has_profile_access_unknown(mock_neo4j_get_session): """Test unknown resource type exception.""" with pytest.raises(permissions.UnknownResourceAccessType): profile = ('ContentProfile', 123) resource_ids = [456] resource_type = 'Unknown' permissions.has_profile_access( profile, resource_ids, resource_type ) assert not mock_neo4j_get_session.run.called