import pytest from app.main import validate_users_has_vendor_access_and_role from app.dtos import User, Vendor from app.config import ALL_ORCHARD_LABELS_UUID class TestValidateUsersHasVendorAccessAndRole: """Test validation function for user vendor access""" def test_validate_user_with_all_orchard_labels_uuid(self): """Test that user with ALL_ORCHARD_LABELS_UUID passes validation for any vendor""" # Create user with ALL_ORCHARD_LABELS_UUID vendor_all_orchard = Vendor( name="All Orchard Access", id=999, uuid=ALL_ORCHARD_LABELS_UUID, emulated=False ) user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = True user.vendors = [vendor_all_orchard] users = [user] vendors_uuids = ["vendor-uuid-1", "vendor-uuid-2", "vendor-uuid-3"] # Should not raise any exceptions validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_user_with_direct_vendor_access(self): """Test that user with direct vendor access passes validation""" # Create user with direct vendor access vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) vendor2 = Vendor( name="Vendor 2", id=2, uuid="vendor-uuid-2", emulated=False ) user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = True user.vendors = [vendor1, vendor2] users = [user] vendors_uuids = ["vendor-uuid-1", "vendor-uuid-2"] # Should not raise any exceptions validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_user_without_vendor_access_fails(self): """Test that user without vendor access fails validation""" # Create user without required vendor access vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = True user.vendors = [vendor1] users = [user] vendors_uuids = ["vendor-uuid-1", "vendor-uuid-2"] # user doesn't have access to vendor-uuid-2 # Should raise SystemExit for vendor-uuid-2 with pytest.raises(SystemExit): validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_user_without_audience_profile_fails(self): """Test that user without audience profile fails validation""" # Create user without audience profile vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = False # No audience profile user.vendors = [vendor1] users = [user] vendors_uuids = ["vendor-uuid-1"] # Should raise SystemExit with pytest.raises(SystemExit): validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_user_without_role_fails(self): """Test that user without role fails validation when check_role=True""" # Create user without role vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) user = User(email="test@example.com", identity="user1", role=None) # No role user.exists = True user.audience_profile = True user.vendors = [vendor1] users = [user] vendors_uuids = ["vendor-uuid-1"] # Should raise SystemExit when check_role=True (default) with pytest.raises(SystemExit): validate_users_has_vendor_access_and_role(users, vendors_uuids, check_role=True) def test_validate_user_without_role_passes_when_check_role_false(self): """Test that user without role passes validation when check_role=False""" # Create user without role vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) user = User(email="test@example.com", identity="user1", role=None) # No role user.exists = True user.audience_profile = True user.vendors = [vendor1] users = [user] vendors_uuids = ["vendor-uuid-1"] # Should not raise any exceptions when check_role=False validate_users_has_vendor_access_and_role(users, vendors_uuids, check_role=False) def test_validate_mixed_users_with_all_orchard_and_direct_access(self): """Test validation with mixed users - some with ALL_ORCHARD_LABELS_UUID, some with direct access""" # Create user with ALL_ORCHARD_LABELS_UUID vendor_all_orchard = Vendor( name="All Orchard Access", id=999, uuid=ALL_ORCHARD_LABELS_UUID, emulated=False ) user1 = User(email="user1@example.com", identity="user1", role="admin") user1.exists = True user1.audience_profile = True user1.vendors = [vendor_all_orchard] # Create user with direct vendor access vendor1 = Vendor( name="Vendor 1", id=1, uuid="vendor-uuid-1", emulated=False ) vendor2 = Vendor( name="Vendor 2", id=2, uuid="vendor-uuid-2", emulated=False ) user2 = User(email="user2@example.com", identity="user2", role="admin") user2.exists = True user2.audience_profile = True user2.vendors = [vendor1, vendor2] users = [user1, user2] vendors_uuids = ["vendor-uuid-1", "vendor-uuid-2"] # Should not raise any exceptions validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_empty_vendors_list(self): """Test validation with empty vendors list""" user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = True user.vendors = [] users = [user] vendors_uuids = [] # Should not raise any exceptions with empty vendors list validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_all_orchard_labels_uuid_constant(self): """Test that ALL_ORCHARD_LABELS_UUID constant is being used correctly""" # This test ensures we're using the correct constant assert ALL_ORCHARD_LABELS_UUID == "053a1a75-acc5-4cd8-9206-a194335d2afa" # Create user with the constant vendor_all_orchard = Vendor( name="All Orchard Access", id=999, uuid=ALL_ORCHARD_LABELS_UUID, emulated=False ) user = User(email="test@example.com", identity="user1", role="admin") user.exists = True user.audience_profile = True user.vendors = [vendor_all_orchard] users = [user] vendors_uuids = ["any-vendor-uuid"] # Should pass validation validate_users_has_vendor_access_and_role(users, vendors_uuids) def test_validate_fte_scenario_with_all_orchard_labels_uuid_no_role(self): """Test FTE scenario: user with ALL_ORCHARD_LABELS_UUID but no role passes when check_role=False""" # Create user with ALL_ORCHARD_LABELS_UUID but no role (typical FTE scenario) vendor_all_orchard = Vendor( name="All Orchard Access", id=999, uuid=ALL_ORCHARD_LABELS_UUID, emulated=False ) user = User(email="fte@example.com", identity="user1", role=None) # No role in CSV user.exists = True user.audience_profile = True user.vendors = [vendor_all_orchard] users = [user] vendors_uuids = ["vendor-uuid-1", "vendor-uuid-2"] # Should pass validation when check_role=False (FTE scenario) validate_users_has_vendor_access_and_role(users, vendors_uuids, check_role=False)