示例#1
0
def test_chalice_swagger_generator_parameters():
    app = Chalice(app_name="leangle_tests")

    @app.route('/the_works')
    @leangle.describe.parameter(
        name='body',
        _in='body',
        description='Gimme all you got.',
        schema='TheWorksSchema',
    )
    @leangle.describe.response('201', description="All-dressed, baby.")
    def the_works():
        """Just do it."""
        return {"hello": "world"}

    swagger_gen = SwaggerGenerator(
        region='unit_test',
        deployed_resources={'api_handler_arn': '123456'},
    )
    swagger = swagger_gen.generate_swagger(app)

    expected = {
        'name': 'body',
        'description': 'Gimme all you got.',
        'schema': {
            '$ref': '#/definitions/TheWorksSchema',
        },
        'in': 'body',
    }

    assert swagger['paths']['/the_works']['get']['parameters'][0] == expected
示例#2
0
 def _create_resources_for_api(self, config, rest_api_id, lambda_arn):
     # type: (Config, str, str) -> Tuple[str, str, str]
     generator = SwaggerGenerator(self._aws_client.region_name, lambda_arn)
     swagger_doc = generator.generate_swagger(config.chalice_app)
     self._aws_client.update_api_from_swagger(rest_api_id, swagger_doc)
     api_gateway_stage = config.api_gateway_stage or DEFAULT_STAGE_NAME
     self._deploy_api_to_stage(rest_api_id, api_gateway_stage, lambda_arn)
     return rest_api_id, self._aws_client.region_name, api_gateway_stage
示例#3
0
 def _create_resources_for_api(self, config, rest_api_id):
     # type: (Config, str) -> Tuple[str, str, str]
     generator = SwaggerGenerator(self._aws_client.region_name,
                                  config.lambda_arn)
     swagger_doc = generator.generate_swagger(config.chalice_app)
     self._aws_client.update_api_from_swagger(rest_api_id, swagger_doc)
     stage = config.stage or 'dev'
     self._deploy_api_to_stage(rest_api_id, stage, config)
     return rest_api_id, self._aws_client.region_name, stage
示例#4
0
 def _first_time_deploy(self, config):
     # type: (Config) -> Tuple[str, str, str]
     generator = SwaggerGenerator(self._aws_client.region_name,
                                  config.lambda_arn)
     swagger_doc = generator.generate_swagger(config.chalice_app)
     rest_api_id = self._aws_client.import_rest_api(swagger_doc)
     stage = config.stage or 'dev'
     self._deploy_api_to_stage(rest_api_id, stage, config)
     return rest_api_id, self._aws_client.region_name, stage
示例#5
0
 def _create_resources_for_api(self, config, rest_api_id,
                               deployed_resources):
     # type: (Config, str, Dict[str, Any]) -> Tuple[str, str, str]
     generator = SwaggerGenerator(self._aws_client.region_name,
                                  deployed_resources)
     LOGGER.debug("Generating swagger document for rest API.")
     swagger_doc = generator.generate_swagger(config.chalice_app)
     self._aws_client.update_api_from_swagger(rest_api_id, swagger_doc)
     api_gateway_stage = config.api_gateway_stage
     self._deploy_api_to_stage(rest_api_id, api_gateway_stage,
                               deployed_resources)
     return rest_api_id, self._aws_client.region_name, api_gateway_stage
示例#6
0
def test_builtin_auth_with_scopes(sample_app):
    swagger_gen = SwaggerGenerator(
        region='us-west-2',
        deployed_resources={
            'api_handler_arn': 'arn:aws:lambda:mars-west-1:123456789'
            ':function:lambda_arn',
            'api_handler_name': 'api-dev',
            'lambda_functions': {
                'api-dev-myauth': {
                    'arn': 'arn:aws:lambda:mars-west-1:123456789'
                    ':function:auth_arn',
                    'type': 'authorizer',
                }
            }
        })

    @sample_app.authorizer(name='myauth',
                           ttl_seconds=10,
                           execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth',
                      authorizer=auth.with_scopes(["write:test", "read:test"]))
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    single_method = doc['paths']['/auth']['get']
    assert single_method.get('security') == [{
        'myauth': ["write:test", "read:test"]
    }]
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['myauth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type':
            'token',
            'authorizerCredentials':
            'arn:role',
            'authorizerResultTtlInSeconds':
            10,
            'authorizerUri': ('arn:aws:apigateway:us-west-2:'
                              'lambda:path/2015-03-31/functions'
                              '/arn:aws:lambda:mars-west-1:123456789:'
                              'function:auth_arn/invocations'),
        }
    }
