def test_should_show_archived_services_last(
    client,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    restricted,
    table_index
):
    services = [
        service_json(name='C', restricted=restricted, active=False, created_at='2002-02-02 12:00:00'),
        service_json(name='B', restricted=restricted, active=True, created_at='2001-01-01 12:00:00'),
        service_json(name='A', restricted=restricted, active=True, created_at='2003-03-03 12:00:00'),
    ]
    services[0]['statistics'] = create_stats()
    services[1]['statistics'] = create_stats()
    services[2]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    table_body = page.find_all('table')[table_index].find_all('tbody')[0]
    services = [service.tr for service in table_body.find_all('tbody')]
    assert len(services) == 3
    assert services[0].td.text.strip() == 'A'
    assert services[1].td.text.strip() == 'B'
    assert services[2].td.text.strip() == 'C'
def test_should_show_research_and_restricted_mode(
    restricted,
    table_index,
    research_mode,
    displayed,
    app_,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    fake_uuid
):
    services = [service_json(fake_uuid, 'My Service', [], restricted=restricted, research_mode=research_mode)]
    services[0]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    # get first column in second row, which contains flags as text.
    table_body = page.find_all('table')[table_index].find_all('tbody')[0]
    service_mode = table_body.find_all('tbody')[0].find_all('tr')[1].find_all('td')[0].text.strip()
    assert service_mode == displayed
def test_platform_admin_toggle_including_from_test_key(
    include_from_test_key,
    expected_text,
    unexpected_text,
    api_args,
    app_,
    platform_admin_user,
    mocker,
    mock_get_detailed_services
):
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin', include_from_test_key=str(include_from_test_key)))

    assert response.status_code == 200
    resp_data = response.get_data(as_text=True)
    assert expected_text in resp_data
    assert unexpected_text not in resp_data

    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    change_link = page.find('a', text='change')
    assert change_link['href']
    query_param = 'include_from_test_key=False'
    if include_from_test_key:
        assert query_param in change_link['href']
    else:
        assert query_param not in change_link['href']

    mock_get_detailed_services.assert_called_once_with(api_args)
def test_should_403_if_not_platform_admin(app_, active_user_with_permissions, mocker):
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=active_user_with_permissions)
            client.login(active_user_with_permissions)

            response = client.get(url_for('main.platform_admin'))

            assert response.status_code == 403
def test_should_show_email_and_sms_stats_for_all_service_types(
    restricted,
    table_index,
    research_mode,
    app_,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    fake_uuid
):
    services = [service_json(fake_uuid, 'My Service', [], restricted=restricted, research_mode=research_mode)]
    services[0]['statistics'] = create_stats(
        emails_requested=10,
        emails_delivered=3,
        emails_failed=5,
        sms_requested=50,
        sms_delivered=7,
        sms_failed=11
    )

    mock_get_detailed_services.return_value = {'data': services}
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    table_body = page.find_all('table')[table_index].find_all('tbody')[0]
    service_row_group = table_body.find_all('tbody')[0].find_all('tr')
    email_stats = service_row_group[0].find_all('div', class_='big-number-number')
    sms_stats = service_row_group[1].find_all('div', class_='big-number-number')

    email_sending, email_delivered, email_failed = [int(x.text.strip()) for x in email_stats]
    sms_sending, sms_delivered, sms_failed = [int(x.text.strip()) for x in sms_stats]

    assert email_sending == 2
    assert email_delivered == 3
    assert email_failed == 5
    assert sms_sending == 32
    assert sms_delivered == 7
    assert sms_failed == 11
def test_should_render_platform_admin_page(
    app_,
    platform_admin_user,
    mocker,
    mock_get_services,
    mock_get_all_service_statistics
):
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

            assert response.status_code == 200
            resp_data = response.get_data(as_text=True)
            assert 'Platform admin' in resp_data
            assert 'Today' in resp_data
            assert 'Services' in resp_data
