def test_assume_identity(): b3 = boto3_for_some_secrets_testing( ) # this sets things up with some_secret_string in the SecretsManager with mock.patch.object(secrets_utils_module, 'boto3', b3): with override_environ(IDENTITY=some_secret_identity): identity = assume_identity() assert identity == some_secret_table # this is the parsed form of some_secret_string
def test_mock_secrets_table_as_dict_and_assume_identity_equivalence(): b3 = boto3_for_some_secrets_testing() with mock.patch.object(secrets_utils_module, 'boto3', b3): with override_environ(IDENTITY=some_secret_identity): identity = assume_identity() secret_table = SecretsTable.find_application_secrets_table() secret_table_as_dict = secret_table.as_dict() assert identity == secret_table_as_dict == some_secret_table
def test_script_catch_errors(): with shown_output() as shown: with pytest.raises(SystemExit) as caught: with script_catch_errors(): print("foo") # foo = sys.exc_info() assert caught.value.code == 0 assert shown.lines == [] with shown_output() as shown: with pytest.raises(SystemExit) as caught: with script_catch_errors(): raise Exception("Foo") assert caught.value.code == 1 assert shown.lines == [ERROR_HERALD, "Exception: Foo"] # Not enough to override env var DEBUG_CGAP, since the module is already loaded and the env var's value # is already seen and parsed. We must change module value at this point if we want to exercise the relevant # code path. with mock.patch.object(utils_module, "DEBUG_CGAP", True): with shown_output() as shown: with pytest.raises(Exception) as caught: with script_catch_errors(): raise Exception("Foo") assert shown.lines == [] # The value was not trapped, so not shown # Since we couldn't override the setting of the variable in the preceding code, we at least verify that setting # the value would have produced the right effect. with override_environ(DEBUG_CGAP=None): assert not environ_bool("DEBUG_CGAP") # Unset, the value is False with override_environ(DEBUG_CGAP=""): assert not environ_bool( "DEBUG_CGAP") # Set to empty string, the value is False with override_environ(DEBUG_CGAP="FALSE"): assert not environ_bool( "DEBUG_CGAP") # Set to "FALSE", the value is False with override_environ(DEBUG_CGAP="TRUE"): assert environ_bool("DEBUG_CGAP") # Set to "TRUE", the value is True
def test_get_stage_info(self): # Testing assumes CHECK_RUNNER env var is not set and gets a default, since a developer # might have it set in their debugging environment in a way that thwartsthe test. with override_environ(CHECK_RUNNER=None): os.environ['chalice_stage'] = 'test' stage_obj = stage.Stage('placeholder_prefix') assert stage_obj.get_stage() == 'dev' assert stage.Stage.get_stage( ) == 'dev' # this one is also a classmethod assert 'dev' in stage_obj.get_runner_name() assert 'test' in stage_obj.get_queue_name()
def mocked_s3utils(beanstalks=None, require_sse=False, other_access_key_names=None): """ This context manager sets up a mock version of boto3 for use by s3_utils and ff_utils during the context of its test. It also sets up the S3_ENCRYPT_KEY environment variable with a sample value for testing, and it sets up a set of mocked beanstalks for fourfront-foo and fourfront-bar, so that s3_utils will not get confused when it does discovery operations to find them. """ if beanstalks is None: beanstalks = TestScenarios.DEFAULT_BEANSTALKS # First we make a mocked boto3 that will use an S3 mock with mock server side encryption. s3_class = (make_mock_boto_s3_with_sse( beanstalks=beanstalks, other_access_key_names=other_access_key_names) if require_sse else MockBotoS3Client) mock_boto3 = MockBoto3( s3=s3_class, elasticbeanstalk=make_mock_boto_eb_client_class(beanstalks=beanstalks)) # Now we arrange that both s3_utils and ff_utils modules share the illusion that our mock IS the boto3 library with mock.patch.object(s3_utils, "boto3", mock_boto3): with mock.patch.object(ff_utils, "boto3", mock_boto3): with mock.patch.object(beanstalk_utils, "boto3", mock_boto3): with mock.patch.object(base, "boto3", mock_boto3): with mock.patch.object( s3_utils.EnvManager, "fetch_health_page_json") as mock_fetcher: # This is all that's needed for s3Utils to initialize an EnvManager. # We might have to add more later. def mocked_fetch_health_page_json( url, use_urllib=True): ignored(use_urllib) # we don't test this m = re.match(r'.*(fourfront-[a-z0-9-]+)(?:[.]|$)', url) if m: env_name = m.group(1) return make_mock_health_page(env_name) else: raise NotImplementedError( f"Mock can't handle URL: {url}") mock_fetcher.side_effect = mocked_fetch_health_page_json # The mocked encrypt key is expected by various tools in the s3_utils module to be supplied # as an environment variable (i.e., in os.environ), so this sets up that environment variable. if require_sse: with override_environ( S3_ENCRYPT_KEY=s3_class.SSE_ENCRYPT_KEY): yield else: yield