Esempio n. 1
0
def test_override(minimal_swagger_dict):
    class Byte(object):
        def __init__(self, x):
            self.x = x

        def __str__(self):
            return str(self.x)

        def __repr__(self):
            return '%s(%r)' % (self.__class__, self.x)

    byteformat = SwaggerFormat(
        format='byte',
        to_wire=lambda x: str(x),
        to_python=lambda x: Byte(x),
        validate=lambda x: isinstance(x, str),
        description=None,
    )

    number_spec = {'type': 'string', 'format': 'byte'}

    swagger_spec = Spec.from_dict(minimal_swagger_dict,
                                  config={'formats': [byteformat]})
    result = to_wire(swagger_spec, number_spec, '8bits')

    assert '8bits' == result
    assert isinstance(result, str)
    assert type(result) is StringType
Esempio n. 2
0
def cerise_client():
    # Disable Bravado warning about uri format not being registered
    # It's all done by frameworks, so we're not testing that here
    uri_format = SwaggerFormat(description='A Uniform Resource Identifier',
                               format='uri',
                               to_wire=lambda uri: uri,
                               to_python=lambda uri: uri,
                               validate=lambda uri_string: True)

    bravado_config = {'also_return_response': True, 'formats': [uri_format]}

    service = None
    start_time = time.perf_counter()
    cur_time = start_time
    while cur_time < start_time + 10:
        try:
            service = SwaggerClient.from_url(
                'http://localhost:29593/swagger.json', config=bravado_config)
            _, response = service.jobs.get_jobs().result()
            if response.status_code == 200:
                break
        except HTTPBadGateway:
            pass
        except requests.exceptions.ConnectionError:
            pass
        time.sleep(0.1)
        cur_time = time.perf_counter()

    if cur_time >= start_time + 10:
        print("Warning: Cerise container failed to come up")
    return service
Esempio n. 3
0
    def get_format(self, format_name):
        """
        One of the first things I noticed was that developers create custom
        formats like:

            orderId:
              description: Order reference
              example: e475f288-4e9b-43ea-966c-d3912e7a25b2
              format: uuid      <-------- HERE
              type: string

        And because the format was not defined in the OpenAPI specification the
        parser would send a warning and not handle the parameter properly.

        This method tries to fix the issue by always returning a generic
        user defined format
        """
        #
        # First try to handle the case with the default (OpenAPI spec-defined)
        # formats that are already included and handled by bravado-core
        #
        default_format = formatter.DEFAULT_FORMATS.get(format_name)
        if default_format is not None:
            return default_format

        #
        # Now we have to create a generic format handler, because the remote
        # OpenAPI spec uses it and we want to fuzz it
        #
        generic_format = SwaggerFormat(
            # name of the format as used in the Swagger spec
            format=format_name,

            # Callable to convert a python object to a string
            to_wire=lambda input_string: input_string,

            # Callable to convert a string to a python object
            to_python=lambda input_string: input_string,

            # Callable to validate the input string
            validate=validate_generic,

            # Description
            description='Generic format for w3af fuzzer')

        return generic_format
Esempio n. 4
0
        request = self.authenticator.apply(
            requests.Request(method='POST',
                             url=url,
                             data=data,
                             headers=headers))

        return session.send(session.prepare_request(request))

    def _upload_tar_data(self, experiment, api_method, data):
        url = self.api_address + api_method.operation.path_name
        url = url.replace("{experimentId}", experiment.internal_id)

        session = self._http_client.session

        request = self.authenticator.apply(
            requests.Request(
                method='POST',
                url=url,
                data=io.BytesIO(data),
                headers={"Content-Type": "application/octet-stream"}))

        return session.send(session.prepare_request(request))


uuid_format = SwaggerFormat(format='uuid',
                            to_python=lambda x: x,
                            to_wire=lambda x: x,
                            validate=lambda x: None,
                            description='')
Esempio n. 5
0
from six.moves import urllib

from neptune.exceptions import (
    CannotResolveHostname,
    DeprecatedApiToken,
    UnsupportedClientVersion,
)
from neptune.internal.api_clients.client_config import ClientConfig, MultipartConfig
from neptune.utils import with_api_exceptions_handler

