def setup_fixture(): """ Index 2 documents. """ reset() # save initial docs with paths already transformed mygene = APIDoc(meta={'id': MYGENE_ID}, **MYGENE_ES) mygene._raw = decoder.compress(MYGENE_RAW) mygene.save() mychem = APIDoc(meta={'id': MYCHEM_ID}, **MYCHEM_ES) mychem._raw = decoder.compress(MYCHEM_RAW) mychem.save() # refresh index refresh()
def myvariant(): with open(os.path.join(dirname, 'myvariant.es.json'), 'r') as file: MYVARIANT_ES = json.load(file) with open(os.path.join(dirname, 'myvariant.yml'), 'rb') as file: MYVARIANT_RAW = file.read() MYVARIANT_ID = MYVARIANT_ES.pop("_id") myvariant = APIDoc(meta={'id': MYVARIANT_ID}, **MYVARIANT_ES) myvariant._raw = decoder.compress(MYVARIANT_RAW) myvariant.save() refresh() yield MYVARIANT_ID refresh() try: APIDoc.get(MYVARIANT_ID).delete() except elasticsearch.exceptions.NotFoundError: pass
def test_delete(self): # setup assert SmartAPI.exists(MYGENE_ID) self.request("/api/metadata/" + MYGENE_ID, method='DELETE', expect=401) self.request("/api/metadata/" + MYGENE_ID, method='DELETE', headers=self.evil_user, expect=403) self.request("/api/metadata/" + MYGENE_ID, method='DELETE', headers=self.auth_user) refresh() assert not SmartAPI.exists(MYGENE_ID) # teardown refresh() if not SmartAPI.exists(MYGENE_ID): # recover the deleted file mygene = APIDoc(meta={'id': MYGENE_ID}, **MYGENE_ES) mygene._raw = decoder.compress(MYGENE_RAW) mygene.save() refresh()
def save(self): # TODO DOCSTRING if not self.raw: raise ControllerError("No content.") if not self.username: raise ControllerError("Username is required.") if self.url is self.VALIDATION_ONLY: raise ControllerError("In validation-only mode.") if not self.last_updated: self.last_updated = datetime.now(timezone.utc) warn("Filling in date_updated with current time.") if not self.date_created: self.date_created = self.last_updated warn("Filling in date_created with current time.") if not isinstance(self.date_created, datetime): raise ControllerError("Invalid created time.") if not isinstance(self.last_updated, datetime): raise ControllerError("Invalid updated time.") if self.date_created > self.last_updated: raise ControllerError("Invalid timestamps.") # NOTE # why not enforce validation here? # we add additional constraints to the application from time to time # it's actually hard to retrospectively make sure all previously # submitted API document always meet our latest requirements _doc = self._validate_dispatch() _doc.transform() if self.slug: _id = self.find(self.slug) if _id and _id != self._id: # another doc same slug. raise ConflictError("Slug is already registered.") # NOTE # if the slug of another document changed at this point # it's possible to have two documents with the same slug # registered. but it should be rare enough in reality. doc = APIDoc(**_doc) doc.meta.id = self._id doc._meta.url = self.url doc._meta.username = self.username doc._meta.slug = self.slug doc._meta.date_created = self.date_created doc._meta.last_updated = self.last_updated if self.uptime.status: doc._status.uptime_status = self.uptime.status[0] doc._status.uptime_msg = self.uptime.status[1] doc._status.uptime_ts = self.uptime.timestamp doc._status.refresh_status = self.webdoc.status doc._status.refresh_ts = self.webdoc.timestamp doc._raw = decoder.compress(self.raw) doc.save(skip_empty=False) return self._id
def test_update_slug(self): mygene = SmartAPI.get(MYGENE_ID) assert mygene.slug == "mygene" self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "mygeeni"}, expect=401) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "mygeeni"}, headers=self.evil_user, expect=403) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "my"}, headers=self.auth_user, expect=400) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "www"}, headers=self.auth_user, expect=400) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "MYGENE"}, headers=self.auth_user, expect=400) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "mygene!!"}, headers=self.auth_user, expect=400) self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "mygeeni"}, headers=self.auth_user) refresh() assert not SmartAPI.find("mygene") assert SmartAPI.find("mygeeni") self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": ""}, headers=self.auth_user) refresh() assert not SmartAPI.find("mygeeni") assert not SmartAPI.find("mygene") self.request("/api/metadata/" + MYGENE_ID, method='PUT', data={"slug": "mygene"}, headers=self.auth_user) refresh() assert not SmartAPI.find("mygeeni") assert SmartAPI.find("mygene") # teardown refresh() if not SmartAPI.find("mygene"): mygene = APIDoc(meta={'id': MYGENE_ID}, **MYGENE_ES) mygene._raw = decoder.compress(MYGENE_RAW) mygene.save()