Exemplo n.º 1
0
def test_count_inbound_sms_for_service(notify_db_session):
    service_one = create_service(service_name='one')
    service_two = create_service(service_name='two')

    create_inbound_sms(service_one)
    create_inbound_sms(service_one)
    create_inbound_sms(service_two)

    assert dao_count_inbound_sms_for_service(service_one.id, limit_days=1) == 2
Exemplo n.º 2
0
def test_get_inbound_sms_generate_page_links(client, sample_service, mocker):
    mocker.patch.dict("app.v2.inbound_sms.get_inbound_sms.current_app.config",
                      {"API_PAGE_SIZE": 2})
    all_inbound_sms = [
        create_inbound_sms(service=sample_service,
                           user_number='+447800900111',
                           content='Hi'),
        create_inbound_sms(service=sample_service,
                           user_number='+447800900111'),
        create_inbound_sms(service=sample_service,
                           user_number='+447800900111',
                           content='End'),
    ]

    reversed_inbound_sms = sorted(all_inbound_sms,
                                  key=lambda sms: sms.created_at,
                                  reverse=True)

    auth_header = create_authorization_header(service_id=sample_service.id)
    response = client.get(url_for('v2_inbound_sms.get_inbound_sms'),
                          headers=[('Content-Type', 'application/json'),
                                   auth_header])

    assert response.status_code == 200

    json_response = json.loads(response.get_data(as_text=True))
    expected_inbound_sms_list = [
        i.serialize() for i in reversed_inbound_sms[:2]
    ]

    assert json_response['received_text_messages'] == expected_inbound_sms_list
    assert url_for('v2_inbound_sms.get_inbound_sms',
                   _external=True) == json_response['links']['current']
    assert url_for('v2_inbound_sms.get_inbound_sms',
                   older_than=reversed_inbound_sms[1].id,
                   _external=True) == json_response['links']['next']
Exemplo n.º 3
0
def test_count_inbound_sms_for_service_filters_messages_older_than_n_days(
        sample_service):
    # test between evening sunday 2nd of june and morning of monday 3rd
    create_inbound_sms(sample_service, created_at=datetime(2019, 6, 2, 22, 59))
    create_inbound_sms(sample_service, created_at=datetime(2019, 6, 2, 22, 59))
    create_inbound_sms(sample_service, created_at=datetime(2019, 6, 2, 23, 1))

    with freeze_time('Monday 10th June 2019 12:00'):
        assert dao_count_inbound_sms_for_service(sample_service.id,
                                                 limit_days=7) == 1
Exemplo n.º 4
0
def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
    yesterday = datetime.utcnow() - timedelta(days=1)
    just_before_seven_days = datetime.utcnow() - timedelta(days=6, hours=23, minutes=59, seconds=59)
    older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)

    create_inbound_sms(sample_service, created_at=yesterday)
    create_inbound_sms(sample_service, created_at=just_before_seven_days)
    create_inbound_sms(sample_service, created_at=older_than_seven_days)

    delete_inbound_sms_created_more_than_a_week_ago()

    assert len(InboundSms.query.all()) == 2
    def test_does_not_retry_if_request_returns_404(self, notify_api,
                                                   sample_service, mocker):
        inbound_api = create_service_inbound_api(  # nosec
            service=sample_service,
            url="https://some.service.gov.uk/",
            bearer_token="something_unique")
        inbound_sms = create_inbound_sms(service=sample_service,
                                         notify_number="0751421",
                                         user_number="447700900111",
                                         provider_date=datetime(2017, 6, 20),
                                         content="Here is some content")

        mocked = mocker.patch(
            'app.celery.service_callback_tasks.send_inbound_sms_to_service.retry'
        )

        with requests_mock.Mocker() as request_mock:
            request_mock.post(inbound_api.url, json={}, status_code=404)
            send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id)

        assert mocked.call_count == 0
