2016-07-25 15:26:43 +01:00
|
|
|
|
import pytest
|
2018-02-20 11:22:17 +00:00
|
|
|
|
from tests.conftest import SERVICE_ONE_ID, fake_uuid
|
2016-07-25 15:26:43 +01:00
|
|
|
|
|
2018-02-21 14:37:27 +00:00
|
|
|
|
from app import service_api_client
|
2016-07-15 15:23:23 +01:00
|
|
|
|
from app.notify_client.service_api_client import ServiceAPIClient
|
2016-05-23 13:59:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_client_posts_archived_true_when_deleting_template(mocker):
|
|
|
|
|
|
service_id = fake_uuid
|
|
|
|
|
|
template_id = fake_uuid
|
2016-08-11 14:20:43 +01:00
|
|
|
|
mocker.patch('app.notify_client.current_user', id='1')
|
2016-05-23 13:59:33 +01:00
|
|
|
|
|
|
|
|
|
|
expected_data = {
|
|
|
|
|
|
'archived': True,
|
2016-08-11 14:20:43 +01:00
|
|
|
|
'created_by': '1'
|
2016-05-23 13:59:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
expected_url = '/service/{}/template/{}'.format(service_id, template_id)
|
|
|
|
|
|
|
|
|
|
|
|
client = ServiceAPIClient()
|
2016-07-15 15:23:23 +01:00
|
|
|
|
mock_post = mocker.patch('app.notify_client.service_api_client.ServiceAPIClient.post')
|
2016-05-23 13:59:33 +01:00
|
|
|
|
|
|
|
|
|
|
client.delete_service_template(service_id, template_id)
|
|
|
|
|
|
mock_post.assert_called_once_with(expected_url, data=expected_data)
|
2016-07-21 17:32:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
2016-07-25 15:26:43 +01:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
|
'function,params', [
|
|
|
|
|
|
(ServiceAPIClient.get_service, {}),
|
|
|
|
|
|
(ServiceAPIClient.get_detailed_service, {'detailed': True}),
|
|
|
|
|
|
(ServiceAPIClient.get_detailed_service_for_today, {'detailed': True, 'today_only': True})
|
|
|
|
|
|
],
|
|
|
|
|
|
ids=lambda x: x.__name__
|
|
|
|
|
|
)
|
|
|
|
|
|
def test_client_gets_service(mocker, function, params):
|
2016-07-21 17:32:28 +01:00
|
|
|
|
client = ServiceAPIClient()
|
2016-08-10 15:55:13 +01:00
|
|
|
|
mock_get = mocker.patch.object(client, 'get')
|
2016-07-21 17:32:28 +01:00
|
|
|
|
|
2016-07-25 15:26:43 +01:00
|
|
|
|
function(client, 'foo')
|
2016-08-10 15:55:13 +01:00
|
|
|
|
mock_get.assert_called_once_with('/service/foo', params=params)
|
2016-08-11 13:55:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_client_only_updates_allowed_attributes(mocker):
|
2016-08-11 14:20:43 +01:00
|
|
|
|
mocker.patch('app.notify_client.current_user', id='1')
|
2016-08-11 13:55:42 +01:00
|
|
|
|
with pytest.raises(TypeError) as error:
|
|
|
|
|
|
ServiceAPIClient().update_service('service_id', foo='bar')
|
|
|
|
|
|
assert str(error.value) == 'Not allowed to update service attributes: foo'
|
2017-10-04 11:49:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_client_creates_service_with_correct_data(
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
active_user_with_permissions,
|
|
|
|
|
|
fake_uuid,
|
|
|
|
|
|
):
|
|
|
|
|
|
client = ServiceAPIClient()
|
|
|
|
|
|
mock_post = mocker.patch.object(client, 'post')
|
|
|
|
|
|
mocker.patch('app.notify_client.current_user', id='123')
|
|
|
|
|
|
|
|
|
|
|
|
client.create_service(
|
|
|
|
|
|
service_name='My first service',
|
|
|
|
|
|
organisation_type='central_government',
|
|
|
|
|
|
message_limit=1,
|
|
|
|
|
|
restricted=True,
|
|
|
|
|
|
user_id=fake_uuid,
|
|
|
|
|
|
email_from='test@example.com',
|
|
|
|
|
|
)
|
|
|
|
|
|
mock_post.assert_called_once_with(
|
|
|
|
|
|
'/service',
|
|
|
|
|
|
dict(
|
|
|
|
|
|
# Autogenerated arguments
|
|
|
|
|
|
created_by='123',
|
|
|
|
|
|
active=True,
|
|
|
|
|
|
# ‘service_name’ argument is coerced to ‘name’
|
|
|
|
|
|
name='My first service',
|
|
|
|
|
|
# The rest pass through with the same names
|
|
|
|
|
|
organisation_type='central_government',
|
|
|
|
|
|
message_limit=1,
|
|
|
|
|
|
restricted=True,
|
|
|
|
|
|
user_id=fake_uuid,
|
|
|
|
|
|
email_from='test@example.com',
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
2018-02-21 14:37:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('template_data, extra_args, expected_count', (
|
|
|
|
|
|
(
|
|
|
|
|
|
[],
|
|
|
|
|
|
{},
|
|
|
|
|
|
0,
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
[],
|
|
|
|
|
|
{'template_type': 'email'},
|
|
|
|
|
|
0,
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
[
|
|
|
|
|
|
{'template_type': 'email'},
|
|
|
|
|
|
{'template_type': 'sms'},
|
|
|
|
|
|
],
|
|
|
|
|
|
{},
|
|
|
|
|
|
2,
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
[
|
|
|
|
|
|
{'template_type': 'email'},
|
|
|
|
|
|
{'template_type': 'sms'},
|
|
|
|
|
|
],
|
|
|
|
|
|
{'template_type': 'email'},
|
|
|
|
|
|
1,
|
|
|
|
|
|
),
|
|
|
|
|
|
(
|
|
|
|
|
|
[
|
|
|
|
|
|
{'template_type': 'email'},
|
|
|
|
|
|
{'template_type': 'sms'},
|
|
|
|
|
|
],
|
|
|
|
|
|
{'template_type': 'letter'},
|
|
|
|
|
|
0,
|
|
|
|
|
|
),
|
|
|
|
|
|
))
|
|
|
|
|
|
def test_client_returns_count_of_service_templates(
|
|
|
|
|
|
app_,
|
|
|
|
|
|
mocker,
|
|
|
|
|
|
template_data,
|
|
|
|
|
|
extra_args,
|
|
|
|
|
|
expected_count,
|
|
|
|
|
|
):
|
|
|
|
|
|
|
|
|
|
|
|
mocker.patch(
|
|
|
|
|
|
'app.service_api_client.get_service_templates',
|
|
|
|
|
|
return_value={'data': template_data}
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
assert service_api_client.count_service_templates(
|
|
|
|
|
|
SERVICE_ONE_ID, **extra_args
|
|
|
|
|
|
) == expected_count
|