mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Get api_keys for service endpoint
This commit is contained in:
@@ -30,7 +30,7 @@ class TemplateSchema(ma.ModelSchema):
|
||||
class ApiKeySchema(ma.ModelSchema):
|
||||
class Meta:
|
||||
model = models.ApiKey
|
||||
exclude = ("service", "secret", "expiry_date")
|
||||
exclude = ("service", "secret")
|
||||
|
||||
|
||||
class JobSchema(ma.ModelSchema):
|
||||
|
||||
@@ -13,7 +13,7 @@ from app.dao.templates_dao import (
|
||||
from app.dao.api_key_dao import (save_model_api_key, get_model_api_keys, get_unsigned_secret)
|
||||
from app.models import ApiKey
|
||||
from app.schemas import (
|
||||
services_schema, service_schema, template_schema)
|
||||
services_schema, service_schema, template_schema, api_keys_schema)
|
||||
|
||||
from flask import Blueprint
|
||||
service = Blueprint('service', __name__)
|
||||
@@ -109,6 +109,23 @@ def revoke_api_key(service_id, api_key_id):
|
||||
return jsonify(), 202
|
||||
|
||||
|
||||
@service.route('/<int:service_id>/api-key', methods=['GET'])
|
||||
def get_api_keys(service_id):
|
||||
try:
|
||||
service = get_model_services(service_id=service_id)
|
||||
except DataError:
|
||||
return jsonify(result="error", message="Invalid service id"), 400
|
||||
except NoResultFound:
|
||||
return jsonify(result="error", message="Service not found"), 404
|
||||
|
||||
try:
|
||||
api_keys = get_model_api_keys(service_id=service_id)
|
||||
except DAOException as e:
|
||||
return jsonify(result='error', message=str(e)), 400
|
||||
|
||||
return jsonify(apiKeys=api_keys_schema.dump(api_keys)), 200
|
||||
|
||||
|
||||
@service.route('/<int:service_id>/template', methods=['POST'])
|
||||
def create_template(service_id):
|
||||
try:
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import json
|
||||
from datetime import timedelta, datetime
|
||||
|
||||
from flask import url_for
|
||||
|
||||
from app.models import ApiKey
|
||||
from dao.api_key_dao import save_model_api_key
|
||||
from tests import create_authorization_header
|
||||
|
||||
|
||||
@@ -82,3 +84,26 @@ def test_api_key_should_create_multiple_new_api_key_for_service(notify_api, noti
|
||||
assert response2.status_code == 201
|
||||
assert response2.get_data != response.get_data
|
||||
assert ApiKey.query.count() == 2
|
||||
|
||||
|
||||
def test_get_api_keys_should_return_all_keys_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:
|
||||
api_key2 = ApiKey(**{'service_id': sample_api_key.service_id, 'name': 'second_api_key'})
|
||||
api_key3 = ApiKey(**{'service_id': sample_api_key.service_id, 'name': 'third_api_key',
|
||||
'expiry_date': datetime.utcnow() + timedelta(hours=-1)})
|
||||
save_model_api_key(api_key2)
|
||||
save_model_api_key(api_key3)
|
||||
assert ApiKey.query.count() == 3
|
||||
|
||||
auth_header = create_authorization_header(path=url_for('service.get_api_keys',
|
||||
service_id=sample_api_key.service_id),
|
||||
method='GET')
|
||||
response = client.get(url_for('service.get_api_keys',
|
||||
service_id=sample_api_key.service_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']) == 3
|
||||
|
||||
Reference in New Issue
Block a user