"""Test for Feature Control util.""" from collections import namedtuple from unittest.mock import MagicMock, patch import pytest from product.models import ows_account from product.utils import feature_control_util def test_check_feature_control_for_subaccount_success( mocker, test_subaccount, test_feature_control, fixture_subaccount_info_response): """Test feature control for subaccount when it succeeds.""" mocker.patch.object( ows_account, 'get_subaccount_info', return_value=fixture_subaccount_info_response) mocker.patch.object( feature_control_util, 'check_feature_control_for_vendor', return_value=True) result = feature_control_util.check_feature_control_for_subaccount( test_subaccount, test_feature_control) assert result is True def test_check_feature_control_for_subaccount_fail( mocker, test_subaccount, test_feature_control, fixture_subaccount_info_failure): """Test feature control for subaccount when fails.""" mocker.patch.object( ows_account, 'get_subaccount_info', return_value=fixture_subaccount_info_failure) result = feature_control_util.check_feature_control_for_subaccount( test_subaccount, test_feature_control) assert result.status == 403 def test_check_feature_control_for_vendor_success_with_true( mocker, test_vendor, test_feature_control, fixture_enabled_features_response): """Test feature control for vendor when it returns true.""" mocker.patch.object( ows_account, 'get_enabled_vendor_features_control', return_value=fixture_enabled_features_response) result = feature_control_util.check_feature_control_for_vendor( test_vendor, test_feature_control) assert result is True def test_check_feature_control_for_vendor_success_with_false( mocker, test_vendor, fixture_enabled_features_response): """Test feature control for vendor when it returns false.""" feature_control = 'Test new feature' mocker.patch.object( ows_account, 'get_enabled_vendor_features_control', return_value=fixture_enabled_features_response) result = feature_control_util.check_feature_control_for_vendor( test_vendor, feature_control) assert result is False def test_check_feature_control_for_vendor_fail( mocker, test_vendor, test_feature_control, fixture_enabled_features_failure): """Test feature control for vendor when fails.""" mocker.patch.object( ows_account, 'get_enabled_vendor_features_control', return_value=fixture_enabled_features_failure) result = feature_control_util.check_feature_control_for_vendor( test_vendor, test_feature_control) assert result.status == 403 Response = namedtuple('Response', 'message status') @patch('product.utils.feature_control_util.pythonfeatures') @pytest.mark.parametrize( 'feature, response, expected', [ ('yes_please', Response('enabled', 200), True), ('i_have_this', Response('enabled', 200), True), ('no_thanks', Response('control', 200), False), ] ) def test_is_feature_enabled_without_context( features_mock, feature, response, expected): """Test checking if a feature is enabled.""" features_mock.get_single_feature_by_attributes.return_value = response result = feature_control_util.is_feature_enabled(feature) assert result == expected features_mock.get_single_feature_by_attributes.assert_called_with( feature, {} ) @patch('product.utils.feature_control_util.pythonfeatures') @pytest.mark.parametrize( 'feature, response, expected', [ ('yes_please', Response('enabled', 200), True), ('i_have_this', Response('enabled', 200), True), ('no_thanks', Response('control', 200), False), ] ) def test_is_feature_enabled_with_context( features_mock, feature, response, expected): """Test checking if a feature is enabled.""" mocked_context = MagicMock() features_mock.get_single_feature.return_value = response result = feature_control_util.is_feature_enabled(feature, mocked_context) assert result == expected features_mock.get_single_feature.assert_called_with( feature, mocked_context )