From 0027b62224ae84f3c25d97409ea5c5fcc9d483a9 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 21 Jan 2016 12:13:17 +0000 Subject: [PATCH] Updated get_api_keys endpoint to return one or many given key_id or not. --- app/service/rest.py | 10 ++++++++-- tests/app/service/test_api_key_endpoints.py | 18 ++++++++++++++++++ tests/app/user/test_rest.py | 9 +++++---- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/service/rest.py b/app/service/rest.py index e3c9520f2..eddab42bb 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -110,7 +110,8 @@ def revoke_api_key(service_id, api_key_id): @service.route('//api-keys', methods=['GET']) -def get_api_keys(service_id): +@service.route('//api-keys/', methods=['GET']) +def get_api_keys(service_id, key_id=None): try: service = get_model_services(service_id=service_id) except DataError: @@ -119,9 +120,14 @@ def get_api_keys(service_id): return jsonify(result="error", message="Service not found"), 404 try: - api_keys = get_model_api_keys(service_id=service_id) + if key_id: + api_keys = [get_model_api_keys(service_id=service_id, id=key_id)] + else: + api_keys = get_model_api_keys(service_id=service_id) except DAOException as e: return jsonify(result='error', message=str(e)), 400 + except NoResultFound: + return jsonify(result="error", message="API key not found"), 404 return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200 diff --git a/tests/app/service/test_api_key_endpoints.py b/tests/app/service/test_api_key_endpoints.py index 1bc0187ac..d288f995b 100644 --- a/tests/app/service/test_api_key_endpoints.py +++ b/tests/app/service/test_api_key_endpoints.py @@ -113,3 +113,21 @@ def test_get_api_keys_should_return_all_keys_for_service(notify_api, notify_db, assert response.status_code == 200 json_resp = json.loads(response.get_data(as_text=True)) assert len(json_resp['apiKeys']) == 3 + + +def test_get_api_keys_should_return_one_key_for_service(notify_api, notify_db, + notify_db_session, + sample_api_key): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + auth_header = create_authorization_header(path=url_for('service.get_api_keys', + service_id=sample_api_key.service_id, + key_id=sample_api_key.id), + method='GET') + response = client.get(url_for('service.get_api_keys', + service_id=sample_api_key.service_id, + key_id=sample_api_key.id), + headers=[('Content-Type', 'application/json'), auth_header]) + assert response.status_code == 200 + json_resp = json.loads(response.get_data(as_text=True)) + assert len(json_resp['apiKeys']) == 1 diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index 0c01dae75..f55d92118 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -281,11 +281,12 @@ def test_get_user_service_user_not_exists(notify_api, notify_db, notify_db_sessi with notify_api.test_client() as client: assert Service.query.count() == 2 auth_header = create_authorization_header(service_id=sample_admin_service_id, - path=url_for('user.get_service_by_user_id', user_id="123", + path=url_for('user.get_service_by_user_id', user_id="123423", service_id=sample_service.id), method='GET') + print('** service users{}'.format(sample_service.users[0].id)) resp = client.get( - url_for('user.get_service_by_user_id', user_id="123", service_id=sample_service.id), + url_for('user.get_service_by_user_id', user_id="123423", service_id=sample_service.id), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 json_resp = json.loads(resp.get_data(as_text=True)) @@ -303,10 +304,10 @@ def test_get_user_service_service_not_exists(notify_api, notify_db, notify_db_se assert Service.query.count() == 2 auth_header = create_authorization_header(service_id=sample_admin_service_id, path=url_for('user.get_service_by_user_id', user_id=user.id, - service_id="123"), + service_id="12323423"), method='GET') resp = client.get( - url_for('user.get_service_by_user_id', user_id=user.id, service_id="123"), + url_for('user.get_service_by_user_id', user_id=user.id, service_id="12323423"), headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 json_resp = json.loads(resp.get_data(as_text=True))