Merge branch 'master' of github.com:alphagov/notifications-admin into deactivate-services-plat-admin

This commit is contained in:
Leo Hemsted
2016-11-14 17:15:05 +00:00
48 changed files with 751 additions and 411 deletions

View File

@@ -9,14 +9,37 @@ def test_form_contains_next_24h(app_):
choices = ChooseTimeForm().scheduled_for.choices
# Friday
assert choices[0] == ('', 'Now')
assert choices[1] == ('2016-01-01T12:00:00.061258', 'Midday')
assert choices[23] == ('2016-01-02T10:00:00.061258', '10am')
assert choices[1] == ('2016-01-01T12:00:00.061258', 'Today at midday')
assert choices[13] == ('2016-01-02T00:00:00.061258', 'Today at midnight')
# Saturday
assert choices[14] == ('2016-01-02T01:00:00.061258', 'Tomorrow at 1am')
assert choices[37] == ('2016-01-03T00:00:00.061258', 'Tomorrow at midnight')
# Sunday
assert choices[38] == ('2016-01-03T01:00:00.061258', 'Sunday at 1am')
# Monday
assert choices[84] == ('2016-01-04T23:00:00.061258', 'Monday at 11pm')
assert choices[85] == ('2016-01-05T00:00:00.061258', 'Monday at midnight')
with pytest.raises(IndexError):
assert choices[24]
assert choices[
12 + # hours left in the day
(3 * 24) + # 3 days
2 # magic number
]
@freeze_time("2016-01-01 11:09:00.061258")
def test_form_defaults_to_now(app_):
assert ChooseTimeForm().scheduled_for.data == ''
@freeze_time("2016-01-01 11:09:00.061258")
def test_form_contains_next_three_days(app_):
assert ChooseTimeForm().scheduled_for.categories == [
'Later today', 'Tomorrow', 'Sunday', 'Monday'
]

View File

