def test_should_show_cancel_link_for_letter_job( client_request, mocker, mock_get_service_letter_template, mock_get_service_data_retention, active_user_with_permissions, job_status, ): job_id = uuid.uuid4() job = job_json(SERVICE_ONE_ID, active_user_with_permissions, job_id=job_id, created_at="2019-06-20T15:30:00.000001+00:00", job_status=job_status) notifications_json = notification_json(SERVICE_ONE_ID, job=job, status="created", template_type="letter") mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}]) mocker.patch('app.notification_api_client.get_notifications_for_service', side_effect=[notifications_json]) page = client_request.get('main.view_job', service_id=SERVICE_ONE_ID, job_id=str(job_id)) assert page.find( 'a', text='Cancel sending these letters').attrs["href"] == url_for( "main.cancel_letter_job", service_id=SERVICE_ONE_ID, job_id=job_id) assert page.find('p', { 'id': 'printing-info' }).text.strip() == "Printing starts today at 5:30pm"
def test_get_api_notifications_changes_letter_statuses(mocker, letter_status, expected_status): service_id = str(uuid.uuid4()) sms_notification = single_notification_json(service_id, notification_type='sms', status='created') email_notification = single_notification_json(service_id, notification_type='email', status='created') letter_notification = single_notification_json(service_id, notification_type='letter', status=letter_status) notis = notification_json(service_id=service_id, rows=0) notis['notifications'] = [ sms_notification, email_notification, letter_notification ] mocker.patch( 'app.notify_client.notification_api_client.NotificationApiClient.get', return_value=notis) ret = NotificationApiClient().get_api_notifications_for_service(service_id) assert ret['notifications'][0]['notification_type'] == 'sms' assert ret['notifications'][1]['notification_type'] == 'email' assert ret['notifications'][2]['notification_type'] == 'letter' assert ret['notifications'][0]['status'] == 'created' assert ret['notifications'][1]['status'] == 'created' assert ret['notifications'][2]['status'] == expected_status
def test_should_cancel_letter_job(client_request, mocker, active_user_with_permissions): job_id = uuid.uuid4() job = job_json(SERVICE_ONE_ID, active_user_with_permissions, job_id=job_id, created_at="2019-06-20T15:30:00.000001+00:00", job_status="finished") mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}]) notifications_json = notification_json(SERVICE_ONE_ID, job=job, status="created", template_type="letter") mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}]) mocker.patch('app.notification_api_client.get_notifications_for_service', side_effect=[notifications_json]) mock_cancel = mocker.patch( 'app.main.jobs.job_api_client.cancel_letter_job', return_value=5) client_request.post('main.cancel_letter_job', service_id=SERVICE_ONE_ID, job_id=job_id, _expected_status=302, _expected_redirect=url_for( 'main.service_dashboard', service_id=SERVICE_ONE_ID, _external=True, )) mock_cancel.assert_called_once_with(SERVICE_ONE_ID, job_id)
def test_should_not_show_cancel_link_for_letter_job_if_too_late( client_request, mocker, mock_get_service_letter_template, mock_get_service_data_retention, active_user_with_permissions, job_created_at, expected_fragment, ): job_id = uuid.uuid4() job = job_json(SERVICE_ONE_ID, active_user_with_permissions, job_id=job_id, created_at=job_created_at) notifications_json = notification_json(SERVICE_ONE_ID, job=job, status="created", template_type="letter") mocker.patch('app.job_api_client.get_job', side_effect=[{"data": job}]) mocker.patch('app.notification_api_client.get_notifications_for_service', side_effect=[notifications_json]) page = client_request.get('main.view_job', service_id=SERVICE_ONE_ID, job_id=str(job_id)) assert "Cancel sending these letters" not in page assert page.find('p', { 'id': 'printing-info' }).text.strip() == "Printed {} at 5:30pm".format(expected_fragment)
def test_get_api_notifications_changes_letter_statuses(mocker, letter_status, expected_status): service_id = str(uuid.uuid4()) sms_notification = single_notification_json(service_id, notification_type="sms", status="created", api_key="api key id") email_notification = single_notification_json(service_id, notification_type="email", status="created", api_key="api key id") letter_notification = single_notification_json(service_id, notification_type="letter", status=letter_status, api_key="api key id") notis = notification_json(service_id=service_id, rows=0) notis["notifications"] = [ sms_notification, email_notification, letter_notification ] mocker.patch( "app.notify_client.notification_api_client.NotificationApiClient.get", return_value=notis, ) ret = NotificationApiClient().get_api_notifications_for_service(service_id) assert ret["notifications"][0]["notification_type"] == "sms" assert ret["notifications"][1]["notification_type"] == "email" assert ret["notifications"][2]["notification_type"] == "letter" assert ret["notifications"][0]["status"] == "created" assert ret["notifications"][1]["status"] == "created" assert ret["notifications"][2]["status"] == expected_status
def test_get_api_notifications_includes_jobs(mocker): service_id = str(uuid.uuid4()) notis = notification_json(service_id=service_id, rows=0) notis["notifications"] = [] mock_get = mocker.patch( "app.notify_client.notification_api_client.NotificationApiClient.get", return_value=notis, ) NotificationApiClient().get_api_notifications_for_service(service_id) call_args_params = mock_get.call_args[1]["params"] assert call_args_params.get("include_jobs") is True
def test_dont_cancel_letter_job_when_to_early_to_cancel( client_request, mocker, mock_get_service_letter_template, mock_get_service_data_retention, active_user_with_permissions, job_status, number_of_processed_notifications, ): job_id = uuid.uuid4() job = job_json(SERVICE_ONE_ID, active_user_with_permissions, job_id=job_id, created_at="2019-06-20T15:30:00.000001+00:00", job_status=job_status, notification_count=2) mocker.patch('app.job_api_client.get_job', side_effect=[{ "data": job }, { "data": job }]) notifications_json = notification_json( SERVICE_ONE_ID, job=job, status="created", template_type="letter", rows=number_of_processed_notifications) mocker.patch('app.notification_api_client.get_notifications_for_service', return_value=notifications_json) mocker.patch( 'app.notification_api_client.get_notification_count_for_job_id', return_value=number_of_processed_notifications) mock_cancel = mocker.patch('app.job_api_client.cancel_letter_job') page = client_request.post( 'main.cancel_letter_job', service_id=SERVICE_ONE_ID, job_id=str(job_id), _expected_status=200, ) mock_cancel.assert_not_called() flash_message = normalize_spaces( page.find('div', class_='banner-dangerous').text) assert 'We are still processing these letters, please try again in a minute.' in flash_message
def test_get_api_notifications_ignores_non_api_notifications(mocker): service_id = str(uuid.uuid4()) notis = notification_json(service_id=service_id, rows=0) admin_notification = single_notification_json(service_id, notification_type="email", status="created", api_key=None) api_notification = single_notification_json(service_id, notification_type="email", status="created", api_key="api key id") notis["notifications"] = [admin_notification, api_notification] mocker.patch( "app.notify_client.notification_api_client.NotificationApiClient.get", return_value=notis, ) ret = NotificationApiClient().get_api_notifications_for_service(service_id) assert len(ret["notifications"]) == 1 assert ret["notifications"][0]["api_key"] == "api key id"
def test_can_show_notifications( app_, service_one, active_user_with_permissions, mock_get_notifications, mock_get_detailed_service, mocker, message_type, page_title, status_argument, expected_api_call, page_argument, expected_page_argument): with app_.test_request_context(): with app_.test_client() as client: client.login(active_user_with_permissions, mocker, service_one) response = client.get( url_for('main.view_notifications', service_id=service_one['id'], message_type=message_type, status=status_argument, page=page_argument)) assert response.status_code == 200 content = response.get_data(as_text=True) notifications = notification_json(service_one['id']) notification = notifications['notifications'][0] assert notification['to'] in content assert notification['status'] in content assert notification['template']['name'] in content assert 'csv' in content page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') assert page_title in page.h1.text.strip() assert url_for('.view_notifications_csv', service_id=service_one['id'], message_type=message_type, status=status_argument) == page.findAll( "a", {"download": "download"})[0]['href'] path_to_json = page.find( "div", {'data-key': 'notifications'})['data-resource'] url = urlparse(path_to_json) assert url.path == '/services/{}/notifications/{}.json'.format( service_one['id'], message_type) query_dict = parse_qs(url.query) if status_argument: assert query_dict['status'] == [status_argument] if expected_page_argument: assert query_dict['page'] == [str(expected_page_argument)] mock_get_notifications.assert_called_with(limit_days=7, page=expected_page_argument, service_id=service_one['id'], status=expected_api_call, template_type=[message_type]) csv_response = client.get( url_for('main.view_notifications_csv', service_id=service_one['id'], message_type='email', download='csv')) csv_content = generate_notifications_csv( mock_get_notifications(service_one['id'])['notifications']) assert csv_response.status_code == 200 assert csv_response.get_data(as_text=True) == csv_content assert 'text/csv' in csv_response.headers['Content-Type'] json_response = client.get( url_for('main.get_notifications_as_json', service_id=service_one['id'], message_type=message_type, status=status_argument)) json_content = json.loads(json_response.get_data(as_text=True)) assert json_content.keys() == {'counts', 'notifications'}