-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from NYPL/SFR-1827_New_fulfill_endpoint
Add fulfill route and tests
- Loading branch information
Showing
6 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import json | ||
import os | ||
|
||
import jwt | ||
|
||
from flask import Blueprint, request | ||
from ..utils import APIUtils | ||
from logger import createLog | ||
|
||
JWT_ALGORITHM = '' | ||
logger = createLog(__name__) | ||
|
||
fulfill = Blueprint('fulfill', __name__, url_prefix='/fulfill') | ||
|
||
@fulfill.route('/<uuid>', methods=['GET']) | ||
def workFulfill(uuid): | ||
logger.info('Checking if authorization is needed for work {}'.format(uuid)) | ||
|
||
requires_authorization = True | ||
|
||
if requires_authorization: | ||
try: | ||
bearer = request.headers.get('Authorization') | ||
token = bearer.split()[1] | ||
|
||
jwt_secret = os.environ['NYPL_API_CLIENT_PUBLIC_KEY'] | ||
decoded_token =(jwt.decode(token, jwt_secret, 'RS256', | ||
audience="app_myaccount")) | ||
if json.loads(json.dumps(decoded_token))['iss'] == "https://www.nypl.org": | ||
statusCode = 200 | ||
responseBody = uuid | ||
else: | ||
statusCode = 401 | ||
responseBody = 'Invalid access token' | ||
|
||
except jwt.exceptions.ExpiredSignatureError: | ||
statusCode = 401 | ||
responseBody = 'Expired access token' | ||
except (jwt.exceptions.DecodeError, UnicodeDecodeError, IndexError, AttributeError): | ||
statusCode = 401 | ||
responseBody = 'Invalid access token' | ||
except ValueError: | ||
logger.warning("Could not deserialize NYPL-issued public key") | ||
statusCode = 500 | ||
responseBody = 'Server error' | ||
|
||
else: | ||
# TODO: In the future, this could record an analytics timestamp | ||
# and redirect to URL of a work if authentication is not required. | ||
# For now, only use /fulfill endpoint in response if authentication is required. | ||
statusCode = 400 | ||
responseBody = "Bad Request" | ||
|
||
response = APIUtils.formatResponseObject( | ||
statusCode, 'fulfill', responseBody | ||
) | ||
|
||
if statusCode == 401: | ||
response[0].headers['WWW-Authenticate'] = 'Bearer' | ||
|
||
return response | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from flask import Flask | ||
import pytest | ||
|
||
import jwt | ||
|
||
from api.blueprints.drbFulfill import workFulfill | ||
from api.utils import APIUtils | ||
|
||
|
||
class TestSearchBlueprint: | ||
@pytest.fixture | ||
def mockUtils(self, mocker): | ||
return mocker.patch.multiple( | ||
APIUtils, | ||
formatResponseObject=mocker.DEFAULT | ||
) | ||
|
||
@pytest.fixture | ||
def testApp(self): | ||
flaskApp = Flask('test') | ||
flaskApp.config['DB_CLIENT'] = 'testDBClient' | ||
flaskApp.config['READER_VERSION'] = 'test' | ||
|
||
return flaskApp | ||
|
||
def test_workFulfill_invalid_token(self, testApp, mockUtils, monkeypatch): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': 'Bearer Whatever'}): | ||
monkeypatch.setenv('NYPL_API_CLIENT_PUBLIC_KEY', "SomeKeyValue") | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_no_bearer_auth(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': 'Whatever'}): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_empty_token(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345', | ||
headers={'Authorization': ''}): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
def test_workFulfill_no_header(self, testApp, mockUtils): | ||
with testApp.test_request_context('/fulfill/12345'): | ||
workFulfill('12345') | ||
mockUtils['formatResponseObject'].assert_called_once_with( | ||
401, 'fulfill', 'Invalid access token') | ||
|
||
|
||
|