def post_email_notification():
    form = validate(request.get_json(), post_email_request)
    service = services_dao.dao_fetch_service_by_id(api_user.service_id)

    check_service_message_limit(api_user.key_type, service)
    service_can_send_to_recipient(form["email_address"], api_user.key_type, service)

    template, template_with_content = __validate_template(form, service, EMAIL_TYPE)
    notification = persist_notification(
        template_id=template.id,
        template_version=template.version,
        recipient=form["email_address"],
        service_id=service.id,
        personalisation=form.get("personalisation", None),
        notification_type=EMAIL_TYPE,
        api_key_id=api_user.id,
        key_type=api_user.key_type,
        reference=form.get("reference"),
    )

    send_notification_to_queue(notification, service.research_mode)

    resp = create_post_email_response_from_notification(
        notification=notification,
        content=str(template_with_content),
        subject=template_with_content.subject,
        email_from=service.email_from,
        url_root=request.url_root,
    )
    return jsonify(resp), 201
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(
        sample_service):
    with pytest.raises(BadRequestError) as e:
        service_can_send_to_recipient("0758964221", 'team', sample_service)
    assert e.value.status_code == 400
    assert e.value.message == 'Can’t send to this recipient using a team-only API key'
    assert e.value.fields == []
Example #3
0
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(key_type, sample_service):
    assert service_can_send_to_recipient("*****@*****.**",
                                         key_type,
                                         sample_service) is None
    assert service_can_send_to_recipient('07513332413',
                                         key_type,
                                         sample_service) is None
def post_sms_notification():
    form = validate(request.get_json(), post_sms_request)
    service = services_dao.dao_fetch_service_by_id(api_user.service_id)

    check_service_message_limit(api_user.key_type, service)
    service_can_send_to_recipient(form["phone_number"], api_user.key_type, service)

    template, template_with_content = __validate_template(form, service, SMS_TYPE)

    notification = persist_notification(
        template_id=template.id,
        template_version=template.version,
        recipient=form["phone_number"],
        service_id=service.id,
        personalisation=form.get("personalisation", None),
        notification_type=SMS_TYPE,
        api_key_id=api_user.id,
        key_type=api_user.key_type,
        reference=form.get("reference"),
    )
    send_notification_to_queue(notification, service.research_mode)
    sms_sender = service.sms_sender if service.sms_sender else current_app.config.get("FROM_NUMBER")
    resp = create_post_sms_response_from_notification(
        notification, str(template_with_content), sms_sender, request.url_root
    )
    return jsonify(resp), 201
Example #5
0
def test_service_can_send_to_recipient_passes(key_type, notify_db, notify_db_session):
    trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
    assert service_can_send_to_recipient(trial_mode_service.users[0].email_address,
                                         key_type,
                                         trial_mode_service) is None
    assert service_can_send_to_recipient(trial_mode_service.users[0].mobile_number,
                                         key_type,
                                         trial_mode_service) is None
def test_service_can_send_to_recipient_passes(key_type, notify_db, notify_db_session):
    trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
    assert service_can_send_to_recipient(trial_mode_service.users[0].email_address,
                                         key_type,
                                         trial_mode_service) is None
    assert service_can_send_to_recipient(trial_mode_service.users[0].mobile_number,
                                         key_type,
                                         trial_mode_service) is None
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(key_type, notify_db, notify_db_session):
    live_service = create_service(notify_db, notify_db_session, service_name='live', restricted=False)
    assert service_can_send_to_recipient("*****@*****.**",
                                         key_type,
                                         live_service) is None
    assert service_can_send_to_recipient('07513332413',
                                         key_type,
                                         live_service) is None
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(key_type, sample_service):
    serialised_service = SerialisedService.from_id(sample_service.id)
    assert service_can_send_to_recipient("*****@*****.**",
                                         key_type,
                                         serialised_service) is None
    assert service_can_send_to_recipient('07513332413',
                                         key_type,
                                         serialised_service) is None
Example #9
0
def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(key_type, notify_db, notify_db_session):
    live_service = create_service(notify_db, notify_db_session, service_name='live', restricted=False)
    assert service_can_send_to_recipient("*****@*****.**",
                                         key_type,
                                         live_service) is None
    assert service_can_send_to_recipient('07513332413',
                                         key_type,
                                         live_service) is None
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
    live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
    with pytest.raises(BadRequestError) as e:
        service_can_send_to_recipient("0758964221",
                                      'team',
                                      live_service)
    assert e.value.status_code == 400
    assert e.value.message == 'Can’t send to this recipient using a team-only API key'
    assert e.value.fields == []
Example #11
0
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
    live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
    with pytest.raises(BadRequestError) as e:
        service_can_send_to_recipient("0758964221",
                                      'team',
                                      live_service)
    assert e.value.status_code == 400
    assert e.value.message == 'Can’t send to this recipient using a team-only API key'
    assert e.value.fields == []
