Beispiel #1
0
def setup_fixture():
    """
    Index 2 documents.
    """
    reset()

    # save initial docs with paths already transformed
    mygene = SmartAPI(MYGENE_URL)
    mygene.raw = MYGENE_RAW
    mygene.username = '******'
    mygene.slug = 'mygene'
    mygene.save()

    mychem = SmartAPI(MYCHEM_URL)
    mychem.raw = MYCHEM_RAW
    mychem.username = '******'
    mychem.slug = 'mychem'
    mychem.save()

    # refresh index
    refresh()
Beispiel #2
0
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()
Beispiel #3
0
def test_delete(myvariant):

    mv = SmartAPI.get(myvariant)
    mv.delete()

    refresh()

    assert not APIDoc.exists(myvariant)

    URL = "http://example.com/valid.json"
    with open(os.path.join(dirname, './validate/openapi-pass.json'), 'rb') as file:
        smartapi = SmartAPI(URL)
        smartapi.raw = file.read()
        with pytest.raises(NotFoundError):
            smartapi.delete()
Beispiel #4
0
def _restore(smartapis):
    if indices.exists():
        logging.error("Cannot write to an existing index.")
        return
    indices.reset()
    for smartapi in smartapis:
        logging.info(smartapi["url"])
        _smartapi = SmartAPI(smartapi["url"])
        _smartapi.username = smartapi["username"]
        _smartapi.slug = smartapi["slug"]
        _smartapi.date_created = datetime.fromisoformat(
            smartapi["date_created"])
        _smartapi.last_updated = datetime.fromisoformat(
            smartapi["last_updated"])
        _smartapi.raw = smartapi["raw"].encode()  # to bytes
        _smartapi.save()
Beispiel #5
0
    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.'
            })
Beispiel #6
0
def migrate():

    for doc in scan(Elasticsearch(ES_ORIGIN),
                    query={"query": {
                        "match_all": {}
                    }},
                    index="smartapi_oas3",
                    doc_type="api"):
        print(doc["_id"])
        if not doc['_source']['_meta'].get('_archived'):
            url = doc['_source']['_meta']['url']
            raw = decoder.decompress(
                base64.urlsafe_b64decode(doc['_source']['~raw']))

            smartapi = SmartAPI(url)
            smartapi.raw = raw
            smartapi.date_created = parser.parse(
                doc['_source']['_meta']['timestamp']).replace(
                    tzinfo=timezone.utc)
            smartapi.username = doc['_source']['_meta']['github_username']
            smartapi.slug = doc['_source']['_meta'].get('slug')
            smartapi.save()
    print()
Beispiel #7
0
    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)
Beispiel #8
0
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
Beispiel #9
0
    def test_update_doc(self):

        self.request("/api/metadata/" + MYCHEM_ID, method="PUT", expect=401)
        self.request("/api/metadata/notexists",
                     method="PUT",
                     headers=self.auth_user,
                     expect=404)
        self.request("/api/metadata/" + MYCHEM_ID,
                     method="PUT",
                     headers=self.evil_user,
                     expect=403)

        res = self.request("/api/metadata/" + MYCHEM_ID,
                           method="PUT",
                           headers=self.auth_user).json()
        assert res["success"]
        assert res["code"] == 299
        assert res["status"] == "updated"

        mychem = SmartAPI.get(MYCHEM_ID)
        assert mychem.webdoc.status == 299
        assert mychem.webdoc.timestamp
        ts0 = mychem.webdoc.timestamp

        res = self.request("/api/metadata/" + MYCHEM_ID,
                           method="PUT",
                           headers=self.auth_user).json()
        assert res["success"]
        assert res["code"] == 200
        assert res["status"] == "not_modified"

        mychem = SmartAPI.get(MYCHEM_ID)
        assert mychem.webdoc.status == 200
        assert mychem.webdoc.timestamp >= ts0

        # setup
        mygene_ref = SmartAPI(self.get_url('/test/mygene.yml'))
        mygene_ref.raw = MYGENE_RAW
        mygene_ref.username = '******'
        mygene_ref.save()

        # first request, same file, minimul version
        res = self.request("/api/metadata/" + mygene_ref._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert res["success"]
        assert res["code"] == 200
        assert res["status"] == "not_modified"

        mygene = SmartAPI.get(mygene_ref._id)
        assert mygene.webdoc.status == 200

        # second request, full version, updated
        res = self.request("/api/metadata/" + mygene_ref._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert res["success"]
        assert res["code"] == 299
        assert res["status"] == "updated"

        mygene = SmartAPI.get(mygene_ref._id)
        assert mygene.webdoc.status == 299

        # third request, link temporarily unavailable
        res = self.request("/api/metadata/" + mygene_ref._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert not res["success"]
        assert res["code"] == 404
        assert res["status"] == "nofile"

        mygene = SmartAPI.get(mygene_ref._id)
        assert mygene.webdoc.status == 404

        # fourth request, link back up, new version doesn't pass validation
        res = self.request("/api/metadata/" + mygene_ref._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert not res["success"]
        assert res["code"] == 499
        assert res["status"] == "invalid"

        mygene = SmartAPI.get(mygene_ref._id)
        assert mygene.webdoc.status == 499

        # fifth request, link restored to a working version
        res = self.request("/api/metadata/" + mygene_ref._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert res["success"]
        assert res["code"] == 200
        assert res["status"] == "not_modified"

        mygene = SmartAPI.get(mygene_ref._id)
        assert mygene.webdoc.status == 200

        # setup
        mygene_ref_2 = SmartAPI('http://invalidhost/mygene.yml')
        mygene_ref_2.raw = MYGENE_RAW
        mygene_ref_2.username = '******'
        mygene_ref_2.save()

        res = self.request("/api/metadata/" + mygene_ref_2._id,
                           method="PUT",
                           headers=self.auth_user).json()
        assert not res["success"]
        assert res["code"] == 599
        assert res["status"] == "nofile"

        mygene = SmartAPI.get(mygene_ref_2._id)
        assert mygene.webdoc.status == 599