Merge branch 'master' into remove-initial-update-sms-sender

This commit is contained in:
Rebecca Law
2017-11-14 16:27:10 +00:00
36 changed files with 1113 additions and 157 deletions

View File

@@ -2,11 +2,13 @@ from datetime import datetime, timedelta
from functools import partial
from unittest.mock import call, patch, PropertyMock
import functools
from flask import current_app
import pytest
from freezegun import freeze_time
from app import db
from app.celery import scheduled_tasks
from app.celery.scheduled_tasks import (
check_job_status,
@@ -30,7 +32,8 @@ from app.celery.scheduled_tasks import (
send_total_sent_notifications_to_performance_platform,
switch_current_sms_provider_on_slow_delivery,
timeout_job_statistics,
timeout_notifications
timeout_notifications,
daily_stats_template_usage_by_month
)
from app.clients.performance_platform.performance_platform_client import PerformancePlatformClient
from app.config import QueueNames, TaskNames
@@ -41,22 +44,28 @@ from app.dao.provider_details_dao import (
get_current_provider
)
from app.models import (
Service, Template,
SMS_TYPE, LETTER_TYPE,
MonthlyBilling,
NotificationHistory,
Service,
StatsTemplateUsageByMonth,
Template,
JOB_STATUS_READY_TO_SEND,
JOB_STATUS_IN_PROGRESS,
JOB_STATUS_SENT_TO_DVLA,
NOTIFICATION_PENDING,
NOTIFICATION_CREATED,
KEY_TYPE_TEST,
MonthlyBilling
LETTER_TYPE,
NOTIFICATION_CREATED,
NOTIFICATION_PENDING,
SMS_TYPE
)
from app.utils import get_london_midnight_in_utc
from app.v2.errors import JobIncompleteError
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
from tests.app.conftest import (
sample_job as create_sample_job,
sample_notification_history as create_notification_history,
sample_template as create_sample_template,
create_custom_template,
datetime_in_past
)
@@ -834,3 +843,148 @@ def test_check_job_status_task_raises_job_incomplete_error_for_multiple_jobs(moc
args=([str(job.id), str(job_2.id)],),
queue=QueueNames.JOBS
)
def test_daily_stats_template_usage_by_month(notify_db, notify_db_session):
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered'
)
template_one = create_sample_template(notify_db, notify_db_session)
template_two = create_sample_template(notify_db, notify_db_session)
notification_history(created_at=datetime(2017, 10, 1), sample_template=template_one)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime.now(), sample_template=template_two)
daily_stats_template_usage_by_month()
result = db.session.query(
StatsTemplateUsageByMonth
).order_by(
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.month
).all()
assert len(result) == 2
assert result[0].template_id == template_two.id
assert result[0].month == 4
assert result[0].year == 2016
assert result[0].count == 2
assert result[1].template_id == template_one.id
assert result[1].month == 10
assert result[1].year == 2017
assert result[1].count == 1
def test_daily_stats_template_usage_by_month_no_data():
daily_stats_template_usage_by_month()
results = db.session.query(StatsTemplateUsageByMonth).all()
assert len(results) == 0
def test_daily_stats_template_usage_by_month_multiple_runs(notify_db, notify_db_session):
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered'
)
template_one = create_sample_template(notify_db, notify_db_session)
template_two = create_sample_template(notify_db, notify_db_session)
notification_history(created_at=datetime(2017, 11, 1), sample_template=template_one)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime.now(), sample_template=template_two)
daily_stats_template_usage_by_month()
template_three = create_sample_template(notify_db, notify_db_session)
notification_history(created_at=datetime(2017, 10, 1), sample_template=template_three)
notification_history(created_at=datetime(2017, 9, 1), sample_template=template_three)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime.now(), sample_template=template_two)
daily_stats_template_usage_by_month()
result = db.session.query(
StatsTemplateUsageByMonth
).order_by(
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.month
).all()
assert len(result) == 4
assert result[0].template_id == template_two.id
assert result[0].month == 4
assert result[0].year == 2016
assert result[0].count == 4
assert result[1].template_id == template_three.id
assert result[1].month == 9
assert result[1].year == 2017
assert result[1].count == 1
assert result[2].template_id == template_three.id
assert result[2].month == 10
assert result[2].year == 2017
assert result[2].count == 1
assert result[3].template_id == template_one.id
assert result[3].month == 11
assert result[3].year == 2017
assert result[3].count == 1
def test_dao_fetch_monthly_historical_stats_by_template_null_template_id_not_counted(notify_db, notify_db_session):
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered'
)
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
history = notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one)
NotificationHistory.query.filter(
NotificationHistory.id == history.id
).update(
{
'template_id': None
}
)
daily_stats_template_usage_by_month()
result = db.session.query(
StatsTemplateUsageByMonth
).all()
assert len(result) == 0
notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one)
daily_stats_template_usage_by_month()
result = db.session.query(
StatsTemplateUsageByMonth
).order_by(
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.month
).all()
assert len(result) == 1