示例#7
0
def test_should_show_complaints_with_next_previous(mocker, client,
                                                   platform_admin_user,
                                                   service_one, fake_uuid):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)

    api_response = {
        'complaints': [{
            'complaint_date': None,
            'complaint_type': None,
            'created_at': '2017-12-18T05:00:00.000000Z',
            'id': fake_uuid,
            'notification_id': fake_uuid,
            'service_id': service_one['id'],
            'service_name': service_one['name'],
            'ses_feedback_id': 'None'
        }],
        'links': {
            'last': '/complaint?page=3',
            'next': '/complaint?page=3',
            'prev': '/complaint?page=1'
        }
    }

    mocker.patch('app.complaint_api_client.get_all_complaints',
                 return_value=api_response)

    response = client.get(
        url_for('main.platform_admin_list_complaints', page=2))

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    next_page_link = page.find('a', {'rel': 'next'})
    prev_page_link = page.find('a', {'rel': 'previous'})
    assert (url_for('main.platform_admin_list_complaints', page=3)
            in next_page_link['href'])
    assert 'Next page' in next_page_link.text.strip()
    assert 'page 3' in next_page_link.text.strip()
    assert (url_for('main.platform_admin_list_complaints', page=1)
            in prev_page_link['href'])
    assert 'Previous page' in prev_page_link.text.strip()
    assert 'page 1' in prev_page_link.text.strip()
示例#8
0
def test_should_render_platform_admin_page(
    app_,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
):
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    resp_data = response.get_data(as_text=True)
    assert 'Platform admin' in resp_data
    assert 'Today' in resp_data
    assert 'Live services' in resp_data
    assert 'Trial mode services' in resp_data
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
示例#9
0
def test_should_render_platform_admin_page(client, platform_admin_user, mocker,
                                           mock_get_detailed_services,
                                           endpoint, expected_services_shown):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for(endpoint))
    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert len(
        page.select('tbody tr')
    ) == expected_services_shown * 3  # one row for SMS, one for email, one for letter
    mock_get_detailed_services.assert_called_once_with({
        'detailed':
        True,
        'include_from_test_key':
        True,
        'only_active':
        False
    })
示例#10
0
def test_reports_page(
    client,
    platform_admin_user,
    mocker
):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)

    response = client.get(url_for('main.platform_admin_reports'))

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert page.find(
        'a', text="Download trial mode services csv report"
    ).attrs['href'] == '/platform-admin/reports/trial-services.csv'

    assert page.find(
        'a', text="Download live services csv report"
    ).attrs['href'] == '/platform-admin/reports/live-services.csv'
示例#11
0
def test_should_show_correct_sent_totals_for_platform_admin(
    client,
    platform_admin_user,
    mocker,
    mock_get_aggregate_platform_stats,
    fake_uuid,
):
    stats = {
        'email': {
            'requested': 100,
            'delivered': 20,
            'failed': 40
        },
        'sms': {
            'requested': 100,
            'delivered': 10,
            'failed': 30
        },
        'letter': {
            'requested': 60,
            'delivered': 40,
            'failed': 5
        }
    }
    mock_get_aggregate_platform_stats.return_value = stats
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    totals = page.find_all('div', 'big-number-with-status')
    email_total = int(totals[0].find_all('div',
                                         'big-number-number')[0].text.strip())
    sms_total = int(totals[1].find_all('div',
                                       'big-number-number')[0].text.strip())
    letter_total = int(totals[2].find_all('div',
                                          'big-number-number')[0].text.strip())

    assert email_total == 60
    assert sms_total == 40
    assert letter_total == 60
def test_should_render_platform_admin_page(
    app_,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
):
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    resp_data = response.get_data(as_text=True)
    assert 'Platform admin' in resp_data
    assert 'Showing stats for today' in resp_data
    assert 'Live services' in resp_data
    assert 'Trial mode services' in resp_data
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
示例#13
0
def test_platform_admin_with_date_filter(
    client,
    platform_admin_user,
    mocker,
    mock_get_aggregate_platform_stats
):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.platform_admin', start_date='2016-12-20', end_date='2016-12-28'))

    assert response.status_code == 200
    resp_data = response.get_data(as_text=True)
    assert 'Platform admin' in resp_data
    mock_get_aggregate_platform_stats.assert_called_once_with({
        'include_from_test_key': False,
        'end_date': datetime.date(2016, 12, 28),
        'start_date': datetime.date(2016, 12, 20),
        'detailed': True,
        'only_active': False,
    })
