"""Tests for Feature Flag util.""" from os import environ from feature_flags.ows_features \ import NewVariant, OwsFeatures, UnexpectedResponse, UserType from pytest import fixture, mark, raises from requests.exceptions import InvalidSchema BASE_QA_URL = environ['BASE_QA_URL'] @fixture(autouse=True) def setup_environment(): """Resets environment variables to state from start of run.""" environ['BASE_QA_URL'] = BASE_QA_URL @fixture def user_id(): """Returns a mock user ID.""" return 99999999 @fixture def workstation_user(): """Return a UserType.WORKSTATION enum value for tests.""" return UserType.WORKSTATION @fixture def test_feature(): """Return a test feature to update.""" return 'test_feature_do_not_remove' @mark.parametrize('variant', list(NewVariant)) @mark.parametrize('user', list(UserType)) def test_happy_path(variant, user, test_feature, user_id): """Happy path feature test of Feature Flag Util. If we don't raise FeatureNotUpdated, test is a success. """ OwsFeatures(test_feature, user_id, variant, user).update_feature() def test_no_qa_url(workstation_user, user_id, test_feature): """Try to enable a feature with no QA URL specified.""" with raises(ValueError): del environ['BASE_QA_URL'] OwsFeatures(test_feature, user_id, NewVariant.ENABLED, workstation_user).update_feature() def test_bad_qa_url(workstation_user, user_id, test_feature): """Try to enable a feature with a bunk QA URL specified.""" with raises(InvalidSchema): environ['BASE_QA_URL'] = 'bunkurl' OwsFeatures(test_feature, user_id, NewVariant.ENABLED, workstation_user).update_feature() def test_no_user_id(workstation_user, test_feature): """Try to enable a feature with a no user ID specified.""" with raises(ValueError): OwsFeatures(test_feature, None, NewVariant.ENABLED, workstation_user).update_feature() def test_no_feature_name(workstation_user, user_id): """Try to enable a feature with a no feature specified.""" with raises(ValueError): OwsFeatures(None, user_id, NewVariant.ENABLED, workstation_user).update_feature() def test_no_variant(workstation_user, user_id, test_feature): """Try to enable a feature with a no variant specified.""" with raises(ValueError): OwsFeatures(test_feature, user_id, None, workstation_user).update_feature() def test_no_user_type(user_id, test_feature): """Try to enable a feature with a no user type specified.""" with raises(ValueError): OwsFeatures(test_feature, user_id, NewVariant.ENABLED, None).update_feature() def test_string_variant(workstation_user, user_id, test_feature): """Try to enable a feature with a string variant specified.""" with raises(TypeError): OwsFeatures(test_feature, user_id, 'enabled', workstation_user).update_feature() def test_string_user_id(workstation_user, test_feature): """Try to enable a feature with a string user ID specified.""" with raises(TypeError): OwsFeatures(test_feature, '16888', NewVariant.ENABLED, workstation_user).update_feature() def test_string_user_type(user_id, test_feature): """Try to enable a feature with a string user type specified.""" with raises(TypeError): OwsFeatures(test_feature, user_id, NewVariant.ENABLED, 'alw').update_feature() def test_bad_feature_name(workstation_user, user_id): """Try to enable a feature with a no feature specified.""" with raises(UnexpectedResponse): OwsFeatures('FEETURE', user_id, NewVariant.ENABLED, workstation_user).update_feature()