Improvements to the tests.

Update AuthError with a to_dict_v2 method.
This commit is contained in:
Rebecca Law
2016-11-01 10:33:34 +00:00
parent a358f3cb3a
commit 482d10545b
8 changed files with 58 additions and 46 deletions

View File

@@ -30,8 +30,9 @@ def test_create_content_for_notification_fails_with_missing_personalisation(samp
def test_create_content_for_notification_fails_with_additional_personalisation(sample_template_with_placeholders):
template = Template.query.get(sample_template_with_placeholders.id)
with pytest.raises(BadRequestError):
create_content_for_notification(template, {'name': 'Bobbhy', 'Additional': 'Data'})
with pytest.raises(BadRequestError) as e:
create_content_for_notification(template, {'name': 'Bobby', 'Additional placeholder': 'Data'})
assert e.value.message == 'Template personalisation not needed for template: Additional placeholder'
def test_persist_notification_creates_and_save_to_db(sample_template, sample_api_key):
@@ -41,7 +42,7 @@ def test_persist_notification_creates_and_save_to_db(sample_template, sample_api
sample_template.service.id, {}, 'sms', sample_api_key.id,
sample_api_key.key_type)
assert Notification.query.count() == 1
assert Notification.query.get(notification.id).__eq__(notification)
assert Notification.query.get(notification.id) is not None
assert NotificationHistory.query.count() == 1

View File

@@ -97,17 +97,15 @@ def test_service_can_send_to_recipient_passes_for_live_service_non_team_member(k
live_service) is None
@pytest.mark.parametrize('key_type',
['team'])
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(key_type, notify_db, notify_db_session,
def test_service_can_send_to_recipient_passes_for_whitelisted_recipient_passes(notify_db, notify_db_session,
sample_service):
sample_service_whitelist(notify_db, notify_db_session, email_address="some_other_email@test.com")
assert service_can_send_to_recipient("some_other_email@test.com",
key_type,
'team',
sample_service) is None
sample_service_whitelist(notify_db, notify_db_session, mobile_number='07513332413')
assert service_can_send_to_recipient('07513332413',
key_type,
'team',
sample_service) is None
@@ -120,9 +118,9 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recip
notify_db, notify_db_session):
trial_mode_service = create_service(notify_db, notify_db_session, service_name='trial mode', restricted=True)
with pytest.raises(BadRequestError) as exec_info:
assert service_can_send_to_recipient(recipient,
key_type,
trial_mode_service) is None
service_can_send_to_recipient(recipient,
key_type,
trial_mode_service)
assert exec_info.value.status_code == 400
assert exec_info.value.code == 10400
assert exec_info.value.message == error_message
@@ -133,9 +131,9 @@ def test_service_can_send_to_recipient_fails_when_recipient_is_not_on_team(recip
def test_service_can_send_to_recipient_fails_when_mobile_number_is_not_on_team(notify_db, notify_db_session):
live_service = create_service(notify_db, notify_db_session, service_name='live mode', restricted=False)
with pytest.raises(BadRequestError) as e:
assert service_can_send_to_recipient("0758964221",
'team',
live_service) is None
service_can_send_to_recipient("0758964221",
'team',
live_service)
assert e.value.status_code == 400
assert e.value.code == 10400
assert e.value.message == 'Cant send to this recipient using a team-only API key'

View File

@@ -25,16 +25,15 @@ def test_post_sms_schema_is_valid(input):
def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
j = {"template_id": "notUUID"}
try:
with pytest.raises(ValidationError) as e:
validate(j, post_sms_request)
except ValidationError as e:
error = json.loads(e.message)
assert "POST v2/notifications/sms" in error['message']
assert len(error.get('fields')) == 2
assert "phone_number" in e.message
assert "template_id" in e.message
assert error.get('code') == '1001'
assert error.get('link', None) is not None
error = json.loads(e.value.message)
assert "POST v2/notifications/sms" in error['message']
assert len(error.get('fields')) == 2
assert "'phone_number' is a required property" in error['fields']
assert "'template_id' not a valid UUID" in error['fields']
assert error.get('code') == '1001'
assert error.get('link', None) is not None
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
@@ -44,15 +43,14 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
"reference": "reference from caller",
"personalisation": "not_a_dict"
}
try:
with pytest.raises(ValidationError) as e:
validate(j, post_sms_request)
except ValidationError as e:
error = json.loads(e.message)
assert "POST v2/notifications/sms" in error['message']
assert len(error.get('fields')) == 1
assert "personalisation" in e.message
assert error.get('code') == '1001'
assert error.get('link', None) is not None
error = json.loads(e.value.message)
assert "POST v2/notifications/sms" in error['message']
assert len(error.get('fields')) == 1
assert error['fields'][0] == "'personalisation' should contain key value pairs"
assert error.get('code') == '1001'
assert error.get('link', None) is not None
valid_response = {
@@ -85,7 +83,6 @@ def test_post_sms_response_schema_is_valid(input):
def test_post_sms_response_schema_missing_uri():
j = valid_response
del j["uri"]
try:
with pytest.raises(ValidationError) as e:
validate(j, post_sms_response)
except ValidationError as e:
assert 'uri' in e.message
assert 'uri' in e.value.message

View File

@@ -2,6 +2,7 @@ import uuid
from flask import json
from app.models import Notification
from tests import create_authorization_header
@@ -22,10 +23,17 @@ def test_post_sms_notification_returns_201(notify_api, sample_template, mocker):
assert response.status_code == 201
resp_json = json.loads(response.get_data(as_text=True))
notifications = Notification.query.all()
assert len(notifications) == 1
notification_id = notifications[0].id
assert resp_json['id'] is not None
assert resp_json['reference'] is None
assert resp_json['content']['body'] == sample_template.content
assert resp_json['content']['from_number'] == sample_template.service.sms_sender
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
assert resp_json['template']['id'] == str(sample_template.id)
assert resp_json['template']['version'] == sample_template.version
assert 'v2/templates/{}'.format(sample_template.id) in resp_json['template']['uri']
assert mocked.called
@@ -70,7 +78,9 @@ def test_post_sms_notification_returns_403_and_well_formed_auth_error(notify_api
assert response.headers['Content-type'] == 'application/json'
error_resp = json.loads(response.get_data(as_text=True))
assert error_resp['code'] == 401
assert error_resp['message'] == {'token': ['Unauthorized, authentication token must be provided']}
assert error_resp['message'] == 'Unauthorized, authentication token must be provided'
assert error_resp['fields'] == {'token': ['Unauthorized, authentication token must be provided']}
assert error_resp['link'] == 'link to docs'
def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, sample_template):