Use client and logged_in_client fixtures

Wherever possible, because Don’t Repeat Yourself.
This commit is contained in:
Chris Hill-Scott
2017-02-06 10:44:38 +00:00
parent 929dc45224
commit f3b0c0a556
32 changed files with 2389 additions and 2897 deletions
+305 -346
View File
@@ -14,7 +14,7 @@ from app.main.views.templates import get_last_use_message, get_human_readable_de
def test_should_show_page_for_one_template(
app_,
client,
active_user_with_permissions,
mocker,
mock_get_service_template,
@@ -22,16 +22,13 @@ def test_should_show_page_for_one_template(
):
service = create_sample_service(active_user_with_permissions)
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
service_id = service['id']
template_id = fake_uuid
response = client.get(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id))
client.login(active_user_with_permissions, mocker, service)
service_id = service['id']
template_id = fake_uuid
response = client.get(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id))
assert response.status_code == 200
assert "Two week reminder" in response.get_data(as_text=True)
@@ -42,7 +39,7 @@ def test_should_show_page_for_one_template(
def test_should_show_page_template_with_priority_select_if_platform_admin(
app_,
logged_in_client,
platform_admin_user,
mocker,
mock_get_service_template,
@@ -50,16 +47,13 @@ def test_should_show_page_template_with_priority_select_if_platform_admin(
):
service = create_sample_service(platform_admin_user)
with app_.test_request_context():
with app_.test_client() as client:
client.login(platform_admin_user, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[platform_admin_user])
service_id = service['id']
template_id = fake_uuid
response = client.get(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id))
mocker.patch('app.user_api_client.get_users_for_service', return_value=[platform_admin_user])
service_id = service['id']
template_id = fake_uuid
response = logged_in_client.get(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id))
assert response.status_code == 200
assert "Two week reminder" in response.get_data(as_text=True)
@@ -84,7 +78,7 @@ def test_should_show_preview_letter_templates(
extra_view_args,
view_suffix,
expected_content_type,
client,
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
@@ -95,9 +89,8 @@ def test_should_show_preview_letter_templates(
fake_uuid,
mocker,
):
client.login(api_user_active)
service_id, template_id = repeat(fake_uuid, 2)
response = client.get(url_for(
response = logged_in_client.get(url_for(
'{}_{}'.format(view, view_suffix),
service_id=service_id,
template_id=template_id,
@@ -116,7 +109,7 @@ def test_should_show_preview_letter_templates(
def test_should_redirect_when_saving_a_template(
app_,
logged_in_client,
active_user_with_permissions,
mocker,
mock_get_service_template,
@@ -124,35 +117,32 @@ def test_should_redirect_when_saving_a_template(
fake_uuid,
):
service = create_sample_service(active_user_with_permissions)
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
name = "new name"
content = "template <em>content</em> with & entity"
data = {
'id': template_id,
'name': name,
'template_content': content,
'template_type': 'sms',
'service': service['id'],
'process_type': 'normal'
}
response = client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
name = "new name"
content = "template <em>content</em> with & entity"
data = {
'id': template_id,
'name': name,
'template_content': content,
'template_type': 'sms',
'service': service['id'],
'process_type': 'normal'
}
response = logged_in_client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.view_template', service_id=service['id'], template_id=template_id, _external=True)
mock_update_service_template.assert_called_with(
template_id, name, 'sms', content, service['id'], None, 'normal')
assert response.status_code == 302
assert response.location == url_for(
'.view_template', service_id=service['id'], template_id=template_id, _external=True)
mock_update_service_template.assert_called_with(
template_id, name, 'sms', content, service['id'], None, 'normal')
def test_should_edit_content_when_process_type_is_priority_not_platform_admin(
app_,
logged_in_client,
active_user_with_permissions,
mocker,
mock_get_service_template_with_priority,
@@ -160,39 +150,36 @@ def test_should_edit_content_when_process_type_is_priority_not_platform_admin(
fake_uuid,
):
service = create_sample_service(active_user_with_permissions)
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "new template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.view_template', service_id=service['id'], template_id=template_id, _external=True)
mock_update_service_template.assert_called_with(
template_id,
"new name",
'sms',
"new template <em>content</em> with & entity",
service['id'],
None,
'priority'
)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "new template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = logged_in_client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.view_template', service_id=service['id'], template_id=template_id, _external=True)
mock_update_service_template.assert_called_with(
template_id,
"new name",
'sms',
"new template <em>content</em> with & entity",
service['id'],
None,
'priority'
)
def test_should_403_when_edit_template_with_process_type_of_priority_for_non_platform_admin(
app_,
client,
active_user_with_permissions,
mocker,
mock_get_service_template,
@@ -200,29 +187,27 @@ def test_should_403_when_edit_template_with_process_type_of_priority_for_non_pla
fake_uuid,
):
service = create_sample_service(active_user_with_permissions)
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
assert response.status_code == 403
mock_update_service_template.called == 0
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = client.post(url_for(
'.edit_service_template',
service_id=service['id'],
template_id=template_id), data=data)
assert response.status_code == 403
mock_update_service_template.called == 0
def test_should_403_when_create_template_with_process_type_of_priority_for_non_platform_admin(
app_,
client,
active_user_with_permissions,
mocker,
mock_get_service_template,
@@ -230,29 +215,27 @@ def test_should_403_when_create_template_with_process_type_of_priority_for_non_p
fake_uuid,
):
service = create_sample_service(active_user_with_permissions)
with app_.test_request_context():
with app_.test_client() as client:
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = client.post(url_for(
'.add_service_template',
service_id=service['id'],
template_type='sms'), data=data)
assert response.status_code == 403
mock_update_service_template.called == 0
client.login(active_user_with_permissions, mocker, service)
mocker.patch('app.user_api_client.get_users_for_service', return_value=[active_user_with_permissions])
template_id = fake_uuid
data = {
'id': template_id,
'name': "new name",
'template_content': "template <em>content</em> with & entity",
'template_type': 'sms',
'service': service['id'],
'process_type': 'priority'
}
response = client.post(url_for(
'.add_service_template',
service_id=service['id'],
template_type='sms'), data=data)
assert response.status_code == 403
mock_update_service_template.called == 0
def test_should_show_interstitial_when_making_breaking_change(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service_email_template,
@@ -263,41 +246,38 @@ def test_should_show_interstitial_when_making_breaking_change(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
response = client.post(
url_for('.edit_service_template', service_id=service_id, template_id=template_id),
data={
'id': template_id,
'name': "new name",
'template_content': "hello",
'template_type': 'email',
'subject': 'reminder',
'service': service_id,
'process_type': 'normal'
}
)
service_id = fake_uuid
template_id = fake_uuid
response = logged_in_client.post(
url_for('.edit_service_template', service_id=service_id, template_id=template_id),
data={
'id': template_id,
'name': "new name",
'template_content': "hello",
'template_type': 'email',
'subject': 'reminder',
'service': service_id,
'process_type': 'normal'
}
)
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.string.strip() == "Confirm changes"
assert page.find('a', {'class': 'page-footer-back-link'})['href'] == url_for(".edit_service_template",
service_id=service_id,
template_id=template_id)
for key, value in {
'name': 'new name',
'subject': 'reminder',
'template_content': 'hello',
'confirm': 'true'
}.items():
assert page.find('input', {'name': key})['value'] == value
assert response.status_code == 200
page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser')
assert page.h1.string.strip() == "Confirm changes"
assert page.find('a', {'class': 'page-footer-back-link'})['href'] == url_for(".edit_service_template",
service_id=service_id,
template_id=template_id)
for key, value in {
'name': 'new name',
'subject': 'reminder',
'template_content': 'hello',
'confirm': 'true'
}.items():
assert page.find('input', {'name': key})['value'] == value
def test_should_not_create_too_big_template(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service_template,
@@ -308,30 +288,27 @@ def test_should_not_create_too_big_template(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_type = 'sms'
data = {
'name': "new name",
'template_content': "template content",
'template_type': template_type,
'service': service_id,
'process_type': 'normal'
}
resp = client.post(url_for(
'.add_service_template',
service_id=service_id,
template_type=template_type
), data=data)
service_id = fake_uuid
template_type = 'sms'
data = {
'name': "new name",
'template_content': "template content",
'template_type': template_type,
'service': service_id,
'process_type': 'normal'
}
resp = logged_in_client.post(url_for(
'.add_service_template',
service_id=service_id,
template_type=template_type
), data=data)
assert resp.status_code == 200
assert "Content has a character count greater than the limit of 459" in resp.get_data(as_text=True)
assert resp.status_code == 200
assert "Content has a character count greater than the limit of 459" in resp.get_data(as_text=True)
def test_should_not_update_too_big_template(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service_template,
@@ -342,30 +319,27 @@ def test_should_not_update_too_big_template(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
data = {
'id': fake_uuid,
'name': "new name",
'template_content': "template content",
'service': service_id,
'template_type': 'sms',
'process_type': 'normal'
}
resp = client.post(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id), data=data)
service_id = fake_uuid
template_id = fake_uuid
data = {
'id': fake_uuid,
'name': "new name",
'template_content': "template content",
'service': service_id,
'template_type': 'sms',
'process_type': 'normal'
}
resp = logged_in_client.post(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id), data=data)
assert resp.status_code == 200
assert "Content has a character count greater than the limit of 459" in resp.get_data(as_text=True)
assert resp.status_code == 200
assert "Content has a character count greater than the limit of 459" in resp.get_data(as_text=True)
def test_should_redirect_when_saving_a_template_email(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service_email_template,
@@ -376,39 +350,36 @@ def test_should_redirect_when_saving_a_template_email(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
name = "new name"
content = "template <em>content</em> with & entity ((thing)) ((date))"
subject = "subject"
data = {
'id': template_id,
'name': name,
'template_content': content,
'template_type': 'email',
'service': service_id,
'subject': subject,
'process_type': 'normal'
}
response = client.post(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.view_template',
service_id=service_id,
template_id=template_id,
_external=True)
mock_update_service_template.assert_called_with(
template_id, name, 'email', content, service_id, subject, 'normal')
service_id = fake_uuid
template_id = fake_uuid
name = "new name"
content = "template <em>content</em> with & entity ((thing)) ((date))"
subject = "subject"
data = {
'id': template_id,
'name': name,
'template_content': content,
'template_type': 'email',
'service': service_id,
'subject': subject,
'process_type': 'normal'
}
response = logged_in_client.post(url_for(
'.edit_service_template',
service_id=service_id,
template_id=template_id), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.view_template',
service_id=service_id,
template_id=template_id,
_external=True)
mock_update_service_template.assert_called_with(
template_id, name, 'email', content, service_id, subject, 'normal')
def test_should_show_delete_template_page_with_time_block(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
@@ -419,23 +390,20 @@ def test_should_show_delete_template_page_with_time_block(
fake_uuid,
mocker,
):
with app_.test_request_context():
with app_.test_client() as client:
with freeze_time('2012-01-01 12:00:00'):
template = template_json('1234', '1234', "Test template", "sms", "Something very interesting")
notification = single_notification_json('1234', template=template)
with freeze_time('2012-01-01 12:00:00'):
template = template_json('1234', '1234', "Test template", "sms", "Something very interesting")
notification = single_notification_json('1234', template=template)
mocker.patch('app.template_statistics_client.get_template_statistics_for_template',
return_value=notification)
mocker.patch('app.template_statistics_client.get_template_statistics_for_template',
return_value=notification)
with freeze_time('2012-01-01 12:10:00'):
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
response = client.get(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id))
with freeze_time('2012-01-01 12:10:00'):
service_id = fake_uuid
template_id = fake_uuid
response = logged_in_client.get(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id))
content = response.get_data(as_text=True)
assert response.status_code == 200
assert 'Test template was last used 10 minutes ago. Are you sure you want to delete it?' in content
@@ -446,7 +414,7 @@ def test_should_show_delete_template_page_with_time_block(
def test_should_show_delete_template_page_with_never_used_block(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
@@ -457,32 +425,28 @@ def test_should_show_delete_template_page_with_never_used_block(
fake_uuid,
mocker,
):
with app_.test_request_context():
with app_.test_client() as client:
mocker.patch(
'app.template_statistics_client.get_template_statistics_for_template',
side_effect=HTTPError(response=Mock(status_code=404), message="Default message")
)
mocker.patch(
'app.template_statistics_client.get_template_statistics_for_template',
side_effect=HTTPError(response=Mock(status_code=404), message="Default message")
)
service_id = fake_uuid
template_id = fake_uuid
response = logged_in_client.get(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id))
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
response = client.get(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id))
content = response.get_data(as_text=True)
assert response.status_code == 200
assert 'Two week reminder has never been used. Are you sure you want to delete it?' in content
assert 'Are you sure' in content
assert 'Two week reminder' in content
assert 'Template &lt;em&gt;content&lt;/em&gt; with &amp; entity' in content
mock_get_service_template.assert_called_with(service_id, template_id)
content = response.get_data(as_text=True)
assert response.status_code == 200
assert 'Two week reminder has never been used. Are you sure you want to delete it?' in content
assert 'Are you sure' in content
assert 'Two week reminder' in content
assert 'Template &lt;em&gt;content&lt;/em&gt; with &amp; entity' in content
mock_get_service_template.assert_called_with(service_id, template_id)
def test_should_redirect_when_deleting_a_template(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
@@ -493,40 +457,37 @@ def test_should_redirect_when_deleting_a_template(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context():
with app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
name = "new name"
type_ = "sms"
content = "template content"
data = {
'id': str(template_id),
'name': name,
'template_type': type_,
'content': content,
'service': service_id
}
response = client.post(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id
), data=data)
service_id = fake_uuid
template_id = fake_uuid
name = "new name"
type_ = "sms"
content = "template content"
data = {
'id': str(template_id),
'name': name,
'template_type': type_,
'content': content,
'service': service_id
}
response = logged_in_client.post(url_for(
'.delete_service_template',
service_id=service_id,
template_id=template_id
), data=data)
assert response.status_code == 302
assert response.location == url_for(
'.choose_template',
service_id=service_id, template_type=type_, _external=True)
mock_get_service_template.assert_called_with(
service_id, template_id)
mock_delete_service_template.assert_called_with(
service_id, template_id)
assert response.status_code == 302
assert response.location == url_for(
'.choose_template',
service_id=service_id, template_type=type_, _external=True)
mock_get_service_template.assert_called_with(
service_id, template_id)
mock_delete_service_template.assert_called_with(
service_id, template_id)
@freeze_time('2016-01-01T15:00')
def test_should_show_page_for_a_deleted_template(
app_,
logged_in_client,
api_user_active,
mock_login,
mock_get_service,
@@ -536,25 +497,23 @@ def test_should_show_page_for_a_deleted_template(
mock_has_permissions,
fake_uuid,
):
with app_.test_request_context(), app_.test_client() as client:
client.login(api_user_active)
service_id = fake_uuid
template_id = fake_uuid
response = client.get(url_for(
'.view_template',
service_id=service_id,
template_id=template_id
))
service_id = fake_uuid
template_id = fake_uuid
response = logged_in_client.get(url_for(
'.view_template',
service_id=service_id,
template_id=template_id
))
assert response.status_code == 200
assert response.status_code == 200
content = response.get_data(as_text=True)
assert url_for("main.edit_service_template", service_id=fake_uuid, template_id=fake_uuid) not in content
assert url_for("main.send_from_api", service_id=fake_uuid, template_id=fake_uuid) not in content
assert url_for("main.send_test", service_id=fake_uuid, template_id=fake_uuid) not in content
assert "This template was deleted<br/>1 January 2016" in content
content = response.get_data(as_text=True)
assert url_for("main.edit_service_template", service_id=fake_uuid, template_id=fake_uuid) not in content
assert url_for("main.send_from_api", service_id=fake_uuid, template_id=fake_uuid) not in content
assert url_for("main.send_test", service_id=fake_uuid, template_id=fake_uuid) not in content
assert "This template was deleted<br/>1 January 2016" in content
mock_get_deleted_template.assert_called_with(service_id, template_id)
mock_get_deleted_template.assert_called_with(service_id, template_id)
@pytest.mark.parametrize('route', [
@@ -566,49 +525,49 @@ def test_route_permissions(
route,
mocker,
app_,
client,
api_user_active,
service_one,
mock_get_service_template,
mock_get_template_statistics_for_template,
fake_uuid,
):
with app_.test_request_context():
validate_route_permission(
mocker,
app_,
"GET",
200,
url_for(
route,
service_id=service_one['id'],
template_type='sms',
template_id=fake_uuid),
['manage_templates'],
api_user_active,
service_one)
validate_route_permission(
mocker,
app_,
"GET",
200,
url_for(
route,
service_id=service_one['id'],
template_type='sms',
template_id=fake_uuid),
['manage_templates'],
api_user_active,
service_one)
def test_route_permissions_for_choose_template(
mocker,
app_,
client,
api_user_active,
service_one,
mock_get_service_templates,
):
mocker.patch('app.job_api_client.get_job')
with app_.test_request_context():
validate_route_permission(
mocker,
app_,
"GET",
200,
url_for(
'main.choose_template',
service_id=service_one['id'],
template_type='sms'),
['view_activity'],
api_user_active,
service_one)
validate_route_permission(
mocker,
app_,
"GET",
200,
url_for(
'main.choose_template',
service_id=service_one['id'],
template_type='sms'),
['view_activity'],
api_user_active,
service_one)
@pytest.mark.parametrize('route', [
@@ -620,26 +579,26 @@ def test_route_invalid_permissions(
route,
mocker,
app_,
client,
api_user_active,
service_one,
mock_get_service_template,
mock_get_template_statistics_for_template,
fake_uuid,
):
with app_.test_request_context():
validate_route_permission(
mocker,
app_,
"GET",
403,
url_for(
route,
service_id=service_one['id'],
template_type='sms',
template_id=fake_uuid),
['view_activity'],
api_user_active,
service_one)
validate_route_permission(
mocker,
app_,
"GET",
403,
url_for(
route,
service_id=service_one['id'],
template_type='sms',
template_id=fake_uuid),
['view_activity'],
api_user_active,
service_one)
def test_get_last_use_message_returns_no_template_message():