_logger = logging.getLogger(__name__)

uuid_format = SwaggerFormat(
    format="uuid",
    to_python=lambda x: x,
    to_wire=lambda x: x,
    validate=lambda x: None,
    description="",
)


class HostedNeptuneMixin:
    """Mixin containing operation common for both backend and leaderboard api clients"""
    @with_api_exceptions_handler
    def _get_swagger_client(self, url, http_client):
        return SwaggerClient.from_url(
            url,
            config=dict(
                validate_swagger_spec=False,
                validate_requests=False,
                validate_responses=False,
Esempio n. 6
0
        'format': 'url',
    }
    args = (None, prop_schema['format'], None, prop_schema)
    list(format_validator(minimal_swagger_spec, *args))
    m_format_validator.assert_called_once_with(*args)


def validate_dummy(dummy_string):
    if dummy_string != 'dummy':
        raise SwaggerValidationError('dummy')


DummyFormat = SwaggerFormat(
    format="dummy",
    to_wire=lambda x: x,
    to_python=lambda x: x,
    validate=validate_dummy,
    description="dummy format",
)


@pytest.mark.parametrize(
    'value, format_, x_nullable, expect_exception',
    (
        [{'prop': 'dummy'}, 'dummy', False, False],
        [{'prop': 'hello'}, 'dummy', False, True],
        [{'prop': None}, 'dummy', False, True],
        [{'prop': None}, 'dummy', True, False],
    )
)
def test_validate_object_with_different_format_configurations(
Esempio n. 7
0
    Accepts an int64 and checks for numerality. Throws a Swagger Validation
    exception when failing the test.

    :param test:
    :return:
    :raises SwaggerValidationError:
    """
    if str(test) != test:
        raise SwaggerValidationError('int64 are serialized as strings')


# This is to support serializing int64 as strings on the wire. JavaScript
# only supports up to 2^53.
int64_format = SwaggerFormat(
    format='int64',
    to_wire=lambda i: str(i),
    to_python=lambda i: long(i),
    validate=validate_int64,  # jsonschema validates integer
    description='Converts [wire]str:int64 <=> python long')


class Client:
    """
    This class is the instantiated to create a new connection to a DOS. It
    connects to the service to download the swagger.json and returns a client
    in the DataObjectService namespace.

    ::

        from ga4gh.dos.client import Client
        client = Client("http://localhost:8000/ga4gh/dos/v1")
Esempio n. 8
0
# -*- coding: utf-8 -*-
from bravado_core.exception import SwaggerValidationError
from bravado_core.formatter import SwaggerFormat


def validate_email_address(email_address):
    if '@' not in email_address:
        raise SwaggerValidationError('dude, you need an @')


email_address_format = SwaggerFormat(
    format='email_address',
    to_wire=lambda x: x,
    to_python=lambda x: x,
    validate=validate_email_address,
    description='blah',
)
Esempio n. 9
0
def get_md5(d):
    s = json.dumps(d, sort_keys=True).encode("utf-8")
    return md5(s).hexdigest()


def validate_email(email_string):
    d = is_email(email_string, diagnose=True)
    if d > BaseDiagnosis.CATEGORIES["VALID"]:
        raise SwaggerValidationError(f"{email_string} {d.message}")


email_format = SwaggerFormat(
    format="email",
    to_wire=str,
    to_python=str,
    validate=validate_email,
    description="e-mail address",
)


def validate_url(url_string, qualifying=("scheme", "netloc")):
    tokens = urlparse(url_string)
    if not all([getattr(tokens, qual_attr) for qual_attr in qualifying]):
        raise SwaggerValidationError(f"{url_string} invalid")


url_format = SwaggerFormat(
    format="url",
    to_wire=str,
    to_python=str,
Esempio n. 10
0
def nge(host="http://trade", config=None, api_key=None, api_secret=None):
    """

    :rtype: SwaggerClient
    """
    if not config:
        # See full config options at
        # http://bravado.readthedocs.io/en/latest/configuration.html
        config = {
            # Don't use models (Python classes) instead of dicts for
            # #/definitions/{models}
            'use_models':
            False,
            'validate_requests':
            True,
            # bravado has some issues with nullable fields
            'validate_responses':
            False,
            'include_missing_properties':
            False,
            # Returns response in 2-tuple of (body, response);
            # if False, will only return body
            'also_return_response':
            True,
            'formats': [
                SwaggerFormat(format="guid",
                              to_wire=lambda guid_obj: str(guid_obj),
                              to_python=guid_deserializer,
                              description="GUID to uuid",
                              validate=guid_validate),
                SwaggerFormat(format="date-time",
                              to_wire=datetime_serializer,
                              to_python=datetime_deserializer,
                              description="date-time",
                              validate=datetime_validate)
            ]
        }

    spec_dir = path("@/swagger")
    spec_name = ("nge", "bitmex")
    spec_extension = ("yaml", "yml", "json")

    load_method = {
        "yaml": yaml.safe_load,
        "yml": yaml.safe_load,
        "json": json.dumps
    }

    with pushd(spec_dir):
        spec_file = ""

        for name, ext in product(spec_name, spec_extension):
            spec_file = ".".join([name, ext])

            if os.path.isfile(spec_file):
                break

        if not spec_file:
            raise RuntimeError("no valid swagger api define file found.")

        with open(spec_file, encoding="utf-8") as f:
            spec_dict = load_method[ext](f.read())

    if api_key and api_secret:
        request_client = RequestsClient()

        request_client.authenticator = NGEAPIKeyAuthenticator(
            host=host, api_key=api_key, api_secret=api_secret)

        return SwaggerClient.from_spec(spec_dict,
                                       origin_url=host,
                                       config=config,
                                       http_client=request_client)

    else:
        return SwaggerClient.from_spec(spec_dict,
                                       origin_url=host,
                                       config=config)
Esempio n. 11
0
    'validate_requests': True,
    'validate_responses': True
}


def validate_int64(test):
    # TODO improve to check numerality
    if str(test) != test:
        raise SwaggerValidationError('int64 are serialized as strings')


# This is to support serializing int64 as strings on the wire. JavaScript
# only supports up to 2^53.
int64_format = SwaggerFormat(
        format='int64',
        to_wire=lambda i: str(i) if isinstance(int, long) else i,
        to_python=lambda i: i if isinstance(str, long) else long(i),
        validate=validate_int64,  # jsonschema validates integer
        description='Converts [wire]str:int64 <=> python long')


class Client:
    """
    simple wrapper around bravado swagger Client. see
    https://github.com/Yelp/bravado/blob/master/docs/source/configuration.rst#client-configuration # NOQA
    https://github.com/Yelp/bravado#example-with-basic-authentication
    """
    def __init__(
          self, url,
          config=DEFAULT_CONFIG, http_client=None, request_headers=None):
        swagger_path = '{}/swagger.json'.format(url.rstrip("/"))
        config['formats'] = [int64_format]
Esempio n. 12
0
    def __init__(self, uri):
        """
        :param uri: URI in string form.
        """
        self.uri = uri

def validate_uri(uri_string):
    if '/' not in uri_string:
        raise SwaggerValidationError('URI {0} is invalid'.format(uri_string))

uri_format = SwaggerFormat(
    # name of the format as used in the Swagger spec
    format='uri',
    description="Representation of Django Rest Framework's URI Primary key thing",

    # Callable to convert a python CIDR object to a string
    to_wire=lambda uri_object: uri_object.uri,

    # Callable to convert a string to a python CIDR object
    to_python=lambda uri_string: URI(uri_string),

    # Callable to validate the uri in string form
    validate=validate_uri
)


# All the formats we define
as_list = [
    uri_format,
]
Esempio n. 13
0
formats = []

#
# Stripe Card Token
#


def validate_foo(foo):
    if foo != 'foo':
        raise SwaggerValidationError("Foo is not foo")


foo_format = SwaggerFormat(format='foo',
                           to_wire=lambda s: s,
                           to_python=lambda s: s,
                           validate=validate_foo,
                           description='a foo')

#
# Swagger spec
#

yaml_str = """
swagger: '2.0'
info:
  version: '0.0.1'
host: some.server.com
schemes:
  - http
produces: