mirror of
https://github.com/GSA/notifications-api.git
synced 2026-04-02 16:41:08 -04:00
[WIP] use correct dao function for adding user to service.
Check for no user returned from user dao and return correct error message.
This commit is contained in:
@@ -19,8 +19,10 @@ from app.dao.services_dao import (
|
||||
dao_fetch_all_services,
|
||||
dao_create_service,
|
||||
dao_update_service,
|
||||
dao_fetch_all_services_by_user
|
||||
dao_fetch_all_services_by_user,
|
||||
dao_add_user_to_service
|
||||
)
|
||||
|
||||
from app.dao.users_dao import get_model_users
|
||||
from app.models import ApiKey
|
||||
from app.schemas import (
|
||||
@@ -162,12 +164,15 @@ def add_user_to_service(service_id, user_id):
|
||||
return _service_not_found(service_id)
|
||||
user = get_model_users(user_id=user_id)
|
||||
|
||||
if not user:
|
||||
return jsonify(result='error',
|
||||
message='User not found for id: {}'.format(user_id)), 400
|
||||
|
||||
if user in service.users:
|
||||
return jsonify(result='error',
|
||||
message='User id: {} already part of service id: {}'.format(user_id, service_id)), 400
|
||||
|
||||
service.users.append(user)
|
||||
dao_update_service(service)
|
||||
dao_add_user_to_service(service, user)
|
||||
|
||||
data, errors = service_schema.dump(service)
|
||||
return jsonify(data=data), 201
|
||||
|
||||
@@ -575,6 +575,30 @@ def test_add_existing_user_of_service_to_service_returns400(notify_api, notify_d
|
||||
assert result['message'] == expected_message
|
||||
|
||||
|
||||
def test_add_unknown_user_to_service_returns400(notify_api, notify_db, notify_db_session, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
|
||||
incorrect_id = 9876
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
path='/service/{}/users/{}'.format(sample_service.id, incorrect_id),
|
||||
method='POST'
|
||||
)
|
||||
|
||||
resp = client.post(
|
||||
'/service/{}/users/{}'.format(sample_service.id, incorrect_id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
result = json.loads(resp.get_data(as_text=True))
|
||||
expected_message = 'User not found for id: {}'.format(incorrect_id)
|
||||
|
||||
assert resp.status_code == 400
|
||||
assert result['result'] == 'error'
|
||||
assert result['message'] == expected_message
|
||||
|
||||
|
||||
def _user_email_in_list(user_list, email_address):
|
||||
for user in user_list:
|
||||
if user['email_address'] == email_address:
|
||||
|
||||
Reference in New Issue
Block a user