"""Features tests.""" from unittest.mock import call from flask import g as flask_global from oto import response import pytest from pythonfeatures import pythonfeatures from pythonfeatures.constants import split as split_constants from product_digital import api from product_digital import features from product_digital.models import ows_account def test_is_feature_flag_enabled_true(mocker, test_request_context): """Check is_feature_flag_enabled is true when flag is enabled.""" with api.app.test_request_context(): mocker.patch.object(pythonfeatures, 'get_single_feature', return_value=response.Response( message=split_constants.FEATURE_ENABLED)) result = features.is_feature_flag_enabled( 'split_test' ) pythonfeatures.get_single_feature.assert_called_once_with( 'split_test', flask_global.request_context) assert result is True def test_is_feature_flag_enabled_false(mocker, test_request_context): """Check is_feature_flag_enabled is false when flag is disabled.""" with api.app.test_request_context(): mocker.patch.object(pythonfeatures, 'get_single_feature', return_value=response.Response( message=split_constants.FEATURE_DISABLED)) result = features.is_feature_flag_enabled( 'split_test' ) pythonfeatures.get_single_feature.assert_called_once_with( 'split_test', flask_global.request_context) assert result is False def test_is_feature_flag_enabled_for_vendor_true(mocker): """Check is_feature_flag_enabled_for_vendor is true when flag is enabled.""" mocker.patch.object(pythonfeatures, 'get_single_feature_by_attributes', return_value=response.Response( message=split_constants.FEATURE_ENABLED)) result = features.is_feature_flag_enabled_for_vendor( 'split_test', 123 ) pythonfeatures.get_single_feature_by_attributes.assert_called_once_with( 'split_test', {'vendor_id': 123}) assert result is True def test_is_feature_flag_enabled_for_vendor_false(mocker): """Check is_feature_flag_enabled_for_vendor is false when flag is disabled.""" mocker.patch.object(pythonfeatures, 'get_single_feature_by_attributes', return_value=response.Response( message=split_constants.FEATURE_DISABLED)) result = features.is_feature_flag_enabled_for_vendor( 'split_test', 123 ) pythonfeatures.get_single_feature_by_attributes.assert_called_once_with( 'split_test', {'vendor_id': 123}) assert result is False @pytest.mark.parametrize(( 'test_description', 'get_vendor_response', 'get_company_brand_response', 'expected_single_feature_by_attributes_attributes', 'get_single_feature_by_attributes_response', 'expected_result', ), [ ( 'on', {'owner': 'odd', 'assigned_to': None, 'assigned_reviewer': 101}, None, { 'vendor_id': str(1232), 'distribution_format_id': '1', 'company_brand_name': None, 'owner': 'odd', 'assigned_to': None, 'assigned_reviewer': '101' }, response.Response('enabled'), True, ), ( 'off', {'owner': 'odd', 'assigned_to': None, 'assigned_reviewer': 101}, None, { 'vendor_id': str(1232), 'distribution_format_id': '1', 'company_brand_name': None, 'owner': 'odd', 'assigned_to': None, 'assigned_reviewer': '101' }, response.Response('disabled'), False, ), ( 'null vendor', None, None, { 'vendor_id': str(1232), 'distribution_format_id': '1', 'company_brand_name': None, 'owner': None, 'assigned_to': None, 'assigned_reviewer': None }, response.Response('disabled'), False, ), ]) def test_is_rejections_from_content_review_enabled( mocker, test_request_context, test_description, get_vendor_response, get_company_brand_response, expected_single_feature_by_attributes_attributes, get_single_feature_by_attributes_response, expected_result, ): """Test is_rejections_from_content_review_enabled.""" mocker.patch.object( ows_account, 'get_vendor_company_brand', autospec=True, return_value=get_company_brand_response, ) mocker.patch.object( ows_account, 'get_vendor', autospec=True, return_value=get_vendor_response, ) mocker.patch.object( pythonfeatures, 'get_single_feature_by_attributes', autospec=True, return_value=get_single_feature_by_attributes_response ) result = features.is_rejections_from_content_review_enabled(1232, 1) assert ows_account.get_vendor_company_brand.mock_calls == [call(1232)] assert pythonfeatures.get_single_feature_by_attributes.mock_calls == [ call( 'content_get_rejections_for_workstation', expected_single_feature_by_attributes_attributes, )] assert result == expected_result