View File

@@ -47,6 +47,7 @@ from tests.app.db import (
create_api_key,
create_inbound_number,
create_letter_contact,
create_inbound_sms,
)
@@ -1032,6 +1033,11 @@ def sample_inbound_numbers(notify_db, notify_db_session, sample_service):
return inbound_numbers
@pytest.fixture
def sample_inbound_sms(notify_db, notify_db_session, sample_service):
return create_inbound_sms(sample_service)
@pytest.fixture
def restore_provider_details(notify_db, notify_db_session):
"""

View File

@@ -6,7 +6,8 @@ from app.dao.inbound_sms_dao import (
dao_get_inbound_sms_for_service,
dao_count_inbound_sms_for_service,
delete_inbound_sms_created_more_than_a_week_ago,
dao_get_inbound_sms_by_id
dao_get_inbound_sms_by_id,
dao_get_paginated_inbound_sms_for_service
)
from tests.app.db import create_inbound_sms, create_service
@@ -89,9 +90,83 @@ def test_should_not_delete_inbound_sms_before_seven_days(sample_service):
assert len(InboundSms.query.all()) == 2
def test_get_inbound_sms_by_id_returns(sample_service):
inbound = create_inbound_sms(sample_service)
def test_get_inbound_sms_by_id_returns(sample_inbound_sms):
inbound_from_db = dao_get_inbound_sms_by_id(sample_inbound_sms.service.id, sample_inbound_sms.id)
inbound_from_db = dao_get_inbound_sms_by_id(sample_service.id, inbound.id)
assert sample_inbound_sms == inbound_from_db
assert inbound == inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service(sample_inbound_sms):
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_inbound_sms.service.id)
assert sample_inbound_sms == inbound_from_db[0]
def test_dao_get_paginated_inbound_sms_for_service_return_only_for_service(sample_inbound_sms):
another_service = create_service(service_name='another service')
another_inbound_sms = create_inbound_sms(another_service)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_inbound_sms.service.id)
assert sample_inbound_sms in inbound_from_db
assert another_inbound_sms not in inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service_no_inbound_sms_returns_empty_list(sample_service):
inbound_from_db = dao_get_paginated_inbound_sms_for_service(sample_service.id)
assert inbound_from_db == []
def test_dao_get_paginated_inbound_sms_for_service_page_size_returns_correct_size(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
assert len(inbound_from_db) == 2
def test_dao_get_paginated_inbound_sms_for_service_older_than_returns_correct_list(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
expected_inbound_sms = reversed_inbound_sms[2:]
assert expected_inbound_sms == inbound_from_db
def test_dao_get_paginated_inbound_sms_for_service_older_than_end_returns_empty_list(sample_service):
inbound_sms_list = [
create_inbound_sms(sample_service),
create_inbound_sms(sample_service),
]
reversed_inbound_sms = sorted(inbound_sms_list, key=lambda sms: sms.created_at, reverse=True)
inbound_from_db = dao_get_paginated_inbound_sms_for_service(
sample_service.id,
older_than=reversed_inbound_sms[1].id,
page_size=2
)
assert inbound_from_db == []

View File

@@ -31,7 +31,8 @@ from app.dao.services_dao import (
dao_suspend_service,
dao_resume_service,
dao_fetch_active_users_for_service,
dao_fetch_service_by_inbound_number
dao_fetch_service_by_inbound_number,
dao_fetch_monthly_historical_stats_by_template
)
from app.dao.service_permissions_dao import dao_add_service_permission, dao_remove_service_permission
from app.dao.users_dao import save_model_user
@@ -1005,3 +1006,34 @@ def _assert_service_permissions(service_permissions, expected):
assert len(service_permissions) == len(expected)
assert set(expected) == set(p.permission for p in service_permissions)
def test_dao_fetch_monthly_historical_stats_by_template(notify_db, notify_db_session):
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered'
)
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
template_two = create_sample_template(notify_db, notify_db_session, template_name='2')
notification_history(created_at=datetime(2017, 10, 1), sample_template=template_one)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime(2016, 4, 1), sample_template=template_two)
notification_history(created_at=datetime.now(), sample_template=template_two)
result = sorted(dao_fetch_monthly_historical_stats_by_template(), key=lambda x: (x.month, x.year))
assert len(result) == 2
assert result[0].template_id == template_two.id
assert result[0].month == 4
assert result[0].year == 2016
assert result[0].count == 2
assert result[1].template_id == template_one.id
assert result[1].month == 10
assert result[1].year == 2017
assert result[1].count == 1

View File

@@ -0,0 +1,45 @@
import pytest
from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template
from app.models import StatsTemplateUsageByMonth
from tests.app.conftest import sample_notification, sample_email_template, sample_template, sample_job, sample_service
def test_create_stats_for_template(notify_db_session, sample_template):
assert StatsTemplateUsageByMonth.query.count() == 0
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10)
stats_by_month = StatsTemplateUsageByMonth.query.filter(
StatsTemplateUsageByMonth.template_id == sample_template.id
).all()
assert len(stats_by_month) == 1
assert stats_by_month[0].template_id == sample_template.id
assert stats_by_month[0].month == 1
assert stats_by_month[0].year == 2017
assert stats_by_month[0].count == 10
def test_update_stats_for_template(notify_db_session, sample_template):
assert StatsTemplateUsageByMonth.query.count() == 0
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 10)
insert_or_update_stats_for_template(sample_template.id, 1, 2017, 20)
insert_or_update_stats_for_template(sample_template.id, 2, 2017, 30)
stats_by_month = StatsTemplateUsageByMonth.query.filter(
StatsTemplateUsageByMonth.template_id == sample_template.id
).order_by(StatsTemplateUsageByMonth.template_id).all()
assert len(stats_by_month) == 2
assert stats_by_month[0].template_id == sample_template.id
assert stats_by_month[0].month == 1
assert stats_by_month[0].year == 2017
assert stats_by_month[0].count == 20
assert stats_by_month[1].template_id == sample_template.id
assert stats_by_month[1].month == 2
assert stats_by_month[1].year == 2017
assert stats_by_month[1].count == 30

View File

@@ -33,9 +33,7 @@ def test_get_inbound_sms_with_no_params(client, sample_service):
'service_id',
'notify_number',
'user_number',
'content',
'provider_date',
'provider_reference'
'content'
}
@@ -178,9 +176,7 @@ def test_get_inbound_sms(admin_request, sample_service):
'service_id',
'notify_number',
'user_number',
'content',
'provider_date',
'provider_reference'
'content'
}

View File

@@ -12,6 +12,7 @@ from app.notifications.receive_notifications import (
create_inbound_sms_object,
strip_leading_forty_four,
has_inbound_sms_permissions,
unescape_string,
)
from app.models import InboundSms, EMAIL_TYPE, SMS_TYPE, INBOUND_SMS_TYPE
@@ -166,6 +167,36 @@ def test_format_mmg_message(message, expected_output):
assert format_mmg_message(message) == expected_output
@pytest.mark.parametrize('raw, expected', [
(
'😬',
'😬',
),
(
'1\\n2',
'1\n2',
),
(
'\\\'"\\\'',
'\'"\'',
),
(
"""
""",
"""
""",
),
(
'\x79 \\x79 \\\\x79', # we should never see the middle one
'y y \\x79',
),
])
def test_unescape_string(raw, expected):
assert unescape_string(raw) == expected
@pytest.mark.parametrize('provider_date, expected_output', [
('2017-01-21+11%3A56%3A11', datetime(2017, 1, 21, 11, 56, 11)),
('2017-05-21+11%3A56%3A11', datetime(2017, 5, 21, 10, 56, 11))

View File

@@ -374,7 +374,7 @@ def test_check_service_sms_sender_id_where_sms_sender_id_is_none(notification_ty
def test_check_service_sms_sender_id_where_sms_sender_id_is_found(sample_service):
sms_sender = create_service_sms_sender(service=sample_service, sms_sender='123456')
assert check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE) is None
assert check_service_sms_sender_id(sample_service.id, sms_sender.id, SMS_TYPE) == '123456'
def test_check_service_sms_sender_id_where_service_id_is_not_found(sample_service, fake_uuid):

View File

@@ -159,40 +159,53 @@ def test_create_user_missing_attribute_password(client, notify_db, notify_db_ses
assert {'password': ['Missing data for required field.']} == json_resp['message']
def test_put_user(client, sample_service):
"""
Tests PUT endpoint '/' to update a user.
"""
assert User.query.count() == 1
sample_user = sample_service.users[0]
sample_user.failed_login_count = 1
new_email = 'new@digital.cabinet-office.gov.uk'
def test_can_create_user_with_email_auth_and_no_mobile(admin_request, notify_db_session):
data = {
'name': sample_user.name,
'email_address': new_email,
'mobile_number': sample_user.mobile_number
'name': 'Test User',
'email_address': 'user@digital.cabinet-office.gov.uk',
'password': 'password',
'mobile_number': None,
'auth_type': EMAIL_AUTH_TYPE
}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.put(
url_for('user.update_user', user_id=sample_user.id),
data=json.dumps(data),
headers=headers)
assert resp.status_code == 200
assert User.query.count() == 1
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['email_address'] == new_email
expected_permissions = default_service_permissions
fetched = json_resp['data']
assert str(sample_user.id) == fetched['id']
assert sample_user.name == fetched['name']
assert sample_user.mobile_number == fetched['mobile_number']
assert new_email == fetched['email_address']
assert sample_user.state == fetched['state']
assert sorted(expected_permissions) == sorted(fetched['permissions'][str(sample_service.id)])
# password wasn't updated, so failed_login_count stays the same
assert sample_user.failed_login_count == 1
json_resp = admin_request.post('user.create_user', _data=data, _expected_status=201)
assert json_resp['data']['auth_type'] == EMAIL_AUTH_TYPE
assert json_resp['data']['mobile_number'] is None
def test_cannot_create_user_with_sms_auth_and_no_mobile(admin_request, notify_db_session):
data = {
'name': 'Test User',
'email_address': 'user@digital.cabinet-office.gov.uk',
'password': 'password',
'mobile_number': None,
'auth_type': SMS_AUTH_TYPE
}
json_resp = admin_request.post('user.create_user', _data=data, _expected_status=400)
assert json_resp['message'] == 'Mobile number must be set if auth_type is set to sms_auth'
def test_cannot_create_user_with_empty_strings(admin_request, notify_db_session):
data = {
'name': '',
'email_address': '',
'password': 'password',
'mobile_number': '',
'auth_type': EMAIL_AUTH_TYPE
}
resp = admin_request.post(
'user.create_user',
_data=data,
_expected_status=400
)
assert resp['message'] == {
'email_address': ['Not a valid email address'],
'mobile_number': ['Invalid phone number: Not enough digits'],
'name': ['Invalid name']
}
@pytest.mark.parametrize('user_attribute, user_value', [
@@ -218,63 +231,6 @@ def test_post_user_attribute(client, sample_user, user_attribute, user_value):
assert json_resp['data'][user_attribute] == user_value
def test_put_user_update_password(client, sample_service):
"""
Tests PUT endpoint '/' to update a user including their password.
"""
assert User.query.count() == 1
sample_user = sample_service.users[0]
new_password = '1234567890'
data = {
'name': sample_user.name,
'email_address': sample_user.email_address,
'mobile_number': sample_user.mobile_number,
'password': new_password
}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.put(
url_for('user.update_user', user_id=sample_user.id),
data=json.dumps(data),
headers=headers)
assert resp.status_code == 200
assert User.query.count() == 1
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['data']['password_changed_at'] is not None
data = {'password': new_password}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.post(
url_for('user.verify_user_password', user_id=str(sample_user.id)),
data=json.dumps(data),
headers=headers)
assert resp.status_code == 204
def test_put_user_not_exists(client, sample_user, fake_uuid):
"""
Tests PUT endpoint '/' to update a user doesn't exist.
"""
assert User.query.count() == 1
new_email = 'new@digital.cabinet-office.gov.uk'
data = {'email_address': new_email}
auth_header = create_authorization_header()
headers = [('Content-Type', 'application/json'), auth_header]
resp = client.put(
url_for('user.update_user', user_id=fake_uuid),
data=json.dumps(data),
headers=headers)
assert resp.status_code == 404
assert User.query.count() == 1
user = User.query.filter_by(id=str(sample_user.id)).first()
json_resp = json.loads(resp.get_data(as_text=True))
assert json_resp['result'] == "error"
assert json_resp['message'] == 'No result found'
assert user == sample_user
assert user.email_address != new_email
def test_get_user_by_email(client, sample_service):
sample_user = sample_service.users[0]
header = create_authorization_header()
@@ -529,23 +485,20 @@ def test_update_user_password_saves_correctly(client, sample_service):
assert resp.status_code == 204
def test_update_user_resets_failed_login_count_if_updating_password(client, sample_service):
user = sample_service.users[0]
user.failed_login_count = 1
def test_activate_user(admin_request, sample_user):
sample_user.state = 'pending'
resp = client.put(
url_for('user.update_user', user_id=user.id),
data=json.dumps({
'name': user.name,
'email_address': user.email_address,
'mobile_number': user.mobile_number,
'password': 'foo'
}),
headers=[('Content-Type', 'application/json'), create_authorization_header()]
)
resp = admin_request.post('user.activate_user', user_id=sample_user.id)
assert resp.status_code == 200
assert user.failed_login_count == 0
assert resp['data']['id'] == str(sample_user.id)
assert resp['data']['state'] == 'active'
assert sample_user.state == 'active'
def test_activate_user_fails_if_already_active(admin_request, sample_user):
resp = admin_request.post('user.activate_user', user_id=sample_user.id, _expected_status=400)
assert resp['message'] == 'User already active'
assert sample_user.state == 'active'
def test_update_user_auth_type(admin_request, sample_user):
@@ -558,3 +511,66 @@ def test_update_user_auth_type(admin_request, sample_user):
assert resp['data']['id'] == str(sample_user.id)
assert resp['data']['auth_type'] == 'email_auth'
def test_can_set_email_auth_and_remove_mobile_at_same_time(admin_request, sample_user):
sample_user.auth_type = SMS_AUTH_TYPE
admin_request.post(
'user.update_user_attribute',
user_id=sample_user.id,
_data={
'mobile_number': None,
'auth_type': EMAIL_AUTH_TYPE,
}
)
assert sample_user.mobile_number is None
assert sample_user.auth_type == EMAIL_AUTH_TYPE
def test_cannot_remove_mobile_if_sms_auth(admin_request, sample_user):
sample_user.auth_type = SMS_AUTH_TYPE
json_resp = admin_request.post(
'user.update_user_attribute',
user_id=sample_user.id,
_data={'mobile_number': None},
_expected_status=400
)
assert json_resp['message'] == 'Mobile number must be set if auth_type is set to sms_auth'
def test_can_remove_mobile_if_email_auth(admin_request, sample_user):
sample_user.auth_type = EMAIL_AUTH_TYPE
admin_request.post(
'user.update_user_attribute',
user_id=sample_user.id,
_data={'mobile_number': None},
)
assert sample_user.mobile_number is None
def test_cannot_update_user_with_mobile_number_as_empty_string(admin_request, sample_user):
sample_user.auth_type = EMAIL_AUTH_TYPE
resp = admin_request.post(
'user.update_user_attribute',
user_id=sample_user.id,
_data={'mobile_number': ''},
_expected_status=400
)
assert resp['message']['mobile_number'] == ['Invalid phone number: Not enough digits']
def test_cannot_update_user_password_using_attributes_method(admin_request, sample_user):
resp = admin_request.post(
'user.update_user_attribute',
user_id=sample_user.id,
_data={'password': 'foo'},
_expected_status=400
)
assert resp['message']['_schema'] == ['Unknown field name password']

View File

View File

@@ -0,0 +1,154 @@
from flask import json, url_for
from tests import create_authorization_header
from tests.app.db import create_inbound_sms
def test_get_inbound_sms_returns_200(
client, sample_service
):
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='Hi'),
create_inbound_sms(service=sample_service, user_number='447700900112'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='Bye'),
create_inbound_sms(service=sample_service, user_number='07700900113')
]
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages',
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))['received_text_messages']
reversed_all_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
expected_response = [i.serialize() for i in reversed_all_inbound_sms]
assert json_response == expected_response
def test_get_inbound_sms_generate_page_links(client, sample_service, mocker):
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 2}
)
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='Hi'),
create_inbound_sms(service=sample_service, user_number='447700900111'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='End'),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(
url_for('v2_inbound_sms.get_inbound_sms'),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = [i.serialize() for i in reversed_inbound_sms[:2]]
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert url_for(
'v2_inbound_sms.get_inbound_sms',
older_than=reversed_inbound_sms[1].id,
_external=True) == json_response['links']['next']
def test_get_next_inbound_sms_will_get_correct_inbound_sms_list(client, sample_service, mocker):
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 2}
)
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900111', content='1'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='2'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='3'),
create_inbound_sms(service=sample_service, user_number='447700900111', content='4'),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(
path=url_for('v2_inbound_sms.get_inbound_sms', older_than=reversed_inbound_sms[1].id),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = [i.serialize() for i in reversed_inbound_sms[2:]]
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert url_for(
'v2_inbound_sms.get_inbound_sms',
older_than=reversed_inbound_sms[3].id,
_external=True) == json_response['links']['next']
def test_get_next_inbound_sms_at_end_will_return_empty_inbound_sms_list(client, sample_inbound_sms, mocker):
mocker.patch.dict(
"app.v2.inbound_sms.get_inbound_sms.current_app.config",
{"API_PAGE_SIZE": 1}
)
auth_header = create_authorization_header(service_id=sample_inbound_sms.service.id)
response = client.get(
path=url_for('v2_inbound_sms.get_inbound_sms', older_than=sample_inbound_sms.id),
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
json_response = json.loads(response.get_data(as_text=True))
expected_inbound_sms_list = []
assert json_response['received_text_messages'] == expected_inbound_sms_list
assert url_for(
'v2_inbound_sms.get_inbound_sms',
_external=True) == json_response['links']['current']
assert 'next' not in json_response['links'].keys()
def test_get_inbound_sms_for_no_inbound_sms_returns_empty_list(
client, sample_service
):
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages',
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 200
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))['received_text_messages']
expected_response = []
assert json_response == expected_response
def test_get_inbound_sms_with_invalid_query_string_returns_400(client, sample_service):
auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get(
path='/v2/received-text-messages?user_number=447700900000',
headers=[('Content-Type', 'application/json'), auth_header])
assert response.status_code == 400
assert response.headers['Content-type'] == 'application/json'
json_response = json.loads(response.get_data(as_text=True))
assert json_response['status_code'] == 400
assert json_response['errors'][0]['error'] == 'ValidationError'
assert json_response['errors'][0]['message'] == \
'Additional properties are not allowed (user_number was unexpected)'

View File

@@ -0,0 +1,92 @@
import pytest
from flask import json, url_for
from jsonschema.exceptions import ValidationError
from app.v2.inbound_sms.inbound_sms_schemas import (
get_inbound_sms_request,
get_inbound_sms_response,
get_inbound_sms_single_response
)
from app.schema_validation import validate
from tests import create_authorization_header
from tests.app.db import create_inbound_sms
valid_inbound_sms = {
"user_number": "447700900111",
"created_at": "2017-11-02T15:07:57.197546Z",
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
"notify_number": "testing",
"content": "Hello"
}
valid_inbound_sms_list = {
"received_text_messages": [valid_inbound_sms],
"links": {
"current": valid_inbound_sms["id"]
}
}
invalid_inbound_sms = {
"user_number": "447700900111",
"created_at": "2017-11-02T15:07:57.197546",
"service_id": "a5149c32-f03b-4711-af49-ad6993797d45",
"id": "342786aa-23ce-4695-9aad-7f79e68ee29a",
"notify_number": "testing"
}
invalid_inbound_sms_list = {
"received_text_messages": [invalid_inbound_sms]
}
def test_get_inbound_sms_contract(client, sample_service):
all_inbound_sms = [
create_inbound_sms(service=sample_service, user_number='447700900113'),
create_inbound_sms(service=sample_service, user_number='447700900112'),
create_inbound_sms(service=sample_service, user_number='447700900111'),
]
reversed_inbound_sms = sorted(all_inbound_sms, key=lambda sms: sms.created_at, reverse=True)
auth_header = create_authorization_header(service_id=all_inbound_sms[0].service_id)
response = client.get('/v2/received-text-messages', headers=[auth_header])
response_json = json.loads(response.get_data(as_text=True))
validated_resp = validate(response_json, get_inbound_sms_response)
assert validated_resp['received_text_messages'] == [i.serialize() for i in reversed_inbound_sms]
assert validated_resp['links']['current'] == url_for(
'v2_inbound_sms.get_inbound_sms', _external=True)
assert validated_resp['links']['next'] == url_for(
'v2_inbound_sms.get_inbound_sms', older_than=all_inbound_sms[0].id, _external=True)
@pytest.mark.parametrize('request_args', [
{'older_than': "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6"}, {}]
)
def test_valid_inbound_sms_request_json(client, request_args):
validate(request_args, get_inbound_sms_request)
def test_invalid_inbound_sms_request_json(client):
with pytest.raises(expected_exception=ValidationError):
validate({'user_number': '447700900111'}, get_inbound_sms_request)
def test_valid_inbound_sms_response_json():
assert validate(valid_inbound_sms, get_inbound_sms_single_response) == valid_inbound_sms
def test_valid_inbound_sms_list_response_json():
validate(valid_inbound_sms_list, get_inbound_sms_response)
def test_invalid_inbound_sms_response_json():
with pytest.raises(expected_exception=ValidationError):
validate(invalid_inbound_sms, get_inbound_sms_single_response)
def test_invalid_inbound_sms_list_response_json():
with pytest.raises(expected_exception=ValidationError):
validate(invalid_inbound_sms_list, get_inbound_sms_response)

View File

@@ -123,6 +123,7 @@ def test_post_sms_notification_returns_201_with_sms_sender_id(
notification_to_sms_sender = NotificationSmsSender.query.all()
assert len(notification_to_sms_sender) == 1
assert str(notification_to_sms_sender[0].notification_id) == resp_json['id']
assert resp_json['content']['from_number'] == sms_sender.sms_sender
assert notification_to_sms_sender[0].service_sms_sender_id == sms_sender.id
mocked.assert_called_once_with([resp_json['id']], queue='send-sms-tasks')