def test_asyncapi_spec_validation_invalid_security_requirement_on_namespace( faker: Faker, ): data = { "asyncapi": "2.3.0", "info": { "title": faker.sentence(), "version": faker.pystr(), "description": faker.sentence(), }, "channels": { GLOBAL_NAMESPACE: { "x-security": [{ "oauth2": ["undefined"] }], } }, "servers": { "development": { "url": "localhost", "protocol": "ws", } }, "components": { "securitySchemes": { "test": { "type": "http", "scheme": "basic" }, "test2": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }, "testApiKey": { "type": "httpApiKey", "name": "test", "in": "header" }, "oauth2": { "type": "oauth2", "flows": { "implicit": { "authorizationUrl": "https://localhost:12345", "refreshUrl": "https://localhost:12345/refresh", "scopes": { "a": "A", "b": "B" }, } }, }, } }, } with pytest.raises(ValueError): # missing security scheme AsyncApiSpec.from_dict(data)
def load_spec(spec_path: Union[Path, JSONMapping]) -> AsyncApiSpec: if isinstance(spec_path, Path): with open(spec_path) as f: serialized = f.read() spec = yaml.safe_load(serialized) else: spec = spec_path raw_resolved = resolve_references(spec) return AsyncApiSpec.from_dict(raw_resolved)
def test_asyncapi_spec_validation_missing_security_scheme(faker: Faker): data = { "asyncapi": "2.3.0", "info": { "title": faker.sentence(), "version": faker.pystr(), "description": faker.sentence(), }, "channels": {}, "servers": { "development": { "url": "localhost", "protocol": "ws", "security": [{ "test": [] }], } }, } with pytest.raises(ValueError): # missing security scheme AsyncApiSpec.from_dict(data)
def test_async_api_spec_from_and_to_dict(faker: Faker): data = { "asyncapi": "2.3.0", "info": { "title": faker.sentence(), "version": faker.pystr(), "description": faker.sentence(), }, "channels": { GLOBAL_NAMESPACE: { "description": faker.pystr(), "subscribe": { "message": { "oneOf": [{ "name": faker.pystr(), "summary": faker.sentence(), "payload": faker.pydict(value_types=[str, int]), } for _ in range(faker.pyint(min_value=2, max_value=10)) ] } }, "publish": { "message": { "oneOf": [{ "title": faker.word(), "name": faker.pystr(), "payload": faker.pydict(value_types=[str, int]), "x-handler": faker.pydict(value_types=[str, int]), "x-ack": { "args": faker.pydict(value_types=[str, int]), }, } for _ in range( faker.pyint(min_value=2, max_value=10))] } }, "bindings": { "ws": { "method": faker.pystr(), "query": faker.pydict(value_types=[str, int]), } }, "x-handlers": { "connect": faker.pystr(), "disconnect": faker.pystr(), faker.word(): faker.pystr(), }, } }, "servers": { "development": { "url": "localhost", "protocol": "ws", "security": [{ "test": [] }], } }, "components": { "securitySchemes": { "test": { "type": "http", "scheme": "basic" }, "test2": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" }, "testApiKey": { "type": "httpApiKey", "name": "test", "in": "header" }, "oauth2": { "type": "oauth2", "flows": { "implicit": { "authorizationUrl": "https://localhost:12345", "refreshUrl": "https://localhost:12345/refresh", "scopes": { "a": "A", "b": "B" }, } }, }, } }, } spec = AsyncApiSpec.from_dict(data) assert isinstance(spec, AsyncApiSpec) assert spec.to_dict() == data