class TestOpenApiEditor_is_valid(TestCase): @parameterized.expand([ param(OpenApiEditor.gen_skeleton()), # Dict can contain any other unrecognized properties param({ "openapi": "3.1.1", "paths": {}, "foo": "bar", "baz": "bar" }) # TODO check and update the regex accordingly # Fails for this: param({"openapi": "3.1.10", "paths": {}, "foo": "bar", "baz": "bar"}) ]) def test_must_work_on_valid_values(self, openapi): self.assertTrue(OpenApiEditor.is_valid(openapi)) @parameterized.expand([ ({}, "empty dictionary"), ([1, 2, 3], "array data type"), ({ "paths": {} }, "missing openapi property"), ({ "openapi": "hello" }, "missing paths property"), ({ "openapi": "hello", "paths": [1, 2, 3] }, "array value for paths property"), ]) def test_must_fail_for_invalid_values(self, data, case): self.assertFalse( OpenApiEditor.is_valid(data), "openapi dictionary with {} must not be valid".format(case))
def on_before_transform_template(self, template_dict): """ Hook method that gets called before the SAM template is processed. The template has passed the validation and is guaranteed to contain a non-empty "Resources" section. :param dict template_dict: Dictionary of the SAM template :return: Nothing """ template = SamTemplate(template_dict) for api_type in [ SamResourceType.Api.value, SamResourceType.HttpApi.value ]: for logicalId, api in template.iterate({api_type}): if api.properties.get("DefinitionBody") or api.properties.get( "DefinitionUri"): continue if api_type is SamResourceType.HttpApi.value: # If "Properties" is not set in the template, set them here if not api.properties: template.set(logicalId, api) api.properties[ "DefinitionBody"] = OpenApiEditor.gen_skeleton() if api_type is SamResourceType.Api.value: api.properties[ "DefinitionBody"] = SwaggerEditor.gen_skeleton() api.properties["__MANAGE_SWAGGER"] = True
def test_auth_missing_default_auth(self): self.kwargs["auth"] = self.authorizers self.kwargs["auth"]["DefaultAuthorizer"] = "DNE" self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() with pytest.raises(InvalidResourceException): HttpApiGenerator(**self.kwargs)._construct_http_api()
def test_auth_invalid_auth_strategy(self): self.kwargs["auth"] = {"Authorizers": {"Auth1": "invalid"}} self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() with pytest.raises(InvalidResourceException): HttpApiGenerator(**self.kwargs)._construct_http_api()
def test_auth_wrong_properties(self): self.kwargs["auth"] = {"Invalid": "auth"} self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() with pytest.raises(InvalidResourceException): HttpApiGenerator(**self.kwargs)._construct_http_api()
def test_auth_novalue_default_does_not_raise(self): self.kwargs["auth"] = self.authorizers self.kwargs["auth"]["DefaultAuthorizer"] = {"Ref": "AWS::NoValue"} self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() HttpApiGenerator(**self.kwargs)._construct_http_api()
def test_auth_intrinsic_default_auth(self): self.kwargs["auth"] = self.authorizers self.kwargs["auth"]["DefaultAuthorizer"] = {"Ref": "SomeValue"} self.kwargs["definition_body"] = OpenApiEditor.gen_skeleton() with pytest.raises(InvalidResourceException): HttpApiGenerator(**self.kwargs)._construct_http_api()