mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-16 14:20:57 -04:00
add only_active flag to GET /services/
does what it says on the tin
This commit is contained in:
@@ -28,12 +28,17 @@ from app.models import (
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
def dao_fetch_all_services():
|
||||
return Service.query.order_by(
|
||||
def dao_fetch_all_services(only_active=False):
|
||||
query = Service.query.order_by(
|
||||
asc(Service.created_at)
|
||||
).options(
|
||||
joinedload('users')
|
||||
).all()
|
||||
)
|
||||
|
||||
if only_active:
|
||||
query = query.filter(Service.active)
|
||||
|
||||
return query.all()
|
||||
|
||||
|
||||
def dao_fetch_service_by_id(service_id):
|
||||
|
||||
@@ -67,7 +67,7 @@ def get_services():
|
||||
elif request.args.get('detailed') == 'True':
|
||||
return jsonify(data=get_detailed_services())
|
||||
else:
|
||||
services = dao_fetch_all_services()
|
||||
services = dao_fetch_all_services(only_active=request.args.get('only_active') == 'True')
|
||||
data = service_schema.dump(services, many=True).data
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@@ -129,6 +129,7 @@ def sample_service(notify_db,
|
||||
notify_db_session,
|
||||
service_name="Sample service",
|
||||
user=None,
|
||||
active=True,
|
||||
restricted=False,
|
||||
limit=1000,
|
||||
email_from=None):
|
||||
@@ -139,7 +140,7 @@ def sample_service(notify_db,
|
||||
data = {
|
||||
'name': service_name,
|
||||
'message_limit': limit,
|
||||
'active': True,
|
||||
'active': active,
|
||||
'restricted': restricted,
|
||||
'email_from': email_from,
|
||||
'created_by': user
|
||||
|
||||
@@ -38,6 +38,23 @@ def test_get_service_list(notify_api, service_factory):
|
||||
assert json_resp['data'][2]['name'] == 'three'
|
||||
|
||||
|
||||
def test_get_service_list_with_only_active_flag(client, service_factory):
|
||||
inactive = service_factory.get('one', email_from='one')
|
||||
active = service_factory.get('two', email_from='two')
|
||||
|
||||
inactive.active = False
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
response = client.get(
|
||||
'/service?only_active=True',
|
||||
headers=[auth_header]
|
||||
)
|
||||
assert response.status_code == 200
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_resp['data']) == 1
|
||||
assert json_resp['data'][0]['id'] == str(active.id)
|
||||
|
||||
|
||||
def test_get_service_list_by_user(notify_db, notify_db_session, client, sample_user, service_factory):
|
||||
other_user = create_sample_user(notify_db, notify_db_session, email='foo@bar.gov.uk')
|
||||
service_factory.get('one', sample_user, email_from='one')
|
||||
|
||||
Reference in New Issue
Block a user