def test_twilio_inbound_sms_auth(notify_db_session, notify_api, client, mocker,
                                 auth, usernames, passwords, status_code):
    mocker.patch('twilio.request_validator.RequestValidator.validate',
                 return_value=True)
    mocker.patch(
        "app.notifications.receive_notifications.send_inbound_sms_to_service.apply_async"
    )

    create_service_with_inbound_number(
        service_name='b',
        inbound_number='+61412345678',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = urllib.parse.urlencode({
        'MessageSid': '1',
        'From': '+61412999999',
        'To': '+61412345678',
        'Body': 'this is a message'
    })

    with set_config_values(
            notify_api, {
                'TWILIO_INBOUND_SMS_USERNAMES': usernames,
                'TWILIO_INBOUND_SMS_PASSWORDS': passwords,
            }):
        response = twilio_post(client, data, auth=auth)
        assert response.status_code == status_code
def test_dao_fetch_service_by_inbound_number_with_unknown_number(
        notify_db_session):
    create_service_with_inbound_number(inbound_number='1', service_name='a')

    service = dao_fetch_service_by_inbound_number('9')

    assert service is None
Esempio n. 3
0
def test_dao_fetch_service_by_inbound_number_with_unknown_number(
        notify_db_session):
    create_service_with_inbound_number(inbound_number="1", service_name="a")

    service = dao_fetch_service_by_inbound_number("9")

    assert service is None
def test_mmg_receive_notification_error_if_not_single_matching_service(client, notify_db_session, notify_number):
    create_service_with_inbound_number(
        inbound_number='dog',
        service_name='a',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )
    create_service_with_inbound_number(
        inbound_number='bar',
        service_name='b',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )

    data = {
        'Message': 'hello',
        'Number': notify_number,
        'MSISDN': '7700900001',
        'DateRecieved': '2017-01-02 03:04:05',
        'ID': 'bar',
    }
    response = mmg_post(client, data)

    # we still return 'RECEIVED' to MMG
    assert response.status_code == 200
    assert response.get_data(as_text=True) == 'RECEIVED'
    assert InboundSms.query.count() == 0
def test_twilio_no_service_matches_inbound_number(notify_db_session, client,
                                                  mocker):
    mocker.patch('twilio.request_validator.RequestValidator.validate',
                 return_value=True)
    mocked = mocker.patch(
        "app.notifications.receive_notifications.send_inbound_sms_to_service.apply_async"
    )
    mock = mocker.patch(
        'app.notifications.receive_notifications.statsd_client.incr')

    create_service_with_inbound_number(
        inbound_number='+61412345678',
        service_name='b',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = urllib.parse.urlencode({
        'MessageSid': '1',
        'From': '+61412999999',
        'To': '+61412000000',
        'Body': 'this is a message'
    })

    response = twilio_post(client, data)

    assert response.status_code == 200
    assert response.get_data(
        as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    assert not InboundSms.query.all()
    mock.assert_has_calls([call('inbound.twilio.failed')])
    mocked.call_count == 0
Esempio n. 6
0
def test_dao_allocating_inbound_number_shows_on_service(notify_db_session):
    create_service_with_inbound_number()
    create_inbound_number(number='07700900003')

    inbound_numbers = dao_get_available_inbound_numbers()

    service = create_service(service_name='test service')

    dao_set_inbound_number_to_service(service.id, inbound_numbers[0])

    assert service.inbound_number.number == inbound_numbers[0].number
def test_firetext_inbound_sms_auth(notify_db_session, notify_api, client, mocker, auth, keys, status_code):
    mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")

    create_service_with_inbound_number(
        service_name='b', inbound_number='07111111111', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )

    data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"

    with set_config(notify_api, 'FIRETEXT_INBOUND_SMS_AUTH', keys):
        response = firetext_post(client, data, auth=bool(auth), password=auth)
        assert response.status_code == status_code
Esempio n. 8
0
def test_receive_notification_returns_received_to_firetext(
        notify_db_session, client, mocker):
    mocked = mocker.patch(
        "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
    )
    mock = mocker.patch(
        'app.notifications.receive_notifications.statsd_client.incr')

    service = create_service_with_inbound_number(
        service_name='b',
        inbound_number='07111111111',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"

    response = firetext_post(client, data)

    assert response.status_code == 200
    result = json.loads(response.get_data(as_text=True))

    mock.assert_has_calls([call('inbound.firetext.successful')])

    assert result['status'] == 'ok'
    inbound_sms_id = InboundSms.query.all()[0].id
    mocked.assert_called_once_with(
        [str(inbound_sms_id), str(service.id)], queue="notify-internal-tasks")
Esempio n. 9
0
def test_receive_notification_from_twilio_persists_message(notify_db_session, client, mocker):
    mocker.patch('twilio.request_validator.RequestValidator.validate', return_value=True)
    mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch('app.notifications.receive_notifications.statsd_client.incr')

    service = create_service_with_inbound_number(
        inbound_number='+61412345678',
        service_name='b',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = urllib.parse.urlencode({'MessageSid': '1', 'From': '+61487654321', 'To': '+61412345678', 'Body': 'this is a message'})

    response = twilio_post(client, data)

    assert response.status_code == 200
    assert response.get_data(as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    persisted = InboundSms.query.first()
    assert persisted is not None
    assert persisted.notify_number == '+61412345678'
    assert persisted.user_number == '+61487654321'
    assert persisted.service == service
    assert persisted.content == 'this is a message'
    assert persisted.provider == 'twilio'
    assert persisted.provider_date == datetime(2017, 1, 1, 1, 0, 0, 0)
    mocked.assert_called_once_with([str(persisted.id), str(service.id)], queue="notify-internal-tasks")
def test_receive_notification_from_twilio_responds(notify_db_session, client,
                                                   mocker):
    mocker.patch('twilio.request_validator.RequestValidator.validate',
                 return_value=True)
    mocked = mocker.patch(
        "app.notifications.receive_notifications.send_inbound_sms_to_service.apply_async"
    )
    mock = mocker.patch(
        'app.notifications.receive_notifications.statsd_client.incr')

    service = create_service_with_inbound_number(
        service_name='b',
        inbound_number='+61412888888',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = urllib.parse.urlencode({
        'MessageSid': '1',
        'From': '+61412999999',
        'To': '+61412888888',
        'Body': 'this is a message'
    })

    response = twilio_post(client, data)

    assert response.status_code == 200
    assert response.get_data(
        as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    mock.assert_has_calls([call('inbound.twilio.successful')])
    inbound_sms_id = InboundSms.query.all()[0].id
    mocked.assert_called_once_with(
        [str(inbound_sms_id), str(service.id)], queue="notify-internal-tasks")
Esempio n. 11
0
def test_receive_notification_from_firetext_persists_message(
        notify_db_session, client, mocker):
    mocked = mocker.patch(
        "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
    )
    mocker.patch('app.notifications.receive_notifications.statsd_client.incr')

    service = create_service_with_inbound_number(
        inbound_number='0412345678',
        service_name='b',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = "source=0487654321&destination=0412345678&message=this is a message&time=2017-01-01 12:00:00"

    response = firetext_post(client, data)

    assert response.status_code == 200
    result = json.loads(response.get_data(as_text=True))
    assert result['status'] == 'ok'
    persisted = InboundSms.query.first()
    assert persisted is not None
    assert persisted.notify_number == '0412345678'
    assert persisted.user_number == '+61487654321'
    assert persisted.service == service
    assert persisted.content == 'this is a message'
    assert persisted.provider == 'firetext'
    assert persisted.provider_date == datetime(2017, 1, 1, 1, 0, 0, 0)
    mocked.assert_called_once_with(
        [str(persisted.id), str(service.id)], queue="notify-internal-tasks")
Esempio n. 12
0
def test_dao_fetch_service_by_inbound_number_when_inbound_number_set(notify_db_session):
    service_1 = create_service_with_inbound_number(inbound_number='1', service_name='a')
    create_service(service_name='b')

    service = dao_fetch_service_by_inbound_number('1')

    assert service.id == service_1.id
Esempio n. 13
0
def test_post_sms_notification_uses_inbound_number_as_sender(
        client, notify_db_session, mocker):
    service = create_service_with_inbound_number(inbound_number='1')

    template = create_template(
        service=service, content="Hello (( Name))\nYour thing is due soon")
    mocked = mocker.patch('app.celery.provider_tasks.deliver_sms.apply_async')
    data = {
        'phone_number': '+16502532222',
        'template_id': str(template.id),
        'personalisation': {
            ' Name': 'Jo'
        }
    }
    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 == 201
    resp_json = json.loads(response.get_data(as_text=True))
    assert validate(resp_json, post_sms_response) == resp_json
    notifications = Notification.query.all()
    assert len(notifications) == 1
    notification_id = notifications[0].id
    assert resp_json['id'] == str(notification_id)
    assert resp_json['content']['from_number'] == '1'
    assert notifications[0].reply_to_text == '1'
    mocked.assert_called_once_with([str(notification_id)],
                                   queue='send-sms-tasks')
Esempio n. 14
0
def test_dao_fetch_service_by_inbound_number_with_inactive_number_returns_empty(notify_db_session):
    service = create_service_with_inbound_number(inbound_number='1', service_name='a')
    dao_set_inbound_number_active_flag(service_id=service.id, active=False)

    service = dao_fetch_service_by_inbound_number('1')

    assert service is None
def test_receive_notification_from_twilio_without_permissions_does_not_persist(
        client,
        mocker,
        notify_db_session,
        permissions
):
    mocker.patch('twilio.request_validator.RequestValidator.validate', return_value=True)

    service = create_service_with_inbound_number(inbound_number='+61412888888', service_permissions=permissions)
    mocker.patch("app.notifications.receive_notifications.dao_fetch_service_by_inbound_number",
                 return_value=service)
    mocked_send_inbound_sms = mocker.patch(
        "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch("app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False)

    data = urllib.parse.urlencode(
        {
            'MessageSid': '1',
            'From': '+61412999999',
            'To': '+61412888888',
            'Body': 'this is a message'
        }
    )

    response = twilio_post(client, data)

    assert response.status_code == 200
    assert response.get_data(as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    assert InboundSms.query.count() == 0
    assert not mocked_send_inbound_sms.called
Esempio n. 16
0
def test_receive_notification_from_twilio_persists_message_with_normalized_phone(notify_db_session, client, mocker):
    mocker.patch('twilio.request_validator.RequestValidator.validate', return_value=True)
    mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch('app.notifications.receive_notifications.statsd_client.incr')

    create_service_with_inbound_number(
        inbound_number='+61412345678', service_name='b', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    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 response.get_data(as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    persisted = InboundSms.query.first()
    assert persisted is not None
    assert persisted.user_number == '+61412999999'
Esempio n. 17
0
def test_returns_ok_to_firetext_if_mismatched_sms_sender(notify_db_session, client, mocker):
    mocked = mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch('app.notifications.receive_notifications.INBOUND_SMS_COUNTER')

    create_service_with_inbound_number(
        inbound_number='07111111199', service_name='b', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"

    response = firetext_post(client, data)

    assert response.status_code == 200
    result = json.loads(response.get_data(as_text=True))

    assert not InboundSms.query.all()
    assert result['status'] == 'ok'
    mocked.call_count == 0
Esempio n. 18
0
def test_dao_fetch_service_by_inbound_number_when_inbound_number_set(
        notify_db_session):
    service_1 = create_service_with_inbound_number(inbound_number="1",
                                                   service_name="a")
    create_service(service_name="b")

    service = dao_fetch_service_by_inbound_number("1")

    assert service.id == service_1.id
def test_receive_notification_from_firetext_persists_message_with_normalized_phone(notify_db_session, client, mocker):
    mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch('app.notifications.receive_notifications.statsd_client.incr')

    create_service_with_inbound_number(
        inbound_number='07111111111', service_name='b', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE])

    data = "source=(+44)7999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"

    response = firetext_post(client, data)

    assert response.status_code == 200
    result = json.loads(response.get_data(as_text=True))

    persisted = InboundSms.query.first()

    assert result['status'] == 'ok'
    assert persisted.user_number == '( 44)7999999999'
Esempio n. 20
0
def test_dao_fetch_service_by_inbound_number_with_inbound_number(notify_db_session):
    foo1 = create_service_with_inbound_number(service_name='a', inbound_number='1')
    create_service_with_defined_sms_sender(service_name='b', sms_sender_value='2')
    create_service_with_defined_sms_sender(service_name='c', sms_sender_value='3')
    create_inbound_number('2')
    create_inbound_number('3')

    service = dao_fetch_service_by_inbound_number('1')

    assert foo1.id == service.id
Esempio n. 21
0
def test_receive_notification_error_if_not_single_matching_service(client, mocker, notify_db_session, notify_number):
    mocker.patch('twilio.request_validator.RequestValidator.validate', return_value=True)

    create_service_with_inbound_number(
        inbound_number='+61412222222',
        service_name='a',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )
    create_service_with_inbound_number(
        inbound_number='+6141333333',
        service_name='b',
        service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )

    data = urllib.parse.urlencode({'MessageSid': '1', 'From': notify_number, 'To': '+61412888888', 'Body': 'hello'})
    response = twilio_post(client, data)

    assert response.status_code == 200
    assert response.get_data(as_text=True) == '<?xml version="1.0" encoding="UTF-8"?><Response />'
    assert InboundSms.query.count() == 0
def test_mmg_inbound_sms_auth(notify_db_session, notify_api, client, mocker, auth, keys, status_code):
    mocker.patch("app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")

    create_service_with_inbound_number(
        service_name='b', inbound_number='07111111111', service_permissions=[EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE]
    )

    data = {
        "ID": "1234",
        "MSISDN": "07111111111",
        "Message": "Some message to notify",
        "Trigger": "Trigger?",
        "Number": "testing",
        "Channel": "SMS",
        "DateRecieved": "2012-06-27 12:33:00"
    }

    with set_config(notify_api, 'MMG_INBOUND_SMS_AUTH', keys):
        response = mmg_post(client, data, auth=bool(auth), password=auth)
        assert response.status_code == status_code
Esempio n. 23
0
def test_get_inbound_sms_by_id_returns_200(admin_request, notify_db_session):
    service = create_service_with_inbound_number(inbound_number="12345")
    inbound = create_inbound_sms(service=service, user_number="+16502532222")

    response = admin_request.get(
        "inbound_sms.get_inbound_by_id",
        service_id=service.id,
        inbound_sms_id=inbound.id,
    )

    assert response["user_number"] == "+16502532222"
    assert response["service_id"] == str(service.id)
Esempio n. 24
0
def test_get_inbound_sms_by_id_returns_200(admin_request, notify_db_session):
    service = create_service_with_inbound_number(inbound_number='12345')
    inbound = create_inbound_sms(service=service, user_number='447700900001')

    response = admin_request.get(
        'inbound_sms.get_inbound_by_id',
        service_id=service.id,
        inbound_sms_id=inbound.id,
    )

    assert response['user_number'] == '447700900001'
    assert response['service_id'] == str(service.id)
Esempio n. 25
0
def test_receive_notification_from_mmg_without_permissions_does_not_persist(
        client, mocker, notify_db_session, permissions):
    mocked = mocker.patch(
        "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async"
    )
    create_service_with_inbound_number(inbound_number='07111111111',
                                       service_permissions=permissions)
    data = {
        "ID": "1234",
        "MSISDN": "07111111111",
        "Message": "Some message to notify",
        "Trigger": "Trigger?",
        "Number": "testing",
        "Channel": "SMS",
        "DateRecieved": "2012-06-27 12:33:00"
    }
    response = mmg_post(client, data)

    assert response.status_code == 200
    assert response.get_data(as_text=True) == 'RECEIVED'
    assert InboundSms.query.count() == 0
    assert mocked.called is False
Esempio n. 26
0
def test_dao_fetch_service_by_inbound_number_with_inbound_number(
        notify_db_session):
    foo1 = create_service_with_inbound_number(service_name="a",
                                              inbound_number="1")
    create_service_with_defined_sms_sender(service_name="b",
                                           sms_sender_value="2")
    create_service_with_defined_sms_sender(service_name="c",
                                           sms_sender_value="3")
    create_inbound_number("2")
    create_inbound_number("3")

    service = dao_fetch_service_by_inbound_number("1")

    assert foo1.id == service.id
Esempio n. 27
0
def test_delete_service_sms_sender_returns_400_if_archiving_inbound_number(
        admin_request, notify_db_session):
    service = create_service_with_inbound_number(inbound_number='7654321')
    inbound_number = service.service_sms_senders[0]

    response = admin_request.post(
        'service_sms_sender.delete_service_sms_sender',
        service_id=service.id,
        sms_sender_id=service.service_sms_senders[0].id,
        _expected_status=400)
    assert response == {
        'message': 'You cannot delete an inbound number',
        'result': 'error'
    }
    assert inbound_number.archived is False
Esempio n. 28
0
    def test_raises_exception_if_adding_number_to_use_already_allocated_inbound_number(
            self, notify_db_session):
        service_with_inbound_number = create_service_with_inbound_number()
        inbound_number = InboundNumber.query.filter_by(
            service_id=service_with_inbound_number.id).one()

        new_service = create_service(service_name='new service')

        with pytest.raises(SmsSenderInboundNumberIntegrityException) as e:
            dao_add_sms_sender_for_service(service_id=new_service.id,
                                           sms_sender='new-number',
                                           is_default=False,
                                           inbound_number_id=inbound_number.id)

        expected_msg = f'Inbound number: {inbound_number.id} is not available'
        assert expected_msg in str(e.value)
Esempio n. 29
0
def test_archive_sms_sender_raises_an_error_if_attempting_to_archive_an_inbound_number(
        notify_db_session, is_default):
    service = create_service_with_inbound_number(inbound_number='7654321')
    dao_add_sms_sender_for_service(service.id, 'second', is_default=True)

    inbound_number = next(x for x in service.service_sms_senders
                          if x.inbound_number_id)

    # regardless of whether inbound number is default or not, can't delete it
    dao_update_service_sms_sender(service.id,
                                  inbound_number.id,
                                  is_default=is_default)

    with pytest.raises(ArchiveValidationError) as e:
        archive_sms_sender(service_id=service.id,
                           sms_sender_id=inbound_number.id)

    assert 'You cannot delete an inbound number' in str(e.value)
    assert not inbound_number.archived
def test_receive_notification_from_firetext_without_permissions_does_not_persist(
        client,
        mocker,
        notify_db_session,
        permissions
):
    service = create_service_with_inbound_number(inbound_number='07111111111', service_permissions=permissions)
    mocker.patch("app.notifications.receive_notifications.dao_fetch_service_by_inbound_number",
                 return_value=service)
    mocked_send_inbound_sms = mocker.patch(
        "app.notifications.receive_notifications.tasks.send_inbound_sms_to_service.apply_async")
    mocker.patch("app.notifications.receive_notifications.has_inbound_sms_permissions", return_value=False)

    data = "source=07999999999&destination=07111111111&message=this is a message&time=2017-01-01 12:00:00"
    response = firetext_post(client, data)

    assert response.status_code == 200
    result = json.loads(response.get_data(as_text=True))

    assert result['status'] == 'ok'
    assert InboundSms.query.count() == 0
    assert not mocked_send_inbound_sms.called