def test_op_with_security_in_root_without_security_defs( specs_with_security_obj_in_root_and_no_security_specs, ): with pytest.raises(SwaggerSchemaError): build_resources(Spec( specs_with_security_obj_in_root_and_no_security_specs, ))
def build(self): if self.config['validate_swagger_spec']: validator20.validate_spec(self.spec_dict) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.definitions = build_models(self.spec_dict.get('definitions', {})) self.resources = build_resources(self)
def build(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( spec_dict=self.spec_dict, spec_url=self.origin_url or '', http_handlers=build_http_handlers(self.http_client), ) post_process_spec( self, on_container_callbacks=[ functools.partial( tag_models, visited_models={}, swagger_spec=self, ), functools.partial( collect_models, models=self.definitions, swagger_spec=self, ), ], ) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self)
def test_resource_with_vendor_extension(paths_spec): """Make sure vendor extensions are ignored.""" paths_spec['/pet/findByStatus']['x-foo'] = 'bar' spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert 1 == len(resources) assert resources['pet'].findPetsByStatus
def test_get_operations(paths_spec): spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) resource = resources['pet'] assert list(dir(resource)) == [ paths_spec['/pet/findByStatus']['get']['operationId'] ]
def setUpClass(cls): # FIXME: solve memory issues from generating the spec multiple times app = BaseWebTest().make_app(settings=cls.settings) cls.spec_dict = app.get('/__api__').json cls.spec = Spec.from_dict(cls.spec_dict) cls.resources = build_resources(cls.spec)
def test_many_resources_with_the_same_operation_cuz_multiple_tags(paths_spec): tags = ['foo', 'bar', 'baz', 'bing', 'boo'] paths_spec['/pet/findByStatus']['get']['tags'] = tags spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert len(tags) == len(resources) for tag in tags: assert resources[tag].findPetsByStatus
def test_resource_with_sanitized_tag(paths_spec): paths_spec['/pet/findByStatus']['get']['tags'][0] = 'Pets & Animals' spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert 1 == len(resources) assert 'Pets & Animals' in resources assert 'Pets_Animals' in resources assert resources['Pets_Animals'] is resources['Pets & Animals']
def test_op_with_security_in_root_with_security_defs( specs_with_security_obj_in_root_and_security_specs): security_definitions_spec = \ specs_with_security_obj_in_root_and_security_specs['securityDefinitions'] # noqa _validate_resources( resources=build_resources( Spec(specs_with_security_obj_in_root_and_security_specs, ), ), security_definitions_spec=security_definitions_spec, )
def test_get_undefined_operation(paths_spec): paths_spec['/pet/findByStatus']['get']['tags'] = ['tag'] spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) resource = resources['tag'] with pytest.raises(AttributeError) as excinfo: resource.undefined_operation assert "Resource 'tag' has no operation 'undefined_operation'" in str( excinfo.value)
def test_op_with_security_in_root_with_empty_security_spec( specs_with_security_obj_in_root_and_empty_security_spec): resources = build_resources( Spec(specs_with_security_obj_in_root_and_empty_security_spec, ), ) resource = resources.get('pet') assert resource is not None operation = getattr(resource, 'findPetsByStatus') assert operation is not None assert len(operation.security_requirements) == 0
def test_op_with_security_in_root_with_security_defs( specs_with_security_obj_in_root_and_security_specs, ): security_definitions_spec = \ specs_with_security_obj_in_root_and_security_specs['securityDefinitions'] # noqa _validate_resources( resources=build_resources(Spec( specs_with_security_obj_in_root_and_security_specs, )), security_definitions_spec=security_definitions_spec, )
def test_refs(minimal_swagger_dict, paths_spec, pet_spec, internally_dereference_refs): minimal_swagger_dict['paths'] = paths_spec minimal_swagger_dict['definitions'] = {'Pet': pet_spec} swagger_spec = Spec( spec_dict=minimal_swagger_dict, origin_url='', config={'internally_dereference_refs': internally_dereference_refs}, ) resources = build_resources(swagger_spec) assert len(resources) == 1 assert 'pet' in resources
def test_resource_with_a_single_operation_associated_by_path_name(paths_spec): # rename path so we know resource name will not be 'pet' paths_spec['/foo/findByStatus'] = paths_spec['/pet/findByStatus'] del paths_spec['/pet/findByStatus'] # remove tags on operation so path name is used to assoc with a resource del paths_spec['/foo/findByStatus']['get']['tags'] spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert 1 == len(resources) assert resources['foo'].findPetsByStatus
def test_refs(minimal_swagger_dict, paths_spec): minimal_swagger_dict['real_paths'] = paths_spec minimal_swagger_dict['real_op'] = paths_spec['/pet/findByStatus']['get'] minimal_swagger_dict['real_tags'] = \ paths_spec['/pet/findByStatus']['get']['tags'] paths_spec['/pet/findByStatus']['get']['tags'] = {'$ref': '#/real_tags'} paths_spec['/pet/findByStatus']['get'] = {'$ref': '#/real_op'} minimal_swagger_dict['paths'] = {'$ref': '#/real_paths'} swagger_spec = Spec(minimal_swagger_dict) resources = build_resources(swagger_spec) assert len(resources) == 1 assert 'pet' in resources
def setUpClass(cls): super().setUpClass() cls.spec_dict = cls.app.get('/__api__').json bravado_config = { # use_models causes us to break in bravado-core 4.13.0, # probably because of # https://github.com/Yelp/bravado-core/pull/254, and we # don't actually use the generated models in our tests # here anyhow. "use_models": False, } cls.spec = Spec.from_dict(cls.spec_dict, config=bravado_config) cls.resources = build_resources(cls.spec)
def setUpClass(cls): super().setUpClass() cls.spec_dict = cls.app.get("/__api__").json bravado_config = { # use_models causes us to break in bravado-core 4.13.0, # probably because of # https://github.com/Yelp/bravado-core/pull/254, and we # don't actually use the generated models in our tests # here anyhow. "use_models": False } cls.spec = Spec.from_dict(cls.spec_dict, config=bravado_config) cls.resources = build_resources(cls.spec)
def test_op_with_security_in_root_with_empty_security_spec( specs_with_security_obj_in_root_and_empty_security_spec, ): resources = build_resources(Spec( specs_with_security_obj_in_root_and_empty_security_spec, )) resource = resources.get('pet') assert resource is not None operation = getattr(resource, 'findPetsByStatus') assert operation is not None assert len(operation.security_requirements) == 0
def test_resource__associated_by_sanitized_path_name(paths_spec): # rename path so we know resource name will not be 'pet' paths_spec['/foo-bar/findByStatus'] = paths_spec['/pet/findByStatus'] del paths_spec['/pet/findByStatus'] # remove tags on operation so path name is used to assoc with a resource del paths_spec['/foo-bar/findByStatus']['get']['tags'] spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert 1 == len(resources) assert 'foo-bar' in resources assert 'foo_bar' in resources assert resources['foo_bar'] is resources['foo-bar']
def build(self): self._validate_spec() def run_post_processing(swagger_spec): visited_models = {} # Discover all the models post_process_spec( swagger_spec, on_container_callbacks=[ functools.partial( tag_models, visited_models=visited_models, swagger_spec=swagger_spec, ), functools.partial( bless_models, visited_models=visited_models, swagger_spec=swagger_spec, ), functools.partial( collect_models, models=swagger_spec.definitions, swagger_spec=swagger_spec, ), ], ) # This run is needed in order to get all the available models discovered # deref_flattened_spec depends on flattened_spec which assumes that model # discovery is performed run_post_processing(self) if self.config['internally_dereference_refs']: deref_flattened_spec = self.deref_flattened_spec tmp_spec = Spec(deref_flattened_spec, self.origin_url, self.http_client, self.config) # Rebuild definitions using dereferences specs as base # this ensures that the generated models have no references run_post_processing(tmp_spec) self.definitions = tmp_spec.definitions # Avoid to evaluate is_ref every time, no references are possible at this time self.deref = lambda ref_dict: ref_dict for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self)
def test_resource_with_shared_parameters(paths_spec): # insert a shared parameter into the spec shared_parameter = { 'name': 'filter', 'in': 'query', 'description': 'Filter the pets by attribute', 'required': False, 'type': 'string', } paths_spec['/pet/findByStatus']['parameters'] = [shared_parameter] spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) # verify shared param associated with operation assert isinstance( resources['pet'].findPetsByStatus.params['filter'], Param)
def test_resource_with_shared_parameters(paths_spec): # insert a shared parameter into the spec shared_parameter = { 'name': 'filter', 'in': 'query', 'description': 'Filter the pets by attribute', 'required': False, 'type': 'string', } paths_spec['/pet/findByStatus']['parameters'] = [shared_parameter] spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) # verify shared param associated with operation assert isinstance(resources['pet'].findPetsByStatus.params['filter'], Param)
def build(self): self._validate_spec() model_discovery(self) if self.config['internally_dereference_refs']: # Avoid to evaluate is_ref every time, no references are possible at this time self.deref = lambda ref_dict: ref_dict self._internal_spec_dict = self.deref_flattened_spec for user_defined_format in self.config['formats']: self.register_format(user_defined_format) self.resources = build_resources(self) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url)
def setUpClass(cls): super().setUpClass() cls.spec_dict = cls.app.get("/__api__").json bravado_config = { # Use models (Python classes) instead of dicts for #/definitions/{models} # use_models causes us to break in bravado-core 4.13.0, # probably because of # https://github.com/Yelp/bravado-core/pull/254, and we # don't actually use the generated models in our tests # here anyhow. "use_models": False } cls.spec = Spec.from_dict(cls.spec_dict, http_client=RequestsClient(), config=bravado_config) cls.resources = build_resources(cls.spec)
def build(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( self.spec_dict, spec_url=self.origin_url or '', http_handlers=build_http_handlers(self.http_client)) post_process_spec( self, on_container_callbacks=[ functools.partial( tag_models, visited_models={}, swagger_spec=self), functools.partial( collect_models, models=self.definitions, swagger_spec=self) ]) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self)
def build(self): self._validate_spec() post_process_spec( self, on_container_callbacks=[ functools.partial( tag_models, visited_models={}, swagger_spec=self, ), functools.partial( collect_models, models=self.definitions, swagger_spec=self, ), ], ) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self)
def build(self): if self.config['validate_swagger_spec']: validator20.validate_spec(self.spec_dict) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self)
def test_empty(): spec_dict = {'paths': {}} spec = Spec(spec_dict) assert {} == build_resources(spec)
def setUpClass(cls): super().setUpClass() cls.spec_dict = cls.app.get('/__api__').json cls.spec = Spec.from_dict(cls.spec_dict) cls.resources = build_resources(cls.spec)
def test_resource_with_a_single_operation_associated_by_tag(paths_spec): spec_dict = {'paths': paths_spec} resources = build_resources(Spec(spec_dict)) assert 1 == len(resources) assert resources['pet'].findPetsByStatus