summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fatcat/api.py13
-rw-r--r--tests/api.py26
2 files changed, 39 insertions, 0 deletions
diff --git a/fatcat/api.py b/fatcat/api.py
index 838168af..52b0e21a 100644
--- a/fatcat/api.py
+++ b/fatcat/api.py
@@ -101,6 +101,19 @@ def api_release_random():
entity = ReleaseIdent.query.order_by(db.func.random()).first()
return redirect('/v0/release/{}'.format(entity.id))
+@app.route('/v0/release/lookup', methods=['GET'])
+def api_release_lookup():
+ params = request.get_json()
+ doi = params['doi'].lower()
+ if not (doi.startswith("10.") and len(doi.split('/')) == 2):
+ abort(400)
+ # TODO: proper regex
+ entity = ReleaseIdent.query\
+ .join(ReleaseIdent.rev)\
+ .filter(ReleaseRev.doi==doi)\
+ .first_or_404()
+ return release_schema.jsonify(entity)
+
@app.route('/v0/creator/<int:ident>', methods=['GET'])
def api_creator_get(ident):
diff --git a/tests/api.py b/tests/api.py
index 6823aa6d..d9158b87 100644
--- a/tests/api.py
+++ b/tests/api.py
@@ -208,3 +208,29 @@ def test_api_rich_create(app):
# test that editor's active edit group is now invalid
editor = Editor.query.first()
assert editor.active_edit_group == None
+
+def test_api_release_lookup(rich_app):
+ app = rich_app
+
+ rv = app.get('/v0/release/1',
+ headers={"content-type": "application/json"})
+ assert rv.status_code == 200
+ obj = json.loads(rv.data.decode('utf-8'))
+
+ rv = app.get('/v0/release/lookup',
+ data=json.dumps(dict(doi="10.1234/5678")),
+ headers={"content-type": "application/json"})
+ assert rv.status_code == 200
+ obj = json.loads(rv.data.decode('utf-8'))
+ assert obj['doi'] == "10.1234/5678"
+ assert obj.get('id') != None
+
+ rv = app.get('/v0/release/lookup',
+ data=json.dumps(dict(doi="10.1234/5678_noexit")),
+ headers={"content-type": "application/json"})
+ assert rv.status_code == 404
+
+ rv = app.get('/v0/release/lookup',
+ data=json.dumps(dict(doi="not_even_valid_doi")),
+ headers={"content-type": "application/json"})
+ assert rv.status_code == 400