mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
New endpoint to get users for service id.
/service/<service_id>/users returns a list of all users associated with the service
This commit is contained in:
@@ -27,8 +27,8 @@ from app.models import ApiKey
|
|||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
services_schema,
|
services_schema,
|
||||||
service_schema,
|
service_schema,
|
||||||
api_keys_schema
|
api_keys_schema,
|
||||||
)
|
users_schema)
|
||||||
from app import email_safe
|
from app import email_safe
|
||||||
|
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
@@ -155,3 +155,13 @@ def get_api_keys(service_id, key_id=None):
|
|||||||
return jsonify(result="error", message="API key not found"), 404
|
return jsonify(result="error", message="API key not found"), 404
|
||||||
|
|
||||||
return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200
|
return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200
|
||||||
|
|
||||||
|
|
||||||
|
@service.route('/<service_id>/users', methods=['GET'])
|
||||||
|
def get_users_for_service(service_id):
|
||||||
|
fetched = dao_fetch_service_by_id(service_id)
|
||||||
|
if not fetched:
|
||||||
|
return jsonify(result="error", message="Service not found"), 404
|
||||||
|
print(fetched.users)
|
||||||
|
result = users_schema.dump(fetched.users)
|
||||||
|
return jsonify(data=result.data)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import uuid
|
|||||||
from app.dao.users_dao import save_model_user
|
from app.dao.users_dao import save_model_user
|
||||||
from app.models import User
|
from app.models import User
|
||||||
from tests import create_authorization_header
|
from tests import create_authorization_header
|
||||||
|
from tests.app.conftest import sample_service as create_service
|
||||||
|
|
||||||
|
|
||||||
def test_get_service_list(notify_api, service_factory):
|
def test_get_service_list(notify_api, service_factory):
|
||||||
@@ -341,3 +342,27 @@ def test_update_service_should_404_if_id_is_invalid(notify_api, notify_db):
|
|||||||
headers=[('Content-Type', 'application/json'), auth_header]
|
headers=[('Content-Type', 'application/json'), auth_header]
|
||||||
)
|
)
|
||||||
assert resp.status_code == 404
|
assert resp.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_users_by_service(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
with notify_api.test_request_context():
|
||||||
|
with notify_api.test_client() as client:
|
||||||
|
|
||||||
|
sample_service = create_service(notify_db=notify_db, notify_db_session=notify_db_session,
|
||||||
|
service_name='Sample service', user=sample_user)
|
||||||
|
auth_header = create_authorization_header(
|
||||||
|
path='/service/{}/users'.format(sample_service.id),
|
||||||
|
method='GET'
|
||||||
|
)
|
||||||
|
|
||||||
|
resp = client.get(
|
||||||
|
'/service/{}/users'.format(sample_service.id),
|
||||||
|
headers=[('Content-Type', 'application/json'), auth_header]
|
||||||
|
)
|
||||||
|
|
||||||
|
assert resp.status_code == 200
|
||||||
|
result = json.loads(resp.get_data(as_text=True))
|
||||||
|
assert len(result['data']) == 1
|
||||||
|
assert result['data'][0]['name'] == sample_user.name
|
||||||
|
assert result['data'][0]['email_address'] == sample_user.email_address
|
||||||
|
assert result['data'][0]['mobile_number'] == sample_user.mobile_number
|
||||||
|
|||||||
Reference in New Issue
Block a user