示例#14
0
def test_letter_validation_preview_doesnt_call_template_preview_when_file_doesnt_pass_virus_scan(
        mocker, client, platform_admin_user):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    antivirus_scan = mocker.patch(
        'app.main.views.platform_admin.antivirus_client.scan',
        return_value=False)
    validate_letter = mocker.patch(
        'app.main.views.platform_admin.validate_letter')

    with open('tests/test_pdf_files/multi_page_pdf.pdf', 'rb') as file:
        response = client.post(
            url_for('main.platform_admin_letter_validation_preview'),
            data={"file": file},
            content_type='multipart/form-data')
    assert response.status_code == 400
    assert antivirus_scan.called is True
    validate_letter.assert_not_called()

    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert page.find('div', class_='banner-dangerous').text.strip(
    ) == "Document didn't pass the virus scan"
示例#15
0
def test_should_show_research_and_restricted_mode(
    endpoint,
    restricted,
    research_mode,
    displayed,
    client,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    fake_uuid,
):
    services = [
        service_json(fake_uuid,
                     'My Service', [],
                     restricted=restricted,
                     research_mode=research_mode)
    ]
    services[0]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for(endpoint))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({
        'detailed':
        True,
        'include_from_test_key':
        True,
        'only_active':
        False
    })
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    # get first column in second row, which contains flags as text.
    table_body = page.find_all('table')[0].find_all('tbody')[0]
    service_mode = table_body.find_all('tbody')[0].find_all('tr')[1].find_all(
        'td')[0].text.strip()
    assert service_mode == displayed
示例#16
0
def test_get_trial_services_report(client, platform_admin_user, mocker):
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)

    mocker.patch(
        'app.service_api_client.get_trial_services_data',
        return_value={'data': [
            {'service_id': 1, 'service_name': 'jessie the oak tree', 'organisation_name': 'Forest',
                'organisation_type': 'Ecosystem', 'sms_totals': 300, 'email_totals': 1200, 'letter_totals': 0},
            {'service_id': 2, 'service_name': 'james the pine tree', 'organisation_name': 'Forest',
                'organisation_type': 'Ecosystem', 'sms_totals': 0, 'email_totals': 0, 'letter_totals': 0},
        ]}
    )
    response = client.get(url_for('main.trial_services_csv'))
    assert response.status_code == 200
    report = response.get_data(as_text=True)
    assert report.strip() == (
        'Service ID,Organisation,Organisation type,Service name,'
        + 'SMS sent this year,Emails sent this year,Letters sent this year\r\n'
        + '1,Forest,Ecosystem,jessie the oak tree,True,Forest fairy,300,1200,0\r\n'
        + '2,Forest,Ecosystem,james the pine tree,0,0,0'
    )
示例#17
0
def test_should_login_platform_admin_user_and_redirect_to_your_services(
    client,
    platform_admin_user,
    mocker,
    mock_check_verify_code,
    mock_create_event,
    mock_get_security_keys,
    mock_get_login_events,
    method,
):
    mock_get_user(mocker, platform_admin_user)

    with client.session_transaction() as session:
        session["user_details"] = {
            "id": platform_admin_user["id"],
            "email": platform_admin_user["email_address"],
        }
    response = client.post(url_for(f"main.two_factor_{method}_sent"),
                           data={"two_factor_code": "12345"})

    assert response.status_code == 302
    assert response.location == url_for("main.choose_account", _external=True)
示例#18
0
def test_should_render_email_two_factor_page(
    client,
    api_user_active_email_auth,
    mock_get_security_keys,
    mock_get_login_events,
    mocker,
):
    mock_get_user(mocker, api_user_active_email_auth)
    # TODO this lives here until we work out how to
    # reassign the session after it is lost mid register process
    with client.session_transaction() as session:
        session["user_details"] = {
            "id": api_user_active_email_auth["id"],
            "email": api_user_active_email_auth["email_address"],
        }
    response = client.get(url_for("main.two_factor_email_sent"))
    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode("utf-8"), "html.parser")
    assert page.select_one("main p").text.strip() == (
        "We’ve sent you an email with a security code.")
    assert page.select_one("label").text.strip("Text message code")
    assert page.select_one("input")["type"] == "tel"
    assert page.select_one("input")["pattern"] == "[0-9]*"
示例#19
0
def test_shows_archived_label_instead_of_live_or_research_mode_label(
    client,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    research_mode,
):
    services = [
        service_json(restricted=False, research_mode=research_mode, active=False)
    ]
    services[0]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.live_services'))

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    table_body = page.find_all('table')[0].find_all('tbody')[0]
    service_mode = table_body.find_all('tbody')[0].find_all('tr')[1].td.text.strip()
    # get second column, which contains flags as text.
    assert service_mode == 'archived'