示例#7
0
 def _first_time_deploy(self, config, lambda_arn):
     # type: (Config, str) -> Tuple[str, str, str]
     generator = SwaggerGenerator(self._aws_client.region_name, lambda_arn)
     swagger_doc = generator.generate_swagger(config.chalice_app)
     # The swagger_doc that's generated will contain the "name" which is
     # used to set the name for the restAPI.  API Gateway allows you
     # to have multiple restAPIs with the same name, they'll have
     # different restAPI ids.  It might be worth creating unique names
     # for each rest API, but that would require injecting chalice stage
     # information into the swagger generator.
     rest_api_id = self._aws_client.import_rest_api(swagger_doc)
     api_gateway_stage = config.api_gateway_stage or DEFAULT_STAGE_NAME
     self._deploy_api_to_stage(rest_api_id, api_gateway_stage, lambda_arn)
     return rest_api_id, self._aws_client.region_name, api_gateway_stage
示例#8
0
def test_will_default_to_function_name_for_auth(sample_app):
    swagger_gen = SwaggerGenerator(
        region='us-west-2',
        deployed_resources={
            'api_handler_arn': 'arn:aws:lambda:mars-west-1:123456789'
            ':function:lambda_arn',
            'api_handler_name': 'api-dev',
            'lambda_functions': {
                'api-dev-auth': {
                    'arn': 'arn:aws:lambda:mars-west-1:123456789'
                    ':function:auth_arn',
                    'type': 'authorizer',
                }
            }
        })

    # No "name=" kwarg provided should default
    # to a name of "auth".
    @sample_app.authorizer(ttl_seconds=10, execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth', authorizer=auth)
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    single_method = doc['paths']['/auth']['get']
    assert single_method.get('security') == [{'auth': []}]
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['auth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type':
            'token',
            'authorizerCredentials':
            'arn:role',
            'authorizerResultTtlInSeconds':
            10,
            'authorizerUri': ('arn:aws:apigateway:us-west-2:lambda:path'
                              '/2015-03-31/functions/'
                              'arn:aws:lambda:mars-west-1:123456789:function'
                              ':auth_arn/invocations'),
        }
    }
示例#9
0
def test_builtin_auth(sample_app):
    swagger_gen = SwaggerGenerator(region='us-west-2',
                                   deployed_resources={
                                       'api_handler_arn': 'lambda_arn',
                                       'api_handler_name': 'api-dev',
                                       'lambda_functions': {
                                           'api-dev-myauth': {
                                               'arn': 'auth_arn',
                                               'type': 'authorizer',
                                           }
                                       }
                                   })

    @sample_app.authorizer(name='myauth',
                           ttl_seconds=10,
                           execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth', authorizer=auth)
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['myauth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type':
            'token',
            'authorizerCredentials':
            'arn:role',
            'authorizerResultTtlInSeconds':
            10,
            'authorizerUri': ('arn:aws:apigateway:us-west-2:lambda:path'
                              '/2015-03-31/functions/auth_arn/invocations'),
        }
    }
示例#10
0
def test_will_default_to_function_name_for_auth(sample_app):
    swagger_gen = SwaggerGenerator(
        region='us-west-2',
        deployed_resources={
            'api_handler_arn': 'lambda_arn',
            'api_handler_name': 'api-dev',
            'lambda_functions': {
                'api-dev-auth': {
                    'arn': 'auth_arn',
                    'type': 'authorizer',
                }
            }
        }
    )

    # No "name=" kwarg provided should default
    # to a name of "auth".
    @sample_app.authorizer(ttl_seconds=10, execution_role='arn:role')
    def auth(auth_request):
        pass

    @sample_app.route('/auth', authorizer=auth)
    def foo():
        pass

    doc = swagger_gen.generate_swagger(sample_app)
    assert 'securityDefinitions' in doc
    assert doc['securityDefinitions']['auth'] == {
        'in': 'header',
        'name': 'Authorization',
        'type': 'apiKey',
        'x-amazon-apigateway-authtype': 'custom',
        'x-amazon-apigateway-authorizer': {
            'type': 'token',
            'authorizerCredentials': 'arn:role',
            'authorizerResultTtlInSeconds': 10,
            'authorizerUri': ('arn:aws:apigateway:us-west-2:lambda:path'
                              '/2015-03-31/functions/auth_arn/invocations'),
        }
    }