Merge pull request #1327 from alphagov/create-inbound-api

Create inbound api
This commit is contained in:
kentsanggds
2017-06-22 12:36:28 +01:00
committed by GitHub
7 changed files with 350 additions and 10 deletions

View File

@@ -7,6 +7,7 @@ from bs4 import BeautifulSoup
from werkzeug.exceptions import InternalServerError
import app
from app.main.views.service_settings import dummy_bearer_token
from app.utils import email_safe
from tests import validate_route_permission, service_json
from tests.app.test_utils import normalize_spaces
@@ -60,6 +61,25 @@ def test_should_show_overview(
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(
client,
active_user_with_permissions,
@@ -67,25 +87,56 @@ def test_should_show_overview_for_service_with_more_things_set(
service_with_reply_to_addresses,
mock_get_organisation,
mock_get_letter_organisations,
permissions,
expected_rows
):
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
response = client.get(url_for(
'main.service_settings', service_id=service_with_reply_to_addresses['id']
))
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
for index, row in enumerate([
'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',
]):
for index, row in enumerate(expected_rows):
assert row == " ".join(page.find_all('tr')[index + 1].text.split())
@pytest.mark.parametrize('url, elided_url', [
('https://test.url.com/inbound', 'https://test.url.com...'),
('https://test.url.com/', 'https://test.url.com...'),
('https://test.url.com', 'https://test.url.com'),
])
def test_service_settings_show_elided_api_url_if_needed(
logged_in_platform_admin_client,
service_one,
mock_get_letter_organisations,
mocker,
fake_uuid,
url,
elided_url
):
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': url}})
response = logged_in_platform_admin_client.get(
url_for(
'main.service_settings',
service_id=service_one['id']
)
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
non_empty_trs = [tr.find_all('td') for tr in page.find_all('tr') if tr.find_all('td')]
api_url = [api_setting[1].text.strip() for api_setting in non_empty_trs
if api_setting[0].text.strip() == 'API endpoint for received text messages'][0]
assert api_url == elided_url
def test_if_cant_send_letters_then_cant_see_letter_contact_block(
logged_in_client,
service_one,
@@ -736,6 +787,34 @@ def test_set_text_message_sender_validation(
assert not mock_update_service.called
@pytest.mark.parametrize('url, bearer_token, expected_errors', [
("", "", "Cant be empty Cant 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(
logged_in_client,
service_one,
@@ -1008,6 +1087,111 @@ def test_switch_service_disable_international_sms(
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)
@pytest.mark.parametrize(
'inbound_api_data', [
{'url': "https://test.url.com/inbound", 'bearer_token': dummy_bearer_token},
{'url': "https://test.url.com/inbound", 'bearer_token': '1234567890'},
{'url': "https://test.url.com/", 'bearer_token': 'new_1234567890'},
]
)
def test_update_inbound_api_and_valid_bearer_token_calls_update_inbound_api_endpoint(
logged_in_platform_admin_client,
service_one,
mocker,
fake_uuid,
inbound_api_data
):
service_one['permissions'] = ['inbound_sms']
service_one['inbound_api'] = [fake_uuid]
initial_api_data = {'data': {'id': fake_uuid, 'url': "https://test.url.com/"}}
mocked_get_fn = mocker.patch('app.service_api_client.get', return_value=initial_api_data)
mocked_post_fn = mocker.patch('app.service_api_client.post', return_value=service_one)
response = logged_in_platform_admin_client.get(
url_for(
'main.service_set_inbound_api',
service_id=service_one['id']
)
)
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('input', {'id': 'url'}).get('value') == initial_api_data['data']['url']
assert page.find('input', {'id': 'bearer_token'}).get('value') == dummy_bearer_token
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
if inbound_api_data['bearer_token'] == dummy_bearer_token:
del inbound_api_data['bearer_token']
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_save_inbound_api_without_changes_does_not_update_inbound_api(
logged_in_platform_admin_client,
service_one,
mocker,
fake_uuid
):
service_one['permissions'] = ['inbound_sms']
service_one['inbound_api'] = [fake_uuid]
initial_api_data = {'data': {'id': fake_uuid, 'url': "https://test.url.com/"}}
inbound_api_data = {'url': initial_api_data['data']['url'], 'bearer_token': dummy_bearer_token}
mocked_get_fn = mocker.patch('app.service_api_client.get', return_value=initial_api_data)
mocked_post_fn = mocker.patch('app.service_api_client.post', return_value=service_one)
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 is False
def test_archive_service_after_confirm(
logged_in_platform_admin_client,
service_one,