示例#20
0
def test_should_show_research_and_restricted_mode(
        restricted, table_index, research_mode, displayed, app_,
        platform_admin_user, mocker, mock_get_detailed_services, fake_uuid):
    services = [
        service_json(fake_uuid,
                     'My Service', [],
                     restricted=restricted,
                     research_mode=research_mode)
    ]
    services[0]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    with app_.test_request_context():
        with app_.test_client() as client:
            mock_get_user(mocker, user=platform_admin_user)
            client.login(platform_admin_user)
            response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({'detailed': True})
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    # get second column, which contains flags as text.
    assert page.find_all('tbody')[table_index].find_all(
        'td')[1].text.strip() == displayed
示例#21
0
def test_should_show_total_on_platform_admin_page(
        client, platform_admin_user, mocker,
        mock_get_aggregate_platform_stats):
    stats = {
        'email': {
            'requested': 61,
            'delivered': 55,
            'failed': 6
        },
        'sms': {
            'requested': 121,
            'delivered': 110,
            'failed': 11
        },
        'letter': {
            'requested': 45,
            'delivered': 32,
            'failed': 13
        }
    }
    expected = ('61 emails sent 6 failed – 9.8%',
                '121 text messages sent 11 failed – 9.1%',
                '45 letters sent 13 failed – 28.9%')

    mock_get_aggregate_platform_stats.return_value = stats

    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.platform_admin'))
    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
    assert (
        normalize_spaces(page.select('.big-number-with-status')[0].text),
        normalize_spaces(page.select('.big-number-with-status')[1].text),
        normalize_spaces(page.select('.big-number-with-status')[2].text),
    ) == expected
def test_shows_archived_label_instead_of_live_or_research_mode_label(
    client,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    research_mode
):
    services = [
        service_json(restricted=False, research_mode=research_mode, active=False)
    ]
    services[0]['statistics'] = create_stats()

    mock_get_detailed_services.return_value = {'data': services}
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for('main.platform_admin'))

    assert response.status_code == 200
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    table_body = page.find_all('table')[0].find_all('tbody')[0]
    service_mode = table_body.find_all('tbody')[0].find_all('tr')[1].td.text.strip()
    # get second column, which contains flags as text.
    assert service_mode == 'archived'
示例#23
0
def test_should_order_services_by_usage_with_inactive_last(
    endpoint,
    restricted,
    research_mode,
    client,
    platform_admin_user,
    mocker,
    mock_get_detailed_services,
    fake_uuid,
):
    services = [
        service_json(fake_uuid,
                     'My Service 1', [],
                     restricted=restricted,
                     research_mode=research_mode),
        service_json(fake_uuid,
                     'My Service 2', [],
                     restricted=restricted,
                     research_mode=research_mode),
        service_json(fake_uuid,
                     'My Service 3', [],
                     restricted=restricted,
                     research_mode=research_mode,
                     active=False)
    ]
    services[0]['statistics'] = create_stats(emails_requested=100,
                                             emails_delivered=25,
                                             emails_failed=25,
                                             sms_requested=100,
                                             sms_delivered=25,
                                             sms_failed=25)

    services[1]['statistics'] = create_stats(emails_requested=200,
                                             emails_delivered=50,
                                             emails_failed=50,
                                             sms_requested=200,
                                             sms_delivered=50,
                                             sms_failed=50)

    services[2]['statistics'] = create_stats(emails_requested=200,
                                             emails_delivered=50,
                                             emails_failed=50,
                                             sms_requested=200,
                                             sms_delivered=50,
                                             sms_failed=50)

    mock_get_detailed_services.return_value = {'data': services}
    mock_get_user(mocker, user=platform_admin_user)
    client.login(platform_admin_user)
    response = client.get(url_for(endpoint))

    assert response.status_code == 200
    mock_get_detailed_services.assert_called_once_with({
        'detailed':
        True,
        'include_from_test_key':
        True,
        'only_active':
        ANY
    })
    page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')

    table_body = page.find_all('table')[0].find_all('tbody')[0]
    services = [service.tr for service in table_body.find_all('tbody')]
    assert len(services) == 3
    assert services[0].td.text.strip() == 'My Service 2'
    assert services[1].td.text.strip() == 'My Service 1'
    assert services[2].td.text.strip() == 'My Service 3'