Merge branch 'master' into write-to-postgres-not-sqs

Conflicts:
	app/notifications/rest.py
	tests/app/notifications/rest/test_send_notification.py
This commit is contained in:
Martyn Inglis
2016-09-08 09:15:07 +01:00
3 changed files with 40 additions and 30 deletions

View File

@@ -52,7 +52,11 @@ NOTE: The SECRET_KEY and DANGEROUS_SALT should match those in the [notifications
NOTE: Also note the unique prefix for the queue names. This prevents clashing with others queues in shared amazon environment and using a prefix enables filtering by queue name in the SQS interface. NOTE: Also note the unique prefix for the queue names. This prevents clashing with others queues in shared amazon environment and using a prefix enables filtering by queue name in the SQS interface.
Install Postgresql
```shell
brew install postgres
```
## To run the application ## To run the application
@@ -73,7 +77,6 @@ scripts/run_celery_beat.sh
``` ```
## To test the application ## To test the application
First, ensure that `scripts/boostrap.sh` has been run, as it creates the test database. First, ensure that `scripts/boostrap.sh` has been run, as it creates the test database.

View File

@@ -45,7 +45,6 @@ from app.errors import (
register_errors, register_errors,
InvalidRequest InvalidRequest
) )
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
register_errors(notifications) register_errors(notifications)
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
@@ -195,16 +194,16 @@ def send_notification(notification_type):
service_id = str(api_user.service_id) service_id = str(api_user.service_id)
service = services_dao.dao_fetch_service_by_id(service_id) service = services_dao.dao_fetch_service_by_id(service_id)
service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
if all((api_user.key_type != KEY_TYPE_TEST, service_stats >= service.message_limit)):
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
raise InvalidRequest(error, status_code=429)
notification, errors = ( notification, errors = (
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
).load(request.get_json()) ).load(request.get_json())
if all((api_user.key_type != KEY_TYPE_TEST, service.restricted)):
service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
if service_stats >= service.message_limit:
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
raise InvalidRequest(error, status_code=429)
if errors: if errors:
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
@@ -233,8 +232,8 @@ def send_notification(notification_type):
raise InvalidRequest(errors, status_code=400) raise InvalidRequest(errors, status_code=400)
if ( if (
template_object.template_type == SMS_TYPE and template_object.template_type == SMS_TYPE and
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT') template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
): ):
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT') char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
message = 'Content has a character count greater than the limit of {}'.format(char_count) message = 'Content has a character count greater than the limit of {}'.format(char_count)

View File

@@ -550,19 +550,18 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
assert response_data['template_version'] == sample_email_template.version assert response_data['template_version'] == sample_email_template.version
@pytest.mark.parametrize('restricted', [True, False])
@freeze_time("2016-01-01 12:00:00.061258") @freeze_time("2016-01-01 12:00:00.061258")
def test_should_block_api_call_if_over_day_limit_for_restricted_and_live_service(notify_db, def test_should_not_block_api_call_if_over_day_limit_for_live_service(
notify_db_session, notify_db,
notify_api, notify_db_session,
mocker, notify_api,
restricted): mocker):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async') mocker.patch('app.celery.provider_tasks.send_email_to_provider.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=restricted) service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=False)
email_template = create_sample_email_template(notify_db, notify_db_session, service=service) email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
create_sample_notification( create_sample_notification(
notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow() notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow()
@@ -579,14 +578,17 @@ def test_should_block_api_call_if_over_day_limit_for_restricted_and_live_service
path='/notifications/email', path='/notifications/email',
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True)) json.loads(response.get_data(as_text=True))
assert response.status_code == 429 assert response.status_code == 201
assert 'Exceeded send limits (1) for today' in json_resp['message']
@freeze_time("2016-01-01 12:00:00.061258") @freeze_time("2016-01-01 12:00:00.061258")
def test_should_block_api_call_if_over_day_limit_regardless_of_type(notify_db, notify_db_session, notify_api, mocker): def test_should_block_api_call_if_over_day_limit_for_restricted_service(
notify_db,
notify_db_session,
notify_api,
mocker):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
@@ -594,41 +596,47 @@ def test_should_block_api_call_if_over_day_limit_regardless_of_type(notify_db, n
service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True) service = create_sample_service(notify_db, notify_db_session, limit=1, restricted=True)
email_template = create_sample_email_template(notify_db, notify_db_session, service=service) email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
sms_template = create_sample_template(notify_db, notify_db_session, service=service)
create_sample_notification( create_sample_notification(
notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow() notify_db, notify_db_session, template=email_template, service=service, created_at=datetime.utcnow()
) )
data = { data = {
'to': '+447234123123', 'to': 'ok@ok.com',
'template': str(sms_template.id) 'template': str(email_template.id)
} }
auth_header = create_authorization_header(service_id=service.id) auth_header = create_authorization_header(service_id=service.id)
response = client.post( response = client.post(
path='/notifications/sms', path='/notifications/email',
data=json.dumps(data), data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]) headers=[('Content-Type', 'application/json'), auth_header])
json_resp = json.loads(response.get_data(as_text=True)) json.loads(response.get_data(as_text=True))
assert response.status_code == 429 assert response.status_code == 429
assert 'Exceeded send limits (1) for today' in json_resp['message']
@pytest.mark.parametrize('restricted', [True, False])
@freeze_time("2016-01-01 12:00:00.061258") @freeze_time("2016-01-01 12:00:00.061258")
def test_should_allow_api_call_if_under_day_limit_regardless_of_type(notify_db, notify_db_session, notify_api, mocker): def test_should_allow_api_call_if_under_day_limit_regardless_of_type(
notify_db,
notify_db_session,
notify_api,
sample_user,
mocker,
restricted):
with notify_api.test_request_context(): with notify_api.test_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client:
mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async') mocker.patch('app.celery.provider_tasks.send_sms_to_provider.apply_async')
mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
service = create_sample_service(notify_db, notify_db_session, limit=2) service = create_sample_service(notify_db, notify_db_session, limit=2, restricted=restricted)
email_template = create_sample_email_template(notify_db, notify_db_session, service=service) email_template = create_sample_email_template(notify_db, notify_db_session, service=service)
sms_template = create_sample_template(notify_db, notify_db_session, service=service) sms_template = create_sample_template(notify_db, notify_db_session, service=service)
create_sample_notification(notify_db, notify_db_session, template=email_template, service=service) create_sample_notification(notify_db, notify_db_session, template=email_template, service=service)
data = { data = {
'to': '+447634123123', 'to': sample_user.mobile_number,
'template': str(sms_template.id) 'template': str(sms_template.id)
} }