def test_post_sms_notification_returns_400_if_not_allowed_to_send_int_sms( client, notify_db, notify_db_session, ): service = sample_service(notify_db, notify_db_session, permissions=[SMS_TYPE]) template = create_sample_template(notify_db, notify_db_session, service=service) data = {'phone_number': '20-12-1234-1234', 'template_id': template.id} auth_header = create_authorization_header(service_id=service.id) response = client.post(path='/v2/notifications/sms', data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 400 assert response.headers['Content-type'] == 'application/json' error_json = json.loads(response.get_data(as_text=True)) assert error_json['status_code'] == 400 assert error_json['errors'] == [{ "error": "BadRequestError", "message": 'Cannot send to international mobile numbers' }]
def test_should_by_able_to_get_template_count_from_notifications_history_for_service( notify_db, notify_db_session): service_1 = sample_service(notify_db, notify_db_session, service_name="test1", email_from="test1") service_2 = sample_service(notify_db, notify_db_session, service_name="test2", email_from="test2") service_3 = sample_service(notify_db, notify_db_session, service_name="test3", email_from="test3") sms = sample_template(notify_db, notify_db_session) sample_notification(notify_db, notify_db_session, service=service_1, template=sms) sample_notification(notify_db, notify_db_session, service=service_1, template=sms) sample_notification(notify_db, notify_db_session, service=service_2, template=sms) assert dao_get_template_usage(service_1.id)[0].count == 2 assert dao_get_template_usage(service_2.id)[0].count == 1 assert len(dao_get_template_usage(service_3.id)) == 0
def test_should_process_email_job_into_research_mode_queue_if_research_mode_service( notify_db, notify_db_session, mocker ): mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('sms')) mocker.patch('app.celery.tasks.send_email.apply_async') mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.celery.tasks.create_uuid', return_value="uuid") service = sample_service(notify_db, notify_db_session) service.research_mode = True services_dao.dao_update_service(service) template = sample_email_template(notify_db, notify_db_session, service=service) job = sample_job(notify_db, notify_db_session, template=template, service=service) process_job(job.id) s3.get_job_from_s3.assert_called_once_with( str(job.service.id), str(job.id) ) tasks.send_email.apply_async.assert_called_once_with( (str(job.service_id), "uuid", "something_encrypted", "2016-01-01T11:09:00.061258Z"), queue="research-mode" )
def test_post_notification_without_document_upload_permission( client, notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, permissions=[EMAIL_TYPE]) template = create_sample_template(notify_db, notify_db_session, service=service, template_type='email', content="Document: ((document))") mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') document_download_mock = mocker.patch( 'app.v2.notifications.post_notifications.document_download_client') document_download_mock.upload_document.return_value = 'https://document-url/' data = { "email_address": service.users[0].email_address, "template_id": template.id, "personalisation": { "document": { "file": "abababab" } } } auth_header = create_authorization_header(service_id=service.id) response = client.post(path="v2/notifications/email", data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 400, response.get_data(as_text=True)
def test_should_process_email_job_if_exactly_on_send_limits(notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, limit=10) template = sample_email_template(notify_db, notify_db_session, service=service) job = sample_job(notify_db, notify_db_session, service=service, template=template, notification_count=10) mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('multiple_email')) mocker.patch('app.celery.tasks.send_email.apply_async') mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.celery.tasks.create_uuid', return_value="uuid") process_job(job.id) s3.get_job_from_s3.assert_called_once_with( str(job.service.id), str(job.id) ) job = jobs_dao.dao_get_job_by_id(job.id) assert job.job_status == 'finished' tasks.send_email.apply_async.assert_called_with( ( str(job.service_id), "uuid", "something_encrypted", "2016-01-01T11:09:00.061258Z" ), queue="db-email" )
def test_receive_notification_without_permissions_does_not_create_inbound_even_with_inbound_number_set( client, mocker, notify_db, notify_db_session): service = sample_service(notify_db, notify_db_session, permissions=[SMS_TYPE]) inbound_number = create_inbound_number('1', service_id=service.id, active=True) mocked_send_inbound_sms = mocker.patch( "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async" ) mocked_has_permissions = mocker.patch( "app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False) data = { "ID": "1234", "MSISDN": "447800900855", "Message": "Some message to notify", "Trigger": "Trigger?", "Number": inbound_number.number, "Channel": "SMS", "DateRecieved": "2012-06-27 12:33:00" } response = mmg_post(client, data) assert response.status_code == 200 assert len(InboundSms.query.all()) == 0 assert mocked_has_permissions.called mocked_send_inbound_sms.assert_not_called()
def test_get_total_notifications_counts_ignores_research_mode(notify_db, notify_db_session): created_at = datetime.utcnow() service = sample_service(notify_db, notify_db_session, research_mode=True) template = sample_template(notify_db, notify_db_session, service=service) create_notification(template, status='created', sent_at=None) sample_notification_history( notify_db, notify_db_session, template, notification_type='email', sent_at=created_at + timedelta(seconds=5) ) sample_notification_history( notify_db, notify_db_session, template, notification_type='sms', sent_at=created_at + timedelta(seconds=5) ) result = dao_get_total_notifications_sent_per_day_for_performance_platform(BEGINNING_OF_DAY, END_OF_DAY) assert result.messages_total == 2 assert result.messages_within_10_secs == 2
def test_should_put_send_email_task_in_research_mode_queue_if_research_mode_service( notify_db, notify_db_session, mocker ): service = sample_service(notify_db, notify_db_session) service.research_mode = True services_dao.dao_update_service(service) template = sample_email_template(notify_db, notify_db_session, service=service) notification = _notification_json(template, to="*****@*****.**") mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') notification_id = uuid.uuid4() send_email( template.service_id, notification_id, encryption.encrypt(notification), datetime.utcnow().strftime(DATETIME_FORMAT) ) persisted_notification = Notification.query.one() provider_tasks.deliver_email.apply_async.assert_called_once_with( [str(persisted_notification.id)], queue="research-mode" )
def test_twilio_receive_notification_without_permissions_does_not_create_inbound_even_with_inbound_number_set( client, mocker, notify_db, notify_db_session): mocker.patch('twilio.request_validator.RequestValidator.validate', return_value=True) service = sample_service(notify_db, notify_db_session, permissions=[SMS_TYPE]) create_inbound_number('+61412345678', service_id=service.id, active=True) mocked_send_inbound_sms = mocker.patch( "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async") mocked_has_permissions = mocker.patch( "app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False) data = urllib.parse.urlencode( { 'MessageSid': '1', 'From': '+61412999999', 'To': '+61412345678', 'Body': 'this is a message' } ) response = twilio_post(client, data) assert response.status_code == 200 assert len(InboundSms.query.all()) == 0 assert mocked_has_permissions.called mocked_send_inbound_sms.assert_not_called()
def test_should_send_sms_if_restricted_service_and_valid_number(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900890") service = sample_service(notify_db, notify_db_session, user=user, restricted=True) template = sample_template(notify_db, notify_db_session, service=service) notification = _notification_json(template, "+447700900890") # The user’s own number, but in a different format mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') notification_id = uuid.uuid4() encrypt_notification = encryption.encrypt(notification) send_sms( service.id, notification_id, encrypt_notification, datetime.utcnow().strftime(DATETIME_FORMAT) ) persisted_notification = Notification.query.one() assert persisted_notification.to == '+447700900890' assert persisted_notification.template_id == template.id assert persisted_notification.template_version == template.version assert persisted_notification.status == 'created' assert persisted_notification.created_at <= datetime.utcnow() assert not persisted_notification.sent_at assert not persisted_notification.sent_by assert not persisted_notification.job_id assert not persisted_notification.personalisation assert persisted_notification.notification_type == 'sms' provider_tasks.deliver_sms.apply_async.assert_called_once_with( [str(persisted_notification.id)], queue="send-sms" )
def test_should_send_email_if_restricted_service_and_non_team_email_address_with_test_key(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session) service = sample_service(notify_db, notify_db_session, user=user, restricted=True) template = sample_template( notify_db, notify_db_session, service=service, template_type='email', subject_line='Hello' ) notification = _notification_json(template, to="*****@*****.**") mocked_deliver_email = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') notification_id = uuid.uuid4() send_email( service.id, notification_id, encryption.encrypt(notification), datetime.utcnow().strftime(DATETIME_FORMAT), key_type=KEY_TYPE_TEST ) persisted_notification = Notification.query.one() mocked_deliver_email.assert_called_once_with( [str(persisted_notification.id)], queue="send-email" )
def test_should_not_process_email_job_if_would_exceed_send_limits(notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, limit=0) template = sample_email_template(notify_db, notify_db_session, service=service) job = sample_job(notify_db, notify_db_session, service=service, template=template) mocker.patch('app.celery.tasks.s3.get_job_from_s3') mocker.patch('app.celery.tasks.send_email.apply_async') process_job(job.id) job = jobs_dao.dao_get_job_by_id(job.id) assert job.job_status == 'sending limits exceeded' assert s3.get_job_from_s3.called is False assert tasks.send_email.apply_async.called is False
def test_should_not_process_email_job_if_would_exceed_send_limits_inc_today(notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, limit=1) template = sample_email_template(notify_db, notify_db_session, service=service) job = sample_job(notify_db, notify_db_session, service=service, template=template) sample_notification(notify_db, notify_db_session, service=service, job=job) mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email')) mocker.patch('app.celery.tasks.send_email.apply_async') process_job(job.id) job = jobs_dao.dao_get_job_by_id(job.id) assert job.job_status == 'sending limits exceeded' assert s3.get_job_from_s3.called is False assert tasks.send_email.apply_async.called is False
def test_should_not_send_email_if_restricted_service_and_invalid_email_address(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session) service = sample_service(notify_db, notify_db_session, user=user, restricted=True) template = sample_template( notify_db, notify_db_session, service=service, template_type='email', subject_line='Hello' ) notification = _notification_json(template, to="*****@*****.**") notification_id = uuid.uuid4() send_email( service.id, notification_id, encryption.encrypt(notification), datetime.utcnow().strftime(DATETIME_FORMAT) ) assert Notification.query.count() == 0
def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205") service = sample_service(notify_db, notify_db_session, user=user, restricted=True) template = sample_template(notify_db, notify_db_session, service=service) notification = _notification_json(template, "07700 900849") mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') notification_id = uuid.uuid4() send_sms( service.id, notification_id, encryption.encrypt(notification), datetime.utcnow().strftime(DATETIME_FORMAT) ) assert provider_tasks.deliver_sms.apply_async.called is False assert Notification.query.count() == 0
def test_should_not_process_sms_job_if_would_exceed_send_limits(notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, limit=9) job = sample_job(notify_db, notify_db_session, service=service, notification_count=10) mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('multiple_sms')) mocker.patch('app.celery.tasks.send_sms.apply_async') mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.celery.tasks.create_uuid', return_value="uuid") process_job(job.id) job = jobs_dao.dao_get_job_by_id(job.id) assert job.job_status == 'sending limits exceeded' assert s3.get_job_from_s3.called is False assert tasks.send_sms.apply_async.called is False
def test_post_notification_with_document_upload(client, notify_db, notify_db_session, mocker): service = sample_service(notify_db, notify_db_session, permissions=[EMAIL_TYPE, UPLOAD_DOCUMENT]) template = create_sample_template(notify_db, notify_db_session, service=service, template_type='email', content="Document: ((document))") mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') document_download_mock = mocker.patch( 'app.v2.notifications.post_notifications.document_download_client') document_download_mock.upload_document.return_value = 'https://document-url/' data = { "email_address": service.users[0].email_address, "template_id": template.id, "personalisation": { "document": { "file": "abababab" } } } auth_header = create_authorization_header(service_id=service.id) response = client.post(path="v2/notifications/email", data=json.dumps(data), headers=[('Content-Type', 'application/json'), auth_header]) assert response.status_code == 201, response.get_data(as_text=True) resp_json = json.loads(response.get_data(as_text=True)) assert validate(resp_json, post_email_response) == resp_json notification = Notification.query.one() assert notification.status == NOTIFICATION_CREATED assert notification.personalisation == { 'document': 'https://document-url/' } assert resp_json['content']['body'] == 'Document: https://document-url/'
def test_should_send_sms_if_restricted_service_and_non_team_number_with_test_key(notify_db, notify_db_session, mocker): user = sample_user(notify_db, notify_db_session, mobile_numnber="07700 900205") service = sample_service(notify_db, notify_db_session, user=user, restricted=True) template = sample_template(notify_db, notify_db_session, service=service) notification = _notification_json(template, "07700 900849") mocked_deliver_sms = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async') notification_id = uuid.uuid4() send_sms( service.id, notification_id, encryption.encrypt(notification), datetime.utcnow().strftime(DATETIME_FORMAT), key_type=KEY_TYPE_TEST ) persisted_notification = Notification.query.one() mocked_deliver_sms.assert_called_once_with( [str(persisted_notification.id)], queue="send-sms" )