Beispiel #1
0
def test_send_email_failure(mock_send, db):
    mock_send.return_value = MockResponse(False)

    email_address = 'asdf@!'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(email_address, message, NotificationChannel.EMAIL)

    notification = db.query(Notification).filter(
        Notification.recipient == email_address).first()

    assert notification.status == NotificationStatus.FAILED
    assert notification.sent_date is None

    db.delete(notification)
    db.commit()
Beispiel #2
0
def test_validate_bad_email(mock_send, db):
    bad_email_address = 'this is not an email'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(bad_email_address, message, NotificationChannel.EMAIL)

    notification = db.query(Notification).filter(
        Notification.recipient == bad_email_address).first()

    assert notification.recipient == bad_email_address
    assert not mock_send.called
    assert notification.status == NotificationStatus.FAILED
    assert notification.sent_date is None

    db.delete(notification)
    db.commit()
Beispiel #3
0
def test_send_slack_failure(mock_send, db):
    mock_send.side_effect = SlackApiError(None, None)

    slack_user_id = 'still not a real slack user ID'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(slack_user_id, message, NotificationChannel.SLACK)

    notification = db.query(Notification).filter(
        Notification.recipient == slack_user_id).first()

    assert notification.status == NotificationStatus.FAILED
    assert notification.sent_date is None

    db.delete(notification)
    db.commit()
Beispiel #4
0
def test_send_sms_failure(mock_send, db):
    mock_send.return_value = MockResponse(False)

    phone_number = fake.phone_number()
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(phone_number, message, NotificationChannel.SMS)

    notification = db.query(Notification).filter(
        Notification.recipient == phone_number).first()

    assert notification.status == NotificationStatus.FAILED
    assert notification.sent_date is None

    db.delete(notification)
    db.commit()
Beispiel #5
0
def test_validate_bad_phone_number(mock_send, db):
    bad_phone_number = '123456'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(bad_phone_number, message, NotificationChannel.SMS)

    notification = db.query(Notification).filter(
        Notification.recipient == bad_phone_number).first()

    assert notification.recipient == bad_phone_number
    assert not mock_send.called
    assert notification.status == NotificationStatus.FAILED
    assert notification.sent_date is None

    db.delete(notification)
    db.commit()
Beispiel #6
0
def test_send_email_success(mock_send, db):
    mock_send.return_value = MockResponse(True)

    email_address = '*****@*****.**'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(email_address, message, NotificationChannel.EMAIL)

    notification = db.query(Notification).filter(
        Notification.recipient == email_address).first()

    assert notification.channel == NotificationChannel.EMAIL
    assert notification.recipient == email_address
    assert notification.message == message
    assert notification.status == NotificationStatus.SENT
    assert notification.sent_date is not None

    db.delete(notification)
    db.commit()
Beispiel #7
0
def test_send_slack_success(mock_send, db):
    mock_send.return_value = MockResponse(True)

    slack_user_id = 'not a real slack user ID'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(slack_user_id, message, NotificationChannel.SLACK)

    notification = db.query(Notification).filter(
        Notification.recipient == slack_user_id).first()

    assert mock_send.called
    assert notification.channel == NotificationChannel.SLACK
    assert notification.recipient == slack_user_id
    assert notification.message == message
    assert notification.status == NotificationStatus.SENT
    assert notification.sent_date is not None

    db.delete(notification)
    db.commit()
Beispiel #8
0
def test_send_sms_success(mock_send, db):
    mock_send.return_value = MockResponse(True)

    phone_number = '+1 1-800-444-4444'
    message = fake.paragraph(nb_sentences=10)
    nm.send_notification(phone_number, message, NotificationChannel.SMS)

    notification = db.query(Notification).filter(
        Notification.recipient == phone_number).first()

    assert mock_send.called
    assert notification.channel == NotificationChannel.SMS
    assert notification.recipient == phone_number
    assert notification.message == message
    assert notification.status == NotificationStatus.SENT
    assert notification.sent_date is not None

    db.delete(notification)
    db.commit()
Beispiel #9
0
def test_notification_validation():
    with pytest.raises(error_wrappers.ValidationError):
        recipient = 123
        message = []
        nm.send_notification(recipient, message, 'email')
def create_and_send_email(notification_payload: AccountNotificationSchema,
                          existing_acct: Account, db: Session):
    email_message = None
    if existing_acct is not None:
        if notification_payload.notification_type == 'password_reset':
            email_message = f'<p>Dear {existing_acct.first_name},</p>'
            if existing_acct.oauth is None:
                base_url = Config['routes']['client']
                acct_settings = db.query(AccountSettings).filter_by(
                    uuid=existing_acct.uuid).first()
                curr_time = datetime.now()
                password_reset_hash = generate_str_for_hash(
                    existing_acct.username, curr_time)
                acct_settings.password_reset_hash = password_reset_hash
                acct_settings.password_reset_time = curr_time
                db.merge(acct_settings)
                db.commit()

                reset_url = f'{base_url}/reset_password?hash={password_reset_hash}'
                message_body = CreateParagraph(
                    f"""We have received a request to reset your password. To reset your password,
                        please click the following""")
                message_body += CreateButton("Reset Password", reset_url)
                message_body += CreateParagraph(
                    "This link will expire in 15 minutes")
            else:
                oauth_type = existing_acct.oauth.capitalize()
                message_body = CreateParagraph(
                    f"""We have received a request to reset your password.
                    Your account was created with {oauth_type} OAuth; therefore,
                    you cannot set or reset a password.
                    Please try signing in with {oauth_type}.""")
            message_body += CreateParagraph(
                '<b>If this action was not performed by you, please ignore this message.</b>'
            )

            email_message = BASE_EMAIL_TEMPLATE.format(body_text=message_body)
            nm.send_notification(recipient=existing_acct.email,
                                 message=email_message,
                                 channel=NotificationChannel.EMAIL,
                                 scheduled_send_date=datetime.now(),
                                 subject='HF Volunteer Portal Password Reset')
        elif notification_payload.notification_type == 'verify_registration':
            base_url = Config['routes']['client']
            acct_settings = db.query(AccountSettings).filter_by(
                uuid=existing_acct.uuid).first()
            curr_time = datetime.now()
            verify_account_hash = generate_str_for_hash(
                existing_acct.username, curr_time)
            acct_settings.verify_account_hash = verify_account_hash
            cancel_registration_hash = generate_str_for_hash(
                existing_acct.username, curr_time)
            acct_settings.cancel_registration_hash = cancel_registration_hash
            db.merge(acct_settings)
            db.commit()

            verify_url = f'{base_url}/verify_account?hash={verify_account_hash}'
            cancel_url = f'{base_url}/cancel_registration?hash={cancel_registration_hash}'

            message_body = CreateParagraph(f'Hi {existing_acct.first_name},')
            message_body += CreateParagraph(
                f"""We have received a request to create an account associated with this email.
                Please click below to verify your account""")
            message_body += CreateButton("Verify My Account", verify_url)

            message_body += CreateParagraph(
                f"""If this action was not performed by you or performed by accident,
                you may click the following to undo account creation""")
            message_body += CreateButton("Undo Account Registration",
                                         cancel_url)

            email_message = BASE_EMAIL_TEMPLATE.format(body_text=message_body)

            nm.send_notification(
                recipient=existing_acct.email,
                message=email_message,
                channel=NotificationChannel.EMAIL,
                scheduled_send_date=datetime.now(),
                subject='HF Volunteer Portal Account Registration')