mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-09 05:13:12 -04:00
Refactored tests for inbound api
This commit is contained in:
@@ -289,24 +289,19 @@ def service_set_reply_to_email(service_id):
|
|||||||
def service_set_sms_sender(service_id):
|
def service_set_sms_sender(service_id):
|
||||||
form = ServiceSmsSender()
|
form = ServiceSmsSender()
|
||||||
|
|
||||||
def update_service(permissions, sms_sender):
|
|
||||||
service_api_client.update_service_with_properties(
|
|
||||||
current_service['id'],
|
|
||||||
{'permissions': permissions, 'sms_sender': sms_sender}
|
|
||||||
)
|
|
||||||
|
|
||||||
set_inbound_sms = request.args.get('set_inbound_sms')
|
|
||||||
if set_inbound_sms == 'True':
|
|
||||||
if 'inbound_sms' in current_service['permissions']:
|
|
||||||
current_service['permissions'].remove('inbound_sms')
|
|
||||||
update_service(current_service['permissions'], current_service['sms_sender'])
|
|
||||||
return redirect(url_for('.service_settings', service_id=service_id))
|
|
||||||
|
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
|
set_inbound_sms = request.args.get('set_inbound_sms', False)
|
||||||
if set_inbound_sms == 'True':
|
if set_inbound_sms == 'True':
|
||||||
permissions = current_service['permissions']
|
permissions = current_service['permissions']
|
||||||
permissions.append('inbound_sms')
|
if 'inbound_sms' in permissions:
|
||||||
update_service(permissions, form.sms_sender.data)
|
permissions.remove('inbound_sms')
|
||||||
|
else:
|
||||||
|
permissions.append('inbound_sms')
|
||||||
|
service_api_client.update_service_with_properties(
|
||||||
|
current_service['id'],
|
||||||
|
{'permissions': permissions,
|
||||||
|
'sms_sender': form.sms_sender.data or None}
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
service_api_client.update_service(
|
service_api_client.update_service(
|
||||||
current_service['id'],
|
current_service['id'],
|
||||||
@@ -455,7 +450,7 @@ def service_set_inbound_api(service_id):
|
|||||||
url=form.url.data,
|
url=form.url.data,
|
||||||
bearer_token=form.bearer_token.data,
|
bearer_token=form.bearer_token.data,
|
||||||
user_id=current_user.id,
|
user_id=current_user.id,
|
||||||
inbound_api_id=inbound_api.get('id') if inbound_api else ''
|
inbound_api_id=inbound_api.get('id')
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
service_api_client.create_service_inbound_api(
|
service_api_client.create_service_inbound_api(
|
||||||
|
|||||||
@@ -56,11 +56,14 @@ def service_json(
|
|||||||
created_at=None,
|
created_at=None,
|
||||||
letter_contact_block=None,
|
letter_contact_block=None,
|
||||||
permissions=None,
|
permissions=None,
|
||||||
|
inbound_api=None,
|
||||||
):
|
):
|
||||||
if users is None:
|
if users is None:
|
||||||
users = []
|
users = []
|
||||||
if permissions is None:
|
if permissions is None:
|
||||||
permissions = []
|
permissions = []
|
||||||
|
if inbound_api is None:
|
||||||
|
inbound_api = []
|
||||||
return {
|
return {
|
||||||
'id': id_,
|
'id': id_,
|
||||||
'name': name,
|
'name': name,
|
||||||
@@ -80,6 +83,7 @@ def service_json(
|
|||||||
'letter_contact_block': letter_contact_block,
|
'letter_contact_block': letter_contact_block,
|
||||||
'dvla_organisation': '001',
|
'dvla_organisation': '001',
|
||||||
'permissions': permissions,
|
'permissions': permissions,
|
||||||
|
'inbound_api': inbound_api,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,25 @@ def test_should_show_overview(
|
|||||||
app.service_api_client.get_service.assert_called_with(service_one['id'])
|
app.service_api_client.get_service.assert_called_with(service_one['id'])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('permissions, expected_rows', [
|
||||||
|
(['email', 'sms', 'inbound_sms'], [
|
||||||
|
'Service name service one Change',
|
||||||
|
'Email reply to address test@example.com Change',
|
||||||
|
'Text message sender elevenchars',
|
||||||
|
'International text messages On Change',
|
||||||
|
'Receive text messages On Change',
|
||||||
|
'API endpoint for received text messages None Change',
|
||||||
|
'Letters Off Change',
|
||||||
|
]),
|
||||||
|
(['email', 'sms'], [
|
||||||
|
'Service name service one Change',
|
||||||
|
'Email reply to address test@example.com Change',
|
||||||
|
'Text message sender elevenchars Change',
|
||||||
|
'International text messages On Change',
|
||||||
|
'Receive text messages Off Change',
|
||||||
|
'Letters Off Change',
|
||||||
|
]),
|
||||||
|
])
|
||||||
def test_should_show_overview_for_service_with_more_things_set(
|
def test_should_show_overview_for_service_with_more_things_set(
|
||||||
client,
|
client,
|
||||||
active_user_with_permissions,
|
active_user_with_permissions,
|
||||||
@@ -67,22 +86,17 @@ def test_should_show_overview_for_service_with_more_things_set(
|
|||||||
service_with_reply_to_addresses,
|
service_with_reply_to_addresses,
|
||||||
mock_get_organisation,
|
mock_get_organisation,
|
||||||
mock_get_letter_organisations,
|
mock_get_letter_organisations,
|
||||||
|
permissions,
|
||||||
|
expected_rows
|
||||||
):
|
):
|
||||||
client.login(active_user_with_permissions, mocker, service_with_reply_to_addresses)
|
client.login(active_user_with_permissions, mocker, service_with_reply_to_addresses)
|
||||||
service_with_reply_to_addresses['permissions'] = ['inbound_sms']
|
service_with_reply_to_addresses['permissions'] = permissions
|
||||||
service_with_reply_to_addresses['can_send_international_sms'] = True
|
service_with_reply_to_addresses['can_send_international_sms'] = True
|
||||||
response = client.get(url_for(
|
response = client.get(url_for(
|
||||||
'main.service_settings', service_id=service_with_reply_to_addresses['id']
|
'main.service_settings', service_id=service_with_reply_to_addresses['id']
|
||||||
))
|
))
|
||||||
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
for index, row in enumerate([
|
for index, row in enumerate(expected_rows):
|
||||||
'Service name service one Change',
|
|
||||||
'Email reply to address test@example.com Change',
|
|
||||||
'Text message sender elevenchars',
|
|
||||||
'International text messages On Change',
|
|
||||||
'Receive text messages On Change',
|
|
||||||
'Letters Off Change',
|
|
||||||
]):
|
|
||||||
assert row == " ".join(page.find_all('tr')[index + 1].text.split())
|
assert row == " ".join(page.find_all('tr')[index + 1].text.split())
|
||||||
|
|
||||||
|
|
||||||
@@ -736,6 +750,34 @@ def test_set_text_message_sender_validation(
|
|||||||
assert not mock_update_service.called
|
assert not mock_update_service.called
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('url, bearer_token, expected_errors', [
|
||||||
|
("", "", "Can’t be empty Can’t be empty"),
|
||||||
|
("http://not_https.com", "1234567890", "Must be a valid https url"),
|
||||||
|
("https://test.com", "123456789", "Must be at least 10 characters"),
|
||||||
|
])
|
||||||
|
def test_set_inbound_api_validation(
|
||||||
|
logged_in_client,
|
||||||
|
mock_update_service,
|
||||||
|
service_one,
|
||||||
|
mock_get_letter_organisations,
|
||||||
|
url,
|
||||||
|
bearer_token,
|
||||||
|
expected_errors,
|
||||||
|
):
|
||||||
|
service_one['permissions'] = ['inbound_sms']
|
||||||
|
response = logged_in_client.post(url_for(
|
||||||
|
'main.service_set_inbound_api',
|
||||||
|
service_id=service_one['id']),
|
||||||
|
data={"url": url, "bearer_token": bearer_token}
|
||||||
|
)
|
||||||
|
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
|
||||||
|
error_msgs = ' '.join(msg.text.strip() for msg in page.select(".error-message"))
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert error_msgs == expected_errors
|
||||||
|
assert not mock_update_service.called
|
||||||
|
|
||||||
|
|
||||||
def test_if_sms_sender_set_then_form_populated(
|
def test_if_sms_sender_set_then_form_populated(
|
||||||
logged_in_client,
|
logged_in_client,
|
||||||
service_one,
|
service_one,
|
||||||
@@ -1008,6 +1050,65 @@ def test_switch_service_disable_international_sms(
|
|||||||
assert mocked_fn.call_args == call(service_one['id'], {"can_send_international_sms": False})
|
assert mocked_fn.call_args == call(service_one['id'], {"can_send_international_sms": False})
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_new_inbound_api_and_valid_bearer_token_calls_create_inbound_api_endpoint(
|
||||||
|
logged_in_platform_admin_client,
|
||||||
|
service_one,
|
||||||
|
mocker,
|
||||||
|
):
|
||||||
|
service_one['permissions'] = ['inbound_sms']
|
||||||
|
service_one['inbound_api'] = []
|
||||||
|
|
||||||
|
mocked_post_fn = mocker.patch('app.service_api_client.post', return_value=service_one)
|
||||||
|
|
||||||
|
inbound_api_data = {'url': "https://test.url.com/", 'bearer_token': '1234567890'}
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for(
|
||||||
|
'main.service_set_inbound_api',
|
||||||
|
service_id=service_one['id']
|
||||||
|
),
|
||||||
|
data=inbound_api_data
|
||||||
|
)
|
||||||
|
assert response.status_code == 302
|
||||||
|
assert response.location == url_for('main.service_settings', service_id=service_one['id'], _external=True)
|
||||||
|
assert mocked_post_fn.called
|
||||||
|
|
||||||
|
inbound_api_data['updated_by_id'] = service_one['users'][0]
|
||||||
|
assert mocked_post_fn.call_args == call("/service/{}/inbound-api".format(service_one['id']), inbound_api_data)
|
||||||
|
|
||||||
|
|
||||||
|
def test_update_inbound_api_and_valid_bearer_token_calls_update_inbound_api_endpoint(
|
||||||
|
logged_in_platform_admin_client,
|
||||||
|
service_one,
|
||||||
|
mocker,
|
||||||
|
fake_uuid
|
||||||
|
):
|
||||||
|
service_one['permissions'] = ['inbound_sms']
|
||||||
|
service_one['inbound_api'] = [fake_uuid]
|
||||||
|
|
||||||
|
mocked_get_fn = mocker.patch(
|
||||||
|
'app.service_api_client.get',
|
||||||
|
return_value={'data': {'id': fake_uuid, 'url': "https://test.url.com/"}})
|
||||||
|
mocked_post_fn = mocker.patch('app.service_api_client.post', return_value=service_one)
|
||||||
|
|
||||||
|
inbound_api_data = {'url': "https://test.url.com/", 'bearer_token': '1234567890', 'inbound_api_id': fake_uuid}
|
||||||
|
response = logged_in_platform_admin_client.post(
|
||||||
|
url_for(
|
||||||
|
'main.service_set_inbound_api',
|
||||||
|
service_id=service_one['id']
|
||||||
|
),
|
||||||
|
data=inbound_api_data
|
||||||
|
)
|
||||||
|
assert response.status_code == 302
|
||||||
|
assert response.location == url_for('main.service_settings', service_id=service_one['id'], _external=True)
|
||||||
|
assert mocked_post_fn.called
|
||||||
|
|
||||||
|
del inbound_api_data['inbound_api_id']
|
||||||
|
inbound_api_data['updated_by_id'] = service_one['users'][0]
|
||||||
|
|
||||||
|
assert mocked_post_fn.call_args == call(
|
||||||
|
"/service/{}/inbound-api/{}".format(service_one['id'], fake_uuid), inbound_api_data)
|
||||||
|
|
||||||
|
|
||||||
def test_archive_service_after_confirm(
|
def test_archive_service_after_confirm(
|
||||||
logged_in_platform_admin_client,
|
logged_in_platform_admin_client,
|
||||||
service_one,
|
service_one,
|
||||||
|
|||||||
Reference in New Issue
Block a user