def test_get_precompiled_template(mocker): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get') client.get_precompiled_template(SERVICE_ONE_ID) mock_get.assert_called_once_with( '/service/{}/template/precompiled'.format(SERVICE_ONE_ID))
def test_client_creates_service_with_correct_data( mocker, active_user_with_permissions, fake_uuid, ): client = ServiceAPIClient() mock_post = mocker.patch.object(client, 'post', return_value={'data': {'id': None}}) mocker.patch('app.notify_client.current_user', id='123') client.create_service( 'My first service', 'central_government', 1, True, fake_uuid, '*****@*****.**', ) 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='*****@*****.**', ), )
def test_client_gets_guest_list(mocker): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get', return_value=['a', 'b', 'c']) response = client.get_guest_list('foo') assert response == ['a', 'b', 'c'] mock_get.assert_called_once_with(url='/service/foo/guest-list', )
def test_client_updates_guest_list(mocker): client = ServiceAPIClient() mock_put = mocker.patch.object(client, 'put') client.update_guest_list('foo', data=['a', 'b', 'c']) mock_put.assert_called_once_with( url='/service/foo/guest-list', data=['a', 'b', 'c'], )
def test_client_gets_service_statistics(mocker, today_only, limit_days): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get', return_value={'data': {'a': 'b'}}) ret = client.get_service_statistics('foo', today_only, limit_days) assert ret == {'a': 'b'} mock_get.assert_called_once_with('/service/foo/statistics', params={ 'today_only': today_only, 'limit_days': limit_days })
def test_client_posts_archived_true_when_deleting_template(mocker): mocker.patch("app.notify_client.current_user", id="1") expected_data = {"archived": True, "created_by": "1"} expected_url = "/service/{}/template/{}".format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) client = ServiceAPIClient() mock_post = mocker.patch( "app.notify_client.service_api_client.ServiceAPIClient.post") client.delete_service_template(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) mock_post.assert_called_once_with(expected_url, data=expected_data)
def test_client_posts_archived_true_when_deleting_template(mocker): mocker.patch('app.notify_client.current_user', id='1') expected_data = {'archived': True, 'created_by': '1'} expected_url = '/service/{}/template/{}'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) client = ServiceAPIClient() mock_post = mocker.patch( 'app.notify_client.service_api_client.ServiceAPIClient.post') client.delete_service_template(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) mock_post.assert_called_once_with(expected_url, data=expected_data)
def test_client_posts_archived_true_when_deleting_template(mocker): service_id = fake_uuid() template_id = fake_uuid() mocker.patch('app.notify_client.current_user', id='1') expected_data = {'archived': True, 'created_by': '1'} expected_url = '/service/{}/template/{}'.format(service_id, template_id) client = ServiceAPIClient() mock_post = mocker.patch( 'app.notify_client.service_api_client.ServiceAPIClient.post') client.delete_service_template(service_id, template_id) mock_post.assert_called_once_with(expected_url, data=expected_data)
def test_client_updates_service_with_allowed_attributes(mocker, ): client = ServiceAPIClient() mock_post = mocker.patch.object(client, 'post', return_value={'data': { 'id': None }}) mocker.patch('app.notify_client.current_user', id='123') allowed_attributes = [ 'active', 'consent_to_research', 'contact_link', 'count_as_live', 'email_branding', 'email_from', 'free_sms_fragment_limit', 'go_live_at', 'go_live_user', 'letter_branding', 'letter_contact_block', 'message_limit', 'name', 'notes', 'organisation_type', 'permissions', 'prefix_sms', 'rate_limit', 'reply_to_email_address', 'research_mode', 'restricted', 'sms_sender', 'volume_email', 'volume_letter', 'volume_sms', ] attrs_dict = {} for attr in allowed_attributes: attrs_dict[attr] = "value" client.update_service(SERVICE_ONE_ID, **attrs_dict) mock_post.assert_called_once_with(f'/service/{SERVICE_ONE_ID}', { **{ 'created_by': '123' }, **attrs_dict })
def test_client_posts_archived_true_when_deleting_template(mocker): mocker.patch('app.notify_client.current_user', id='1') mock_redis_delete_by_pattern = mocker.patch('app.extensions.RedisClient.delete_cache_keys_by_pattern') expected_data = { 'archived': True, 'created_by': '1' } expected_url = '/service/{}/template/{}'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) client = ServiceAPIClient() mock_post = mocker.patch('app.notify_client.service_api_client.ServiceAPIClient.post') mocker.patch('app.notify_client.service_api_client.ServiceAPIClient.get', return_value={'data': {'id': str(FAKE_TEMPLATE_ID)}}) client.delete_service_template(SERVICE_ONE_ID, FAKE_TEMPLATE_ID) mock_post.assert_called_once_with(expected_url, data=expected_data) assert call(f'service-{SERVICE_ONE_ID}-template-*') in mock_redis_delete_by_pattern.call_args_list
def test_client_posts_archived_true_when_deleting_template(mocker): service_id = fake_uuid template_id = fake_uuid expected_data = { 'archived': True, 'created_by': fake_uuid } expected_url = '/service/{}/template/{}'.format(service_id, template_id) client = ServiceAPIClient() mock_post = mocker.patch('app.notify_client.service_api_client.ServiceAPIClient.post') mock_attach_user = mocker.patch('app.notify_client.service_api_client._attach_current_user', side_effect=lambda x: x.update({'created_by': fake_uuid})) client.delete_service_template(service_id, template_id) mock_post.assert_called_once_with(expected_url, data=expected_data)
def test_client_gets_service_statistics(mocker, today_only, limit_days): client = ServiceAPIClient() mock_get = mocker.patch.object(client, "get", return_value={"data": { "a": "b" }}) ret = client.get_service_statistics("foo", today_only, limit_days) assert ret == {"a": "b"} mock_get.assert_called_once_with( "/service/foo/statistics", params={ "today_only": today_only, "limit_days": limit_days }, )
def test_client_creates_service_with_correct_data( mocker, active_user_with_permissions, fake_uuid, ): client = ServiceAPIClient() mock_post = mocker.patch.object(client, "post", return_value={"data": { "id": None }}) mocker.patch("app.notify_client.current_user", id="123") client.create_service( "My first service", "central_government", 1, True, fake_uuid, "*****@*****.**", False, ) 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="*****@*****.**", default_branding_is_french=False, ), )
def test_client_only_updates_allowed_attributes(mocker): mocker.patch("app.notify_client.current_user", id="1") with pytest.raises(TypeError) as error: ServiceAPIClient().update_service("service_id", foo="bar") assert str(error.value) == "Not allowed to update service attributes: foo"
def test_client_gets_service_with_detailed_params(mocker): client = ServiceAPIClient() mock_post = mocker.patch('app.notify_client.service_api_client.ServiceAPIClient.get') client.get_detailed_service('foo') mock_post.assert_called_once_with('/service/foo', params={'detailed': True})
from app.notify_client.letter_jobs_client import LetterJobsClient from app.notify_client.inbound_number_client import InboundNumberClient from app.notify_client.billing_api_client import BillingAPIClient from app.notify_client.complaint_api_client import complaint_api_client from app.notify_client.platform_stats_api_client import ( platform_stats_api_client, ) from app.commands import setup_commands from app.utils import requires_auth from app.utils import get_cdn_domain from app.utils import gmt_timezones login_manager = LoginManager() csrf = CSRFProtect() service_api_client = ServiceAPIClient() user_api_client = UserApiClient() api_key_api_client = ApiKeyApiClient() job_api_client = JobApiClient() notification_api_client = NotificationApiClient() support_api_client = SupportApiClient() status_api_client = StatusApiClient() invite_api_client = InviteApiClient() template_statistics_client = TemplateStatisticsApiClient() events_api_client = EventsApiClient() provider_client = ProviderClient() email_branding_client = EmailBrandingClient() organisations_client = OrganisationsClient() org_invite_api_client = OrgInviteApiClient() asset_fingerprinter = AssetFingerprinter() statsd_client = StatsdClient()
def test_client_gets_service(mocker): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get', return_value={}) client.get_service('foo') mock_get.assert_called_once_with('/service/foo')
def test_client_only_updates_allowed_attributes(mocker): mocker.patch('app.notify_client.current_user', id='1') with pytest.raises(TypeError) as error: ServiceAPIClient().update_service('service_id', foo='bar') assert str(error.value) == 'Not allowed to update service attributes: foo'
def test_client_gets_service(mocker, function, params): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get') function(client, 'foo') mock_get.assert_called_once_with('/service/foo', params=params)