"""Tests for mech admin.""" from unittest.mock import MagicMock import pytest from contracts import response from contracts.logic import account, mech_admin from contracts.models import vendor_contract @pytest.fixture def fixture_mechadmin_vendor(): """Fixture for mechadmin vendor.""" return response.Response({'vendor_id': 1, 'vendor_contract_id': 1}) @pytest.fixture def fixture_not_found_response(): """Fixture for not found response.""" return response.create_not_found_response() @pytest.fixture def fixture_error_response(): """Fixture for error response.""" return response.create_error_response( code='not found', message='not found', status=204 ) @pytest.fixture def fixture_get_vendor_by_subaccount(): """Fixture for vendor by subaccount.""" return response.Response(5) @pytest.fixture def fixture_account_is_mech_admin(): """Fixture for mechadmin vendor.""" return response.Response({'mechadmin_digital': True, 'mechadmin_physical': False}) def test_is_mech_admin_true_for_subaccount( monkeypatch, fixture_get_vendor_by_subaccount, fixture_mechadmin_vendor ): """Test checks where subaccount is a mechadmin.""" monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=fixture_get_vendor_by_subaccount), ) monkeypatch.setattr( mech_admin, 'get_mech_admin', MagicMock(return_value=fixture_mechadmin_vendor) ) result = mech_admin.is_mech_admin('subaccount', 1) assert result.status == 200 def test_is_mech_admin_vendor_not_found_for_subaccount( monkeypatch, fixture_error_response ): """Test checks where vendor is not found for subaccount.""" monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=fixture_error_response), ) result = mech_admin.is_mech_admin('subaccount', 2) assert result.status == 204 def test_is_mech_admin_false_for_subaccount( monkeypatch, fixture_get_vendor_by_subaccount, fixture_error_response ): """Test checks where subaccount is not a mechadmin.""" monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=fixture_get_vendor_by_subaccount), ) monkeypatch.setattr( mech_admin, 'get_mech_admin', MagicMock(return_value=fixture_error_response) ) result = mech_admin.is_mech_admin('subaccount', 2) assert result.status == 204 def test_get_mech_admin_true(monkeypatch, fixture_mechadmin_vendor): """Test checks where vendor is a mechadmin.""" monkeypatch.setattr( vendor_contract, 'get_active_contract', MagicMock(return_value=fixture_mechadmin_vendor), ) monkeypatch.setattr( vendor_contract, 'get_digital_mech_admin', MagicMock(return_value=fixture_mechadmin_vendor), ) result = mech_admin.get_mech_admin(1) assert result.status == 200 def test_get_mech_admin_false(monkeypatch, fixture_error_response): """Test checks where vendor is not a mechadmin.""" monkeypatch.setattr( vendor_contract, 'get_active_contract', MagicMock(return_value=fixture_error_response), ) result = mech_admin.get_mech_admin(4) assert result.status == 204 def test_get_mech_admin_no_vendor_contract( monkeypatch, fixture_mechadmin_vendor, fixture_not_found_response ): """Test checks where vendor is not a mechadmin based on vendor contract.""" monkeypatch.setattr( vendor_contract, 'get_active_contract', MagicMock(return_value=fixture_mechadmin_vendor), ) monkeypatch.setattr( vendor_contract, 'get_digital_mech_admin', MagicMock(return_value=fixture_not_found_response), ) result = mech_admin.get_mech_admin(4) assert result.status == 204 def test_is_subaccount_mechadmin_forbidden_user( monkeypatch, fixture_get_vendor_by_subaccount ): """Test for is_subaccount_mechadmin function. Test checks where grass authenticated vendor is not the owner of the subaccount.specified by subaccountId. """ monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=fixture_get_vendor_by_subaccount), ) result = mech_admin.is_subaccount_mechadmin(1, 1) assert result.status == 403 def test_is_subaccount_mechadmin_vendor_not_found( monkeypatch, fixture_not_found_response ): """Test checks where vendor not found for given subaccount.""" monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=fixture_not_found_response), ) result = mech_admin.is_subaccount_mechadmin(1, 1) assert result.status == 404 def test_is_subaccount_mechadmin_success(monkeypatch): """Test checks where subaccount is mechadmin.""" mock_response = MagicMock() mock_response.message = 5 mock_response.status = 200 monkeypatch.setattr( account, 'get_vendor_by_subaccount', MagicMock(return_value=mock_response) ) monkeypatch.setattr( mech_admin, 'is_mech_admin', MagicMock(return_value=mock_response) ) result = mech_admin.is_subaccount_mechadmin(1, 5) assert result def test_account_is_mech_admin( mocker, fixture_get_vendor_by_subaccount, fixture_mechadmin_vendor, fixture_account_is_mech_admin, ): """Test check if subaccount is a mechadmin.""" mocker.patch.object(account, 'get_vendor_by_subaccount') mock_get_active_contract = mocker.patch.object( vendor_contract, 'get_active_contract', return_value=fixture_mechadmin_vendor ) mock_get_mech_admin_for_account = mocker.patch.object( vendor_contract, 'get_mech_admin_for_account', return_value=fixture_account_is_mech_admin, ) result = mech_admin.account_is_mech_admin('vendor', 1) assert not account.get_vendor_by_subaccount.called mock_get_active_contract.asser_called_with(5) mock_get_mech_admin_for_account.assert_called_with(1, 1) assert result.status == 200 assert result.message == fixture_account_is_mech_admin.message def test_account_is_mech_admin_for_subaccount( mocker, fixture_get_vendor_by_subaccount, fixture_mechadmin_vendor, fixture_account_is_mech_admin, ): """Test check if vendor is a mechadmin.""" mock_get_vendor_by_subaccount = mocker.patch.object( account, 'get_vendor_by_subaccount', return_value=fixture_get_vendor_by_subaccount, ) mock_get_active_contract = mocker.patch.object( vendor_contract, 'get_active_contract', return_value=fixture_mechadmin_vendor ) mock_get_mech_admin_for_account = mocker.patch.object( vendor_contract, 'get_mech_admin_for_account', return_value=fixture_account_is_mech_admin, ) result = mech_admin.account_is_mech_admin('subaccount', 1) mock_get_vendor_by_subaccount.assert_called_with(1) mock_get_active_contract.asser_called_with(5) mock_get_mech_admin_for_account.assert_called_with(1, 5) assert result.status == 200 assert result.message == fixture_account_is_mech_admin.message def test_account_is_mech_admin_no_vendor_contract( mocker, fixture_get_vendor_by_subaccount ): """Test checks where vendor is not a mechadmin based on vendor contract.""" mock_get_vendor_by_subaccount = mocker.patch.object( account, 'get_vendor_by_subaccount', return_value=fixture_get_vendor_by_subaccount, ) mock_get_active_contract = mocker.patch.object( vendor_contract, 'get_active_contract', return_value=response.create_not_found_response(), ) mocker.patch.object(vendor_contract, 'get_mech_admin_for_account') result = mech_admin.account_is_mech_admin('subaccount', 1) mock_get_vendor_by_subaccount.assert_called_with(1) mock_get_active_contract.asser_called_with(5) assert not vendor_contract.get_mech_admin_for_account.called assert result.status == 404