"""Unit tests for user permissions.""" from unittest.mock import patch import pytest from sound_recordings.constants.access import ACCESS_ANALYTICS from sound_recordings.permissions.user import apply_features, apply_permissions user = {"account_type": "vendor", "account_id": "1234", "user_id": "alw:5678"} user_features = {"alw:5678": ["god-mode"]} @pytest.fixture def mock_has_analytics_false(): """Set has_analytics to return False.""" with patch("sound_recordings.permissions.user.has_analytics") as has_analytics: has_analytics.return_value = False yield has_analytics @pytest.fixture def mock_has_analytics_true(): """Set has_analytics to return True.""" with patch("sound_recordings.permissions.user.has_analytics") as has_analytics: has_analytics.return_value = True yield has_analytics @pytest.fixture def mock_analytics_features(): """Mock features.""" with patch( "sound_recordings.permissions.user.get_mobile_features_parallel" ) as features: features.return_value = user_features yield features class TestUserDoesNotHaveAnalytics: """Test when the user does not have analytics.""" def test_access_does_not_contain_analytics(self, mock_has_analytics_false): """Assert that the access array is empty.""" assert apply_permissions([user]) == [{**user, "access": []}] class TestUserHasAnalytics: """Test when the user does have analytics.""" def test_access_contains_analytics(self, mock_has_analytics_true): """Assert that the access array contains ACCESS_ANALYTICS.""" assert apply_permissions([user]) == [{**user, "access": [ACCESS_ANALYTICS]}] class TestUserFeatures: """Test when the user has features.""" def test_access_contains_features(self, mock_analytics_features): """Assert that the access array contains features for user.""" assert apply_features([user]) == [{**user, "features": ["god-mode"]}]