diff --git a/.gitignore b/.gitignore index 003ccbe..923273a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ parts/ sdist/ var/ venv/ +.venv/ *.egg-info/ .installed.cfg *.egg diff --git a/vault_service/tests/test_bumblebee.py b/vault_service/tests/test_bumblebee.py index f9f3e17..fc048ea 100644 --- a/vault_service/tests/test_bumblebee.py +++ b/vault_service/tests/test_bumblebee.py @@ -23,7 +23,8 @@ def create_app(self): 'TESTING': True, 'PROPAGATE_EXCEPTIONS': True, 'TRAP_BAD_REQUEST_ERRORS': True, - 'VAULT_BUMBLEBEE_OPTIONS': {'foo': 'bar'} + 'VAULT_BUMBLEBEE_OPTIONS': {'foo': 'bar'}, + 'VAULT_NECTAR_OPTIONS': {'foo': 'other-bar'} }) return a @@ -31,6 +32,8 @@ def create_app(self): def test_store_data(self): '''Tests the ability to query site config''' + headers = {'Referer': 'https://dev.scixplorer.org'} + r = self.client.get(url_for('bumblebee.configuration'), content_type='application/json') self.assertStatus(r, 200) @@ -45,6 +48,25 @@ def test_store_data(self): content_type='application/json') self.assertStatus(r, 404) + r = self.client.get(url_for('bumblebee.configuration'), + content_type='application/json', headers=headers) + self.assertStatus(r, 200) + self.assertTrue(r.json == {'foo': 'other-bar'}, 'missing json response') + + r = self.client.get(url_for('bumblebee.configuration') + '/foo', + content_type='application/json', headers=headers) + self.assertStatus(r, 200) + self.assertTrue(r.json == 'other-bar', 'missing json response') + + r = self.client.get(url_for('bumblebee.configuration') + '/foo', + content_type='application/json', headers=headers) + self.assertStatus(r, 200) + self.assertFalse(r.json == 'bar', 'missing json response') + + r = self.client.get(url_for('bumblebee.configuration') + '/foox', + content_type='application/json', headers=headers) + self.assertStatus(r, 404) + class TestOpenURL(TestCaseDatabase): '''Tests that each route is an http response''' diff --git a/vault_service/views/bumblebee.py b/vault_service/views/bumblebee.py index e315caa..cd55eac 100644 --- a/vault_service/views/bumblebee.py +++ b/vault_service/views/bumblebee.py @@ -1,8 +1,9 @@ from flask import Blueprint -from flask import current_app +from flask import current_app, request from ..models import Library from operator import itemgetter import json +import urllib from flask_discoverer import advertise @@ -12,9 +13,12 @@ @bp.route('/configuration', methods=['GET']) @bp.route('/configuration/', methods=['GET']) def configuration(key=None): - '''Allows you to retrieve JSON data from VAULT_BUMBLEBEE_OPTIONS''' + '''Allows you to retrieve JSON data from VAULT_BUMBLEBEE_OPTIONS or VAULT_NECTAR_OPTIONS''' - opts = current_app.config.get('VAULT_BUMBLEBEE_OPTIONS') or {} + if urllib.parse.urlparse(request.referrer).netloc in current_app.config.get("NECTAR_REFERRERS",["dev.scixplorer.org"]): + opts = current_app.config.get('VAULT_NECTAR_OPTIONS') or current_app.config.get('VAULT_BUMBLEBEE_OPTIONS') or {} + else: + opts = current_app.config.get('VAULT_BUMBLEBEE_OPTIONS') or {} if not isinstance(opts, dict): return json.dumps({'msg': 'Server misconfiguration, VAULT_BUMBLEBEE_OPTIONS is of an invalid type'}), 500