mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-28 11:49:42 -04:00
Allow add_user_to_service endpoint to accept new data format
The data posted to the `add_user_to_service` endpoint is currently sent as a
list of permissions:
`[{'permission': MANAGE_SETTINGS}, {'permission': MANAGE_TEMPLATES}]`.
This endpoint is going to also be used for folder permissions, so the
data now needs to be nested:
`{'permissions': [{'permission': MANAGE_SETTINGS}, {'permission': MANAGE_TEMPLATES}]}`
This changes the add_user_to_service endpoint to accept data in either
format. Once admin is sending data in the new format, the code can be
simplified.
This commit is contained in:
@@ -285,7 +285,13 @@ def add_user_to_service(service_id, user_id):
|
||||
error = 'User id: {} already part of service id: {}'.format(user_id, service_id)
|
||||
raise InvalidRequest(error, status_code=400)
|
||||
|
||||
permissions = permission_schema.load(request.get_json(), many=True).data
|
||||
data = request.get_json()
|
||||
if 'permissions' in data:
|
||||
user_permissions = data['permissions']
|
||||
else:
|
||||
user_permissions = data
|
||||
|
||||
permissions = permission_schema.load(user_permissions, many=True).data
|
||||
dao_add_user_to_service(service, user, permissions)
|
||||
data = service_schema.dump(service).data
|
||||
return jsonify(data=data), 201
|
||||
|
||||
@@ -1154,6 +1154,86 @@ def test_add_existing_user_to_another_service_with_all_permissions(notify_api,
|
||||
assert sorted(expected_permissions) == sorted(permissions)
|
||||
|
||||
|
||||
def test_add_existing_user_to_another_service_with_all_permissions_with_new_data_format(
|
||||
notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_user
|
||||
):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
# check which users part of service
|
||||
user_already_in_service = sample_service.users[0]
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
resp = client.get(
|
||||
'/service/{}/users'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
)
|
||||
|
||||
assert resp.status_code == 200
|
||||
result = resp.json
|
||||
assert len(result['data']) == 1
|
||||
assert result['data'][0]['email_address'] == user_already_in_service.email_address
|
||||
|
||||
# add new user to service
|
||||
user_to_add = User(
|
||||
name='Invited User',
|
||||
email_address='invited@digital.cabinet-office.gov.uk',
|
||||
password='password',
|
||||
mobile_number='+4477123456'
|
||||
)
|
||||
# they must exist in db first
|
||||
save_model_user(user_to_add)
|
||||
|
||||
data = {
|
||||
"permissions": [
|
||||
{"permission": "send_emails"},
|
||||
{"permission": "send_letters"},
|
||||
{"permission": "send_texts"},
|
||||
{"permission": "manage_users"},
|
||||
{"permission": "manage_settings"},
|
||||
{"permission": "manage_api_keys"},
|
||||
{"permission": "manage_templates"},
|
||||
{"permission": "view_activity"},
|
||||
]
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
resp = client.post(
|
||||
'/service/{}/users/{}'.format(sample_service.id, user_to_add.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
data=json.dumps(data)
|
||||
)
|
||||
|
||||
assert resp.status_code == 201
|
||||
|
||||
# check new user added to service
|
||||
auth_header = create_authorization_header()
|
||||
|
||||
resp = client.get(
|
||||
'/service/{}'.format(sample_service.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header],
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
json_resp = resp.json
|
||||
assert str(user_to_add.id) in json_resp['data']['users']
|
||||
|
||||
# check user has all permissions
|
||||
auth_header = create_authorization_header()
|
||||
resp = client.get(url_for('user.get_user', user_id=user_to_add.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert resp.status_code == 200
|
||||
json_resp = resp.json
|
||||
permissions = json_resp['data']['permissions'][str(sample_service.id)]
|
||||
expected_permissions = ['send_texts', 'send_emails', 'send_letters', 'manage_users',
|
||||
'manage_settings', 'manage_templates', 'manage_api_keys', 'view_activity']
|
||||
assert sorted(expected_permissions) == sorted(permissions)
|
||||
|
||||
|
||||
def test_add_existing_user_to_another_service_with_send_permissions(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
|
||||
Reference in New Issue
Block a user