@@ -22,7 +22,6 @@ def test_should_show_api_page(
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.string.strip() == 'API integration'
assert 'Your service is in trial mode' in page.find('div', {'class': 'banner-warning'}).text
rows = page.find_all('details')
assert len(rows) == 5
for index, row in enumerate(rows):
@@ -202,10 +201,7 @@ def test_cant_create_normal_api_key_in_trial_mode(
'key_type': 'normal'
}
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('span', {'class': 'error-message'}).text.strip() == 'Not a valid choice'
assert response.status_code == 400
mock_post.assert_not_called()

View File

@@ -194,10 +194,10 @@ def test_should_show_upcoming_jobs_on_dashboard(
assert len(table_rows) == 2
assert 'send_me_later.csv' in table_rows[0].find_all('th')[0].text
assert 'Sending at 11:09am' in table_rows[0].find_all('th')[0].text
assert 'Sending today at 11:09am' in table_rows[0].find_all('th')[0].text
assert table_rows[0].find_all('td')[0].text.strip() == '1'
assert 'even_later.csv' in table_rows[1].find_all('th')[0].text
assert 'Sending at 11:09pm' in table_rows[1].find_all('th')[0].text
assert 'Sending today at 11:09pm' in table_rows[1].find_all('th')[0].text
assert table_rows[1].find_all('td')[0].text.strip() == '1'

View File

@@ -142,6 +142,7 @@ def test_should_show_job_in_progress(
assert page.find('p', {'class': 'hint'}).text.strip() == 'Report is 50% complete…'
@freeze_time("2016-01-01T00:00:00.061258")
def test_should_show_scheduled_job(
app_,
service_one,
@@ -162,7 +163,7 @@ def test_should_show_scheduled_job(
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.find('main').find_all('p')[2].text.strip() == 'Sending will start at midnight'
assert page.find('main').find_all('p')[2].text.strip() == 'Sending will start today at midnight'
assert page.find('input', {'type': 'submit', 'value': 'Cancel sending'})
@@ -266,7 +267,7 @@ def test_should_show_updates_for_one_job_as_json(
assert 'Status' in content['notifications']
assert 'Delivered' in content['notifications']
assert '12:01am' in content['notifications']
assert 'Uploaded by Test User on 1 January at midnight' in content['status']
assert 'Sent by Test User on 1 January at midnight' in content['status']
@pytest.mark.parametrize(

View File

@@ -17,30 +17,7 @@ def test_letters_access_restricted(logged_in_client, mocker, can_send_letters, r
assert response.status_code == response_code
@pytest.mark.parametrize('permission', [
'send_letters',
'manage_templates'
])
def test_letters_lets_in_with_permissions(
client,
mocker,
mock_login,
mock_has_permissions,
api_user_active,
permission,
):
service = service_json(can_send_letters=True)
mocker.patch('app.service_api_client.get_service', return_value={"data": service})
api_user_active._permissions[str(service['id'])] = [permission]
client.login(api_user_active)
response = client.get(url_for('main.letters', service_id=service['id']))
assert response.status_code == 200
def test_letters_rejects_without_permissions(
def test_letters_lets_in_without_permission(
client,
mocker,
mock_login,
@@ -53,4 +30,5 @@ def test_letters_rejects_without_permissions(
client.login(api_user_active)
response = client.get(url_for('main.letters', service_id=service['id']))
assert api_user_active.permissions == {}
assert response.status_code == 200

View File

@@ -24,3 +24,21 @@ def test_cant_see_letters_if_not_allowed(logged_in_client, mocker):
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert 'Letter templates' not in page.find('nav', class_='navigation').text
def test_can_see_letters_without_permissions(
client,
mocker,
mock_login,
mock_has_permissions,
api_user_active
):
service = service_json(can_send_letters=True)
mocker.patch('app.service_api_client.get_service', return_value={"data": service})
client.login(api_user_active)
response = client.get(url_for('main.service_settings', service_id=service['id']))
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert 'Letter templates' in page.find('nav', class_='navigation').text

View File

@@ -60,8 +60,10 @@ def test_should_show_research_and_restricted_mode(
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
# 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_should_render_platform_admin_page(
@@ -141,7 +143,7 @@ def create_stats(
}
def test_format_stats_by_service_sums_values_for_sending(fake_uuid):
def test_format_stats_by_service_returns_correct_values(fake_uuid):
services = [service_json(fake_uuid, 'a', [])]
services[0]['statistics'] = create_stats(
emails_requested=10,
@@ -153,11 +155,65 @@ def test_format_stats_by_service_sums_values_for_sending(fake_uuid):
)
ret = list(format_stats_by_service(services))
assert len(ret) == 1
assert ret[0]['sending'] == 34
assert ret[0]['delivered'] == 10
assert ret[0]['failed'] == 16
assert ret[0]['stats']['email']['requested'] == 10
assert ret[0]['stats']['email']['delivered'] == 3
assert ret[0]['stats']['email']['failed'] == 5
assert ret[0]['stats']['sms']['requested'] == 50
assert ret[0]['stats']['sms']['delivered'] == 7
assert ret[0]['stats']['sms']['failed'] == 11
@pytest.mark.parametrize('restricted, table_index, research_mode', [
(True, 1, False),
(False, 0, False)
])
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('td')[2:]
sms_stats = service_row_group[1].find_all('td')[2:]
email_sending, email_delivered, email_failed = [int(stat.text.split()[0]) for stat in email_stats]
sms_sending, sms_delivered, sms_failed = [int(stat.text.split()[0]) for stat in sms_stats]
assert email_sending == 10
assert email_delivered == 3
assert email_failed == 5
assert sms_sending == 50
assert sms_delivered == 7
assert sms_failed == 11
@pytest.mark.parametrize('restricted, table_index', [

View File

@@ -315,11 +315,12 @@ def test_should_redirect_after_request_to_go_live(
response = client.post(
url_for('main.service_request_to_go_live', service_id='6ce466d0-fd6a-11e5-82f5-e0accb9d11a6'),
data={
'channel': 'Email',
'mou': 'yes',
'channel': 'emails',
'start_date': '01/01/2017',
'start_volume': '100,000',
'peak_volume': '2,000,000',
'upload_or_api': 'api'
'upload_or_api': 'API'
},
follow_redirects=True
)
@@ -338,11 +339,11 @@ def test_should_redirect_after_request_to_go_live(
)
returned_message = mock_post.call_args[1]['data']['message']
assert 'Email' in returned_message
assert 'emails' in returned_message
assert '01/01/2017' in returned_message
assert '100,000' in returned_message
assert '2,000,000' in returned_message
assert 'api' in returned_message
assert 'API' in returned_message
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
flash_banner = page.find('div', class_='banner-default').string.strip()
@@ -377,11 +378,12 @@ def test_log_error_on_request_to_go_live(
resp = client.post(
url_for('main.service_request_to_go_live', service_id='6ce466d0-fd6a-11e5-82f5-e0accb9d11a6'),
data={
'channel': 'channel',
'mou': 'yes',
'channel': 'emails',
'start_date': 'start_date',
'start_volume': 'start_volume',
'peak_volume': 'peak_volume',
'upload_or_api': 'upload_or_api'
'upload_or_api': 'API'
}
)
mock_logger.assert_called_with(

View File

@@ -32,8 +32,8 @@ def test_should_show_name_page(app_,
def test_should_redirect_after_name_change(app_,
api_user_active,
mock_login,
mock_update_user,
mock_get_user):
mock_get_user,
mock_update_user_attribute):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
@@ -46,7 +46,7 @@ def test_should_redirect_after_name_change(app_,
assert response.location == url_for(
'main.user_profile', _external=True)
api_user_active.name = new_name
assert mock_update_user.called
assert mock_update_user_attribute.called
def test_should_show_email_page(app_,
@@ -116,7 +116,8 @@ def test_should_render_change_email_continue_after_authenticate_email(app_,
def test_should_redirect_to_user_profile_when_user_confirms_email_link(app_,
api_user_active,
mock_login
mock_login,
mock_update_user_attribute
):
with app_.test_request_context():
with app_.test_client() as client:
@@ -218,6 +219,7 @@ def test_should_redirect_after_mobile_number_confirm(app_,
api_user_active,
mock_login,
mock_get_user,
mock_update_user_attribute,
mock_check_verify_code):
with app_.test_request_context():
with app_.test_client() as client:

View File

@@ -1,3 +1,5 @@
import pytest
from app.notify_client.user_api_client import UserApiClient
@@ -13,3 +15,10 @@ def test_client_uses_correct_find_by_email(mocker, api_user_active):
client.get_user_by_email(api_user_active.email_address)
mock_get.assert_called_once_with(expected_url, params=expected_params)
def test_client_only_updates_allowed_attributes(mocker):
mocker.patch('app.notify_client.current_user', id='1')
with pytest.raises(TypeError) as error:
UserApiClient().update_user_attribute('user_id', id='1')
assert str(error.value) == 'Not allowed to update user attributes: id'