def test_payload_missing_scholarly_field_expected_fail(self): """tests articles endpoint validation missing a Scholary required field. Expect to fail with return value of False """ payload = { "references": [ { "id": "item2", "title": "Some older article about some domokuns.", "author": [ {"given": "chevy", "family": "chaser"} ], "type": "book" }, { "id": "item3", "title": "Some article about some domokun lovers.", "author": [ {"given": "kim", "family": "possibly"} ], "type": "book", "doi": "asdflkj209asdlkfj209sadfkj2" } ] } # call articles endpoint validation on payload self.assertFalse(articles_endpoint_validator.validate(payload))
def test_reference_missing_csl_field_expected_fail(self): """tests articles endpoint validation with citation missing required field. Expect to fail with return value of False """ payload = { "citation": { "id": "item1", "title": "Some article about domokuns.", "author": [ {"given": "tim", "family": "tom"} ], "type": "book" }, "references": [ { "id": "item2", "author": [ {"given": "chevy", "family": "chaser"} ], "type": "book" } ] } # call articles endpoint validation on payload self.assertFalse(articles_endpoint_validator.validate(payload))
def test_citation_missing_csl_field_expected_fail(self): """tests articles endpoint validation with citation missing required field. Expect to fail with return value of False """ payload = { "citation": { "id": "item1", "author": [ {"given": "tim", "family": "tom"} ], "type": "book" } } # call articles endpoint validation on payload self.assertFalse(articles_endpoint_validator.validate(payload))
def ArticleEndpoint(): """ RESTFull API endpoint for retrieving / submitting articles @TODO: Switch function to pluggable view approach """ if request.method == 'GET': # load articles endpoint informational page return render_template("articles.html") elif request.method == 'POST': if request.headers['content-type'] == 'application/json': # if post's content-type is JSON try: # try to convert post data user_submission = json.loads(request.data) except ValunteError: # return error if fail return Response(status=405) # validate data or return error if not articles_endpoint_validator.validate(user_submission): return Response(status=405) # add meta-data to user submission -- headers and what not # @todo user submission is a string at this point. Need to make it a string. # @todo find proper way to append request.headers to the json # user_submission['headers'] = request.headers # add parsed data to DB #submission_id = articles_db.add(user_submission) # return URI of new resource to submitter #@todo: format return body with objectid? return Response(status=201) else: # return HTTP submission error code to user return Response(status=405) else: # return HTTP submission error code to user return Response(status=405)
def test_payload_without_references_expected_pass(self): """tests articles endpoint validation on known good payload without references. Expect to pass with return value of True """ payload = { "citation": { "id": "item1", "title": "Some article about domokuns.", "author": [ {"given": "tim", "family": "tom"} ], "type": "book", "date": 2009, "container-title": "Domokuns" } } # call articles endpoint validation on payload self.assertTrue(articles_endpoint_validator.validate(payload))
def test_payload_with_references_expected_pass(self): """tests articles endpoint validation on known good payload with references. Expect to pass with return value of True """ payload = { "citation": { "id": "item1", "title": "Some article about domokuns.", "author": [ {"given": "tim", "family": "tom"} ], "type": "book", "date": 2009, "container-title": "Domokuns" }, "references": [ { "id": "item2", "title": "Some older article about some domokuns.", "author": [ {"given": "chevy", "family": "chaser"} ], "type": "book", "date": 2009, "container-title": "Domokuns" }, { "id": "item3", "title": "Some article about some domokun lovers.", "author": [ {"given": "kim", "family": "possibly"} ], "type": "book", "date": 2009, "container-title": "Domokuns", "doi": "asdflkj209asdlkfj209sadfkj2" }, { "id": "item4", "title": "Some article about some people who made their fortunes from selling domokuns.", "author": [ {"given": "lyla", "family": "lilly"}, {"given": "jimmy", "family": "jaseper"} ], "type": "webpage", "date": 2009, "container-title": "Domokuns", "url": "http://www.pewpew.com/domokun_seller_gets_rich" } ], "metadata": { "source": "some submitter", "parse_style": "manual / parscite / scraping" } } # call articles endpoint validation on payload self.assertTrue(articles_endpoint_validator.validate(payload))