def test_add_reply_to_email_address_ensure_there_is_not_more_than_one_default(sample_service): create_reply_to_email(service=sample_service, email_address='*****@*****.**', is_default=True) create_reply_to_email(service=sample_service, email_address='*****@*****.**', is_default=True) with pytest.raises(Exception): add_reply_to_email_address_for_service(service_id=sample_service.id, email_address='*****@*****.**', is_default=False)
def test_archive_reply_to_email_address(sample_service): create_reply_to_email(service=sample_service, email_address="*****@*****.**") second_reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**", is_default=False) archive_reply_to_email_address(sample_service.id, second_reply_to.id) assert second_reply_to.archived is True assert second_reply_to.updated_at is not None
def test_dao_get_reply_to_by_id_raises_sqlalchemy_error_when_reply_to_is_archived(sample_service): create_reply_to_email(service=sample_service, email_address='*****@*****.**') archived_reply_to = create_reply_to_email( service=sample_service, email_address='*****@*****.**', is_default=False, archived=True) with pytest.raises(SQLAlchemyError): dao_get_reply_to_by_id(service_id=sample_service.id, reply_to_id=archived_reply_to.id)
def test_archive_reply_to_email_address_raises_an_error_if_attempting_to_archive_a_default( sample_service, ): create_reply_to_email(service=sample_service, email_address="*****@*****.**", is_default=False) default_reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**") with pytest.raises(ArchiveValidationError) as e: archive_reply_to_email_address(sample_service.id, default_reply_to.id) assert "You cannot delete a default email reply to address" in str(e.value) assert not default_reply_to.archived
def test_dao_get_reply_to_by_service_id(notify_db_session): service = create_service() default_reply_to = create_reply_to_email(service=service, email_address='*****@*****.**') second_reply_to = create_reply_to_email(service=service, email_address='*****@*****.**', is_default=False) another_reply_to = create_reply_to_email(service=service, email_address='*****@*****.**', is_default=False) results = dao_get_reply_to_by_service_id(service_id=service.id) assert len(results) == 3 assert default_reply_to == results[0] assert another_reply_to == results[1] assert second_reply_to == results[2]
def test_send_email_should_use_service_reply_to_email( sample_service, sample_email_template, mock_email_client, mocked_build_ga_pixel_url ): db_notification = create_notification(template=sample_email_template, reply_to_text='*****@*****.**') create_reply_to_email(service=sample_service, email_address='*****@*****.**') send_to_providers.send_email_to_provider(db_notification) _, kwargs = mock_email_client.send_email.call_args assert kwargs['reply_to_address'] == '*****@*****.**'
def test_send_email_should_use_service_reply_to_email(sample_service, sample_email_template, mocker): mocker.patch('app.aws_ses_client.send_email', return_value='reference') db_notification = create_notification(template=sample_email_template, reply_to_text='*****@*****.**') create_reply_to_email(service=sample_service, email_address='*****@*****.**') send_to_providers.send_email_to_provider(db_notification, ) app.aws_ses_client.send_email.assert_called_once_with( ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address='*****@*****.**')
def test_add_reply_to_email_address_new_reply_to_is_default_existing_reply_to_is_not(notify_db_session): service = create_service() create_reply_to_email(service=service, email_address="*****@*****.**", is_default=True) add_reply_to_email_address_for_service(service_id=service.id, email_address='*****@*****.**', is_default=True) results = dao_get_reply_to_by_service_id(service_id=service.id) assert len(results) == 2 for x in results: if x.email_address == '*****@*****.**': assert not x.is_default elif x.email_address == '*****@*****.**': assert x.is_default else: assert False
def test_add_reply_to_email_address_for_service_creates_another_email_for_service(notify_db_session): service = create_service() create_reply_to_email(service=service, email_address="*****@*****.**") add_reply_to_email_address_for_service(service_id=service.id, email_address='*****@*****.**', is_default=False) results = dao_get_reply_to_by_service_id(service_id=service.id) assert len(results) == 2 for x in results: if x.email_address == '*****@*****.**': assert x.is_default elif x.email_address == '*****@*****.**': assert not x.is_default else: raise AssertionError()
def test_dao_get_reply_to_by_service_id_does_not_return_archived_reply_tos(notify_db_session): service = create_service() create_reply_to_email(service=service, email_address='*****@*****.**') create_reply_to_email(service=service, email_address='*****@*****.**', is_default=False) archived_reply_to = create_reply_to_email( service=service, email_address='*****@*****.**', is_default=False, archived=True ) results = dao_get_reply_to_by_service_id(service_id=service.id) assert len(results) == 2 assert archived_reply_to not in results
def test_post_email_notification_with_valid_reply_to_id_returns_201( client, sample_email_template, mocker): reply_to_email = create_reply_to_email(sample_email_template.service, '*****@*****.**') mocked = mocker.patch( 'app.celery.provider_tasks.deliver_email.apply_async') data = { "email_address": sample_email_template.service.users[0].email_address, "template_id": sample_email_template.id, 'email_reply_to_id': reply_to_email.id } auth_header = create_authorization_header( service_id=sample_email_template.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 resp_json = json.loads(response.get_data(as_text=True)) assert validate(resp_json, post_email_response) == resp_json notification = Notification.query.first() assert notification.reply_to_text == '*****@*****.**' assert resp_json['id'] == str(notification.id) assert mocked.called assert notification.reply_to_text == reply_to_email.email_address
def test_dao_get_reply_to_by_id_raises_sqlalchemy_error_when_service_does_not_exist( sample_service): reply_to = create_reply_to_email(service=sample_service, email_address='*****@*****.**') with pytest.raises(SQLAlchemyError): dao_get_reply_to_by_id(service_id=uuid.uuid4(), reply_to_id=reply_to.id)
def test_update_reply_to_email_address_raises_exception_if_single_reply_to_and_setting_default_to_false(sample_service): first_reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**") with pytest.raises(expected_exception=InvalidRequest): update_reply_to_email_address(service_id=sample_service.id, reply_to_id=first_reply_to.id, email_address='*****@*****.**', is_default=False)
def test_check_service_email_reply_to_id_where_service_id_is_not_found(sample_service, fake_uuid): reply_to_address = create_reply_to_email(sample_service, "*****@*****.**") with pytest.raises(BadRequestError) as e: check_service_email_reply_to_id(fake_uuid, reply_to_address.id, EMAIL_TYPE) assert e.value.status_code == 400 assert e.value.message == 'email_reply_to_id {} does not exist in database for service id {}' \ .format(reply_to_address.id, fake_uuid)
def test_send_email_should_use_service_reply_to_email( sample_service, sample_email_template, mock_email_client, mocked_build_ga_pixel_url): db_notification = create_notification(template=sample_email_template, reply_to_text='*****@*****.**') create_reply_to_email(service=sample_service, email_address='*****@*****.**') send_to_providers.send_email_to_provider(db_notification, ) mock_email_client.send_email.assert_called_once_with( ANY, ANY, ANY, body=ANY, html_body=ANY, reply_to_address='*****@*****.**', attachments=[])
def test_archive_reply_to_email_address_does_not_archive_a_reply_to_for_a_different_service(sample_service): service = create_service(service_name="First service") reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**", is_default=False) with pytest.raises(SQLAlchemyError): archive_reply_to_email_address(service.id, reply_to.id) assert not reply_to.archived
def test_update_reply_to_email_address(sample_service): first_reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**") update_reply_to_email_address(service_id=sample_service.id, reply_to_id=first_reply_to.id, email_address='*****@*****.**', is_default=True) updated_reply_to = ServiceEmailReplyTo.query.get(first_reply_to.id) assert updated_reply_to.email_address == '*****@*****.**' assert updated_reply_to.updated_at assert updated_reply_to.is_default
def test_update_reply_to_email_address_set_updated_to_default(sample_service): create_reply_to_email(service=sample_service, email_address="*****@*****.**") second_reply_to = create_reply_to_email(service=sample_service, email_address="*****@*****.**", is_default=False) update_reply_to_email_address(service_id=sample_service.id, reply_to_id=second_reply_to.id, email_address='*****@*****.**', is_default=True) results = ServiceEmailReplyTo.query.all() assert len(results) == 2 for x in results: if x.email_address == '*****@*****.**': assert x.is_default elif x.email_address == '*****@*****.**': assert not x.is_default else: assert False
def test_post_notification_should_set_reply_to_text(client, sample_service, mocker, notification_type, recipient): mocker.patch('app.celery.provider_tasks.deliver_{}.apply_async'.format(notification_type)) template = create_template(sample_service, template_type=notification_type) expected_reply_to = current_app.config['FROM_NUMBER'] if notification_type == EMAIL_TYPE: expected_reply_to = '*****@*****.**' create_reply_to_email(service=sample_service, email_address=expected_reply_to, is_default=True) data = { 'to': recipient, 'template': str(template.id) } response = client.post("/notifications/{}".format(notification_type), data=json.dumps(data), headers=[('Content-Type', 'application/json'), create_authorization_header(service_id=sample_service.id)] ) assert response.status_code == 201 notifications = Notification.query.all() assert len(notifications) == 1 assert notifications[0].reply_to_text == expected_reply_to
def test_send_one_off_notification_should_add_email_reply_to_text_for_notification(sample_email_template, celery_mock): reply_to_email = create_reply_to_email(sample_email_template.service, '*****@*****.**') data = { 'to': '*****@*****.**', 'template_id': str(sample_email_template.id), 'sender_id': reply_to_email.id, 'created_by': str(sample_email_template.service.created_by_id) } notification_id = send_one_off_notification(service_id=sample_email_template.service.id, post_data=data) notification = Notification.query.get(notification_id['id']) celery_mock.assert_called_once_with( notification=notification, research_mode=False, queue=None ) assert notification.reply_to_text == reply_to_email.email_address
def test_send_one_off_notification_should_add_email_reply_to_text_for_notification( sample_email_template, celery_mock): reply_to_email = create_reply_to_email(sample_email_template.service, "*****@*****.**") data = { "to": "*****@*****.**", "template_id": str(sample_email_template.id), "sender_id": reply_to_email.id, "created_by": str(sample_email_template.service.created_by_id), } notification_id = send_one_off_notification( service_id=sample_email_template.service.id, post_data=data) notification = Notification.query.get(notification_id["id"]) celery_mock.assert_called_once_with(notification=notification, research_mode=False, queue=None) assert notification.reply_to_text == reply_to_email.email_address
def test_post_email_notification_with_archived_reply_to_id_returns_400(client, sample_email_template, mocker): archived_reply_to = create_reply_to_email( sample_email_template.service, '*****@*****.**', is_default=False, archived=True) mocker.patch('app.celery.provider_tasks.deliver_email.apply_async') data = { "email_address": '*****@*****.**', "template_id": sample_email_template.id, 'email_reply_to_id': archived_reply_to.id } auth_header = create_authorization_header(service_id=sample_email_template.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 resp_json = json.loads(response.get_data(as_text=True)) assert 'email_reply_to_id {} does not exist in database for service id {}'. \ format(archived_reply_to.id, sample_email_template.service_id) in resp_json['errors'][0]['message'] assert 'BadRequestError' in resp_json['errors'][0]['error']
def test_check_reply_to_email_type(sample_service): reply_to_address = create_reply_to_email(sample_service, "*****@*****.**") assert check_reply_to(sample_service.id, reply_to_address.id, EMAIL_TYPE) == '*****@*****.**'
def test_check_service_email_reply_to_where_email_reply_to_is_found(sample_service): reply_to_address = create_reply_to_email(sample_service, "*****@*****.**") assert check_service_email_reply_to_id(sample_service.id, reply_to_address.id, EMAIL_TYPE) == "*****@*****.**"
def test_service_get_default_reply_to_email_address(sample_service): create_reply_to_email(service=sample_service, email_address="*****@*****.**") assert sample_service.get_default_reply_to_email_address( ) == '*****@*****.**'
def test_dao_get_reply_to_by_id(sample_service): reply_to = create_reply_to_email(service=sample_service, email_address='*****@*****.**') result = dao_get_reply_to_by_id(service_id=sample_service.id, reply_to_id=reply_to.id) assert result == reply_to