def validate(self, raw): try: smartapi = SmartAPI(SmartAPI.VALIDATION_ONLY) smartapi.raw = raw smartapi.validate() except (ControllerError, AssertionError) as err: raise BadRequest(details=str(err)) else: self.finish({ 'success': True, 'details': f'valid SmartAPI ({smartapi.version}) metadata.' })
async def post(self): """ Add an API document """ if SmartAPI.find(self.args.url, "url"): raise HTTPError(409) try: file = await download_async(self.args.url) except DownloadError as err: raise BadRequest(details=str(err)) from err try: smartapi = SmartAPI(self.args.url) smartapi.raw = file.raw smartapi.validate() except (ControllerError, AssertionError) as err: raise BadRequest(details=str(err)) from err if self.args.dryrun: raise Finish({ 'success': True, 'details': f"[Dryrun] Valid {smartapi.version} Metadata" }) try: smartapi.username = self.current_user['login'] smartapi.refresh(file) # populate webdoc meta _id = smartapi.save() except ControllerError as err: raise BadRequest(details=str(err)) from err else: self.finish({'success': True, '_id': _id}) await self._notify(smartapi)
def test_save(openapi): """ SmartAPI.slug.validate(slug) smartapi.slug smartapi.save() """ _t0 = datetime.now(timezone.utc) time.sleep(0.1) URL = "http://example.com/valid.json" with pytest.raises(ValueError): SmartAPI.slug.validate("a") with pytest.raises(ValueError): SmartAPI.slug.validate("AAA") with pytest.raises(ValueError): SmartAPI.slug.validate("www") with pytest.raises(ValueError): SmartAPI.slug.validate("^_^") with open(os.path.join(dirname, './validate/openapi-pass.json'), 'rb') as file: raw = file.read() smartapi = SmartAPI(URL) with pytest.raises(ControllerError): smartapi.raw = None smartapi.raw = raw smartapi.slug = "mygene" smartapi.validate() with pytest.raises(ControllerError): smartapi.save() smartapi.username = "******" with pytest.raises(ConflictError): smartapi.save() smartapi.slug = "mychem" with pytest.raises(ConflictError): smartapi.save() smartapi.slug = "openapi" smartapi.save() refresh() assert SmartAPI.find("openapi") == smartapi._id assert smartapi.date_created > _t0 assert smartapi.last_updated > _t0 assert smartapi.date_created == smartapi.last_updated apidoc = APIDoc.get(smartapi._id) assert apidoc._meta.date_created == smartapi.date_created assert apidoc._meta.last_updated == smartapi.last_updated _t1 = smartapi.date_created smartapi.save() # no change refresh() assert SmartAPI.find("openapi") == smartapi._id assert smartapi.date_created == _t1 assert smartapi.last_updated == _t1 assert smartapi.date_created == smartapi.last_updated apidoc = APIDoc.get(smartapi._id) assert apidoc._meta.date_created == smartapi.date_created assert apidoc._meta.last_updated == smartapi.last_updated smartapi.slug = None smartapi.save() refresh() assert not SmartAPI.find("openapi") found = SmartAPI.get(openapi) assert dict(smartapi) == dict(found) assert smartapi.username == found.username assert smartapi.slug == found.slug assert smartapi.url == found.url assert smartapi.date_created == _t1 assert smartapi.last_updated == _t1 assert smartapi.date_created == smartapi.last_updated apidoc = APIDoc.get(smartapi._id) assert apidoc._meta.date_created == smartapi.date_created assert apidoc._meta.last_updated == smartapi.last_updated smartapi.raw = raw # should trigger ts update smartapi.save() refresh() assert smartapi.date_created == _t1 assert smartapi.last_updated > _t1 apidoc = APIDoc.get(smartapi._id) assert apidoc._meta.date_created == smartapi.date_created assert apidoc._meta.last_updated == smartapi.last_updated
def test_validation(): """ smartapi.validate() """ URL = "http://example.com/invalid.json" with open(os.path.join(dirname, './validate/openapi-pass.json'), 'rb') as file: smartapi = SmartAPI(URL) smartapi.raw = file.read() smartapi.validate() with open(os.path.join(dirname, './validate/swagger-pass.json'), 'rb') as file: smartapi = SmartAPI(URL) smartapi.raw = file.read() smartapi.validate() with open(os.path.join(dirname, './validate/x-translator-pass.json'), 'rb') as file: smartapi = SmartAPI(URL) smartapi.raw = file.read() smartapi.validate() with open(os.path.join(dirname, './validate/x-translator-fail-1.yml'), 'rb') as file: smartapi = SmartAPI(URL) smartapi.raw = file.read() with pytest.raises(ControllerError): smartapi.validate() with open(os.path.join(dirname, './validate/x-translator-fail-2.yml'), 'rb') as file: smartapi = SmartAPI(URL) smartapi.raw = file.read() with pytest.raises(ControllerError): smartapi.validate() smartapi = SmartAPI(URL) smartapi.raw = b'{}' with pytest.raises(ControllerError): smartapi.validate()