mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-22 16:31:15 -05:00
Updated get_api_keys endpoint to return one or many given key_id or not.
This commit is contained in:
@@ -110,7 +110,8 @@ def revoke_api_key(service_id, api_key_id):
|
||||
|
||||
|
||||
@service.route('/<int:service_id>/api-keys', methods=['GET'])
|
||||
def get_api_keys(service_id):
|
||||
@service.route('/<int:service_id>/api-keys/<int:key_id>', 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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user