Make email a required field for the email_data_schema.

This commit is contained in:
Rebecca Law
2016-07-08 10:57:20 +01:00
parent 36ecdca04c
commit f4976539e4
3 changed files with 34 additions and 6 deletions

View File

@@ -319,7 +319,7 @@ class EmailDataSchema(ma.Schema):
class Meta:
strict = True
email = fields.Str(required=False)
email = fields.Str(required=True)
@validates('email')
def validate_email(self, value):

View File

@@ -23,8 +23,7 @@ from app.schemas import (
user_schema,
request_verify_code_schema,
user_schema_load_json,
permission_schema
)
permission_schema)
from app.celery.tasks import (
send_sms,
@@ -176,13 +175,13 @@ def send_user_email_verification(user_id):
@user.route('/<uuid:user_id>/email-already-registered', methods=['POST'])
def send_already_registered_email(user_id):
to, errors = email_data_request_schema.load(request.get_json())
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_TEMPLATE_ID'])
to, errors = request_verify_code_schema.load(request.get_json())
message = {
'template': str(template.id),
'template_version': template.version,
'to': to['to'],
'to': to['email'],
'personalisation': {
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',

View File

@@ -424,6 +424,21 @@ def test_send_user_reset_password_should_send_reset_password_link(notify_api,
queue="email-reset-password")
def test_send_user_reset_password_should_return_400_when_email_is_missing(notify_api):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = json.dumps({})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_user_reset_password'),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}
def test_send_user_reset_password_should_return_400_when_user_doesnot_exist(notify_api,
mocker):
with notify_api.test_request_context():
@@ -461,7 +476,7 @@ def test_send_user_reset_password_should_return_400_when_data_is_not_email_addre
def test_send_already_registered_email(notify_api, sample_user, already_registered_template, mocker):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = json.dumps({'to': sample_user.email_address})
data = json.dumps({'email': sample_user.email_address})
auth_header = create_authorization_header()
mocker.patch('app.celery.tasks.send_email.apply_async')
mocker.patch('uuid.uuid4', return_value='some_uuid') # for the notification id
@@ -487,3 +502,17 @@ def test_send_already_registered_email(notify_api, sample_user, already_register
app.encryption.encrypt(message),
"2016-01-01T11:09:00.061258"),
queue="email-already-registered")
def test_send_already_registered_email_returns_400_when_data_is_missing(notify_api, sample_user):
with notify_api.test_request_context():
with notify_api.test_client() as client:
data = json.dumps({})
auth_header = create_authorization_header()
resp = client.post(
url_for('user.send_already_registered_email', user_id=str(sample_user.id)),
data=data,
headers=[('Content-Type', 'application/json'), auth_header])
assert resp.status_code == 400
assert json.loads(resp.get_data(as_text=True))['message'] == {'email': ['Missing data for required field.']}