def test_fetch_count_of_complaints(sample_email_notification):
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 6, 6, 22, 00, 00),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 6, 6, 23, 00, 00),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 6, 7, 00, 00, 00),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 6, 7, 13, 00, 00),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 6, 7, 23),
    )

    count_of_complaints = fetch_count_of_complaints(start_date=datetime(2018, 6, 7), end_date=datetime(2018, 6, 7))
    assert count_of_complaints == 2
Exemplo n.º 2
0
def test_get_all_complaints_returns_complaints_for_multiple_services(client, notify_db_session):
    service = create_service(service_name='service1')
    template = create_template(service=service)
    notification = create_notification(template=template)
    complaint_1 = create_complaint()  # default service
    complaint_2 = create_complaint(service=service, notification=notification)

    response = client.get('/complaint', headers=[create_authorization_header()])

    assert response.status_code == 200
    assert json.loads(response.get_data(as_text=True))['complaints'] == [
        complaint_2.serialize(), complaint_1.serialize()]
Exemplo n.º 3
0
def test_send_complaint_to_service_posts_https_request_to_service_with_encrypted_data(notify_db_session):
    with freeze_time('2001-01-01T12:00:00'):
        callback_api, template = _set_up_test_data('email', "complaint")

        notification = create_notification(template=template)
        complaint = create_complaint(service=template.service, notification=notification)
        complaint_data = _set_up_data_for_complaint(callback_api, complaint, notification)
        with requests_mock.Mocker() as request_mock:
            request_mock.post(callback_api.url,
                              json={},
                              status_code=200)
            send_complaint_to_service(complaint_data)

        mock_data = {
            "notification_id": str(notification.id),
            "complaint_id": str(complaint.id),
            "reference": notification.client_reference,
            "to": notification.to,
            "complaint_date": datetime.utcnow().strftime(
                DATETIME_FORMAT),
        }

        assert request_mock.call_count == 1
        assert request_mock.request_history[0].url == callback_api.url
        assert request_mock.request_history[0].method == 'POST'
        assert request_mock.request_history[0].text == json.dumps(mock_data)
        assert request_mock.request_history[0].headers["Content-type"] == "application/json"
        assert request_mock.request_history[0].headers["Authorization"] == "Bearer {}".format(callback_api.bearer_token)
Exemplo n.º 4
0
def test_create_complaint_callback_data(
    notify_db,
    notify_db_session,
    sample_email_template,
):
    notification = create_sample_notification(
        notify_db,
        notify_db_session,
        template=sample_email_template,
        status="delivered",
        sent_at=datetime.utcnow(),
    )
    complaint = create_complaint(notification=notification,
                                 service=notification.service)
    callback_api = create_service_callback_api(
        service=sample_email_template.service, url="https://original_url.com")

    assert encryption.decrypt(
        create_complaint_callback_data(
            complaint, notification, callback_api,
            "*****@*****.**")) == {
                "complaint_id": str(complaint.id),
                "notification_id": str(notification.id),
                "reference": notification.client_reference,
                "to": "*****@*****.**",
                "complaint_date":
                complaint.complaint_date.strftime(DATETIME_FORMAT),
                "service_callback_api_url": callback_api.url,
                "service_callback_api_bearer_token": callback_api.bearer_token,
            }
Exemplo n.º 5
0
def test_fetch_complaint_by_id(sample_email_notification):
    complaint = create_complaint(service=sample_email_notification.service,
                                 notification=sample_email_notification,
                                 created_at=datetime(2018, 1, 1))

    complaints_from_db = fetch_complaint_by_id(complaint.id)

    assert complaints_from_db[0].id == complaint.id
def complaint_and_template_name_to_vanotify():
    service = create_service(service_name="Sample VANotify service",
                             restricted=True)
    template = create_template(service=service,
                               template_name="Sample VANotify service",
                               template_type="email",
                               subject='Hello')
    notification = create_notification(template=template)
    complaint = create_complaint(service=template.service,
                                 notification=notification)
    return complaint, template.name
def test_fetch_paginated_complaints(mocker, sample_email_notification):
    mocker.patch.dict("app.dao.complaint_dao.current_app.config", {"PAGE_SIZE": 2})
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 1),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 2),
    )
    create_complaint(
        service=sample_email_notification.service,
        notification=sample_email_notification,
        created_at=datetime(2018, 1, 3),
    )

    res = fetch_paginated_complaints(page=1)

    assert len(res.items) == 2
    assert res.items[0].created_at == datetime(2018, 1, 3)
    assert res.items[1].created_at == datetime(2018, 1, 2)

    res = fetch_paginated_complaints(page=2)

    assert len(res.items) == 1
    assert res.items[0].created_at == datetime(2018, 1, 1)
Exemplo n.º 8
0
def test_get_all_complaints_returns_pagination_links(mocker, client, notify_db_session):
    mocker.patch.dict('app.dao.complaint_dao.current_app.config', {'PAGE_SIZE': 1})
    service_1 = create_service(service_name='service1')
    service_2 = create_service(service_name='service2')

    create_complaint()
    create_complaint(service=service_1)
    create_complaint(service=service_2)

    response = client.get(url_for('complaint.get_all_complaints', page=2), headers=[create_authorization_header()])

    assert response.status_code == 200
    assert json.loads(response.get_data(as_text=True))['links'] == {
        'last': '/complaint?page=3',
        'next': '/complaint?page=3',
        'prev': '/complaint?page=1'}
Exemplo n.º 9
0
def test_get_all_complaints_returns_pagination_links(mocker, client,
                                                     notify_db_session):
    mocker.patch.dict("app.dao.complaint_dao.current_app.config",
                      {"PAGE_SIZE": 1})
    service_1 = create_service(service_name="service1")
    service_2 = create_service(service_name="service2")

    create_complaint()
    create_complaint(service=service_1)
    create_complaint(service=service_2)

    response = client.get(
        url_for("complaint.get_all_complaints", page=2),
        headers=[create_authorization_header()],
    )

    assert response.status_code == 200
    assert json.loads(response.get_data(as_text=True))["links"] == {
        "last": "/complaint?page=3",
        "next": "/complaint?page=3",
        "prev": "/complaint?page=1",
    }