mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-24 00:07:02 -04:00
notify-api-412 use black to enforce python coding style
This commit is contained in:
@@ -7,56 +7,77 @@ from app.notify_client.billing_api_client import BillingAPIClient
|
||||
|
||||
def test_get_free_sms_fragment_limit_for_year_correct_endpoint(mocker, api_user_active):
|
||||
service_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/billing/free-sms-fragment-limit'.format(service_id)
|
||||
expected_url = "/service/{}/billing/free-sms-fragment-limit".format(service_id)
|
||||
client = BillingAPIClient()
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.billing_api_client.BillingAPIClient.get')
|
||||
mock_get = mocker.patch("app.notify_client.billing_api_client.BillingAPIClient.get")
|
||||
|
||||
client.get_free_sms_fragment_limit_for_year(service_id, year=1999)
|
||||
mock_get.assert_called_once_with(expected_url, params={'financial_year_start': 1999})
|
||||
mock_get.assert_called_once_with(
|
||||
expected_url, params={"financial_year_start": 1999}
|
||||
)
|
||||
|
||||
|
||||
def test_post_free_sms_fragment_limit_for_current_year_endpoint(mocker, api_user_active):
|
||||
def test_post_free_sms_fragment_limit_for_current_year_endpoint(
|
||||
mocker, api_user_active
|
||||
):
|
||||
service_id = uuid.uuid4()
|
||||
sms_limit_data = {'free_sms_fragment_limit': 1111, 'financial_year_start': None}
|
||||
mock_post = mocker.patch('app.notify_client.billing_api_client.BillingAPIClient.post')
|
||||
sms_limit_data = {"free_sms_fragment_limit": 1111, "financial_year_start": None}
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.billing_api_client.BillingAPIClient.post"
|
||||
)
|
||||
client = BillingAPIClient()
|
||||
|
||||
client.create_or_update_free_sms_fragment_limit(service_id=service_id, free_sms_fragment_limit=1111)
|
||||
client.create_or_update_free_sms_fragment_limit(
|
||||
service_id=service_id, free_sms_fragment_limit=1111
|
||||
)
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
url='/service/{}/billing/free-sms-fragment-limit'.format(service_id),
|
||||
data=sms_limit_data
|
||||
url="/service/{}/billing/free-sms-fragment-limit".format(service_id),
|
||||
data=sms_limit_data,
|
||||
)
|
||||
|
||||
|
||||
def test_post_free_sms_fragment_limit_for_year_endpoint(mocker, api_user_active):
|
||||
service_id = uuid.uuid4()
|
||||
sms_limit_data = {'free_sms_fragment_limit': 1111, 'financial_year_start': 2017}
|
||||
mock_post = mocker.patch('app.notify_client.billing_api_client.BillingAPIClient.post')
|
||||
sms_limit_data = {"free_sms_fragment_limit": 1111, "financial_year_start": 2017}
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.billing_api_client.BillingAPIClient.post"
|
||||
)
|
||||
client = BillingAPIClient()
|
||||
|
||||
client.create_or_update_free_sms_fragment_limit(service_id=service_id,
|
||||
free_sms_fragment_limit=1111,
|
||||
year=2017)
|
||||
client.create_or_update_free_sms_fragment_limit(
|
||||
service_id=service_id, free_sms_fragment_limit=1111, year=2017
|
||||
)
|
||||
mock_post.assert_called_once_with(
|
||||
url='/service/{}/billing/free-sms-fragment-limit'.format(service_id),
|
||||
data=sms_limit_data
|
||||
url="/service/{}/billing/free-sms-fragment-limit".format(service_id),
|
||||
data=sms_limit_data,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('func, expected_url', [
|
||||
(BillingAPIClient.get_data_for_volumes_by_service_report, '/platform-stats/volumes-by-service'),
|
||||
(BillingAPIClient.get_data_for_daily_volumes_report, '/platform-stats/daily-volumes-report'),
|
||||
(
|
||||
BillingAPIClient.get_data_for_daily_sms_provider_volumes_report,
|
||||
'/platform-stats/daily-sms-provider-volumes-report'
|
||||
),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"func, expected_url",
|
||||
[
|
||||
(
|
||||
BillingAPIClient.get_data_for_volumes_by_service_report,
|
||||
"/platform-stats/volumes-by-service",
|
||||
),
|
||||
(
|
||||
BillingAPIClient.get_data_for_daily_volumes_report,
|
||||
"/platform-stats/daily-volumes-report",
|
||||
),
|
||||
(
|
||||
BillingAPIClient.get_data_for_daily_sms_provider_volumes_report,
|
||||
"/platform-stats/daily-sms-provider-volumes-report",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_get_data_for_volume_reports(mocker, api_user_active, func, expected_url):
|
||||
mock_get = mocker.patch('app.notify_client.billing_api_client.BillingAPIClient.get')
|
||||
mock_get = mocker.patch("app.notify_client.billing_api_client.BillingAPIClient.get")
|
||||
client = BillingAPIClient()
|
||||
|
||||
func(client, '2022-03-01', '2022-03-31')
|
||||
func(client, "2022-03-01", "2022-03-31")
|
||||
|
||||
mock_get.assert_called_once_with(url=expected_url, params={'start_date': '2022-03-01', 'end_date': '2022-03-31'})
|
||||
mock_get.assert_called_once_with(
|
||||
url=expected_url, params={"start_date": "2022-03-01", "end_date": "2022-03-31"}
|
||||
)
|
||||
|
||||
@@ -4,25 +4,25 @@ from app.notify_client.complaint_api_client import ComplaintApiClient
|
||||
def test_get_all_complaints(mocker):
|
||||
client = ComplaintApiClient()
|
||||
|
||||
mock = mocker.patch('app.notify_client.complaint_api_client.ComplaintApiClient.get')
|
||||
mock = mocker.patch("app.notify_client.complaint_api_client.ComplaintApiClient.get")
|
||||
|
||||
client.get_all_complaints()
|
||||
mock.assert_called_once_with('/complaint', params={'page': 1})
|
||||
mock.assert_called_once_with("/complaint", params={"page": 1})
|
||||
|
||||
|
||||
def test_get_all_complaints_with_a_page_number_specified(mocker):
|
||||
client = ComplaintApiClient()
|
||||
|
||||
mock = mocker.patch('app.notify_client.complaint_api_client.ComplaintApiClient.get')
|
||||
mock = mocker.patch("app.notify_client.complaint_api_client.ComplaintApiClient.get")
|
||||
|
||||
client.get_all_complaints(page=3)
|
||||
mock.assert_called_once_with('/complaint', params={'page': 3})
|
||||
mock.assert_called_once_with("/complaint", params={"page": 3})
|
||||
|
||||
|
||||
def test_get_complaint_count(mocker):
|
||||
client = ComplaintApiClient()
|
||||
mock = mocker.patch.object(client, 'get')
|
||||
params_dict = {'start_date': '2018-06-01', 'end_date': '2018-06-15'}
|
||||
mock = mocker.patch.object(client, "get")
|
||||
params_dict = {"start_date": "2018-06-01", "end_date": "2018-06-15"}
|
||||
|
||||
client.get_complaint_count(params_dict=params_dict)
|
||||
mock.assert_called_once_with('/complaint/count-by-date-range', params=params_dict)
|
||||
mock.assert_called_once_with("/complaint/count-by-date-range", params=params_dict)
|
||||
|
||||
@@ -5,23 +5,21 @@ from app.notify_client.email_branding_client import EmailBrandingClient
|
||||
|
||||
def test_get_email_branding(mocker, fake_uuid):
|
||||
mock_get = mocker.patch(
|
||||
'app.notify_client.email_branding_client.EmailBrandingClient.get',
|
||||
return_value={'foo': 'bar'}
|
||||
"app.notify_client.email_branding_client.EmailBrandingClient.get",
|
||||
return_value={"foo": "bar"},
|
||||
)
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=None,
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
EmailBrandingClient().get_email_branding(fake_uuid)
|
||||
mock_get.assert_called_once_with(
|
||||
url='/email-branding/{}'.format(fake_uuid)
|
||||
)
|
||||
mock_redis_get.assert_called_once_with('email_branding-{}'.format(fake_uuid))
|
||||
mock_get.assert_called_once_with(url="/email-branding/{}".format(fake_uuid))
|
||||
mock_redis_get.assert_called_once_with("email_branding-{}".format(fake_uuid))
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'email_branding-{}'.format(fake_uuid),
|
||||
"email_branding-{}".format(fake_uuid),
|
||||
'{"foo": "bar"}',
|
||||
ex=604800,
|
||||
)
|
||||
@@ -29,62 +27,78 @@ def test_get_email_branding(mocker, fake_uuid):
|
||||
|
||||
def test_get_all_email_branding(mocker):
|
||||
mock_get = mocker.patch(
|
||||
'app.notify_client.email_branding_client.EmailBrandingClient.get',
|
||||
return_value={'email_branding': [1, 2, 3]}
|
||||
"app.notify_client.email_branding_client.EmailBrandingClient.get",
|
||||
return_value={"email_branding": [1, 2, 3]},
|
||||
)
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=None,
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
EmailBrandingClient().get_all_email_branding()
|
||||
mock_get.assert_called_once_with(
|
||||
url='/email-branding'
|
||||
)
|
||||
mock_redis_get.assert_called_once_with('email_branding')
|
||||
mock_get.assert_called_once_with(url="/email-branding")
|
||||
mock_redis_get.assert_called_once_with("email_branding")
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'email_branding',
|
||||
'[1, 2, 3]',
|
||||
"email_branding",
|
||||
"[1, 2, 3]",
|
||||
ex=604800,
|
||||
)
|
||||
|
||||
|
||||
def test_create_email_branding(mocker):
|
||||
org_data = {'logo': 'test.png', 'name': 'test name', 'text': 'test name', 'colour': 'red',
|
||||
'brand_type': 'org'}
|
||||
org_data = {
|
||||
"logo": "test.png",
|
||||
"name": "test name",
|
||||
"text": "test name",
|
||||
"colour": "red",
|
||||
"brand_type": "org",
|
||||
}
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.email_branding_client.EmailBrandingClient.post')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.email_branding_client.EmailBrandingClient.post"
|
||||
)
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
EmailBrandingClient().create_email_branding(
|
||||
logo=org_data['logo'], name=org_data['name'], text=org_data['text'], colour=org_data['colour'],
|
||||
brand_type='org'
|
||||
logo=org_data["logo"],
|
||||
name=org_data["name"],
|
||||
text=org_data["text"],
|
||||
colour=org_data["colour"],
|
||||
brand_type="org",
|
||||
)
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
url='/email-branding',
|
||||
data=org_data
|
||||
)
|
||||
mock_post.assert_called_once_with(url="/email-branding", data=org_data)
|
||||
|
||||
mock_redis_delete.assert_called_once_with('email_branding')
|
||||
mock_redis_delete.assert_called_once_with("email_branding")
|
||||
|
||||
|
||||
def test_update_email_branding(mocker, fake_uuid):
|
||||
org_data = {'logo': 'test.png', 'name': 'test name', 'text': 'test name', 'colour': 'red',
|
||||
'brand_type': 'org'}
|
||||
org_data = {
|
||||
"logo": "test.png",
|
||||
"name": "test name",
|
||||
"text": "test name",
|
||||
"colour": "red",
|
||||
"brand_type": "org",
|
||||
}
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.email_branding_client.EmailBrandingClient.post')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.email_branding_client.EmailBrandingClient.post"
|
||||
)
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
EmailBrandingClient().update_email_branding(
|
||||
branding_id=fake_uuid, logo=org_data['logo'], name=org_data['name'], text=org_data['text'],
|
||||
colour=org_data['colour'], brand_type='org')
|
||||
branding_id=fake_uuid,
|
||||
logo=org_data["logo"],
|
||||
name=org_data["name"],
|
||||
text=org_data["text"],
|
||||
colour=org_data["colour"],
|
||||
brand_type="org",
|
||||
)
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
url='/email-branding/{}'.format(fake_uuid),
|
||||
data=org_data
|
||||
url="/email-branding/{}".format(fake_uuid), data=org_data
|
||||
)
|
||||
assert mock_redis_delete.call_args_list == [
|
||||
call('email_branding-{}'.format(fake_uuid)),
|
||||
call('email_branding'),
|
||||
call("email_branding-{}".format(fake_uuid)),
|
||||
call("email_branding"),
|
||||
]
|
||||
|
||||
@@ -2,15 +2,14 @@ from app.notify_client.events_api_client import EventsApiClient
|
||||
|
||||
|
||||
def test_events_client_calls_correct_api_endpoint(mocker):
|
||||
|
||||
expected_url = '/events'
|
||||
event_type = 'anything'
|
||||
event_data = {'does_not': 'matter'}
|
||||
expected_data = {'event_type': event_type, 'data': event_data}
|
||||
expected_url = "/events"
|
||||
event_type = "anything"
|
||||
event_data = {"does_not": "matter"}
|
||||
expected_data = {"event_type": event_type, "data": event_data}
|
||||
|
||||
client = EventsApiClient()
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.events_api_client.EventsApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.events_api_client.EventsApiClient.post")
|
||||
|
||||
client.create_event(event_type, event_data)
|
||||
|
||||
|
||||
@@ -9,49 +9,61 @@ def test_client_creates_invite(
|
||||
fake_uuid,
|
||||
sample_invite,
|
||||
):
|
||||
|
||||
mocker.patch('app.notify_client.current_user')
|
||||
mocker.patch("app.notify_client.current_user")
|
||||
|
||||
mock_post = mocker.patch(
|
||||
'app.invite_api_client.post',
|
||||
return_value={'data': dict.fromkeys({
|
||||
'id', 'service', 'from_user', 'email_address',
|
||||
'permissions', 'status', 'created_at', 'auth_type', 'folder_permissions'
|
||||
})}
|
||||
"app.invite_api_client.post",
|
||||
return_value={
|
||||
"data": dict.fromkeys(
|
||||
{
|
||||
"id",
|
||||
"service",
|
||||
"from_user",
|
||||
"email_address",
|
||||
"permissions",
|
||||
"status",
|
||||
"created_at",
|
||||
"auth_type",
|
||||
"folder_permissions",
|
||||
}
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
invite_api_client.create_invite(
|
||||
'12345', '67890', 'test@example.com', {'send_messages'}, 'sms_auth', [fake_uuid]
|
||||
"12345", "67890", "test@example.com", {"send_messages"}, "sms_auth", [fake_uuid]
|
||||
)
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
url='/service/{}/invite'.format('67890'),
|
||||
url="/service/{}/invite".format("67890"),
|
||||
data={
|
||||
'auth_type': 'sms_auth',
|
||||
'email_address': 'test@example.com',
|
||||
'from_user': '12345',
|
||||
'service': '67890',
|
||||
'created_by': ANY,
|
||||
'permissions': 'send_emails,send_texts',
|
||||
'invite_link_host': 'http://localhost:6012',
|
||||
'folder_permissions': [fake_uuid]
|
||||
}
|
||||
"auth_type": "sms_auth",
|
||||
"email_address": "test@example.com",
|
||||
"from_user": "12345",
|
||||
"service": "67890",
|
||||
"created_by": ANY,
|
||||
"permissions": "send_emails,send_texts",
|
||||
"invite_link_host": "http://localhost:6012",
|
||||
"folder_permissions": [fake_uuid],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_client_returns_invite(mocker, sample_invite):
|
||||
sample_invite["status"] = "pending"
|
||||
service_id = sample_invite["service"]
|
||||
|
||||
sample_invite['status'] = 'pending'
|
||||
service_id = sample_invite['service']
|
||||
expected_data = {"data": [sample_invite]}
|
||||
|
||||
expected_data = {'data': [sample_invite]}
|
||||
expected_url = "/service/{}/invite".format(service_id)
|
||||
|
||||
expected_url = '/service/{}/invite'.format(service_id)
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.invite_api_client.InviteApiClient.get', return_value=expected_data)
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.invite_api_client.InviteApiClient.get",
|
||||
return_value=expected_data,
|
||||
)
|
||||
|
||||
invites = invite_api_client.get_invites_for_service(service_id)
|
||||
|
||||
mock_get.assert_called_once_with(expected_url)
|
||||
assert len(invites) == 1
|
||||
assert invites[0]['status'] == 'pending'
|
||||
assert invites[0]["status"] == "pending"
|
||||
|
||||
@@ -10,54 +10,48 @@ from app.notify_client.job_api_client import JobApiClient
|
||||
def test_client_creates_job_data_correctly(mocker, fake_uuid):
|
||||
job_id = fake_uuid
|
||||
service_id = fake_uuid
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_redis_set = mocker.patch('app.extensions.RedisClient.set')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_set = mocker.patch("app.extensions.RedisClient.set")
|
||||
|
||||
expected_data = {
|
||||
"id": job_id,
|
||||
"created_by": '1'
|
||||
}
|
||||
expected_data = {"id": job_id, "created_by": "1"}
|
||||
|
||||
expected_url = '/service/{}/job'.format(service_id)
|
||||
expected_url = "/service/{}/job".format(service_id)
|
||||
|
||||
client = JobApiClient()
|
||||
mock_post = mocker.patch(
|
||||
'app.notify_client.job_api_client.JobApiClient.post',
|
||||
return_value={'data': dict(statistics=[], **expected_data)}
|
||||
"app.notify_client.job_api_client.JobApiClient.post",
|
||||
return_value={"data": dict(statistics=[], **expected_data)},
|
||||
)
|
||||
|
||||
client.create_job(service_id, job_id)
|
||||
mock_post.assert_called_once_with(url=expected_url, data=expected_data)
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'has_jobs-{}'.format(service_id),
|
||||
b'true',
|
||||
"has_jobs-{}".format(service_id),
|
||||
b"true",
|
||||
ex=604800,
|
||||
)
|
||||
|
||||
|
||||
def test_client_schedules_job(mocker, fake_uuid):
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_post = mocker.patch("app.notify_client.job_api_client.JobApiClient.post")
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.job_api_client.JobApiClient.post')
|
||||
when = "2016-08-25T13:04:21.767198"
|
||||
|
||||
when = '2016-08-25T13:04:21.767198'
|
||||
JobApiClient().create_job(fake_uuid, 1, scheduled_for=when)
|
||||
|
||||
JobApiClient().create_job(
|
||||
fake_uuid, 1, scheduled_for=when
|
||||
)
|
||||
|
||||
assert mock_post.call_args[1]['data']['scheduled_for'] == when
|
||||
assert mock_post.call_args[1]["data"]["scheduled_for"] == when
|
||||
|
||||
|
||||
def test_client_gets_job_by_service_and_job(mocker):
|
||||
service_id = 'service_id'
|
||||
job_id = 'job_id'
|
||||
service_id = "service_id"
|
||||
job_id = "job_id"
|
||||
|
||||
expected_url = '/service/{}/job/{}'.format(service_id, job_id)
|
||||
expected_url = "/service/{}/job/{}".format(service_id, job_id)
|
||||
|
||||
client = JobApiClient()
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get')
|
||||
mock_get = mocker.patch("app.notify_client.job_api_client.JobApiClient.get")
|
||||
|
||||
client.get_job(service_id, job_id)
|
||||
|
||||
@@ -65,56 +59,60 @@ def test_client_gets_job_by_service_and_job(mocker):
|
||||
|
||||
|
||||
def test_client_gets_jobs_with_status_filter(mocker):
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get')
|
||||
mock_get = mocker.patch("app.notify_client.job_api_client.JobApiClient.get")
|
||||
|
||||
JobApiClient().get_jobs(uuid.uuid4(), statuses=['foo', 'bar'])
|
||||
JobApiClient().get_jobs(uuid.uuid4(), statuses=["foo", "bar"])
|
||||
|
||||
mock_get.assert_called_once_with(url=ANY, params={'page': 1, 'statuses': 'foo,bar'})
|
||||
mock_get.assert_called_once_with(url=ANY, params={"page": 1, "statuses": "foo,bar"})
|
||||
|
||||
|
||||
def test_client_gets_jobs_with_page_parameter(mocker):
|
||||
client = JobApiClient()
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get')
|
||||
mock_get = mocker.patch("app.notify_client.job_api_client.JobApiClient.get")
|
||||
|
||||
client.get_jobs('foo', page=2)
|
||||
client.get_jobs("foo", page=2)
|
||||
|
||||
mock_get.assert_called_once_with(url=ANY, params={'page': 2})
|
||||
mock_get.assert_called_once_with(url=ANY, params={"page": 2})
|
||||
|
||||
|
||||
def test_client_parses_job_stats(mocker):
|
||||
service_id = 'service_id'
|
||||
job_id = 'job_id'
|
||||
expected_data = {'data': {
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [
|
||||
{'status': 'failed', 'count': 10},
|
||||
{'status': 'technical-failure', 'count': 10},
|
||||
{'status': 'temporary-failure', 'count': 10},
|
||||
{'status': 'permanent-failure', 'count': 10},
|
||||
{'status': 'created', 'count': 10},
|
||||
{'status': 'sending', 'count': 10},
|
||||
{'status': 'pending', 'count': 10},
|
||||
{'status': 'delivered', 'count': 10}
|
||||
],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 80,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}}
|
||||
service_id = "service_id"
|
||||
job_id = "job_id"
|
||||
expected_data = {
|
||||
"data": {
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [
|
||||
{"status": "failed", "count": 10},
|
||||
{"status": "technical-failure", "count": 10},
|
||||
{"status": "temporary-failure", "count": 10},
|
||||
{"status": "permanent-failure", "count": 10},
|
||||
{"status": "created", "count": 10},
|
||||
{"status": "sending", "count": 10},
|
||||
{"status": "pending", "count": 10},
|
||||
{"status": "delivered", "count": 10},
|
||||
],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 80,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
expected_url = '/service/{}/job/{}'.format(service_id, job_id)
|
||||
expected_url = "/service/{}/job/{}".format(service_id, job_id)
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.job_api_client.JobApiClient.get", return_value=expected_data
|
||||
)
|
||||
|
||||
result = Job.from_id(job_id, service_id=service_id)
|
||||
|
||||
@@ -126,30 +124,34 @@ def test_client_parses_job_stats(mocker):
|
||||
|
||||
|
||||
def test_client_parses_empty_job_stats(mocker):
|
||||
service_id = 'service_id'
|
||||
job_id = 'job_id'
|
||||
expected_data = {'data': {
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 80,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}}
|
||||
service_id = "service_id"
|
||||
job_id = "job_id"
|
||||
expected_data = {
|
||||
"data": {
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 80,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
expected_url = '/service/{}/job/{}'.format(service_id, job_id)
|
||||
expected_url = "/service/{}/job/{}".format(service_id, job_id)
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.job_api_client.JobApiClient.get", return_value=expected_data
|
||||
)
|
||||
|
||||
result = Job.from_id(job_id, service_id=service_id)
|
||||
|
||||
@@ -161,70 +163,79 @@ def test_client_parses_empty_job_stats(mocker):
|
||||
|
||||
|
||||
def test_client_parses_job_stats_for_service(mocker):
|
||||
service_id = 'service_id'
|
||||
job_1_id = 'job_id_1'
|
||||
job_2_id = 'job_id_2'
|
||||
expected_data = {'data': [{
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_1_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [
|
||||
{'status': 'failed', 'count': 10},
|
||||
{'status': 'technical-failure', 'count': 10},
|
||||
{'status': 'temporary-failure', 'count': 10},
|
||||
{'status': 'permanent-failure', 'count': 10},
|
||||
{'status': 'created', 'count': 10},
|
||||
{'status': 'sending', 'count': 10},
|
||||
{'status': 'pending', 'count': 10},
|
||||
{'status': 'delivered', 'count': 10}
|
||||
],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 80,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}, {
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_2_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [
|
||||
{'status': 'failed', 'count': 5},
|
||||
{'status': 'technical-failure', 'count': 5},
|
||||
{'status': 'temporary-failure', 'count': 5},
|
||||
{'status': 'permanent-failure', 'count': 5},
|
||||
{'status': 'created', 'count': 5},
|
||||
{'status': 'sending', 'count': 5},
|
||||
{'status': 'pending', 'count': 5},
|
||||
{'status': 'delivered', 'count': 5}
|
||||
],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 40,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}]}
|
||||
service_id = "service_id"
|
||||
job_1_id = "job_id_1"
|
||||
job_2_id = "job_id_2"
|
||||
expected_data = {
|
||||
"data": [
|
||||
{
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_1_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [
|
||||
{"status": "failed", "count": 10},
|
||||
{"status": "technical-failure", "count": 10},
|
||||
{"status": "temporary-failure", "count": 10},
|
||||
{"status": "permanent-failure", "count": 10},
|
||||
{"status": "created", "count": 10},
|
||||
{"status": "sending", "count": 10},
|
||||
{"status": "pending", "count": 10},
|
||||
{"status": "delivered", "count": 10},
|
||||
],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 80,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
},
|
||||
{
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_2_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [
|
||||
{"status": "failed", "count": 5},
|
||||
{"status": "technical-failure", "count": 5},
|
||||
{"status": "temporary-failure", "count": 5},
|
||||
{"status": "permanent-failure", "count": 5},
|
||||
{"status": "created", "count": 5},
|
||||
{"status": "sending", "count": 5},
|
||||
{"status": "pending", "count": 5},
|
||||
{"status": "delivered", "count": 5},
|
||||
],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 40,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
expected_url = '/service/{}/job'.format(service_id)
|
||||
expected_url = "/service/{}/job".format(service_id)
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.job_api_client.JobApiClient.get", return_value=expected_data
|
||||
)
|
||||
|
||||
result = PaginatedJobs(service_id)
|
||||
|
||||
mock_get.assert_called_once_with(url=expected_url, params={'page': 1, 'statuses': ANY})
|
||||
mock_get.assert_called_once_with(
|
||||
url=expected_url, params={"page": 1, "statuses": ANY}
|
||||
)
|
||||
assert result[0].id == job_1_id
|
||||
assert result[0].notifications_requested == 80
|
||||
assert result[0].notifications_sent == 50
|
||||
@@ -238,52 +249,61 @@ def test_client_parses_job_stats_for_service(mocker):
|
||||
|
||||
|
||||
def test_client_parses_empty_job_stats_for_service(mocker):
|
||||
service_id = 'service_id'
|
||||
job_1_id = 'job_id_1'
|
||||
job_2_id = 'job_id_2'
|
||||
expected_data = {'data': [{
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_1_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 80,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}, {
|
||||
'status': 'finished',
|
||||
'template_version': 3,
|
||||
'id': job_2_id,
|
||||
'updated_at': '2016-08-24T08:29:28.332972+00:00',
|
||||
'service': service_id,
|
||||
'processing_finished': '2016-08-24T08:11:48.676365+00:00',
|
||||
'statistics': [],
|
||||
'original_file_name': 'test-notify-email.csv',
|
||||
'created_by': {
|
||||
'name': 'test-user@digital.cabinet-office.gov.uk',
|
||||
'id': '3571f2ae-7a39-4fb4-9ad7-8453f5257072'
|
||||
},
|
||||
'created_at': '2016-08-24T08:09:56.371073+00:00',
|
||||
'template': 'c0309261-9c9e-4530-8fed-5f67b02260d2',
|
||||
'notification_count': 40,
|
||||
'processing_started': '2016-08-24T08:09:57.661246+00:00'
|
||||
}]}
|
||||
service_id = "service_id"
|
||||
job_1_id = "job_id_1"
|
||||
job_2_id = "job_id_2"
|
||||
expected_data = {
|
||||
"data": [
|
||||
{
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_1_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 80,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
},
|
||||
{
|
||||
"status": "finished",
|
||||
"template_version": 3,
|
||||
"id": job_2_id,
|
||||
"updated_at": "2016-08-24T08:29:28.332972+00:00",
|
||||
"service": service_id,
|
||||
"processing_finished": "2016-08-24T08:11:48.676365+00:00",
|
||||
"statistics": [],
|
||||
"original_file_name": "test-notify-email.csv",
|
||||
"created_by": {
|
||||
"name": "test-user@digital.cabinet-office.gov.uk",
|
||||
"id": "3571f2ae-7a39-4fb4-9ad7-8453f5257072",
|
||||
},
|
||||
"created_at": "2016-08-24T08:09:56.371073+00:00",
|
||||
"template": "c0309261-9c9e-4530-8fed-5f67b02260d2",
|
||||
"notification_count": 40,
|
||||
"processing_started": "2016-08-24T08:09:57.661246+00:00",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
expected_url = '/service/{}/job'.format(service_id)
|
||||
expected_url = "/service/{}/job".format(service_id)
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.job_api_client.JobApiClient.get', return_value=expected_data)
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.job_api_client.JobApiClient.get", return_value=expected_data
|
||||
)
|
||||
|
||||
result = PaginatedJobs(service_id)
|
||||
|
||||
mock_get.assert_called_once_with(url=expected_url, params={'page': 1, 'statuses': ANY})
|
||||
mock_get.assert_called_once_with(
|
||||
url=expected_url, params={"page": 1, "statuses": ANY}
|
||||
)
|
||||
assert result[0].id == job_1_id
|
||||
assert result[0].notifications_requested == 0
|
||||
assert result[0].notifications_sent == 0
|
||||
@@ -297,26 +317,28 @@ def test_client_parses_empty_job_stats_for_service(mocker):
|
||||
|
||||
|
||||
def test_cancel_job(mocker):
|
||||
mock_post = mocker.patch('app.notify_client.job_api_client.JobApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.job_api_client.JobApiClient.post")
|
||||
|
||||
JobApiClient().cancel_job('service_id', 'job_id')
|
||||
JobApiClient().cancel_job("service_id", "job_id")
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
url='/service/{}/job/{}/cancel'.format('service_id', 'job_id'),
|
||||
data={}
|
||||
url="/service/{}/job/{}/cancel".format("service_id", "job_id"), data={}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('job_data, expected_cache_value', [
|
||||
(
|
||||
[{'data': [1, 2, 3], 'statistics': []}],
|
||||
'true',
|
||||
),
|
||||
(
|
||||
[],
|
||||
'false',
|
||||
),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"job_data, expected_cache_value",
|
||||
[
|
||||
(
|
||||
[{"data": [1, 2, 3], "statistics": []}],
|
||||
"true",
|
||||
),
|
||||
(
|
||||
[],
|
||||
"false",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_has_jobs_sets_cache(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
@@ -324,44 +346,42 @@ def test_has_jobs_sets_cache(
|
||||
expected_cache_value,
|
||||
):
|
||||
mock_get = mocker.patch(
|
||||
'app.notify_client.job_api_client.JobApiClient.get',
|
||||
return_value={'data': job_data}
|
||||
"app.notify_client.job_api_client.JobApiClient.get",
|
||||
return_value={"data": job_data},
|
||||
)
|
||||
mock_redis_set = mocker.patch('app.extensions.RedisClient.set')
|
||||
mock_redis_set = mocker.patch("app.extensions.RedisClient.set")
|
||||
|
||||
JobApiClient().has_jobs(fake_uuid)
|
||||
|
||||
mock_get.assert_called_once_with(
|
||||
url='/service/{}/job'.format(fake_uuid),
|
||||
params={'page': 1}
|
||||
url="/service/{}/job".format(fake_uuid), params={"page": 1}
|
||||
)
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'has_jobs-{}'.format(fake_uuid),
|
||||
"has_jobs-{}".format(fake_uuid),
|
||||
expected_cache_value,
|
||||
ex=604800,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('cache_value, return_value', [
|
||||
(b'true', True),
|
||||
(b'false', False),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"cache_value, return_value",
|
||||
[
|
||||
(b"true", True),
|
||||
(b"false", False),
|
||||
],
|
||||
)
|
||||
def test_has_jobs_returns_from_cache(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
cache_value,
|
||||
return_value,
|
||||
):
|
||||
mock_get = mocker.patch(
|
||||
'app.notify_client.job_api_client.JobApiClient.get'
|
||||
)
|
||||
mock_get = mocker.patch("app.notify_client.job_api_client.JobApiClient.get")
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=cache_value,
|
||||
)
|
||||
|
||||
assert JobApiClient().has_jobs(fake_uuid) is return_value
|
||||
assert not mock_get.called
|
||||
mock_redis_get.assert_called_once_with(
|
||||
'has_jobs-{}'.format(fake_uuid)
|
||||
)
|
||||
mock_redis_get.assert_called_once_with("has_jobs-{}".format(fake_uuid))
|
||||
|
||||
@@ -3,107 +3,149 @@ import pytest
|
||||
from app.notify_client.notification_api_client import NotificationApiClient
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arguments,expected_call", [
|
||||
(
|
||||
{},
|
||||
{'url': '/service/abcd1234/notifications', 'params': {}}
|
||||
),
|
||||
(
|
||||
{'page': 99},
|
||||
{'url': '/service/abcd1234/notifications', 'params': {'page': 99}}
|
||||
),
|
||||
(
|
||||
{'include_jobs': False},
|
||||
{'url': '/service/abcd1234/notifications', 'params': {'include_jobs': False}}
|
||||
),
|
||||
(
|
||||
{'include_from_test_key': True},
|
||||
{'url': '/service/abcd1234/notifications', 'params': {'include_from_test_key': True}}
|
||||
),
|
||||
(
|
||||
{'page': 48, 'limit_days': 3},
|
||||
{'url': '/service/abcd1234/notifications', 'params': {'page': 48, 'limit_days': 3}}
|
||||
),
|
||||
(
|
||||
{'job_id': 'efgh5678'},
|
||||
{'url': '/service/abcd1234/job/efgh5678/notifications', 'params': {}}
|
||||
),
|
||||
(
|
||||
{'job_id': 'efgh5678', 'page': 48},
|
||||
{'url': '/service/abcd1234/job/efgh5678/notifications', 'params': {'page': 48}}
|
||||
),
|
||||
(
|
||||
{'job_id': 'efgh5678', 'page': 48, 'limit_days': 3},
|
||||
{'url': '/service/abcd1234/job/efgh5678/notifications', 'params': {'page': 48}}
|
||||
),
|
||||
])
|
||||
def test_client_gets_notifications_for_service_and_job_by_page(mocker, arguments, expected_call):
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get')
|
||||
NotificationApiClient().get_notifications_for_service('abcd1234', **arguments)
|
||||
@pytest.mark.parametrize(
|
||||
"arguments,expected_call",
|
||||
[
|
||||
({}, {"url": "/service/abcd1234/notifications", "params": {}}),
|
||||
(
|
||||
{"page": 99},
|
||||
{"url": "/service/abcd1234/notifications", "params": {"page": 99}},
|
||||
),
|
||||
(
|
||||
{"include_jobs": False},
|
||||
{
|
||||
"url": "/service/abcd1234/notifications",
|
||||
"params": {"include_jobs": False},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"include_from_test_key": True},
|
||||
{
|
||||
"url": "/service/abcd1234/notifications",
|
||||
"params": {"include_from_test_key": True},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"page": 48, "limit_days": 3},
|
||||
{
|
||||
"url": "/service/abcd1234/notifications",
|
||||
"params": {"page": 48, "limit_days": 3},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"job_id": "efgh5678"},
|
||||
{"url": "/service/abcd1234/job/efgh5678/notifications", "params": {}},
|
||||
),
|
||||
(
|
||||
{"job_id": "efgh5678", "page": 48},
|
||||
{
|
||||
"url": "/service/abcd1234/job/efgh5678/notifications",
|
||||
"params": {"page": 48},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"job_id": "efgh5678", "page": 48, "limit_days": 3},
|
||||
{
|
||||
"url": "/service/abcd1234/job/efgh5678/notifications",
|
||||
"params": {"page": 48},
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_client_gets_notifications_for_service_and_job_by_page(
|
||||
mocker, arguments, expected_call
|
||||
):
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.notification_api_client.NotificationApiClient.get"
|
||||
)
|
||||
NotificationApiClient().get_notifications_for_service("abcd1234", **arguments)
|
||||
mock_get.assert_called_once_with(**expected_call)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("arguments,expected_call", [
|
||||
(
|
||||
{'to': "2028675309"},
|
||||
{'url': '/service/abcd1234/notifications', 'data': {'to': "2028675309"}}
|
||||
),
|
||||
(
|
||||
{'to': "2028675309", 'job_id': 'efgh5678'},
|
||||
{'url': '/service/abcd1234/job/efgh5678/notifications', 'data': {'to': "2028675309"}}
|
||||
),
|
||||
(
|
||||
{'to': "2028675309", 'page': 99},
|
||||
{'url': '/service/abcd1234/notifications', 'data': {'to': "2028675309", 'page': 99}}
|
||||
),
|
||||
(
|
||||
{'to': "2028675309", 'limit_days': 3},
|
||||
{'url': '/service/abcd1234/notifications', 'data': {'to': "2028675309", 'limit_days': 3}}
|
||||
),
|
||||
(
|
||||
{'to': "2028675309", 'job_id': 'efgh5678', 'limit_days': 3},
|
||||
{'url': '/service/abcd1234/job/efgh5678/notifications', 'data': {'to': "2028675309"}}
|
||||
),
|
||||
])
|
||||
def test_client_gets_notifications_for_service_and_job_by_page_posts_for_to(mocker, arguments, expected_call):
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.post')
|
||||
NotificationApiClient().get_notifications_for_service('abcd1234', **arguments)
|
||||
@pytest.mark.parametrize(
|
||||
"arguments,expected_call",
|
||||
[
|
||||
(
|
||||
{"to": "2028675309"},
|
||||
{"url": "/service/abcd1234/notifications", "data": {"to": "2028675309"}},
|
||||
),
|
||||
(
|
||||
{"to": "2028675309", "job_id": "efgh5678"},
|
||||
{
|
||||
"url": "/service/abcd1234/job/efgh5678/notifications",
|
||||
"data": {"to": "2028675309"},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"to": "2028675309", "page": 99},
|
||||
{
|
||||
"url": "/service/abcd1234/notifications",
|
||||
"data": {"to": "2028675309", "page": 99},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"to": "2028675309", "limit_days": 3},
|
||||
{
|
||||
"url": "/service/abcd1234/notifications",
|
||||
"data": {"to": "2028675309", "limit_days": 3},
|
||||
},
|
||||
),
|
||||
(
|
||||
{"to": "2028675309", "job_id": "efgh5678", "limit_days": 3},
|
||||
{
|
||||
"url": "/service/abcd1234/job/efgh5678/notifications",
|
||||
"data": {"to": "2028675309"},
|
||||
},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_client_gets_notifications_for_service_and_job_by_page_posts_for_to(
|
||||
mocker, arguments, expected_call
|
||||
):
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.notification_api_client.NotificationApiClient.post"
|
||||
)
|
||||
NotificationApiClient().get_notifications_for_service("abcd1234", **arguments)
|
||||
mock_post.assert_called_once_with(**expected_call)
|
||||
|
||||
|
||||
def test_send_notification(mocker, client_request, active_user_with_permissions):
|
||||
mock_post = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.post')
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.notification_api_client.NotificationApiClient.post"
|
||||
)
|
||||
NotificationApiClient().send_notification(
|
||||
'foo',
|
||||
template_id='bar',
|
||||
recipient='2028675301',
|
||||
"foo",
|
||||
template_id="bar",
|
||||
recipient="2028675301",
|
||||
personalisation=None,
|
||||
sender_id=None
|
||||
sender_id=None,
|
||||
)
|
||||
mock_post.assert_called_once_with(
|
||||
url='/service/foo/send-notification',
|
||||
url="/service/foo/send-notification",
|
||||
data={
|
||||
'template_id': 'bar',
|
||||
'to': '2028675301',
|
||||
'personalisation': None,
|
||||
'created_by': active_user_with_permissions['id']
|
||||
}
|
||||
"template_id": "bar",
|
||||
"to": "2028675301",
|
||||
"personalisation": None,
|
||||
"created_by": active_user_with_permissions["id"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_get_notification(mocker):
|
||||
mock_get = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get')
|
||||
NotificationApiClient().get_notification('foo', 'bar')
|
||||
mock_get.assert_called_once_with(
|
||||
url='/service/foo/notifications/bar'
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.notification_api_client.NotificationApiClient.get"
|
||||
)
|
||||
NotificationApiClient().get_notification("foo", "bar")
|
||||
mock_get.assert_called_once_with(url="/service/foo/notifications/bar")
|
||||
|
||||
|
||||
def test_get_notification_count_for_job_id(mocker):
|
||||
mock_get = mocker.patch('app.notify_client.notification_api_client.NotificationApiClient.get')
|
||||
NotificationApiClient().get_notification_count_for_job_id(service_id='foo', job_id='bar')
|
||||
mock_get.assert_called_once_with(
|
||||
url='/service/foo/job/bar/notification_count',
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.notification_api_client.NotificationApiClient.get"
|
||||
)
|
||||
NotificationApiClient().get_notification_count_for_job_id(
|
||||
service_id="foo", job_id="bar"
|
||||
)
|
||||
mock_get.assert_called_once_with(
|
||||
url="/service/foo/job/bar/notification_count",
|
||||
)
|
||||
|
||||
@@ -15,19 +15,18 @@ from tests.conftest import (
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method', [
|
||||
'put',
|
||||
'post',
|
||||
'delete'
|
||||
])
|
||||
@pytest.mark.parametrize('user', [
|
||||
create_api_user_active(),
|
||||
create_platform_admin_user(),
|
||||
], ids=['api_user', 'platform_admin'])
|
||||
@pytest.mark.parametrize('service', [
|
||||
service_json(active=True),
|
||||
None
|
||||
], ids=['active_service', 'no_service'])
|
||||
@pytest.mark.parametrize("method", ["put", "post", "delete"])
|
||||
@pytest.mark.parametrize(
|
||||
"user",
|
||||
[
|
||||
create_api_user_active(),
|
||||
create_platform_admin_user(),
|
||||
],
|
||||
ids=["api_user", "platform_admin"],
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"service", [service_json(active=True), None], ids=["active_service", "no_service"]
|
||||
)
|
||||
def test_active_service_can_be_modified(notify_admin, method, user, service):
|
||||
api_client = NotifyAdminAPIClient()
|
||||
|
||||
@@ -35,46 +34,42 @@ def test_active_service_can_be_modified(notify_admin, method, user, service):
|
||||
client.login(user)
|
||||
request_context.service = Service(service)
|
||||
|
||||
with patch.object(api_client, 'request') as request:
|
||||
ret = getattr(api_client, method)('url', 'data')
|
||||
with patch.object(api_client, "request") as request:
|
||||
ret = getattr(api_client, method)("url", "data")
|
||||
|
||||
assert request.called
|
||||
assert ret == request.return_value
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method', [
|
||||
'put',
|
||||
'post',
|
||||
'delete'
|
||||
])
|
||||
def test_inactive_service_cannot_be_modified_by_normal_user(notify_admin, api_user_active, method):
|
||||
@pytest.mark.parametrize("method", ["put", "post", "delete"])
|
||||
def test_inactive_service_cannot_be_modified_by_normal_user(
|
||||
notify_admin, api_user_active, method
|
||||
):
|
||||
api_client = NotifyAdminAPIClient()
|
||||
|
||||
with notify_admin.test_request_context() as request_context, notify_admin.test_client() as client:
|
||||
client.login(api_user_active)
|
||||
request_context.service = Service(service_json(active=False))
|
||||
|
||||
with patch.object(api_client, 'request') as request:
|
||||
with patch.object(api_client, "request") as request:
|
||||
with pytest.raises(werkzeug.exceptions.Forbidden):
|
||||
getattr(api_client, method)('url', 'data')
|
||||
getattr(api_client, method)("url", "data")
|
||||
|
||||
assert not request.called
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method', [
|
||||
'put',
|
||||
'post',
|
||||
'delete'
|
||||
])
|
||||
def test_inactive_service_can_be_modified_by_platform_admin(notify_admin, platform_admin_user, method):
|
||||
@pytest.mark.parametrize("method", ["put", "post", "delete"])
|
||||
def test_inactive_service_can_be_modified_by_platform_admin(
|
||||
notify_admin, platform_admin_user, method
|
||||
):
|
||||
api_client = NotifyAdminAPIClient()
|
||||
|
||||
with notify_admin.test_request_context() as request_context, notify_admin.test_client() as client:
|
||||
client.login(platform_admin_user)
|
||||
request_context.service = Service(service_json(active=False))
|
||||
|
||||
with patch.object(api_client, 'request') as request:
|
||||
ret = getattr(api_client, method)('url', 'data')
|
||||
with patch.object(api_client, "request") as request:
|
||||
ret = getattr(api_client, method)("url", "data")
|
||||
|
||||
assert request.called
|
||||
assert ret == request.return_value
|
||||
@@ -82,17 +77,22 @@ def test_inactive_service_can_be_modified_by_platform_admin(notify_admin, platfo
|
||||
|
||||
def test_generate_headers_sets_standard_headers(notify_admin):
|
||||
api_client = NotifyAdminAPIClient()
|
||||
with set_config(notify_admin, 'ROUTE_SECRET_KEY_1', 'proxy-secret'):
|
||||
with set_config(notify_admin, "ROUTE_SECRET_KEY_1", "proxy-secret"):
|
||||
api_client.init_app(notify_admin)
|
||||
|
||||
# with patch('app.notify_client.has_request_context', return_value=False):
|
||||
headers = api_client.generate_headers('api_token')
|
||||
headers = api_client.generate_headers("api_token")
|
||||
|
||||
assert set(headers.keys()) == {'Authorization', 'Content-type', 'User-agent', 'X-Custom-Forwarder'}
|
||||
assert headers['Authorization'] == 'Bearer api_token'
|
||||
assert headers['Content-type'] == 'application/json'
|
||||
assert headers['User-agent'].startswith('NOTIFY-API-PYTHON-CLIENT')
|
||||
assert headers['X-Custom-Forwarder'] == 'proxy-secret'
|
||||
assert set(headers.keys()) == {
|
||||
"Authorization",
|
||||
"Content-type",
|
||||
"User-agent",
|
||||
"X-Custom-Forwarder",
|
||||
}
|
||||
assert headers["Authorization"] == "Bearer api_token"
|
||||
assert headers["Content-type"] == "application/json"
|
||||
assert headers["User-agent"].startswith("NOTIFY-API-PYTHON-CLIENT")
|
||||
assert headers["X-Custom-Forwarder"] == "proxy-secret"
|
||||
|
||||
|
||||
def test_generate_headers_sets_request_id_if_in_request_context(notify_admin):
|
||||
@@ -100,23 +100,28 @@ def test_generate_headers_sets_request_id_if_in_request_context(notify_admin):
|
||||
api_client.init_app(notify_admin)
|
||||
|
||||
with notify_admin.test_request_context() as request_context:
|
||||
headers = api_client.generate_headers('api_token')
|
||||
headers = api_client.generate_headers("api_token")
|
||||
|
||||
assert set(headers.keys()) == {
|
||||
'Authorization', 'Content-type', 'User-agent', 'X-Custom-Forwarder', 'X-B3-TraceId', 'X-B3-SpanId',
|
||||
"Authorization",
|
||||
"Content-type",
|
||||
"User-agent",
|
||||
"X-Custom-Forwarder",
|
||||
"X-B3-TraceId",
|
||||
"X-B3-SpanId",
|
||||
}
|
||||
assert headers['X-B3-TraceId'] == request_context.request.request_id
|
||||
assert headers['X-B3-SpanId'] == request_context.request.span_id
|
||||
assert headers["X-B3-TraceId"] == request_context.request.request_id
|
||||
assert headers["X-B3-SpanId"] == request_context.request.span_id
|
||||
|
||||
|
||||
def test_get_notification_status_by_service(mocker):
|
||||
mock_get = mocker.patch.object(notification_api_client, 'get')
|
||||
mock_get = mocker.patch.object(notification_api_client, "get")
|
||||
start_date = date(2019, 4, 1)
|
||||
end_date = date(2019, 4, 30)
|
||||
|
||||
notification_api_client.get_notification_status_by_service(start_date, end_date)
|
||||
|
||||
mock_get.assert_called_once_with(
|
||||
url='service/monthly-data-by-service',
|
||||
params={'start_date': '2019-04-01', 'end_date': '2019-04-30'}
|
||||
url="service/monthly-data-by-service",
|
||||
params={"start_date": "2019-04-01", "end_date": "2019-04-30"},
|
||||
)
|
||||
|
||||
@@ -7,18 +7,18 @@ from app import organizations_client
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'client_method,'
|
||||
'expected_cache_get_calls,'
|
||||
'cache_value,'
|
||||
'expected_api_calls,'
|
||||
'expected_cache_set_calls,'
|
||||
'expected_return_value,'
|
||||
"client_method,"
|
||||
"expected_cache_get_calls,"
|
||||
"cache_value,"
|
||||
"expected_api_calls,"
|
||||
"expected_cache_set_calls,"
|
||||
"expected_return_value,"
|
||||
),
|
||||
[
|
||||
(
|
||||
'get_domains',
|
||||
"get_domains",
|
||||
[
|
||||
call('domains'),
|
||||
call("domains"),
|
||||
],
|
||||
b"""
|
||||
[
|
||||
@@ -28,36 +28,30 @@ from app import organizations_client
|
||||
""",
|
||||
[],
|
||||
[],
|
||||
['a', 'b', 'c', 'd', 'e'],
|
||||
["a", "b", "c", "d", "e"],
|
||||
),
|
||||
(
|
||||
'get_domains',
|
||||
"get_domains",
|
||||
[
|
||||
call('domains'),
|
||||
call('organizations'),
|
||||
call("domains"),
|
||||
call("organizations"),
|
||||
],
|
||||
None,
|
||||
[
|
||||
call(url='/organizations')
|
||||
],
|
||||
[call(url="/organizations")],
|
||||
[
|
||||
call(
|
||||
'organizations',
|
||||
"organizations",
|
||||
'[{"domains": ["x", "y", "z"]}]',
|
||||
ex=604800,
|
||||
),
|
||||
call(
|
||||
'domains',
|
||||
'["x", "y", "z"]',
|
||||
ex=604800
|
||||
),
|
||||
call("domains", '["x", "y", "z"]', ex=604800),
|
||||
],
|
||||
'from api',
|
||||
"from api",
|
||||
),
|
||||
(
|
||||
'get_organizations',
|
||||
"get_organizations",
|
||||
[
|
||||
call('organizations'),
|
||||
call("organizations"),
|
||||
],
|
||||
b"""
|
||||
[
|
||||
@@ -69,28 +63,26 @@ from app import organizations_client
|
||||
[],
|
||||
[
|
||||
{"name": "org 1", "domains": ["a", "b", "c"]},
|
||||
{"name": "org 2", "domains": ["c", "d", "e"]}
|
||||
{"name": "org 2", "domains": ["c", "d", "e"]},
|
||||
],
|
||||
),
|
||||
(
|
||||
'get_organizations',
|
||||
"get_organizations",
|
||||
[
|
||||
call('organizations'),
|
||||
call("organizations"),
|
||||
],
|
||||
None,
|
||||
[
|
||||
call(url='/organizations')
|
||||
],
|
||||
[call(url="/organizations")],
|
||||
[
|
||||
call(
|
||||
'organizations',
|
||||
"organizations",
|
||||
'[{"domains": ["x", "y", "z"]}]',
|
||||
ex=604800,
|
||||
),
|
||||
],
|
||||
'from api',
|
||||
"from api",
|
||||
),
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_returns_value_from_cache(
|
||||
notify_admin,
|
||||
@@ -102,19 +94,16 @@ def test_returns_value_from_cache(
|
||||
expected_api_calls,
|
||||
expected_cache_set_calls,
|
||||
):
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=cache_value,
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value=[
|
||||
{'domains': ['x', 'y', 'z']}
|
||||
],
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value=[{"domains": ["x", "y", "z"]}],
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
getattr(organizations_client, client_method)()
|
||||
@@ -130,118 +119,137 @@ def test_deletes_domain_cache(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_request = mocker.patch(
|
||||
"notifications_python_client.base.BaseAPIClient.request"
|
||||
)
|
||||
|
||||
organizations_client.update_organization(fake_uuid, foo='bar')
|
||||
organizations_client.update_organization(fake_uuid, foo="bar")
|
||||
|
||||
assert call('domains') in mock_redis_delete.call_args_list
|
||||
assert call("domains") in mock_redis_delete.call_args_list
|
||||
assert len(mock_request.call_args_list) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('post_data, expected_cache_delete_calls', (
|
||||
({'foo': 'bar'}, [
|
||||
call('organizations'),
|
||||
call('domains'),
|
||||
]),
|
||||
({'name': 'new name'}, [
|
||||
call('organization-6ce466d0-fd6a-11e5-82f5-e0accb9d11a6-name'),
|
||||
call('organizations'),
|
||||
call('domains'),
|
||||
]),
|
||||
))
|
||||
@pytest.mark.parametrize(
|
||||
"post_data, expected_cache_delete_calls",
|
||||
(
|
||||
(
|
||||
{"foo": "bar"},
|
||||
[
|
||||
call("organizations"),
|
||||
call("domains"),
|
||||
],
|
||||
),
|
||||
(
|
||||
{"name": "new name"},
|
||||
[
|
||||
call("organization-6ce466d0-fd6a-11e5-82f5-e0accb9d11a6-name"),
|
||||
call("organizations"),
|
||||
call("domains"),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
def test_update_organization_when_not_updating_org_type(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
post_data,
|
||||
expected_cache_delete_calls,
|
||||
):
|
||||
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch('app.notify_client.organizations_api_client.OrganizationsClient.post')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.organizations_api_client.OrganizationsClient.post"
|
||||
)
|
||||
|
||||
organizations_client.update_organization(fake_uuid, **post_data)
|
||||
|
||||
mock_post.assert_called_with(
|
||||
url='/organizations/{}'.format(fake_uuid),
|
||||
data=post_data
|
||||
url="/organizations/{}".format(fake_uuid), data=post_data
|
||||
)
|
||||
assert mock_redis_delete.call_args_list == expected_cache_delete_calls
|
||||
|
||||
|
||||
def test_update_organization_when_updating_org_type_and_org_has_services(mocker, fake_uuid):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch('app.notify_client.organizations_api_client.OrganizationsClient.post')
|
||||
def test_update_organization_when_updating_org_type_and_org_has_services(
|
||||
mocker, fake_uuid
|
||||
):
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.organizations_api_client.OrganizationsClient.post"
|
||||
)
|
||||
|
||||
organizations_client.update_organization(
|
||||
fake_uuid,
|
||||
cached_service_ids=['a', 'b', 'c'],
|
||||
organization_type='central',
|
||||
cached_service_ids=["a", "b", "c"],
|
||||
organization_type="central",
|
||||
)
|
||||
|
||||
mock_post.assert_called_with(
|
||||
url='/organizations/{}'.format(fake_uuid),
|
||||
data={'organization_type': 'central'}
|
||||
url="/organizations/{}".format(fake_uuid), data={"organization_type": "central"}
|
||||
)
|
||||
assert mock_redis_delete.call_args_list == [
|
||||
call('service-a', 'service-b', 'service-c'),
|
||||
call('organizations'),
|
||||
call('domains'),
|
||||
call("service-a", "service-b", "service-c"),
|
||||
call("organizations"),
|
||||
call("domains"),
|
||||
]
|
||||
|
||||
|
||||
def test_update_organization_when_updating_org_type_but_org_has_no_services(mocker, fake_uuid):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch('app.notify_client.organizations_api_client.OrganizationsClient.post')
|
||||
def test_update_organization_when_updating_org_type_but_org_has_no_services(
|
||||
mocker, fake_uuid
|
||||
):
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.organizations_api_client.OrganizationsClient.post"
|
||||
)
|
||||
|
||||
organizations_client.update_organization(
|
||||
fake_uuid,
|
||||
cached_service_ids=[],
|
||||
organization_type='central',
|
||||
organization_type="central",
|
||||
)
|
||||
|
||||
mock_post.assert_called_with(
|
||||
url='/organizations/{}'.format(fake_uuid),
|
||||
data={'organization_type': 'central'}
|
||||
url="/organizations/{}".format(fake_uuid), data={"organization_type": "central"}
|
||||
)
|
||||
assert mock_redis_delete.call_args_list == [
|
||||
call('organizations'),
|
||||
call('domains'),
|
||||
call("organizations"),
|
||||
call("domains"),
|
||||
]
|
||||
|
||||
|
||||
def test_update_service_organization_deletes_cache(mocker, fake_uuid):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_post = mocker.patch('app.notify_client.organizations_api_client.OrganizationsClient.post')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.organizations_api_client.OrganizationsClient.post"
|
||||
)
|
||||
|
||||
organizations_client.update_service_organization(
|
||||
service_id=fake_uuid,
|
||||
org_id=fake_uuid
|
||||
service_id=fake_uuid, org_id=fake_uuid
|
||||
)
|
||||
|
||||
assert sorted(mock_redis_delete.call_args_list) == [
|
||||
call('live-service-and-organization-counts'),
|
||||
call('organizations'),
|
||||
call('service-{}'.format(fake_uuid)),
|
||||
call("live-service-and-organization-counts"),
|
||||
call("organizations"),
|
||||
call("service-{}".format(fake_uuid)),
|
||||
]
|
||||
mock_post.assert_called_with(
|
||||
url='/organizations/{}/service'.format(fake_uuid),
|
||||
data=ANY
|
||||
url="/organizations/{}/service".format(fake_uuid), data=ANY
|
||||
)
|
||||
|
||||
|
||||
def test_remove_user_from_organization_deletes_user_cache(mocker):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_delete = mocker.patch('app.notify_client.organizations_api_client.OrganizationsClient.delete')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_delete = mocker.patch(
|
||||
"app.notify_client.organizations_api_client.OrganizationsClient.delete"
|
||||
)
|
||||
|
||||
org_id = 'abcd-1234'
|
||||
user_id = 'efgh-5678'
|
||||
org_id = "abcd-1234"
|
||||
user_id = "efgh-5678"
|
||||
|
||||
organizations_client.remove_user_from_organization(
|
||||
org_id=org_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
assert mock_redis_delete.call_args_list == [call(f'user-{user_id}')]
|
||||
mock_delete.assert_called_with(f'/organizations/{org_id}/users/{user_id}')
|
||||
assert mock_redis_delete.call_args_list == [call(f"user-{user_id}")]
|
||||
mock_delete.assert_called_with(f"/organizations/{org_id}/users/{user_id}")
|
||||
|
||||
@@ -6,47 +6,48 @@ from app.notify_client.performance_dashboard_api_client import (
|
||||
|
||||
|
||||
def test_get_aggregate_platform_stats(mocker):
|
||||
mocker.patch('app.extensions.RedisClient.get', return_value=None)
|
||||
mocker.patch("app.extensions.RedisClient.get", return_value=None)
|
||||
client = PerformanceDashboardAPIClient()
|
||||
mock = mocker.patch.object(client, 'get', return_value={})
|
||||
mock = mocker.patch.object(client, "get", return_value={})
|
||||
|
||||
client.get_performance_dashboard_stats(
|
||||
start_date=date(2021, 3, 1),
|
||||
end_date=date(2021, 3, 31),
|
||||
)
|
||||
|
||||
mock.assert_called_once_with('/performance-dashboard', params={
|
||||
'start_date': '2021-03-01',
|
||||
'end_date': '2021-03-31'
|
||||
})
|
||||
mock.assert_called_once_with(
|
||||
"/performance-dashboard",
|
||||
params={"start_date": "2021-03-01", "end_date": "2021-03-31"},
|
||||
)
|
||||
|
||||
|
||||
def test_sets_value_in_cache(mocker):
|
||||
client = PerformanceDashboardAPIClient()
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=None,
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value={'data_from': 'api'},
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value={"data_from": "api"},
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
assert client.get_performance_dashboard_stats(
|
||||
start_date=date(2021, 1, 1),
|
||||
end_date=date(2022, 2, 2),
|
||||
) == {'data_from': 'api'}
|
||||
) == {"data_from": "api"}
|
||||
|
||||
mock_redis_get.assert_called_once_with('performance-stats-2021-01-01-to-2022-02-02')
|
||||
mock_api_get.assert_called_once_with('/performance-dashboard', params={
|
||||
'start_date': '2021-01-01', 'end_date': '2022-02-02'
|
||||
})
|
||||
mock_redis_get.assert_called_once_with("performance-stats-2021-01-01-to-2022-02-02")
|
||||
mock_api_get.assert_called_once_with(
|
||||
"/performance-dashboard",
|
||||
params={"start_date": "2021-01-01", "end_date": "2022-02-02"},
|
||||
)
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'performance-stats-2021-01-01-to-2022-02-02',
|
||||
"performance-stats-2021-01-01-to-2022-02-02",
|
||||
'{"data_from": "api"}',
|
||||
ex=3600,
|
||||
)
|
||||
@@ -56,21 +57,21 @@ def test_returns_value_from_cache(mocker):
|
||||
client = PerformanceDashboardAPIClient()
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=b'{"data_from": "cache"}',
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
assert client.get_performance_dashboard_stats(
|
||||
start_date=date(2021, 1, 1),
|
||||
end_date=date(2022, 2, 2),
|
||||
) == {'data_from': 'cache'}
|
||||
) == {"data_from": "cache"}
|
||||
|
||||
mock_redis_get.assert_called_once_with('performance-stats-2021-01-01-to-2022-02-02')
|
||||
mock_redis_get.assert_called_once_with("performance-stats-2021-01-01-to-2022-02-02")
|
||||
assert mock_api_get.called is False
|
||||
assert mock_redis_set.called is False
|
||||
|
||||
@@ -3,8 +3,8 @@ from app.notify_client.platform_stats_api_client import PlatformStatsAPIClient
|
||||
|
||||
def test_get_aggregate_platform_stats(mocker):
|
||||
client = PlatformStatsAPIClient()
|
||||
mock = mocker.patch.object(client, 'get')
|
||||
params_dict = {'start_date': '2018-06-01', 'end_date': '2018-06-15'}
|
||||
mock = mocker.patch.object(client, "get")
|
||||
params_dict = {"start_date": "2018-06-01", "end_date": "2018-06-15"}
|
||||
|
||||
client.get_aggregate_platform_stats(params_dict=params_dict)
|
||||
mock.assert_called_once_with('/platform-stats', params=params_dict)
|
||||
mock.assert_called_once_with("/platform-stats", params=params_dict)
|
||||
|
||||
@@ -11,48 +11,56 @@ FAKE_TEMPLATE_ID = uuid4()
|
||||
|
||||
|
||||
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_by_pattern')
|
||||
expected_data = {
|
||||
'archived': True,
|
||||
'created_by': '1'
|
||||
}
|
||||
expected_url = '/service/{}/template/{}'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID)
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_delete_by_pattern = mocker.patch(
|
||||
"app.extensions.RedisClient.delete_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)}})
|
||||
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
|
||||
assert (
|
||||
call(f"service-{SERVICE_ONE_ID}-template-*")
|
||||
in mock_redis_delete_by_pattern.call_args_list
|
||||
)
|
||||
|
||||
|
||||
def test_client_gets_service(mocker):
|
||||
client = ServiceAPIClient()
|
||||
mock_get = mocker.patch.object(client, 'get', return_value={})
|
||||
mock_get = mocker.patch.object(client, "get", return_value={})
|
||||
|
||||
client.get_service('foo')
|
||||
mock_get.assert_called_once_with('/service/foo')
|
||||
client.get_service("foo")
|
||||
mock_get.assert_called_once_with("/service/foo")
|
||||
|
||||
|
||||
@pytest.mark.parametrize('limit_days', [None, 30])
|
||||
@pytest.mark.parametrize("limit_days", [None, 30])
|
||||
def test_client_gets_service_statistics(mocker, limit_days):
|
||||
client = ServiceAPIClient()
|
||||
mock_get = mocker.patch.object(client, 'get', return_value={'data': {'a': 'b'}})
|
||||
mock_get = mocker.patch.object(client, "get", return_value={"data": {"a": "b"}})
|
||||
|
||||
ret = client.get_service_statistics('foo', limit_days)
|
||||
ret = client.get_service_statistics("foo", limit_days)
|
||||
|
||||
assert ret == {'a': 'b'}
|
||||
mock_get.assert_called_once_with('/service/foo/statistics', params={'limit_days': limit_days})
|
||||
assert ret == {"a": "b"}
|
||||
mock_get.assert_called_once_with(
|
||||
"/service/foo/statistics", params={"limit_days": limit_days}
|
||||
)
|
||||
|
||||
|
||||
def test_client_only_updates_allowed_attributes(mocker):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
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'
|
||||
ServiceAPIClient().update_service("service_id", foo="bar")
|
||||
assert str(error.value) == "Not allowed to update service attributes: foo"
|
||||
|
||||
|
||||
def test_client_creates_service_with_correct_data(
|
||||
@@ -61,71 +69,76 @@ def test_client_creates_service_with_correct_data(
|
||||
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')
|
||||
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',
|
||||
"My first service",
|
||||
"central_government",
|
||||
1,
|
||||
True,
|
||||
fake_uuid,
|
||||
'test@example.com',
|
||||
"test@example.com",
|
||||
)
|
||||
mock_post.assert_called_once_with(
|
||||
'/service',
|
||||
"/service",
|
||||
dict(
|
||||
# Autogenerated arguments
|
||||
created_by='123',
|
||||
created_by="123",
|
||||
active=True,
|
||||
# ‘service_name’ argument is coerced to ‘name’
|
||||
name='My first service',
|
||||
name="My first service",
|
||||
# The rest pass through with the same names
|
||||
organization_type='central_government',
|
||||
organization_type="central_government",
|
||||
message_limit=1,
|
||||
restricted=True,
|
||||
user_id=fake_uuid,
|
||||
email_from='test@example.com',
|
||||
email_from="test@example.com",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_get_precompiled_template(mocker):
|
||||
client = ServiceAPIClient()
|
||||
mock_get = mocker.patch.object(client, 'get')
|
||||
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))
|
||||
mock_get.assert_called_once_with(
|
||||
"/service/{}/template/precompiled".format(SERVICE_ONE_ID)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_data, extra_args, expected_count', (
|
||||
@pytest.mark.parametrize(
|
||||
"template_data, extra_args, expected_count",
|
||||
(
|
||||
[],
|
||||
{},
|
||||
0,
|
||||
(
|
||||
[],
|
||||
{},
|
||||
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'},
|
||||
0,
|
||||
),
|
||||
(
|
||||
[
|
||||
{'template_type': 'email'},
|
||||
{'template_type': 'sms'},
|
||||
],
|
||||
{},
|
||||
2,
|
||||
),
|
||||
(
|
||||
[
|
||||
{'template_type': 'email'},
|
||||
{'template_type': 'sms'},
|
||||
],
|
||||
{'template_type': 'email'},
|
||||
1,
|
||||
),
|
||||
))
|
||||
)
|
||||
def test_client_returns_count_of_service_templates(
|
||||
notify_admin,
|
||||
mocker,
|
||||
@@ -133,179 +146,203 @@ def test_client_returns_count_of_service_templates(
|
||||
extra_args,
|
||||
expected_count,
|
||||
):
|
||||
|
||||
mocker.patch(
|
||||
'app.service_api_client.get_service_templates',
|
||||
return_value={'data': template_data}
|
||||
"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
|
||||
assert (
|
||||
service_api_client.count_service_templates(SERVICE_ONE_ID, **extra_args)
|
||||
== expected_count
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'client_method,'
|
||||
'extra_args,'
|
||||
'expected_cache_get_calls,'
|
||||
'cache_value,'
|
||||
'expected_api_calls,'
|
||||
'expected_cache_set_calls,'
|
||||
'expected_return_value,'
|
||||
"client_method,"
|
||||
"extra_args,"
|
||||
"expected_cache_get_calls,"
|
||||
"cache_value,"
|
||||
"expected_api_calls,"
|
||||
"expected_cache_set_calls,"
|
||||
"expected_return_value,"
|
||||
),
|
||||
[
|
||||
(
|
||||
service_api_client.get_service,
|
||||
[SERVICE_ONE_ID],
|
||||
[
|
||||
call('service-{}'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("service-{}".format(SERVICE_ONE_ID))],
|
||||
b'{"data_from": "cache"}',
|
||||
[],
|
||||
[],
|
||||
{'data_from': 'cache'},
|
||||
{"data_from": "cache"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service,
|
||||
[SERVICE_ONE_ID],
|
||||
[
|
||||
call('service-{}'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("service-{}".format(SERVICE_ONE_ID))],
|
||||
None,
|
||||
[
|
||||
call('/service/{}'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("/service/{}".format(SERVICE_ONE_ID))],
|
||||
[
|
||||
call(
|
||||
'service-{}'.format(SERVICE_ONE_ID),
|
||||
"service-{}".format(SERVICE_ONE_ID),
|
||||
'{"data_from": "api"}',
|
||||
ex=604800,
|
||||
)
|
||||
],
|
||||
{'data_from': 'api'},
|
||||
{"data_from": "api"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
call('service-{}-template-{}-version-None'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
call(
|
||||
"service-{}-template-{}-version-None".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
)
|
||||
],
|
||||
b'{"data_from": "cache"}',
|
||||
[],
|
||||
[],
|
||||
{'data_from': 'cache'},
|
||||
{"data_from": "cache"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
call('service-{}-template-{}-version-None'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID)),
|
||||
call(
|
||||
"service-{}-template-{}-version-None".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
),
|
||||
],
|
||||
None,
|
||||
[
|
||||
call('/service/{}/template/{}'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
],
|
||||
[call("/service/{}/template/{}".format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))],
|
||||
[
|
||||
call(
|
||||
'service-{}-template-{}-version-None'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID),
|
||||
"service-{}-template-{}-version-None".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
),
|
||||
'{"data_from": "api"}',
|
||||
ex=604800,
|
||||
),
|
||||
],
|
||||
{'data_from': 'api'},
|
||||
{"data_from": "api"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 1],
|
||||
[
|
||||
call('service-{}-template-{}-version-1'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
call(
|
||||
"service-{}-template-{}-version-1".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
)
|
||||
],
|
||||
b'{"data_from": "cache"}',
|
||||
[],
|
||||
[],
|
||||
{'data_from': 'cache'},
|
||||
{"data_from": "cache"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 1],
|
||||
[
|
||||
call('service-{}-template-{}-version-1'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID)),
|
||||
call(
|
||||
"service-{}-template-{}-version-1".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
),
|
||||
],
|
||||
None,
|
||||
[
|
||||
call('/service/{}/template/{}/version/1'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
call(
|
||||
"/service/{}/template/{}/version/1".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
)
|
||||
],
|
||||
[
|
||||
call(
|
||||
'service-{}-template-{}-version-1'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID),
|
||||
"service-{}-template-{}-version-1".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
),
|
||||
'{"data_from": "api"}',
|
||||
ex=604800,
|
||||
),
|
||||
],
|
||||
{'data_from': 'api'},
|
||||
{"data_from": "api"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_templates,
|
||||
[SERVICE_ONE_ID],
|
||||
[
|
||||
call('service-{}-templates'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("service-{}-templates".format(SERVICE_ONE_ID))],
|
||||
b'{"data_from": "cache"}',
|
||||
[],
|
||||
[],
|
||||
{'data_from': 'cache'},
|
||||
{"data_from": "cache"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_templates,
|
||||
[SERVICE_ONE_ID],
|
||||
[
|
||||
call('service-{}-templates'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("service-{}-templates".format(SERVICE_ONE_ID))],
|
||||
None,
|
||||
[
|
||||
call('/service/{}/template?detailed=False'.format(SERVICE_ONE_ID))
|
||||
],
|
||||
[call("/service/{}/template?detailed=False".format(SERVICE_ONE_ID))],
|
||||
[
|
||||
call(
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
'{"data_from": "api"}',
|
||||
ex=604800,
|
||||
)
|
||||
],
|
||||
{'data_from': 'api'},
|
||||
{"data_from": "api"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template_versions,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
call('service-{}-template-{}-versions'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
call(
|
||||
"service-{}-template-{}-versions".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
)
|
||||
],
|
||||
b'{"data_from": "cache"}',
|
||||
[],
|
||||
[],
|
||||
{'data_from': 'cache'},
|
||||
{"data_from": "cache"},
|
||||
),
|
||||
(
|
||||
service_api_client.get_service_template_versions,
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
call('service-{}-template-{}-versions'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID)),
|
||||
call(
|
||||
"service-{}-template-{}-versions".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
),
|
||||
],
|
||||
None,
|
||||
[
|
||||
call('/service/{}/template/{}/versions'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID))
|
||||
call(
|
||||
"/service/{}/template/{}/versions".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
)
|
||||
)
|
||||
],
|
||||
[
|
||||
call(
|
||||
'service-{}-template-{}-versions'.format(SERVICE_ONE_ID, FAKE_TEMPLATE_ID),
|
||||
"service-{}-template-{}-versions".format(
|
||||
SERVICE_ONE_ID, FAKE_TEMPLATE_ID
|
||||
),
|
||||
'{"data_from": "api"}',
|
||||
ex=604800,
|
||||
),
|
||||
],
|
||||
{'data_from': 'api'},
|
||||
{"data_from": "api"},
|
||||
),
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_returns_value_from_cache(
|
||||
mocker,
|
||||
@@ -317,17 +354,16 @@ def test_returns_value_from_cache(
|
||||
expected_api_calls,
|
||||
expected_cache_set_calls,
|
||||
):
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=cache_value,
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value={'data_from': 'api'},
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value={"data_from": "api"},
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
assert client_method(*extra_args) == expected_return_value
|
||||
@@ -337,27 +373,60 @@ def test_returns_value_from_cache(
|
||||
assert mock_redis_set.call_args_list == expected_cache_set_calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize('client, method, extra_args, extra_kwargs', [
|
||||
(service_api_client, 'update_service', [SERVICE_ONE_ID], {'name': 'foo'}),
|
||||
(service_api_client, 'update_service_with_properties', [SERVICE_ONE_ID], {'properties': {}}),
|
||||
(service_api_client, 'archive_service', [SERVICE_ONE_ID, []], {}),
|
||||
(service_api_client, 'suspend_service', [SERVICE_ONE_ID], {}),
|
||||
(service_api_client, 'resume_service', [SERVICE_ONE_ID], {}),
|
||||
(service_api_client, 'remove_user_from_service', [SERVICE_ONE_ID, ''], {}),
|
||||
(service_api_client, 'update_guest_list', [SERVICE_ONE_ID, {}], {}),
|
||||
(service_api_client, 'create_service_inbound_api', [SERVICE_ONE_ID] + [''] * 3, {}),
|
||||
(service_api_client, 'update_service_inbound_api', [SERVICE_ONE_ID] + [''] * 4, {}),
|
||||
(service_api_client, 'add_reply_to_email_address', [SERVICE_ONE_ID, ''], {}),
|
||||
(service_api_client, 'update_reply_to_email_address', [SERVICE_ONE_ID] + [''] * 2, {}),
|
||||
(service_api_client, 'delete_reply_to_email_address', [SERVICE_ONE_ID, ''], {}),
|
||||
(service_api_client, 'add_sms_sender', [SERVICE_ONE_ID, ''], {}),
|
||||
(service_api_client, 'update_sms_sender', [SERVICE_ONE_ID] + [''] * 2, {}),
|
||||
(service_api_client, 'delete_sms_sender', [SERVICE_ONE_ID, ''], {}),
|
||||
(service_api_client, 'update_service_callback_api', [SERVICE_ONE_ID] + [''] * 4, {}),
|
||||
(service_api_client, 'create_service_callback_api', [SERVICE_ONE_ID] + [''] * 3, {}),
|
||||
(user_api_client, 'add_user_to_service', [SERVICE_ONE_ID, uuid4(), [], []], {}),
|
||||
(invite_api_client, 'accept_invite', [SERVICE_ONE_ID, uuid4()], {}),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"client, method, extra_args, extra_kwargs",
|
||||
[
|
||||
(service_api_client, "update_service", [SERVICE_ONE_ID], {"name": "foo"}),
|
||||
(
|
||||
service_api_client,
|
||||
"update_service_with_properties",
|
||||
[SERVICE_ONE_ID],
|
||||
{"properties": {}},
|
||||
),
|
||||
(service_api_client, "archive_service", [SERVICE_ONE_ID, []], {}),
|
||||
(service_api_client, "suspend_service", [SERVICE_ONE_ID], {}),
|
||||
(service_api_client, "resume_service", [SERVICE_ONE_ID], {}),
|
||||
(service_api_client, "remove_user_from_service", [SERVICE_ONE_ID, ""], {}),
|
||||
(service_api_client, "update_guest_list", [SERVICE_ONE_ID, {}], {}),
|
||||
(
|
||||
service_api_client,
|
||||
"create_service_inbound_api",
|
||||
[SERVICE_ONE_ID] + [""] * 3,
|
||||
{},
|
||||
),
|
||||
(
|
||||
service_api_client,
|
||||
"update_service_inbound_api",
|
||||
[SERVICE_ONE_ID] + [""] * 4,
|
||||
{},
|
||||
),
|
||||
(service_api_client, "add_reply_to_email_address", [SERVICE_ONE_ID, ""], {}),
|
||||
(
|
||||
service_api_client,
|
||||
"update_reply_to_email_address",
|
||||
[SERVICE_ONE_ID] + [""] * 2,
|
||||
{},
|
||||
),
|
||||
(service_api_client, "delete_reply_to_email_address", [SERVICE_ONE_ID, ""], {}),
|
||||
(service_api_client, "add_sms_sender", [SERVICE_ONE_ID, ""], {}),
|
||||
(service_api_client, "update_sms_sender", [SERVICE_ONE_ID] + [""] * 2, {}),
|
||||
(service_api_client, "delete_sms_sender", [SERVICE_ONE_ID, ""], {}),
|
||||
(
|
||||
service_api_client,
|
||||
"update_service_callback_api",
|
||||
[SERVICE_ONE_ID] + [""] * 4,
|
||||
{},
|
||||
),
|
||||
(
|
||||
service_api_client,
|
||||
"create_service_callback_api",
|
||||
[SERVICE_ONE_ID] + [""] * 3,
|
||||
{},
|
||||
),
|
||||
(user_api_client, "add_user_to_service", [SERVICE_ONE_ID, uuid4(), [], []], {}),
|
||||
(invite_api_client, "accept_invite", [SERVICE_ONE_ID, uuid4()], {}),
|
||||
],
|
||||
)
|
||||
def test_deletes_service_cache(
|
||||
notify_admin,
|
||||
mock_get_user,
|
||||
@@ -368,37 +437,66 @@ def test_deletes_service_cache(
|
||||
extra_args,
|
||||
extra_kwargs,
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_request = mocker.patch(
|
||||
"notifications_python_client.base.BaseAPIClient.request"
|
||||
)
|
||||
|
||||
getattr(client, method)(*extra_args, **extra_kwargs)
|
||||
|
||||
assert call('service-{}'.format(SERVICE_ONE_ID)) in mock_redis_delete.call_args_list
|
||||
assert call("service-{}".format(SERVICE_ONE_ID)) in mock_redis_delete.call_args_list
|
||||
assert len(mock_request.call_args_list) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize('method, extra_args, expected_cache_deletes', [
|
||||
('create_service_template', ['name', 'type_', 'content', SERVICE_ONE_ID], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
('update_service_template', [FAKE_TEMPLATE_ID, 'foo', 'sms', 'bar', SERVICE_ONE_ID], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
('redact_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
('update_service_template_sender', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID, 'foo'], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
('delete_service_template', [SERVICE_ONE_ID, FAKE_TEMPLATE_ID], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
('archive_service', [SERVICE_ONE_ID, []], [
|
||||
'service-{}-templates'.format(SERVICE_ONE_ID),
|
||||
'service-{}'.format(SERVICE_ONE_ID),
|
||||
]),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"method, extra_args, expected_cache_deletes",
|
||||
[
|
||||
(
|
||||
"create_service_template",
|
||||
["name", "type_", "content", SERVICE_ONE_ID],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
(
|
||||
"update_service_template",
|
||||
[FAKE_TEMPLATE_ID, "foo", "sms", "bar", SERVICE_ONE_ID],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
(
|
||||
"redact_service_template",
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
(
|
||||
"update_service_template_sender",
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID, "foo"],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
(
|
||||
"delete_service_template",
|
||||
[SERVICE_ONE_ID, FAKE_TEMPLATE_ID],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
(
|
||||
"archive_service",
|
||||
[SERVICE_ONE_ID, []],
|
||||
[
|
||||
"service-{}-templates".format(SERVICE_ONE_ID),
|
||||
"service-{}".format(SERVICE_ONE_ID),
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_deletes_caches_when_modifying_templates(
|
||||
notify_admin,
|
||||
mock_get_user,
|
||||
@@ -407,54 +505,72 @@ def test_deletes_caches_when_modifying_templates(
|
||||
extra_args,
|
||||
expected_cache_deletes,
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete_by_pattern = mocker.patch('app.extensions.RedisClient.delete_by_pattern')
|
||||
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_redis_delete_by_pattern = mocker.patch(
|
||||
"app.extensions.RedisClient.delete_by_pattern"
|
||||
)
|
||||
mock_request = mocker.patch(
|
||||
"notifications_python_client.base.BaseAPIClient.request"
|
||||
)
|
||||
|
||||
getattr(service_api_client, method)(*extra_args)
|
||||
|
||||
assert mock_redis_delete.call_args_list == [call(x) for x in expected_cache_deletes]
|
||||
assert len(mock_request.call_args_list) == 1
|
||||
if method != 'create_service_template':
|
||||
if method != "create_service_template":
|
||||
# no deletes for template cach on create_service_template
|
||||
assert len(mock_redis_delete_by_pattern.call_args_list) == 1
|
||||
assert mock_redis_delete_by_pattern.call_args_list[0] == call(f'service-{SERVICE_ONE_ID}-template-*')
|
||||
assert mock_redis_delete_by_pattern.call_args_list[0] == call(
|
||||
f"service-{SERVICE_ONE_ID}-template-*"
|
||||
)
|
||||
|
||||
|
||||
def test_deletes_cached_users_when_archiving_service(mocker, mock_get_service_templates):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete_by_pattern = mocker.patch('app.extensions.RedisClient.delete_by_pattern')
|
||||
def test_deletes_cached_users_when_archiving_service(
|
||||
mocker, mock_get_service_templates
|
||||
):
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_redis_delete_by_pattern = mocker.patch(
|
||||
"app.extensions.RedisClient.delete_by_pattern"
|
||||
)
|
||||
|
||||
mocker.patch('notifications_python_client.base.BaseAPIClient.request', return_value={'data': ""})
|
||||
mocker.patch(
|
||||
"notifications_python_client.base.BaseAPIClient.request",
|
||||
return_value={"data": ""},
|
||||
)
|
||||
|
||||
service_api_client.archive_service(SERVICE_ONE_ID, ["my-user-id1", "my-user-id2"])
|
||||
|
||||
assert call('user-my-user-id1', 'user-my-user-id2') in mock_redis_delete.call_args_list
|
||||
assert call(f'service-{SERVICE_ONE_ID}-template-*') in mock_redis_delete_by_pattern.call_args_list
|
||||
assert (
|
||||
call("user-my-user-id1", "user-my-user-id2") in mock_redis_delete.call_args_list
|
||||
)
|
||||
assert (
|
||||
call(f"service-{SERVICE_ONE_ID}-template-*")
|
||||
in mock_redis_delete_by_pattern.call_args_list
|
||||
)
|
||||
|
||||
|
||||
def test_client_gets_guest_list(mocker):
|
||||
client = ServiceAPIClient()
|
||||
mock_get = mocker.patch.object(client, 'get', return_value=['a', 'b', 'c'])
|
||||
mock_get = mocker.patch.object(client, "get", return_value=["a", "b", "c"])
|
||||
|
||||
response = client.get_guest_list('foo')
|
||||
response = client.get_guest_list("foo")
|
||||
|
||||
assert response == ['a', 'b', 'c']
|
||||
assert response == ["a", "b", "c"]
|
||||
mock_get.assert_called_once_with(
|
||||
url='/service/foo/guest-list',
|
||||
url="/service/foo/guest-list",
|
||||
)
|
||||
|
||||
|
||||
def test_client_updates_guest_list(mocker):
|
||||
client = ServiceAPIClient()
|
||||
mock_put = mocker.patch.object(client, 'put')
|
||||
mock_put = mocker.patch.object(client, "put")
|
||||
|
||||
client.update_guest_list('foo', data=['a', 'b', 'c'])
|
||||
client.update_guest_list("foo", data=["a", "b", "c"])
|
||||
|
||||
mock_put.assert_called_once_with(
|
||||
url='/service/foo/guest-list',
|
||||
data=['a', 'b', 'c'],
|
||||
url="/service/foo/guest-list",
|
||||
data=["a", "b", "c"],
|
||||
)
|
||||
|
||||
|
||||
@@ -462,79 +578,85 @@ def test_client_doesnt_delete_service_template_cache_when_none_exist(
|
||||
notify_admin,
|
||||
mock_get_user,
|
||||
mock_get_service_templates_when_no_templates_exist,
|
||||
mocker
|
||||
mocker,
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete_by_pattern = mocker.patch('app.extensions.RedisClient.delete_by_pattern')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mocker.patch("notifications_python_client.base.BaseAPIClient.request")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_redis_delete_by_pattern = mocker.patch(
|
||||
"app.extensions.RedisClient.delete_by_pattern"
|
||||
)
|
||||
|
||||
service_api_client.update_reply_to_email_address(SERVICE_ONE_ID, uuid4(), 'foo@bar.com')
|
||||
service_api_client.update_reply_to_email_address(
|
||||
SERVICE_ONE_ID, uuid4(), "foo@bar.com"
|
||||
)
|
||||
|
||||
assert len(mock_redis_delete.call_args_list) == 1
|
||||
assert mock_redis_delete.call_args_list[0] == call('service-{}'.format(SERVICE_ONE_ID))
|
||||
assert mock_redis_delete.call_args_list[0] == call(
|
||||
"service-{}".format(SERVICE_ONE_ID)
|
||||
)
|
||||
|
||||
assert len(mock_redis_delete_by_pattern.call_args_list) == 1
|
||||
|
||||
|
||||
def test_client_deletes_service_template_cache_when_service_is_updated(
|
||||
notify_admin,
|
||||
mock_get_user,
|
||||
mocker
|
||||
notify_admin, mock_get_user, mocker
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete_by_pattern = mocker.patch('app.extensions.RedisClient.delete_by_pattern')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mocker.patch("notifications_python_client.base.BaseAPIClient.request")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_redis_delete_by_pattern = mocker.patch(
|
||||
"app.extensions.RedisClient.delete_by_pattern"
|
||||
)
|
||||
|
||||
service_api_client.update_reply_to_email_address(SERVICE_ONE_ID, uuid4(), 'foo@bar.com')
|
||||
service_api_client.update_reply_to_email_address(
|
||||
SERVICE_ONE_ID, uuid4(), "foo@bar.com"
|
||||
)
|
||||
|
||||
assert len(mock_redis_delete.call_args_list) == 1
|
||||
assert mock_redis_delete.call_args_list[0] == call(f'service-{SERVICE_ONE_ID}')
|
||||
assert mock_redis_delete_by_pattern.call_args_list[0] == call(f'service-{SERVICE_ONE_ID}-template-*')
|
||||
assert mock_redis_delete.call_args_list[0] == call(f"service-{SERVICE_ONE_ID}")
|
||||
assert mock_redis_delete_by_pattern.call_args_list[0] == call(
|
||||
f"service-{SERVICE_ONE_ID}-template-*"
|
||||
)
|
||||
|
||||
|
||||
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')
|
||||
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',
|
||||
'message_limit',
|
||||
'name',
|
||||
'notes',
|
||||
'organization_type',
|
||||
'permissions',
|
||||
'prefix_sms',
|
||||
'rate_limit',
|
||||
'reply_to_email_address',
|
||||
'research_mode',
|
||||
'restricted',
|
||||
'sms_sender',
|
||||
'volume_email',
|
||||
'volume_sms',
|
||||
"active",
|
||||
"consent_to_research",
|
||||
"contact_link",
|
||||
"count_as_live",
|
||||
"email_branding",
|
||||
"email_from",
|
||||
"free_sms_fragment_limit",
|
||||
"go_live_at",
|
||||
"go_live_user",
|
||||
"message_limit",
|
||||
"name",
|
||||
"notes",
|
||||
"organization_type",
|
||||
"permissions",
|
||||
"prefix_sms",
|
||||
"rate_limit",
|
||||
"reply_to_email_address",
|
||||
"research_mode",
|
||||
"restricted",
|
||||
"sms_sender",
|
||||
"volume_email",
|
||||
"volume_sms",
|
||||
]
|
||||
|
||||
attrs_dict = {}
|
||||
for attr in allowed_attributes:
|
||||
attrs_dict[attr] = "value"
|
||||
|
||||
client.update_service(
|
||||
SERVICE_ONE_ID,
|
||||
**attrs_dict
|
||||
)
|
||||
client.update_service(SERVICE_ONE_ID, **attrs_dict)
|
||||
mock_post.assert_called_once_with(
|
||||
f'/service/{SERVICE_ONE_ID}',
|
||||
{**{'created_by': '123'}, **attrs_dict}
|
||||
f"/service/{SERVICE_ONE_ID}", {**{"created_by": "123"}, **attrs_dict}
|
||||
)
|
||||
|
||||
@@ -2,38 +2,35 @@ from app.notify_client.status_api_client import StatusApiClient
|
||||
|
||||
|
||||
def test_get_count_of_live_services_and_organizations(mocker):
|
||||
mocker.patch('app.extensions.RedisClient.get', return_value=None)
|
||||
mocker.patch("app.extensions.RedisClient.get", return_value=None)
|
||||
client = StatusApiClient()
|
||||
mock = mocker.patch.object(client, 'get', return_value={})
|
||||
mock = mocker.patch.object(client, "get", return_value={})
|
||||
|
||||
client.get_count_of_live_services_and_organizations()
|
||||
|
||||
mock.assert_called_once_with(url='/_status/live-service-and-organization-counts')
|
||||
mock.assert_called_once_with(url="/_status/live-service-and-organization-counts")
|
||||
|
||||
|
||||
def test_sets_value_in_cache(mocker):
|
||||
client = StatusApiClient()
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
return_value=None
|
||||
)
|
||||
mock_redis_get = mocker.patch("app.extensions.RedisClient.get", return_value=None)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value={'data_from': 'api'},
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value={"data_from": "api"},
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
assert client.get_count_of_live_services_and_organizations() == {'data_from': 'api'}
|
||||
assert client.get_count_of_live_services_and_organizations() == {"data_from": "api"}
|
||||
|
||||
mock_redis_get.assert_called_once_with('live-service-and-organization-counts')
|
||||
mock_api_get.assert_called_once_with(url='/_status/live-service-and-organization-counts')
|
||||
mock_redis_get.assert_called_once_with("live-service-and-organization-counts")
|
||||
mock_api_get.assert_called_once_with(
|
||||
url="/_status/live-service-and-organization-counts"
|
||||
)
|
||||
mock_redis_set.assert_called_once_with(
|
||||
'live-service-and-organization-counts',
|
||||
'{"data_from": "api"}',
|
||||
ex=3600
|
||||
"live-service-and-organization-counts", '{"data_from": "api"}', ex=3600
|
||||
)
|
||||
|
||||
|
||||
@@ -41,19 +38,21 @@ def test_returns_value_from_cache(mocker):
|
||||
client = StatusApiClient()
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=b'{"data_from": "cache"}',
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
assert client.get_count_of_live_services_and_organizations() == {'data_from': 'cache'}
|
||||
assert client.get_count_of_live_services_and_organizations() == {
|
||||
"data_from": "cache"
|
||||
}
|
||||
|
||||
mock_redis_get.assert_called_once_with('live-service-and-organization-counts')
|
||||
mock_redis_get.assert_called_once_with("live-service-and-organization-counts")
|
||||
|
||||
assert mock_api_get.called is False
|
||||
assert mock_redis_set.called is False
|
||||
|
||||
@@ -7,41 +7,45 @@ from orderedset import OrderedSet
|
||||
from app.notify_client.template_folder_api_client import TemplateFolderAPIClient
|
||||
|
||||
|
||||
@pytest.mark.parametrize('parent_id', [uuid.uuid4(), None])
|
||||
@pytest.mark.parametrize("parent_id", [uuid.uuid4(), None])
|
||||
def test_create_template_folder_calls_correct_api_endpoint(mocker, parent_id):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-folder'.format(some_service_id)
|
||||
data = {'name': 'foo', 'parent_id': parent_id}
|
||||
expected_url = "/service/{}/template-folder".format(some_service_id)
|
||||
data = {"name": "foo", "parent_id": parent_id}
|
||||
|
||||
client = TemplateFolderAPIClient()
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.template_folder_api_client.TemplateFolderAPIClient.post')
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.template_folder_api_client.TemplateFolderAPIClient.post"
|
||||
)
|
||||
|
||||
client.create_template_folder(some_service_id, name='foo', parent_id=parent_id)
|
||||
client.create_template_folder(some_service_id, name="foo", parent_id=parent_id)
|
||||
|
||||
mock_post.assert_called_once_with(expected_url, data)
|
||||
mock_redis_delete.assert_called_once_with('service-{}-template-folders'.format(some_service_id))
|
||||
mock_redis_delete.assert_called_once_with(
|
||||
"service-{}-template-folders".format(some_service_id)
|
||||
)
|
||||
|
||||
|
||||
def test_get_template_folders_calls_correct_api_endpoint(mocker):
|
||||
mock_redis_get = mocker.patch('app.extensions.RedisClient.get', return_value=None)
|
||||
mock_redis_set = mocker.patch('app.extensions.RedisClient.set')
|
||||
mock_redis_get = mocker.patch("app.extensions.RedisClient.get", return_value=None)
|
||||
mock_redis_set = mocker.patch("app.extensions.RedisClient.set")
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value={'template_folders': {'a': 'b'}}
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value={"template_folders": {"a": "b"}},
|
||||
)
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-folder'.format(some_service_id)
|
||||
redis_key = 'service-{}-template-folders'.format(some_service_id)
|
||||
expected_url = "/service/{}/template-folder".format(some_service_id)
|
||||
redis_key = "service-{}-template-folders".format(some_service_id)
|
||||
|
||||
client = TemplateFolderAPIClient()
|
||||
|
||||
ret = client.get_template_folders(some_service_id)
|
||||
|
||||
assert ret == {'a': 'b'}
|
||||
assert ret == {"a": "b"}
|
||||
|
||||
mock_redis_get.assert_called_once_with(redis_key)
|
||||
mock_api_get.assert_called_once_with(expected_url)
|
||||
@@ -49,9 +53,8 @@ def test_get_template_folders_calls_correct_api_endpoint(mocker):
|
||||
|
||||
|
||||
def test_move_templates_and_folders(mocker):
|
||||
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_api_post = mocker.patch('app.notify_client.NotifyAdminAPIClient.post')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_api_post = mocker.patch("app.notify_client.NotifyAdminAPIClient.post")
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
some_folder_id = uuid.uuid4()
|
||||
@@ -59,82 +62,98 @@ def test_move_templates_and_folders(mocker):
|
||||
TemplateFolderAPIClient().move_to_folder(
|
||||
some_service_id,
|
||||
some_folder_id,
|
||||
template_ids=OrderedSet(('a', 'b', 'c')),
|
||||
folder_ids=OrderedSet(('1', '2', '3')),
|
||||
template_ids=OrderedSet(("a", "b", "c")),
|
||||
folder_ids=OrderedSet(("1", "2", "3")),
|
||||
)
|
||||
|
||||
mock_api_post.assert_called_once_with(
|
||||
'/service/{}/template-folder/{}/contents'.format(
|
||||
"/service/{}/template-folder/{}/contents".format(
|
||||
some_service_id, some_folder_id
|
||||
),
|
||||
{
|
||||
'folders': ['1', '2', '3'],
|
||||
'templates': ['a', 'b', 'c'],
|
||||
"folders": ["1", "2", "3"],
|
||||
"templates": ["a", "b", "c"],
|
||||
},
|
||||
)
|
||||
assert mock_redis_delete.call_args_list == [
|
||||
call(
|
||||
f'service-{some_service_id}-template-a-version-None',
|
||||
f'service-{some_service_id}-template-b-version-None',
|
||||
f'service-{some_service_id}-template-c-version-None',
|
||||
f"service-{some_service_id}-template-a-version-None",
|
||||
f"service-{some_service_id}-template-b-version-None",
|
||||
f"service-{some_service_id}-template-c-version-None",
|
||||
),
|
||||
call('service-{}-templates'.format(some_service_id)),
|
||||
call('service-{}-template-folders'.format(some_service_id)),
|
||||
call("service-{}-templates".format(some_service_id)),
|
||||
call("service-{}-template-folders".format(some_service_id)),
|
||||
]
|
||||
|
||||
|
||||
def test_move_templates_and_folders_to_root(mocker):
|
||||
|
||||
mock_api_post = mocker.patch('app.notify_client.NotifyAdminAPIClient.post')
|
||||
mock_api_post = mocker.patch("app.notify_client.NotifyAdminAPIClient.post")
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
|
||||
TemplateFolderAPIClient().move_to_folder(
|
||||
some_service_id,
|
||||
None,
|
||||
template_ids=OrderedSet(('a', 'b', 'c')),
|
||||
folder_ids=OrderedSet(('1', '2', '3')),
|
||||
template_ids=OrderedSet(("a", "b", "c")),
|
||||
folder_ids=OrderedSet(("1", "2", "3")),
|
||||
)
|
||||
|
||||
mock_api_post.assert_called_once_with(
|
||||
'/service/{}/template-folder/contents'.format(some_service_id),
|
||||
"/service/{}/template-folder/contents".format(some_service_id),
|
||||
{
|
||||
'folders': ['1', '2', '3'],
|
||||
'templates': ['a', 'b', 'c'],
|
||||
"folders": ["1", "2", "3"],
|
||||
"templates": ["a", "b", "c"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_update_template_folder_calls_correct_api_endpoint(mocker):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
template_folder_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-folder/{}'.format(some_service_id, template_folder_id)
|
||||
data = {'name': 'foo', 'users_with_permission': ['some_id']}
|
||||
expected_url = "/service/{}/template-folder/{}".format(
|
||||
some_service_id, template_folder_id
|
||||
)
|
||||
data = {"name": "foo", "users_with_permission": ["some_id"]}
|
||||
|
||||
client = TemplateFolderAPIClient()
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.template_folder_api_client.TemplateFolderAPIClient.post')
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.template_folder_api_client.TemplateFolderAPIClient.post"
|
||||
)
|
||||
|
||||
client.update_template_folder(some_service_id, template_folder_id, name='foo', users_with_permission=['some_id'])
|
||||
client.update_template_folder(
|
||||
some_service_id,
|
||||
template_folder_id,
|
||||
name="foo",
|
||||
users_with_permission=["some_id"],
|
||||
)
|
||||
|
||||
mock_post.assert_called_once_with(expected_url, data)
|
||||
mock_redis_delete.assert_called_once_with('service-{}-template-folders'.format(some_service_id))
|
||||
mock_redis_delete.assert_called_once_with(
|
||||
"service-{}-template-folders".format(some_service_id)
|
||||
)
|
||||
|
||||
|
||||
def test_delete_template_folder_calls_correct_api_endpoint(mocker):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
|
||||
some_service_id = uuid.uuid4()
|
||||
template_folder_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-folder/{}'.format(some_service_id, template_folder_id)
|
||||
expected_url = "/service/{}/template-folder/{}".format(
|
||||
some_service_id, template_folder_id
|
||||
)
|
||||
|
||||
client = TemplateFolderAPIClient()
|
||||
|
||||
mock_delete = mocker.patch('app.notify_client.template_folder_api_client.TemplateFolderAPIClient.delete')
|
||||
mock_delete = mocker.patch(
|
||||
"app.notify_client.template_folder_api_client.TemplateFolderAPIClient.delete"
|
||||
)
|
||||
|
||||
client.delete_template_folder(some_service_id, template_folder_id)
|
||||
|
||||
mock_delete.assert_called_once_with(expected_url, {})
|
||||
mock_redis_delete.assert_called_once_with('service-{}-template-folders'.format(some_service_id))
|
||||
mock_redis_delete.assert_called_once_with(
|
||||
"service-{}-template-folders".format(some_service_id)
|
||||
)
|
||||
|
||||
@@ -3,27 +3,36 @@ import uuid
|
||||
from app.notify_client.template_statistics_api_client import TemplateStatisticsApiClient
|
||||
|
||||
|
||||
def test_template_statistics_client_calls_correct_api_endpoint_for_service(mocker, api_user_active):
|
||||
|
||||
def test_template_statistics_client_calls_correct_api_endpoint_for_service(
|
||||
mocker, api_user_active
|
||||
):
|
||||
some_service_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-statistics'.format(some_service_id)
|
||||
expected_url = "/service/{}/template-statistics".format(some_service_id)
|
||||
|
||||
client = TemplateStatisticsApiClient()
|
||||
|
||||
mock_get = mocker.patch('app.notify_client.template_statistics_api_client.TemplateStatisticsApiClient.get')
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.template_statistics_api_client.TemplateStatisticsApiClient.get"
|
||||
)
|
||||
|
||||
client.get_template_statistics_for_service(some_service_id)
|
||||
|
||||
mock_get.assert_called_once_with(url=expected_url, params={})
|
||||
|
||||
|
||||
def test_template_statistics_client_calls_correct_api_endpoint_for_template(mocker, api_user_active):
|
||||
def test_template_statistics_client_calls_correct_api_endpoint_for_template(
|
||||
mocker, api_user_active
|
||||
):
|
||||
some_service_id = uuid.uuid4()
|
||||
some_template_id = uuid.uuid4()
|
||||
expected_url = '/service/{}/template-statistics/last-used/{}'.format(some_service_id, some_template_id)
|
||||
expected_url = "/service/{}/template-statistics/last-used/{}".format(
|
||||
some_service_id, some_template_id
|
||||
)
|
||||
|
||||
client = TemplateStatisticsApiClient()
|
||||
mock_get = mocker.patch('app.notify_client.template_statistics_api_client.TemplateStatisticsApiClient.get')
|
||||
mock_get = mocker.patch(
|
||||
"app.notify_client.template_statistics_api_client.TemplateStatisticsApiClient.get"
|
||||
)
|
||||
|
||||
client.get_last_used_date_for_template(some_service_id, some_template_id)
|
||||
|
||||
|
||||
@@ -14,59 +14,63 @@ def test_client_gets_all_users_for_service(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
):
|
||||
|
||||
user_api_client.max_failed_login_count = 99 # doesn't matter for this test
|
||||
mock_get = mocker.patch(
|
||||
'app.notify_client.user_api_client.UserApiClient.get',
|
||||
return_value={'data': [
|
||||
{'id': fake_uuid},
|
||||
]}
|
||||
"app.notify_client.user_api_client.UserApiClient.get",
|
||||
return_value={
|
||||
"data": [
|
||||
{"id": fake_uuid},
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
users = user_api_client.get_users_for_service(SERVICE_ONE_ID)
|
||||
|
||||
mock_get.assert_called_once_with('/service/{}/users'.format(SERVICE_ONE_ID))
|
||||
mock_get.assert_called_once_with("/service/{}/users".format(SERVICE_ONE_ID))
|
||||
assert len(users) == 1
|
||||
assert users[0]['id'] == fake_uuid
|
||||
assert users[0]["id"] == fake_uuid
|
||||
|
||||
|
||||
def test_client_uses_correct_find_by_email(mocker, api_user_active):
|
||||
|
||||
expected_url = '/user/email'
|
||||
expected_data = {'email': api_user_active['email_address']}
|
||||
expected_url = "/user/email"
|
||||
expected_data = {"email": api_user_active["email_address"]}
|
||||
|
||||
user_api_client.max_failed_login_count = 1 # doesn't matter for this test
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.get_user_by_email(api_user_active['email_address'])
|
||||
user_api_client.get_user_by_email(api_user_active["email_address"])
|
||||
|
||||
mock_post.assert_called_once_with(expected_url, data=expected_data)
|
||||
|
||||
|
||||
def test_client_only_updates_allowed_attributes(mocker):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
with pytest.raises(TypeError) as error:
|
||||
user_api_client.update_user_attribute('user_id', id='1')
|
||||
assert str(error.value) == 'Not allowed to update user attributes: id'
|
||||
user_api_client.update_user_attribute("user_id", id="1")
|
||||
assert str(error.value) == "Not allowed to update user attributes: id"
|
||||
|
||||
|
||||
def test_client_updates_password_separately(mocker, api_user_active):
|
||||
expected_url = '/user/{}/update-password'.format(api_user_active['id'])
|
||||
expected_params = {'_password': 'newpassword'}
|
||||
expected_url = "/user/{}/update-password".format(api_user_active["id"])
|
||||
expected_params = {"_password": "newpassword"}
|
||||
user_api_client.max_failed_login_count = 1 # doesn't matter for this test
|
||||
mock_update_password = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
mock_update_password = mocker.patch(
|
||||
"app.notify_client.user_api_client.UserApiClient.post"
|
||||
)
|
||||
|
||||
user_api_client.update_password(api_user_active['id'], expected_params['_password'])
|
||||
user_api_client.update_password(api_user_active["id"], expected_params["_password"])
|
||||
mock_update_password.assert_called_once_with(expected_url, data=expected_params)
|
||||
|
||||
|
||||
def test_client_activates_if_pending(mocker, api_user_pending):
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
user_api_client.max_failed_login_count = 1 # doesn't matter for this test
|
||||
|
||||
user_api_client.activate_user(api_user_pending['id'])
|
||||
user_api_client.activate_user(api_user_pending["id"])
|
||||
|
||||
mock_post.assert_called_once_with('/user/{}/activate'.format(api_user_pending['id']), data=None)
|
||||
mock_post.assert_called_once_with(
|
||||
"/user/{}/activate".format(api_user_pending["id"]), data=None
|
||||
)
|
||||
|
||||
|
||||
def test_client_passes_admin_url_when_sending_email_auth(
|
||||
@@ -74,82 +78,91 @@ def test_client_passes_admin_url_when_sending_email_auth(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
):
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.send_verify_code(fake_uuid, 'email', 'ignored@example.com')
|
||||
user_api_client.send_verify_code(fake_uuid, "email", "ignored@example.com")
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
'/user/{}/email-code'.format(fake_uuid),
|
||||
"/user/{}/email-code".format(fake_uuid),
|
||||
data={
|
||||
'to': 'ignored@example.com',
|
||||
'email_auth_link_host': 'http://localhost:6012',
|
||||
}
|
||||
"to": "ignored@example.com",
|
||||
"email_auth_link_host": "http://localhost:6012",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_client_converts_admin_permissions_to_db_permissions_on_edit(notify_admin, mocker):
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
def test_client_converts_admin_permissions_to_db_permissions_on_edit(
|
||||
notify_admin, mocker
|
||||
):
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.set_user_permissions('user_id', 'service_id', permissions={'send_messages', 'view_activity'})
|
||||
user_api_client.set_user_permissions(
|
||||
"user_id", "service_id", permissions={"send_messages", "view_activity"}
|
||||
)
|
||||
|
||||
assert sorted(mock_post.call_args[1]['data']['permissions'], key=lambda x: x['permission']) == sorted([
|
||||
{'permission': 'send_texts'},
|
||||
{'permission': 'send_emails'},
|
||||
{'permission': 'view_activity'},
|
||||
], key=lambda x: x['permission'])
|
||||
assert sorted(
|
||||
mock_post.call_args[1]["data"]["permissions"], key=lambda x: x["permission"]
|
||||
) == sorted(
|
||||
[
|
||||
{"permission": "send_texts"},
|
||||
{"permission": "send_emails"},
|
||||
{"permission": "view_activity"},
|
||||
],
|
||||
key=lambda x: x["permission"],
|
||||
)
|
||||
|
||||
|
||||
def test_client_converts_admin_permissions_to_db_permissions_on_add_to_service(notify_admin, mocker):
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post', return_value={'data': {}})
|
||||
def test_client_converts_admin_permissions_to_db_permissions_on_add_to_service(
|
||||
notify_admin, mocker
|
||||
):
|
||||
mock_post = mocker.patch(
|
||||
"app.notify_client.user_api_client.UserApiClient.post",
|
||||
return_value={"data": {}},
|
||||
)
|
||||
|
||||
user_api_client.add_user_to_service('service_id',
|
||||
'user_id',
|
||||
permissions={'send_messages', 'view_activity'},
|
||||
folder_permissions=[])
|
||||
user_api_client.add_user_to_service(
|
||||
"service_id",
|
||||
"user_id",
|
||||
permissions={"send_messages", "view_activity"},
|
||||
folder_permissions=[],
|
||||
)
|
||||
|
||||
assert sorted(mock_post.call_args[1]['data']['permissions'], key=lambda x: x['permission']) == sorted([
|
||||
{'permission': 'send_texts'},
|
||||
{'permission': 'send_emails'},
|
||||
{'permission': 'view_activity'},
|
||||
], key=lambda x: x['permission'])
|
||||
assert sorted(
|
||||
mock_post.call_args[1]["data"]["permissions"], key=lambda x: x["permission"]
|
||||
) == sorted(
|
||||
[
|
||||
{"permission": "send_texts"},
|
||||
{"permission": "send_emails"},
|
||||
{"permission": "view_activity"},
|
||||
],
|
||||
key=lambda x: x["permission"],
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
(
|
||||
'expected_cache_get_calls,'
|
||||
'cache_value,'
|
||||
'expected_api_calls,'
|
||||
'expected_cache_set_calls,'
|
||||
'expected_return_value,'
|
||||
"expected_cache_get_calls,"
|
||||
"cache_value,"
|
||||
"expected_api_calls,"
|
||||
"expected_cache_set_calls,"
|
||||
"expected_return_value,"
|
||||
),
|
||||
[
|
||||
(
|
||||
[
|
||||
call('user-{}'.format(user_id))
|
||||
],
|
||||
[call("user-{}".format(user_id))],
|
||||
b'{"data": "from cache"}',
|
||||
[],
|
||||
[],
|
||||
'from cache',
|
||||
"from cache",
|
||||
),
|
||||
(
|
||||
[
|
||||
call('user-{}'.format(user_id))
|
||||
],
|
||||
[call("user-{}".format(user_id))],
|
||||
None,
|
||||
[
|
||||
call('/user/{}'.format(user_id))
|
||||
],
|
||||
[
|
||||
call(
|
||||
'user-{}'.format(user_id),
|
||||
'{"data": "from api"}',
|
||||
ex=604800
|
||||
)
|
||||
],
|
||||
'from api',
|
||||
[call("/user/{}".format(user_id))],
|
||||
[call("user-{}".format(user_id), '{"data": "from api"}', ex=604800)],
|
||||
"from api",
|
||||
),
|
||||
]
|
||||
],
|
||||
)
|
||||
def test_returns_value_from_cache(
|
||||
notify_admin,
|
||||
@@ -160,17 +173,16 @@ def test_returns_value_from_cache(
|
||||
expected_api_calls,
|
||||
expected_cache_set_calls,
|
||||
):
|
||||
|
||||
mock_redis_get = mocker.patch(
|
||||
'app.extensions.RedisClient.get',
|
||||
"app.extensions.RedisClient.get",
|
||||
return_value=cache_value,
|
||||
)
|
||||
mock_api_get = mocker.patch(
|
||||
'app.notify_client.NotifyAdminAPIClient.get',
|
||||
return_value={'data': 'from api'},
|
||||
"app.notify_client.NotifyAdminAPIClient.get",
|
||||
return_value={"data": "from api"},
|
||||
)
|
||||
mock_redis_set = mocker.patch(
|
||||
'app.extensions.RedisClient.set',
|
||||
"app.extensions.RedisClient.set",
|
||||
)
|
||||
|
||||
user_api_client.get_user(user_id)
|
||||
@@ -180,23 +192,36 @@ def test_returns_value_from_cache(
|
||||
assert mock_redis_set.call_args_list == expected_cache_set_calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize('client, method, extra_args, extra_kwargs', [
|
||||
(user_api_client, 'add_user_to_service', [SERVICE_ONE_ID, sample_uuid(), [], []], {}),
|
||||
(user_api_client, 'update_user_attribute', [user_id], {}),
|
||||
(user_api_client, 'reset_failed_login_count', [user_id], {}),
|
||||
(user_api_client, 'update_user_attribute', [user_id], {}),
|
||||
(user_api_client, 'update_password', [user_id, 'hunter2'], {}),
|
||||
(user_api_client, 'verify_password', [user_id, 'hunter2'], {}),
|
||||
(user_api_client, 'check_verify_code', [user_id, '', ''], {}),
|
||||
(user_api_client, 'add_user_to_service', [SERVICE_ONE_ID, user_id, [], []], {}),
|
||||
(user_api_client, 'add_user_to_organization', [sample_uuid(), user_id], {}),
|
||||
(user_api_client, 'set_user_permissions', [user_id, SERVICE_ONE_ID, []], {}),
|
||||
(user_api_client, 'activate_user', [user_id], {}),
|
||||
(user_api_client, 'archive_user', [user_id], {}),
|
||||
(service_api_client, 'remove_user_from_service', [SERVICE_ONE_ID, user_id], {}),
|
||||
(service_api_client, 'create_service', ['', '', 0, False, user_id, sample_uuid()], {}),
|
||||
(invite_api_client, 'accept_invite', [SERVICE_ONE_ID, user_id], {}),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"client, method, extra_args, extra_kwargs",
|
||||
[
|
||||
(
|
||||
user_api_client,
|
||||
"add_user_to_service",
|
||||
[SERVICE_ONE_ID, sample_uuid(), [], []],
|
||||
{},
|
||||
),
|
||||
(user_api_client, "update_user_attribute", [user_id], {}),
|
||||
(user_api_client, "reset_failed_login_count", [user_id], {}),
|
||||
(user_api_client, "update_user_attribute", [user_id], {}),
|
||||
(user_api_client, "update_password", [user_id, "hunter2"], {}),
|
||||
(user_api_client, "verify_password", [user_id, "hunter2"], {}),
|
||||
(user_api_client, "check_verify_code", [user_id, "", ""], {}),
|
||||
(user_api_client, "add_user_to_service", [SERVICE_ONE_ID, user_id, [], []], {}),
|
||||
(user_api_client, "add_user_to_organization", [sample_uuid(), user_id], {}),
|
||||
(user_api_client, "set_user_permissions", [user_id, SERVICE_ONE_ID, []], {}),
|
||||
(user_api_client, "activate_user", [user_id], {}),
|
||||
(user_api_client, "archive_user", [user_id], {}),
|
||||
(service_api_client, "remove_user_from_service", [SERVICE_ONE_ID, user_id], {}),
|
||||
(
|
||||
service_api_client,
|
||||
"create_service",
|
||||
["", "", 0, False, user_id, sample_uuid()],
|
||||
{},
|
||||
),
|
||||
(invite_api_client, "accept_invite", [SERVICE_ONE_ID, user_id], {}),
|
||||
],
|
||||
)
|
||||
def test_deletes_user_cache(
|
||||
notify_admin,
|
||||
mock_get_user,
|
||||
@@ -206,35 +231,37 @@ def test_deletes_user_cache(
|
||||
extra_args,
|
||||
extra_kwargs,
|
||||
):
|
||||
mocker.patch('app.notify_client.current_user', id='1')
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_request = mocker.patch('notifications_python_client.base.BaseAPIClient.request')
|
||||
mocker.patch("app.notify_client.current_user", id="1")
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
mock_request = mocker.patch(
|
||||
"notifications_python_client.base.BaseAPIClient.request"
|
||||
)
|
||||
|
||||
getattr(client, method)(*extra_args, **extra_kwargs)
|
||||
|
||||
assert call('user-{}'.format(user_id)) in mock_redis_delete.call_args_list
|
||||
assert call("user-{}".format(user_id)) in mock_redis_delete.call_args_list
|
||||
assert len(mock_request.call_args_list) == 1
|
||||
|
||||
|
||||
def test_add_user_to_service_calls_correct_endpoint_and_deletes_keys_from_cache(mocker):
|
||||
mock_redis_delete = mocker.patch('app.extensions.RedisClient.delete')
|
||||
mock_redis_delete = mocker.patch("app.extensions.RedisClient.delete")
|
||||
|
||||
service_id = uuid.uuid4()
|
||||
user_id = uuid.uuid4()
|
||||
folder_id = uuid.uuid4()
|
||||
|
||||
expected_url = '/service/{}/users/{}'.format(service_id, user_id)
|
||||
data = {'permissions': [], 'folder_permissions': [folder_id]}
|
||||
expected_url = "/service/{}/users/{}".format(service_id, user_id)
|
||||
data = {"permissions": [], "folder_permissions": [folder_id]}
|
||||
|
||||
mock_post = mocker.patch('app.notify_client.user_api_client.UserApiClient.post')
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.add_user_to_service(service_id, user_id, [], [folder_id])
|
||||
|
||||
mock_post.assert_called_once_with(expected_url, data=data)
|
||||
assert mock_redis_delete.call_args_list == [
|
||||
call('user-{user_id}'.format(user_id=user_id)),
|
||||
call('service-{service_id}-template-folders'.format(service_id=service_id)),
|
||||
call('service-{service_id}'.format(service_id=service_id)),
|
||||
call("user-{user_id}".format(user_id=user_id)),
|
||||
call("service-{service_id}-template-folders".format(service_id=service_id)),
|
||||
call("service-{service_id}".format(service_id=service_id)),
|
||||
]
|
||||
|
||||
|
||||
@@ -242,17 +269,15 @@ def test_reset_password(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
):
|
||||
mock_post = mocker.patch(
|
||||
'app.notify_client.user_api_client.UserApiClient.post'
|
||||
)
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.send_reset_password_url('test@example.com')
|
||||
user_api_client.send_reset_password_url("test@example.com")
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
'/user/reset-password',
|
||||
"/user/reset-password",
|
||||
data={
|
||||
'email': 'test@example.com',
|
||||
'admin_base_url': 'http://localhost:6012',
|
||||
"email": "test@example.com",
|
||||
"admin_base_url": "http://localhost:6012",
|
||||
},
|
||||
)
|
||||
|
||||
@@ -261,16 +286,14 @@ def test_send_registration_email(
|
||||
mocker,
|
||||
fake_uuid,
|
||||
):
|
||||
mock_post = mocker.patch(
|
||||
'app.notify_client.user_api_client.UserApiClient.post'
|
||||
)
|
||||
mock_post = mocker.patch("app.notify_client.user_api_client.UserApiClient.post")
|
||||
|
||||
user_api_client.send_verify_email(fake_uuid, 'test@example.com')
|
||||
user_api_client.send_verify_email(fake_uuid, "test@example.com")
|
||||
|
||||
mock_post.assert_called_once_with(
|
||||
f'/user/{fake_uuid}/email-verification',
|
||||
f"/user/{fake_uuid}/email-verification",
|
||||
data={
|
||||
'to': 'test@example.com',
|
||||
'admin_base_url': 'http://localhost:6012',
|
||||
"to": "test@example.com",
|
||||
"admin_base_url": "http://localhost:6012",
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user