Merge branch 'master' into reinstate-new-rate-api

Conflicts:
	app/main/views/dashboard.py
This commit is contained in:
Martyn Inglis
2017-06-07 14:44:27 +01:00
44 changed files with 1079 additions and 186 deletions

View File

@@ -5,6 +5,7 @@ from unittest.mock import call, ANY
from flask import url_for
import pytest
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
from freezegun import freeze_time
from app.main.views.dashboard import (
@@ -18,7 +19,11 @@ from app.main.views.dashboard import (
)
from tests import validate_route_permission
from tests.conftest import SERVICE_ONE_ID
from tests.conftest import (
SERVICE_ONE_ID,
mock_get_inbound_sms_summary,
mock_get_inbound_sms_summary_with_no_messages,
)
from tests.app.test_utils import normalize_spaces
stub_template_stats = [
@@ -44,6 +49,7 @@ def test_get_started(
mock_get_jobs,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
@@ -63,6 +69,7 @@ def test_get_started_is_hidden_once_templates_exist(
mock_get_jobs,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
@@ -74,6 +81,122 @@ def test_get_started_is_hidden_once_templates_exist(
assert 'Get started' not in response.get_data(as_text=True)
def test_inbound_messages_not_visible_to_service_without_permissions(
logged_in_client,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_detailed_service,
mock_get_template_statistics,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
service_one['permissions'] = []
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert response.status_code == 200
assert not page.select('.big-number-meta-wrapper')
assert mock_get_inbound_sms_summary.called is False
@pytest.mark.parametrize('inbound_summary_mock, expected_text', [
(mock_get_inbound_sms_summary_with_no_messages, '0 text messages received'),
(mock_get_inbound_sms_summary, '99 text messages received latest message just now'),
])
def test_inbound_messages_shows_count_of_messages(
logged_in_client,
mocker,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_detailed_service,
mock_get_template_statistics,
mock_get_usage,
inbound_summary_mock,
expected_text,
mock_get_yearly_sms_unit_count_and_cost
):
service_one['permissions'] = ['inbound_sms']
inbound_summary_mock(mocker)
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert response.status_code == 200
assert normalize_spaces(page.select('.big-number-meta-wrapper')[0].text) == expected_text
assert page.select('.big-number-meta-wrapper a')[0]['href'] == url_for(
'main.inbox', service_id=SERVICE_ONE_ID
)
@pytest.mark.parametrize('index, expected_row', enumerate([
'07900900000 foo 1 hour ago',
'07900900001 foo 2 hours ago',
'07900900002 foo 3 hours ago',
'07900900003 foo 4 hours ago',
'07900900004 foo 5 hours ago',
]))
def test_inbox_showing_inbound_messages(
logged_in_client,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_detailed_service,
mock_get_template_statistics,
mock_get_usage,
mock_get_inbound_sms,
index,
expected_row,
):
service_one['permissions'] = ['inbound_sms']
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID))
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert response.status_code == 200
rows = page.select('tbody tr')
assert len(rows) == 5
assert normalize_spaces(rows[index].text) == expected_row
def test_empty_inbox(
logged_in_client,
service_one,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_detailed_service,
mock_get_template_statistics,
mock_get_usage,
mock_get_inbound_sms_with_no_messages,
):
service_one['permissions'] = ['inbound_sms']
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID))
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert response.status_code == 200
assert normalize_spaces(page.select('tbody tr')) == (
'When users text your services phone number (GOVUK) youll see the messages here'
)
def test_inbox_not_accessible_to_service_without_permissions(
logged_in_client,
service_one,
):
service_one['permissions'] = []
response = logged_in_client.get(url_for('main.inbox', service_id=SERVICE_ONE_ID))
assert response.status_code == 403
def test_should_show_recent_templates_on_dashboard(
logged_in_client,
mocker,
@@ -81,6 +204,7 @@ def test_should_show_recent_templates_on_dashboard(
mock_get_jobs,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
mock_template_stats = mocker.patch('app.template_statistics_client.get_template_statistics_for_service',
@@ -147,6 +271,7 @@ def test_should_show_upcoming_jobs_on_dashboard(
mock_get_detailed_service,
mock_get_jobs,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
@@ -178,6 +303,7 @@ def test_should_show_recent_jobs_on_dashboard(
mock_get_detailed_service,
mock_get_jobs,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
response = logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
@@ -297,6 +423,7 @@ def test_menu_send_messages(
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
with app_.test_request_context():
@@ -328,6 +455,7 @@ def test_menu_manage_service(
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
with app_.test_request_context():
@@ -358,6 +486,7 @@ def test_menu_manage_api_keys(
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
with app_.test_request_context():
@@ -388,6 +517,7 @@ def test_menu_all_services_for_platform_admin_user(
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
with app_.test_request_context():
@@ -418,6 +548,7 @@ def test_route_for_service_permissions(
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
with app_.test_request_context():
@@ -455,6 +586,7 @@ def test_service_dashboard_updates_gets_dashboard_totals(
mock_get_detailed_service,
mock_get_jobs,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
mocker.patch('app.main.views.dashboard.get_dashboard_totals', return_value={
@@ -677,11 +809,13 @@ def test_should_show_all_jobs_with_valid_statuses(
logged_in_client,
mock_get_template_statistics,
mock_get_detailed_service,
mock_get_service_templates_when_no_templates_exist,
mock_get_jobs,
mock_get_usage,
mock_get_inbound_sms_summary,
mock_get_yearly_sms_unit_count_and_cost
):
get_dashboard_partials(service_id=SERVICE_ONE_ID)
logged_in_client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID))
first_call = mock_get_jobs.call_args_list[0]
# first call - scheduled jobs only