"""Test vendor_star logic.""" from __future__ import annotations from typing import Any from unittest.mock import MagicMock, patch import pytest from permissions.logic.vendor_star import ( is_allowed_to_receive_vendor_star, is_resource_vendor_star, resource_access_contains_vendor_star, ) @pytest.mark.parametrize( 'email, expected', [ pytest.param('jess@jess.com', False, id='well obviously this is not ok'), pytest.param('jess@sonymusic-pde.com', True, id='PDE employees are allowed'), pytest.param('bey@sonymusic.com', True, id='SME employees are allowed'), pytest.param('flomilli@rcarecords.com', True, id='RCA employees are allowed'), pytest.param('cmat@AWAL.com', True, id='case-insensitive'), pytest.param( 'jess@theorchard.com@sonymusic-pde.com', False, id='Should we do email validation?', marks=pytest.mark.xfail, ), ], ) def test_is_allowed_to_receive_vendor_star( email: str, expected: bool, ) -> None: """Test is_allowed_to_receive_vendor_star.""" actual = is_allowed_to_receive_vendor_star(email) assert actual == expected @pytest.mark.parametrize( 'resource, expect_raises, expected', [ pytest.param( {}, True, True, id='no resource identifier to evaluate', ), pytest.param( {'tenant_uuid': 'this is ignored'}, True, True, id='tenant_uuid is not a resource identifier to evaluate', ), pytest.param( {'resource_uuid': 'here', 'resource_id': 'also here'}, True, True, id='resource_uuid and resource_id cannot be present at the same time', ), pytest.param( {'uuid': 'here', 'resource_id': 'also here'}, True, True, id='uuid and resource_id cannot be present at the same time', ), pytest.param( {'uuid': 'here', 'resource_uuid': 'also here'}, True, True, id='uuid and resource_uuid cannot be present at the same time', ), pytest.param( {'uuid': 'oh jeez', 'resource_uuid': 'here', 'resource_id': 'also here'}, True, True, id='all the resource identifiers cannot be present at the same time', ), pytest.param( {'resource_id': '*'}, False, True, id='resource_id * means vendor star', ), pytest.param( {'resource_id': '053a1a75-acc5-4cd8-9206-a194335d2afa'}, False, True, id='resource_id with the vendor_star uuid means vendor star', ), pytest.param( {'resource_id': 123}, False, False, id='123 is not * so this is not vendor star', ), pytest.param( {'resource_uuid': '*'}, False, True, id='resource_uuid with * means vendor star', ), pytest.param( {'resource_uuid': '053a1a75-acc5-4cd8-9206-a194335d2afa'}, False, True, id='resource_uuid with the vendor_star uuid means vendor star', ), pytest.param( {'resource_uuid': 123}, False, False, id='resource_uuid with a number is not vendor star', ), pytest.param( {'resource_uuid': '206d55b8-b8cd-4811-a331-d62cc9d7a30f'}, False, False, id='resource_uuid with some other uuid is not vendor star', ), pytest.param( {'uuid': '*'}, False, True, id='uuid with * means vendor star', ), pytest.param( {'uuid': '053a1a75-acc5-4cd8-9206-a194335d2afa'}, False, True, id='uuid with the vendor_star uuid means vendor star', ), pytest.param( {'uuid': 123}, False, False, id='uuid with a number is not vendor star', ), pytest.param( {'uuid': '206d55b8-b8cd-4811-a331-d62cc9d7a30f'}, False, False, id='uuid with some other uuid is not vendor star', ), ], ) def test_is_resource_vendor_star( resource: dict[str, Any], expect_raises: bool, expected: bool, ) -> None: """Test is_resource_vendor_star.""" if not expect_raises: actual = is_resource_vendor_star(resource) assert actual == expected else: with pytest.raises(Exception): is_resource_vendor_star(resource) @pytest.mark.parametrize( 'side_effect, expected_call_count, expected', [ pytest.param( [(True)], 1, True, id='First resource is vendor star', ), pytest.param( [(False), (True)], 2, True, id='Second resource is vendor star', ), pytest.param( [(False), (False), (True)], 3, True, id='Third resource is vendor star', ), pytest.param( [(False), (False), (False)], 3, False, id='No resources are vendor star', ), ], ) @patch('permissions.logic.vendor_star.is_resource_vendor_star') def test_resource_access_contains_vendor_star( mock_is_resource_vendor_star: MagicMock, side_effect: list[tuple], expected_call_count: int, expected: bool, ) -> None: """Test resource_access_contains_vendor_star.""" mock_is_resource_vendor_star.side_effect = side_effect actual = resource_access_contains_vendor_star( [ {'mock_resource': 1}, {'mock_resource': 2}, {'mock_resource': 3}, ] ) assert actual == expected assert mock_is_resource_vendor_star.call_count == expected_call_count @patch('permissions.logic.vendor_star.is_resource_vendor_star') def test_resource_access_contains_vendor_star_no_empty_lists( mock_is_resource_vendor_star: MagicMock, ) -> None: """Test resource_access_contains_vendor_star raises on empty list.""" with pytest.raises(Exception): resource_access_contains_vendor_star([]) mock_is_resource_vendor_star.assert_not_called()