attach api_key to app

we previously attached the service id and the key's secret
also more refactoring of auth.py
This commit is contained in:
Leo Hemsted
2016-06-29 16:33:02 +01:00
parent adbe02783d
commit 39519e3f36
3 changed files with 48 additions and 31 deletions

View File

@@ -1,6 +1,10 @@
from datetime import datetime
from notifications_python_client.authentication import create_jwt_token
import pytest
from flask import json, current_app
from notifications_python_client.authentication import create_jwt_token
from app import api_user
from app.dao.api_key_dao import get_unsigned_secrets, save_model_api_key, get_unsigned_secret, expire_api_key
from app.models import ApiKey, KEY_TYPE_NORMAL, KEY_TYPE_TEAM
@@ -164,7 +168,7 @@ def test_authentication_returns_token_expired_when_service_uses_expired_key_and_
headers={'Authorization': 'Bearer {}'.format(token)})
assert response.status_code == 403
data = json.loads(response.get_data())
assert data['message'] == {"token": ['Invalid token: signature']}
assert data['message'] == {"token": ['Invalid token: revoked']}
def test_authentication_returns_error_when_api_client_has_no_secrets(notify_api,
@@ -220,3 +224,17 @@ def __create_post_token(service_id, request_body):
secret=get_unsigned_secrets(service_id)[0],
client_id=str(service_id)
)
def test_should_attach_the_current_api_key_to_current_app(notify_api, sample_service, sample_api_key):
with notify_api.test_request_context() as context, notify_api.test_client() as client:
with pytest.raises(AttributeError):
print(api_user)
token = __create_get_token(sample_api_key.service_id)
response = client.get(
'/service/{}'.format(str(sample_api_key.service_id)),
headers={'Authorization': 'Bearer {}'.format(token)}
)
assert response.status_code == 200
assert api_user == sample_api_key