Some code clean up.

Clean up the unit test.
This commit is contained in:
Rebecca Law
2016-01-28 11:06:24 +00:00
parent 42a4c8b0b1
commit a546665188
2 changed files with 62 additions and 85 deletions

View File

@@ -23,33 +23,17 @@ def get_notifications(notification_id):
return jsonify(notify_alpha_client.fetch_notification_by_id(notification_id)), 200
def _add_notification_to_queue(template_id, service, msg_type, to):
q = boto3.resource('sqs', region_name=current_app.config['AWS_REGION']).create_queue(
QueueName=str(service.queue_name))
import uuid
notification = json.dumps({'message_id': str(uuid.uuid4()),
'service_id': service.id,
'to': to,
'message_type': msg_type,
'template_id': template_id})
serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
encrypted = serializer.dumps(notification, current_app.config.get('DANGEROUS_SALT'))
q.send_message(MessageBody=encrypted,
MessageAttributes={'type': {'StringValue': msg_type, 'DataType': 'String'},
'service_id': {'StringValue': str(service.id), 'DataType': 'String'},
'template_id': {'StringValue': str(template_id), 'DataType': 'String'}})
@notifications.route('/sms', methods=['POST'])
def create_sms_notification():
notification = request.get_json()['notification']
errors = {}
to, to_errors = validate_to(notification)
if to_errors['to']:
errors.update(to_errors)
# TODO: should create a different endpoint for the admin client to send verify codes.
if api_user['client'] == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
to, to_errors = validate_to_for_admin_client(notification)
content, content_errors = validate_content_for_admin_client(notification)
if to_errors['to']:
errors.update(to_errors)
if content_errors['content']:
errors.update(content_errors)
if errors:
@@ -58,16 +42,17 @@ def create_sms_notification():
return jsonify(notify_alpha_client.send_sms(mobile_number=to, message=content)), 200
else:
to, to_errors = validate_to(notification, api_user['client'])
to, restricted_errors = validate_to_for_service(notification, api_user['client'])
if restricted_errors['restricted']:
errors.update(restricted_errors)
template, template_errors = validate_template(notification, api_user['client'])
if to_errors['to']:
errors.update(to_errors)
if template_errors['template']:
errors.update(template_errors)
if errors:
return jsonify(result="error", message=errors), 400
# add notification to the queue
# add notification to the queue
service = services_dao.get_model_services(api_user['client'], _raise=False)
_add_notification_to_queue(template.id, service, 'sms', to)
return jsonify(notify_alpha_client.send_sms(mobile_number=to, message=template.content)), 200
@@ -92,28 +77,21 @@ def create_email_notification():
notification['subject']))
def validate_to(json_body, service_id):
errors = {"to": []}
mob = json_body.get('to', None)
if not mob:
errors['to'].append('Required data missing')
else:
if not mobile_regex.match(mob):
errors['to'].append('invalid phone number, must be of format +441234123123')
service = services_dao.get_model_services(service_id=service_id)
if service.restricted:
valid = False
for usr in service.users:
if mob == usr.mobile_number:
valid = True
break
if not valid:
errors['to'].append('Invalid phone number for restricted service')
def validate_to_for_service(mob, service_id):
errors = {"restricted": []}
service = services_dao.get_model_services(service_id=service_id)
if service.restricted:
valid = False
for usr in service.users:
if mob == usr.mobile_number:
valid = True
break
if not valid:
errors['restricted'].append('Invalid phone number for restricted service')
return mob, errors
def validate_to_for_admin_client(json_body):
def validate_to(json_body):
errors = {"to": []}
mob = json_body.get('to', None)
if not mob:
@@ -154,3 +132,20 @@ def validate_required_and_something(json_body, field):
if field not in json_body and json_body[field]:
errors.append('Required data for field.')
return {field: errors} if errors else None
def _add_notification_to_queue(template_id, service, msg_type, to):
q = boto3.resource('sqs', region_name=current_app.config['AWS_REGION']).create_queue(
QueueName=str(service.queue_name))
import uuid
notification = json.dumps({'message_id': str(uuid.uuid4()),
'service_id': service.id,
'to': to,
'message_type': msg_type,
'template_id': template_id})
serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY'))
encrypted = serializer.dumps(notification, current_app.config.get('DANGEROUS_SALT'))
q.send_message(MessageBody=encrypted,
MessageAttributes={'type': {'StringValue': msg_type, 'DataType': 'String'},
'service_id': {'StringValue': str(service.id), 'DataType': 'String'},
'template_id': {'StringValue': str(template_id), 'DataType': 'String'}})