Example #12
0
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(sample_service):
    create_service_whitelist(sample_service, email_address="*****@*****.**")
    assert service_can_send_to_recipient("*****@*****.**",
                                         'team',
                                         sample_service) is None
    create_service_whitelist(sample_service, mobile_number='6502532222')
    assert service_can_send_to_recipient('6502532222',
                                         'team',
                                         sample_service) is None
Example #13
0
def test_service_can_send_to_recipient_passes_for_guest_list_recipient_passes(sample_service):
    create_service_guest_list(sample_service, email_address="*****@*****.**")
    assert service_can_send_to_recipient("*****@*****.**",
                                         'team',
                                         sample_service) is None
    create_service_guest_list(sample_service, mobile_number='07513332413')
    assert service_can_send_to_recipient('07513332413',
                                         'team',
                                         sample_service) is None
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recipient, key_type, error_message,
                                                                           notify_db, notify_db_session):
    trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
    with pytest.raises(BadRequestError) as exec_info:
        service_can_send_to_recipient(recipient,
                                      key_type,
                                      trial_mode_service)
    assert exec_info.value.status_code == 400
    assert exec_info.value.message == error_message
    assert exec_info.value.fields == []
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(notify_db, notify_db_session,
                                                                               sample_service):
    sample_service_whitelist(notify_db, notify_db_session, email_address="*****@*****.**")
    assert service_can_send_to_recipient("*****@*****.**",
                                         'team',
                                         sample_service) is None
    sample_service_whitelist(notify_db, notify_db_session, mobile_number='07513332413')
    assert service_can_send_to_recipient('07513332413',
                                         'team',
                                         sample_service) is None
Example #16
0
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recipient, key_type, error_message,
                                                                           notify_db, notify_db_session):
    trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
    with pytest.raises(BadRequestError) as exec_info:
        service_can_send_to_recipient(recipient,
                                      key_type,
                                      trial_mode_service)
    assert exec_info.value.status_code == 400
    assert exec_info.value.message == error_message
    assert exec_info.value.fields == []
Example #17
0
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(notify_db, notify_db_session,
                                                                               sample_service):
    sample_service_whitelist(notify_db, notify_db_session, email_address="*****@*****.**")
    assert service_can_send_to_recipient("*****@*****.**",
                                         'team',
                                         sample_service) is None
    sample_service_whitelist(notify_db, notify_db_session, mobile_number='6502532222')
    assert service_can_send_to_recipient('6502532222',
                                         'team',
                                         sample_service) is None
Example #18
0
def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(
        recipient: str, key_type: str, error_message: str,
        notify_db: RoutingSQLAlchemy, notify_db_session: RoutingSQLAlchemy):
    trial_mode_service = create_service(notify_db,
                                        notify_db_session,
                                        service_name='trial mode',
                                        restricted=True)
    with pytest.raises(BadRequestError) as exec_info:
        service_can_send_to_recipient(recipient, key_type, trial_mode_service)
    assert exec_info.value.status_code == 400
    assert error_message in exec_info.value.message, f'Unexpected error message: {exec_info.value.message}'
    assert exec_info.value.fields == []
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(
        notify_db, notify_db_session):
    live_service = create_service(notify_db,
                                  notify_db_session,
                                  service_name="live mode",
                                  restricted=False)
    with pytest.raises(BadRequestError) as e:
        service_can_send_to_recipient("0758964221", "team", live_service)
    assert e.value.status_code == 400
    assert (e.value.message ==
            "Can’t send to this recipient using a team-only API key "
            f'- see {get_document_url("en", "keys.html#team-and-safelist")}')
    assert e.value.fields == []
Example #20
0
def test_service_can_send_to_recipient_fails_when_ignoring_whitelist(
    notify_db,
    notify_db_session,
    sample_service,
    recipient,
):
    sample_service_whitelist(notify_db, notify_db_session, **recipient)
    with pytest.raises(BadRequestError) as exec_info:
        service_can_send_to_recipient(
            next(iter(recipient.values())),
            'team',
            sample_service,
            allow_whitelisted_recipients=False,
        )
    assert exec_info.value.status_code == 400
    assert exec_info.value.message == 'Can’t send to this recipient using a team-only API key'
    assert exec_info.value.fields == []
def test_service_can_send_to_recipient_fails_when_ignoring_safelist(
    notify_db,
    notify_db_session,
    sample_service,
    recipient,
):
    sample_service_safelist(notify_db, notify_db_session, **recipient)
    with pytest.raises(BadRequestError) as exec_info:
        service_can_send_to_recipient(
            next(iter(recipient.values())),
            "team",
            sample_service,
            allow_safelisted_recipients=False,
        )
    assert exec_info.value.status_code == 400
    assert (exec_info.value.message ==
            "Can’t send to this recipient using a team-only API key "
            f'- see {get_document_url("en", "keys.html#team-and-safelist")}')
    assert exec_info.value.fields == []