def test_get_next_inbound_sms_at_end_will_return_empty_inbound_sms_list(client, sample_service, mocker):
    inbound_sms = create_inbound_sms(service=sample_service)
    mocker.patch.dict(
        "app.v2.inbound_sms.get_inbound_sms.current_app.config",
        {"API_PAGE_SIZE": 1}
    )

    auth_header = create_authorization_header(service_id=inbound_sms.service.id)
    response = client.get(
        path=url_for('v2_inbound_sms.get_inbound_sms', older_than=inbound_sms.id),
        headers=[('Content-Type', 'application/json'), auth_header])

    assert response.status_code == 200

    json_response = json.loads(response.get_data(as_text=True))
    expected_inbound_sms_list = []
    assert json_response['received_text_messages'] == expected_inbound_sms_list
    assert url_for(
        'v2_inbound_sms.get_inbound_sms',
        _external=True) == json_response['links']['current']
    assert 'next' not in json_response['links'].keys()
    def test_retries_if_request_throws_unknown(self, notify_api,
                                               sample_service, mocker):
        create_service_inbound_api(  # nosec
            service=sample_service,
            url="https://some.service.gov.uk/",
            bearer_token="something_unique")
        inbound_sms = create_inbound_sms(service=sample_service,
                                         notify_number="0751421",
                                         user_number="447700900111",
                                         provider_date=datetime(2017, 6, 20),
                                         content="Here is some content")

        mocked = mocker.patch(
            'app.celery.service_callback_tasks.send_inbound_sms_to_service.retry'
        )
        mocker.patch("app.celery.service_callback_tasks.request",
                     side_effect=RequestException())

        send_inbound_sms_to_service(inbound_sms.id, inbound_sms.service_id)

        assert mocked.call_count == 1
        assert mocked.call_args[1]['queue'] == 'retry-tasks'
Exemplo n.º 8
0
def test_get_inbound_sms_summary(admin_request, sample_service):
    other_service = create_service(service_name='other_service')
    with freeze_time('2017-01-01'):
        create_inbound_sms(sample_service)
    with freeze_time('2017-01-02'):
        create_inbound_sms(sample_service)
    with freeze_time('2017-01-03'):
        create_inbound_sms(other_service)

    summary = admin_request.get(
        'inbound_sms.get_inbound_sms_summary_for_service',
        service_id=sample_service.id)

    assert summary == {
        'count': 2,
        'most_recent': datetime(2017, 1, 2).isoformat()
    }
Exemplo n.º 9
0
def test_get_inbound_sms_summary(admin_request, sample_service):
    other_service = create_service(service_name="other_service")
    with freeze_time("2017-01-01"):
        create_inbound_sms(sample_service)
    with freeze_time("2017-01-02"):
        create_inbound_sms(sample_service)
    with freeze_time("2017-01-03"):
        create_inbound_sms(other_service)

        summary = admin_request.get(
            "inbound_sms.get_inbound_sms_summary_for_service",
            service_id=sample_service.id,
        )

    assert summary == {
        "count": 2,
        "most_recent": datetime(2017, 1, 2).isoformat()
    }
Exemplo n.º 10
0
def test_delete_inbound_sms_older_than_retention_does_nothing_when_database_conflict_raised(
        sample_service):
    inbound_sms = create_inbound_sms(sample_service,
                                     created_at=datetime(2019, 12, 12, 20, 20),
                                     notify_number='07700900100',
                                     provider_date=datetime(
                                         2019, 12, 12, 20, 19),
                                     provider_reference='from daisy pie',
                                     provider='unicorn')
    inbound_sms_id = inbound_sms.id

    # Insert data directly in to inbound_sms_history to mimic if we had run `delete_inbound_sms_older_than_retention`
    # before but for some reason the delete statement had failed
    conflict_creating_row = InboundSmsHistory(
        id=inbound_sms.id,
        service_id=inbound_sms.service.id,
        created_at=inbound_sms.created_at,
        notify_number=inbound_sms.notify_number,
        provider_date=inbound_sms.provider_date,
        provider_reference=inbound_sms.provider_reference,
        provider=inbound_sms.provider,
    )
    db.session.add(conflict_creating_row)
    db.session.commit()
    assert conflict_creating_row.id

    delete_inbound_sms_older_than_retention()

    history = InboundSmsHistory.query.all()
    assert len(history) == 1

    assert history[0].id == inbound_sms_id
    assert history[0].notify_number == '07700900100'
    assert history[0].provider_date == datetime(2019, 12, 12, 20, 19)
    assert history[0].provider_reference == 'from daisy pie'
    assert history[0].provider == 'unicorn'
    assert history[0].created_at == datetime(2019, 12, 12, 20, 20)
