def test_non_json_keys_omitted(): class WeirdObject: pass loader = load_from_dict( config_convention=dict( enabled="true", ), weird_thing=WeirdObject(), ) secrets = load_from_dict(dict()) partitioned_loader = load_config_and_secrets(config=loader, secrets=secrets) graph = create_object_graph(name="example", testing=True, loader=partitioned_loader) graph.use("config_convention") client = graph.flask.test_client() response = client.get( "/api/config", ) assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, has_entries(config_convention=dict(enabled="true"))) assert_that("weird_thing" not in data.keys())
def test_config_discovery(): """ Config-minus secrets is shareable. """ config = load_from_dict( config_convention=dict( enabled="true", ), ) secrets = load_from_dict( config_convention=dict( enabled="true", ), ) paritioned_loader = load_config_and_secrets(config, secrets) graph = create_object_graph(name="example", testing=True, loader=paritioned_loader) graph.use("config_convention") client = graph.flask.test_client() response = client.get( "/api/config", ) assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, has_entries(config_convention=dict(enabled="true")))
def test_load_partitioned(): metadata = Metadata("foo") loader = load_partitioned( foo=load_from_dict( foo=dict(value="foo", ), baz=dict( value="foo", foo=dict(value="foo", ), ), ), bar=load_from_dict( bar=dict(value="bar", ), baz=dict( value="bar", bar=dict(value="bar", ), ), ), ) config = loader(metadata) # configuration is loaded as a usual merge assert_that( config, is_( equal_to( dict( foo=dict(value="foo", ), bar=dict(value="bar", ), baz=dict( value="bar", foo=dict(value="foo", ), bar=dict(value="bar", ), ), )))) # loader retains partitions assert_that( loader.foo, is_( equal_to( dict( foo=dict(value="foo", ), baz=dict(foo=dict(value="foo", ), ), )))) assert_that( loader.bar, is_( equal_to( dict( bar=dict(value="bar", ), baz=dict( value="bar", bar=dict(value="bar", ), ), ))))
def test_health_check_required_check_failed(): """ Should return 503 on health check failure. """ loader = load_from_dict( health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() graph.health_convention.checks["foo"] = _health_check(False) response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(503))) assert_that(response.json, is_(equal_to({ "name": "example", "ok": False, "checks": { "foo": { "message": "failure!", "ok": False, }, }, })))
def setup(self): self.tmp_file = NamedTemporaryFile() loader = load_from_dict(sqlite=dict(paths=dict( foo=self.tmp_file.name, ), ), ) self.graph = create_object_graph("example", testing=True, loader=loader)
def test_health_check_custom_checks(optional_check, full_check, expect_check_response): loader = load_from_dict( health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() if optional_check: graph.health_convention.optional_checks["foo"] = _health_check() else: graph.health_convention.checks["foo"] = _health_check() response = client.get("/api/health", query_string=dict(full=full_check)) assert_that(response.status_code, is_(equal_to(200))) expected_response = { "name": "example", "ok": True, } if expect_check_response: expected_response.update({ "checks": { "foo": { "message": "hi", "ok": True, }, }, }) assert_that(response.json, is_(equal_to(expected_response)))
def test_get_resource_with_cache_disabled_on_instance(self): config = dict(resource_cache=dict(enabled=True, ), ) graph = create_object_graph("microcosm", testing=True, loader=load_from_dict(config)) graph.use( "opaque", "resource_cache", "sqs_message_context", ) graph.lock() uri = "http://localhost" message = dict(uri=uri, ) json_data = dict(foo="bar", bar="baz") with patch("microcosm_pubsub.handlers.uri_handler.get") as mocked_get: mocked_get.return_value = MockResponse( status_code=200, json_data=json_data, ) handler = URIHandler(graph, resource_cache_enabled=False) handler.get_resource(message, uri) assert_that(handler.resource_cache, is_(None)) mocked_get.assert_called_with( uri, headers=dict(), )
def test_build_info_from_environ(): """ Environment variables can set build info. """ loader = load_from_dict(build_info_convention=dict( build_num="1", sha1="b08fd4a3a685c43521a931ba63872b43ec7c6bda", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("build_info_convention") client = graph.flask.test_client() response = client.get("/api/build_info") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that( data, is_( equal_to( dict( build_num="1", sha1="b08fd4a3a685c43521a931ba63872b43ec7c6bda", ))))
def test_health_check_custom_check(): """ Should return Custom health check results. """ loader = load_from_dict( health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() graph.health_convention.checks["foo"] = lambda graph: "hi" response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, is_(equal_to({ "name": "example", "ok": True, "checks": { "foo": { "message": "hi", "ok": True, }, }, })))
def test_health_check_custom_check(): """ Should return Custom health check results. """ loader = load_from_dict(health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() graph.health_convention.checks["foo"] = lambda graph: "hi" response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that( data, is_( equal_to({ "name": "example", "ok": True, "checks": { "foo": { "message": "hi", "ok": True, }, }, })))
def setup(self): loader = load_from_dict( secret=dict(postgres=dict(host=environ.get( "MICROCOSM_EVENTSOURCE__POSTGRES__HOST", "localhost"), ), ), postgres=dict(host=environ.get( "MICROCOSM_EVENTSOURCE__POSTGRES__HOST", "localhost"), ), sns_producer=dict(mock_sns=False, ), ) self.graph = create_object_graph( "microcosm_eventsource", loader=loader, root_path=dirname(__file__), testing=True, ) self.graph.use( "session_factory", "task_store", "task_event_store", "task_event_controller", "task_crud_routes", ) self.client = self.graph.flask.test_client() recreate_all(self.graph) with SessionContext(self.graph), transaction(): self.task = Task().create() self.graph.sns_producer.sns_client.reset_mock()
def test_health_check_custom_check_failed(): """ Should return 503 on health check failure. """ loader = load_from_dict( health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() def fail(graph): raise Exception("failure!") graph.health_convention.checks["foo"] = fail response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(503))) data = loads(response.get_data().decode("utf-8")) assert_that(data, is_(equal_to({ "name": "example", "ok": False, "checks": { "foo": { "message": "failure!", "ok": False, }, }, })))
def setup(self): self.tmp_file = NamedTemporaryFile() loader = load_from_dict(sqlite=dict(paths=dict( example=self.tmp_file.name, ), ), ) self.graph = create_object_graph("example", testing=True, loader=loader) self.builder = self.graph.sqlite_builder self.dog_store = DogStore() self.person_store = PersonStore() Example.create_all(self.graph) self.people = csv( dedent(""" id,first,last 1,Stephen,Curry 2,Klay,Thompson """)) self.dogs = csv( dedent(""" id,name,owner_id 1,Rocco,2 2,Reza,1 3,Rookie,1 """))
def setup(self): self.tmp_file = NamedTemporaryFile() loader = load_from_dict(sqlite=dict(paths=dict( example=self.tmp_file.name, ), ), ) self.graph = create_object_graph("example", testing=True, loader=loader) self.dumper = self.graph.sqlite_dumper self.person_store = PersonStore() Example.create_all(self.graph) self.outfile = StringIO() with Example.new_context(self.graph): self.person_store.create( Person( id="1", first="Stephen", last="Curry", )) self.person_store.create( Person( id="2", first="Klay", last="Thompson", )) self.person_store.session.commit() self.context = Person.new_context(self.graph).open()
def write(args): loader = load_from_dict( sns_producer=dict( profile_name=args.profile, region_name=args.region, ), sns_topic_arns=dict(default=args.topic_arn, ), ) graph = create_object_graph("pubsub", debug=args.debug, loader=loader) graph.use("pubsub_message_schema_registry") graph.use("sns_topic_arns") graph.use("sns_producer") graph.lock() if args.media_type.startswith("application"): media_type = args.media_type else: media_type = created(args.media_type) message_id = graph.sns_producer.produce( media_type=media_type, uri=args.uri, ) print("Wrote SNS message: {}".format(message_id)) # noqa
def test_health_check_custom_check_failed(): """ Should return 503 on health check failure. """ loader = load_from_dict(health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() def fail(graph): raise Exception("failure!") graph.health_convention.checks["foo"] = fail response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(503))) data = loads(response.get_data().decode("utf-8")) assert_that( data, is_( equal_to({ "name": "example", "ok": False, "checks": { "foo": { "message": "failure!", "ok": False, }, }, })))
def loader(self): """ Define the object graph config loader. """ return load_each( load_from_dict(self.defaults), load_from_environ, )
def loader(self): """ Define the object graph config loader. """ return load_each( load_from_dict(self.defaults), load_from_environ, )
def test_two_stage_loader_basic(): metadata = Metadata("foo") initial_loader = load_from_dict(foo="bar", ) loader = two_stage_loader(initial_loader, secondary_loader) config = loader(metadata) assert_that(config, is_(equal_to(dict( foo="bar", bar="bazman", ))))
def create_statsd_client(args): loader = load_from_dict(dict( metrics=dict( host=args.host, ), )) graph = create_object_graph("example", loader=loader) graph.use("metrics") graph.lock() return graph.metrics
def test_port_forwarding(self): loader = load_from_dict() client = self.init(loader) response = client.get( "/", headers={ "X-Forwarded-Port": "8080", }, ) assert_that(response.status_code, is_(equal_to(200))) assert_that(response.data, is_(equal_to(b"http://localhost:8080/")))
def test_port_forwarding(self): loader = load_from_dict() client = self.init(loader) response = client.get( "/", headers={ "X-Forwarded-Port": "8080", }, ) assert_that(response.status_code, is_(equal_to(200))) assert_that(response.data, is_(equal_to(b"http://localhost:8080/")))
def create_for_testing(cls, loader=None, **kwargs): mock_config = load_from_dict(sns_producer=dict(mock_sns=False, ), ) if loader is None: loader = mock_config else: loader = load_each(loader, mock_config) return super().create_for_testing(sqs_queue_url="queue", loader=loader, envelope=None, stdin=False, **kwargs)
def load_from_dynamodb(environment=None): """ Fluent shorthand for the whole brevity thing. """ if environment is None: try: environment = environ["MICROCOSM_ENVIRONMENT"] except KeyError: # noop return load_from_dict(dict()) return VersionedEncryptedDynamoDBLoader(environment)
def load_from_dynamodb(environment=None): """ Fluent shorthand for the whole brevity thing. """ if environment is None: try: environment = environ["MICROCOSM_ENVIRONMENT"] except KeyError: # noop return load_from_dict(dict()) return VersionedEncryptedDynamoDBLoader(environment)
def test_host_override(self): loader = load_from_dict(port_forwarding=dict(host="service", )) client = self.init(loader) response = client.get( "/", headers={ "X-Forwarded-Port": "8080", }, ) assert_that(response.status_code, is_(equal_to(200))) assert_that(response.data, is_(equal_to(b"http://service/")))
def test_load_config_and_secrets(): metadata = Metadata("foo") loader = load_config_and_secrets( config=load_from_dict(credentials=dict(username="******", ), ), secrets=load_from_dict(credentials=dict(password="******", ), ), ) config = loader(metadata) # configuration is loaded as a usual merge assert_that( config, is_( equal_to( dict(credentials=dict( username="******", password="******", ), )))) assert_that(loader.config, is_(equal_to(dict(credentials=dict(username="******", ), )))) assert_that(loader.secrets, is_(equal_to(dict(credentials=dict(password="******", ), ))))
def setup(self): loader = load_from_dict(route=dict(converters=["cba"], ), ) self.graph = create_object_graph(name="example", testing=True, loader=loader) assert_that(self.graph.config.route.converters, contains_inanyorder("uuid", "cba")) self.person_ns = Namespace( subject=Person, # use custom identifier type identifier_type="cba", ) configure_crud(self.graph, self.person_ns, PERSON_MAPPINGS) self.client = self.graph.flask.test_client()
def test_get_whitelisted_resource_with_cache_enabled_and_cache_miss_and_custom_ttl(self): config = dict( resource_cache=dict( enabled=True, ), ) graph = create_object_graph("microcosm", testing=True, loader=load_from_dict(config)) graph.use( "opaque", "resource_cache", "sqs_message_context", ) graph.lock() uri = "https://service.env.globality.io/api/v2/project_event/0598355c-5b19-49bd-a755-146204220a5b" media_type = "application/vnd.globality.pubsub._.created.project_event.project_brief_submitted" message = dict( uri=uri, mediaType=media_type, ) json_data = dict(foo="bar", bar="baz") with patch("microcosm_pubsub.handlers.uri_handler.get") as mocked_get: mocked_get.return_value = MockResponse( status_code=200, json_data=json_data, ) handler = URIHandler(graph, resource_cache_ttl=100) with patch.object(handler.resource_cache, "get") as mocked_cache_get: mocked_cache_get.return_value = None with patch.object(handler.resource_cache, "set") as mocked_cache_set: handler.get_resource(message, uri) mocked_get.assert_called_with( uri, headers=dict(), ) # Nb. cache get was attempted mocked_cache_get.assert_called_with( uri, ) # Nb. cache set was called due to cache miss mocked_cache_set.assert_called_with( uri, json_data, ttl=100, )
def setup(self): loader = load_from_dict( route=dict( converters=[ "cba" ], ), ) self.graph = create_object_graph(name="example", testing=True, loader=loader) assert_that(self.graph.config.route.converters, contains_inanyorder("uuid", "cba")) self.person_ns = Namespace( subject=Person, # use custom identifier type identifier_type="cba", ) configure_crud(self.graph, self.person_ns, PERSON_MAPPINGS) self.client = self.graph.flask.test_client()
def test_host_override(self): loader = load_from_dict( port_forwarding=dict( host="service", ) ) client = self.init(loader) response = client.get( "/", headers={ "X-Forwarded-Port": "8080", }, ) assert_that(response.status_code, is_(equal_to(200))) assert_that(response.data, is_(equal_to(b"http://service/")))
def test_two_stage_loader_prefer_primary(): metadata = Metadata("foo") initial_loader = load_from_dict( foo="bar", bar="hello", ) loader = two_stage_loader( initial_loader, secondary_loader, prefer_secondary=False, ) config = loader(metadata) assert_that(config, is_(equal_to(dict( foo="bar", bar="hello", ))))
def test_config_discovery_only_works_for_paritioned_loaders(): """ Config sharing is disabled if secrets are not labelled. """ loader = load_from_dict(config_convention=dict(enabled="true", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("config_convention") client = graph.flask.test_client() response = client.get("/api/config", ) assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that( data, has_entries(msg="Config sharing disabled for non-partioned loader"))
def read(args): loader = load_from_dict(sqs_consumer=dict( profile_name=args.profile, region_name=args.region, sqs_queue_url=args.queue_url, ), ) graph = create_object_graph("pubsub", debug=args.debug, loader=loader) graph.use("sqs_consumer") graph.lock() for message in graph.sqs_consumer.consume(): print("Read SQS message: {} ({})".format( # noqa message.message_id, message.approximate_receive_count, )) if args.nack: message.nack(args.nack_timeout) else: message.ack()
def test_collaboration(): """ All microcosm collaborators should have access to the same opaque context. """ # set up a parent collaborator that uses a child collaborator @binding("parent_collaborator") class Parent(object): def __init__(self, graph): self.child_collaborator = graph.child_collaborator def __call__(self): return self.child_collaborator() @binding("child_collaborator") class Child(object): def __init__(self, graph): self.opaque = graph.opaque def __call__(self): return self.opaque.as_dict() # create the object graph with both collaborators and opaque data graph = create_object_graph( "test", testing=True, loader=load_from_dict(opaque={THIS: VALUE})) graph.use( "child_collaborator", "opaque", "parent_collaborator", ) graph.lock() # we should be able to initialize the opaque data and observe it from the collaborators decorated_func = graph.opaque.initialize( example_func, OTHER, OTHER )(graph.parent_collaborator.__call__) assert_that(graph.opaque.as_dict(), is_(equal_to({THIS: VALUE}))) assert_that(decorated_func(), is_(equal_to(example_func(OTHER, OTHER)))) assert_that(graph.opaque.as_dict(), is_(equal_to({THIS: VALUE})))
def test_health_check(): """ Default health check returns OK. """ loader = load_from_dict(health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, is_(equal_to({ "name": "example", "ok": True, })))
def create_for_testing(cls, loader=None, **kwargs): mock_config = load_from_dict( sns_producer=dict( mock_sns=False, ), ) if loader is None: loader = mock_config else: loader = load_each(loader, mock_config) return super().create_for_testing( sqs_queue_url="queue", loader=loader, envelope=None, stdin=False, **kwargs )
def test_get_non_whitelisted_resource_with_cache_enabled(self): config = dict(resource_cache=dict(enabled=True, ), ) graph = create_object_graph("microcosm", testing=True, loader=load_from_dict(config)) graph.use( "opaque", "resource_cache", "sqs_message_context", ) graph.lock() # Nb. changed events should not be whitelisted for caching uri = "https://service.env.globality.io/api/v2/project_event/0598355c-5b19-49bd-a755-146204220a5b" media_type = "application/vnd.globality.pubsub._.changed.project_event.project_brief_submitted" message = dict( uri=uri, mediaType=media_type, ) json_data = dict(foo="bar", bar="baz") with patch("microcosm_pubsub.handlers.uri_handler.get") as mocked_get: mocked_get.return_value = MockResponse( status_code=200, json_data=json_data, ) handler = URIHandler(graph) with patch.object(handler.resource_cache, "get") as mocked_cache_get: mocked_cache_get.return_value = None with patch.object(handler.resource_cache, "set") as mocked_cache_set: handler.get_resource(message, uri) mocked_get.assert_called_with( uri, headers=dict(), ) # Nb. cache get/set were not called due to resource uri not being whitelisted assert_that(mocked_cache_get.called, is_(equal_to(False))) assert_that(mocked_cache_set.called, is_(equal_to(False)))
def test_no_prefix_no_version_path(): loader = load_from_dict( dict( # We want our routes to come directly after the root / build_route_path=dict(prefix=""), )) graph = create_object_graph(name="example", testing=True, loader=loader) ns = Namespace( subject=Person, version="", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) # Test that in a no prefix, no version case we still get a leading slash in our paths assert_that("/person" in swagger_schema["paths"]) assert_that(swagger_schema["basePath"], equal_to("/"))
def test_health_check(): """ Default health check returns OK. """ loader = load_from_dict( health_convention=dict( include_build_info="false", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("health_convention") client = graph.flask.test_client() response = client.get("/api/health") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, is_(equal_to({ "name": "example", "ok": True, })))
def test_no_prefix_no_version_path(): loader = load_from_dict(dict( # We want our routes to come directly after the root / build_route_path=dict( prefix="" ), )) graph = create_object_graph(name="example", testing=True, loader=loader) ns = Namespace( subject=Person, version="", ) configure_crud(graph, ns, PERSON_MAPPINGS) with graph.flask.test_request_context(): operations = list(iter_endpoints(graph, match_function)) swagger_schema = build_swagger(graph, ns, operations) # Test that in a no prefix, no version case we still get a leading slash in our paths assert_that("/person" in swagger_schema["paths"]) assert_that(swagger_schema["basePath"], equal_to("/"))
def test_build_info_from_environ(): """ Environment variables can set build info. """ loader = load_from_dict( build_info_convention=dict( build_num="1", sha1="b08fd4a3a685c43521a931ba63872b43ec7c6bda", ), ) graph = create_object_graph(name="example", testing=True, loader=loader) graph.use("build_info_convention") client = graph.flask.test_client() response = client.get("/api/build_info") assert_that(response.status_code, is_(equal_to(200))) data = loads(response.get_data().decode("utf-8")) assert_that(data, is_(equal_to(dict( build_num="1", sha1="b08fd4a3a685c43521a931ba63872b43ec7c6bda", ))))