Exemplo n.º 11
0
def test_most_recent_inbound_sms_only_returns_most_recent_for_each_number(
        notify_api, sample_service):
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 1",
        created_at=datetime(2017, 1, 1),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 2",
        created_at=datetime(2017, 1, 2),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 3",
        created_at=datetime(2017, 1, 3),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 4",
        created_at=datetime(2017, 1, 4),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 5",
        created_at=datetime(2017, 1, 5),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900222",
        content="222 1",
        created_at=datetime(2017, 1, 1),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900222",
        content="222 2",
        created_at=datetime(2017, 1, 2),
    )

    with set_config(notify_api, "PAGE_SIZE", 3):
        with freeze_time("2017-01-02"):
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=1)  # noqa

    assert len(res.items) == 2
    assert res.has_next is False
    assert res.per_page == 3
    assert res.items[0].content == "111 5"
    assert res.items[1].content == "222 2"
Exemplo n.º 12
0
def test_dao_get_paginated_inbound_sms_for_service_for_public_api(sample_service):
    inbound_sms = create_inbound_sms(service=sample_service)
    inbound_from_db = dao_get_paginated_inbound_sms_for_service_for_public_api(inbound_sms.service.id)

    assert inbound_sms == inbound_from_db[0]
Exemplo n.º 13
0
def test_most_recent_inbound_sms_paginates_properly(notify_api,
                                                    sample_service):
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 1',
                       created_at=datetime(2017, 1, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 2',
                       created_at=datetime(2017, 1, 2))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 1',
                       created_at=datetime(2017, 1, 3))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 2',
                       created_at=datetime(2017, 1, 4))
    create_inbound_sms(sample_service,
                       user_number='447700900333',
                       content='333 1',
                       created_at=datetime(2017, 1, 5))
    create_inbound_sms(sample_service,
                       user_number='447700900333',
                       content='333 2',
                       created_at=datetime(2017, 1, 6))
    create_inbound_sms(sample_service,
                       user_number='447700900444',
                       content='444 1',
                       created_at=datetime(2017, 1, 7))
    create_inbound_sms(sample_service,
                       user_number='447700900444',
                       content='444 2',
                       created_at=datetime(2017, 1, 8))

    with set_config(notify_api, 'PAGE_SIZE', 2):
        with freeze_time('2017-01-02'):
            # first page has most recent 444 and 333
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=1)  # noqa
            assert len(res.items) == 2
            assert res.has_next is True
            assert res.per_page == 2
            assert res.items[0].content == '444 2'
            assert res.items[1].content == '333 2'

            # second page has no 444 or 333 - just most recent 222 and 111
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=2)  # noqa
            assert len(res.items) == 2
            assert res.has_next is False
            assert res.items[0].content == '222 2'
            assert res.items[1].content == '111 2'
Exemplo n.º 14
0
def test_most_recent_inbound_sms_only_returns_most_recent_for_each_number(
        notify_api, sample_service):
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 1',
                       created_at=datetime(2017, 1, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 2',
                       created_at=datetime(2017, 1, 2))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 3',
                       created_at=datetime(2017, 1, 3))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 4',
                       created_at=datetime(2017, 1, 4))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 5',
                       created_at=datetime(2017, 1, 5))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 1',
                       created_at=datetime(2017, 1, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 2',
                       created_at=datetime(2017, 1, 2))

    with set_config(notify_api, 'PAGE_SIZE', 3):
        with freeze_time('2017-01-02'):
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=1)  # noqa

    assert len(res.items) == 2
    assert res.has_next is False
    assert res.per_page == 3
    assert res.items[0].content == '111 5'
    assert res.items[1].content == '222 2'
Exemplo n.º 15
0
def test_get_all_inbound_sms(sample_service):
    inbound = create_inbound_sms(sample_service)

    res = dao_get_inbound_sms_for_service(sample_service.id)
    assert len(res) == 1
    assert res[0] == inbound
Exemplo n.º 16
0
def test_get_inbound_sms_by_id_returns(sample_service):
    inbound_sms = create_inbound_sms(service=sample_service)
    inbound_from_db = dao_get_inbound_sms_by_id(inbound_sms.service.id,
                                                inbound_sms.id)

    assert inbound_sms == inbound_from_db
Exemplo n.º 17
0
def test_most_recent_inbound_sms_paginates_properly(notify_api,
                                                    sample_service):
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 1",
        created_at=datetime(2017, 1, 1),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900111",
        content="111 2",
        created_at=datetime(2017, 1, 2),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900222",
        content="222 1",
        created_at=datetime(2017, 1, 3),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900222",
        content="222 2",
        created_at=datetime(2017, 1, 4),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900333",
        content="333 1",
        created_at=datetime(2017, 1, 5),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900333",
        content="333 2",
        created_at=datetime(2017, 1, 6),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900444",
        content="444 1",
        created_at=datetime(2017, 1, 7),
    )
    create_inbound_sms(
        sample_service,
        user_number="447700900444",
        content="444 2",
        created_at=datetime(2017, 1, 8),
    )

    with set_config(notify_api, "PAGE_SIZE", 2):
        with freeze_time("2017-01-02"):
            # first page has most recent 444 and 333
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=1)  # noqa
            assert len(res.items) == 2
            assert res.has_next is True
            assert res.per_page == 2
            assert res.items[0].content == "444 2"
            assert res.items[1].content == "333 2"

            # second page has no 444 or 333 - just most recent 222 and 111
            res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
                sample_service.id, limit_days=7, page=2)  # noqa
            assert len(res.items) == 2
            assert res.has_next is False
            assert res.items[0].content == "222 2"
            assert res.items[1].content == "111 2"
Exemplo n.º 18
0
def test_most_recent_inbound_sms_only_returns_values_within_7_days(
        notify_api, sample_service):
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 1',
                       created_at=datetime(2017, 4, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900111',
                       content='111 2',
                       created_at=datetime(2017, 4, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 1',
                       created_at=datetime(2017, 4, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900222',
                       content='222 2',
                       created_at=datetime(2017, 4, 1))
    create_inbound_sms(sample_service,
                       user_number='447700900333',
                       content='333 1',
                       created_at=datetime(2017, 4, 2))
    create_inbound_sms(sample_service,
                       user_number='447700900333',
                       content='333 2',
                       created_at=datetime(2017, 4, 3))
    create_inbound_sms(sample_service,
                       user_number='447700900444',
                       content='444 1',
                       created_at=datetime(2017, 4, 4))
    create_inbound_sms(sample_service,
                       user_number='447700900444',
                       content='444 2',
                       created_at=datetime(2017, 4, 5))

    # 7 days ago BST midnight
    create_inbound_sms(sample_service,
                       user_number='447700900666',
                       content='666 1',
                       created_at='2017-04-02T23:00:00')

    with freeze_time('2017-04-09T12:00:00'):
        res = dao_get_paginated_most_recent_inbound_sms_by_user_number_for_service(
            sample_service.id, page=1)

    assert len(res.items) == 3
    assert res.items[0].content == '444 2'
    assert res.items[1].content == '333 2'
    assert res.items[2].content == '666 1'
Exemplo n.º 19
0
def test_should_delete_inbound_sms_older_than_seven_days(sample_service):
    older_than_seven_days = datetime.utcnow() - timedelta(days=7, seconds=1)
    create_inbound_sms(sample_service, created_at=older_than_seven_days)
    delete_inbound_sms_created_more_than_a_week_ago()

    assert len(InboundSms.query.all()) == 0