mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 10:42:41 -05:00
Create schema for RequestVerifyCodeSchema
Previously we were using a schema that mapped onto db.Model. However, the json in the request did not reflect the VerfiyCode db Model. I did not add validation on the to field, we did not have that previously.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
from flask_marshmallow.fields import fields
|
||||||
from . import ma
|
from . import ma
|
||||||
from . import models
|
from . import models
|
||||||
from marshmallow import post_load
|
from marshmallow import post_load, ValidationError
|
||||||
|
|
||||||
|
|
||||||
# TODO I think marshmallow provides a better integration and error handling.
|
# TODO I think marshmallow provides a better integration and error handling.
|
||||||
# Would be better to replace functionality in dao with the marshmallow supported
|
# Would be better to replace functionality in dao with the marshmallow supported
|
||||||
@@ -9,7 +11,6 @@ from marshmallow import post_load
|
|||||||
|
|
||||||
|
|
||||||
class BaseSchema(ma.ModelSchema):
|
class BaseSchema(ma.ModelSchema):
|
||||||
|
|
||||||
def __init__(self, *args, load_json=False, **kwargs):
|
def __init__(self, *args, load_json=False, **kwargs):
|
||||||
self.load_json = load_json
|
self.load_json = load_json
|
||||||
super(BaseSchema, self).__init__(*args, **kwargs)
|
super(BaseSchema, self).__init__(*args, **kwargs)
|
||||||
@@ -27,7 +28,6 @@ class BaseSchema(ma.ModelSchema):
|
|||||||
|
|
||||||
|
|
||||||
class UserSchema(BaseSchema):
|
class UserSchema(BaseSchema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.User
|
model = models.User
|
||||||
exclude = (
|
exclude = (
|
||||||
@@ -36,36 +36,35 @@ class UserSchema(BaseSchema):
|
|||||||
|
|
||||||
|
|
||||||
class ServiceSchema(BaseSchema):
|
class ServiceSchema(BaseSchema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Service
|
model = models.Service
|
||||||
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name")
|
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name")
|
||||||
|
|
||||||
|
|
||||||
class TemplateSchema(BaseSchema):
|
class TemplateSchema(BaseSchema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Template
|
model = models.Template
|
||||||
exclude = ("updated_at", "created_at", "service_id", "jobs")
|
exclude = ("updated_at", "created_at", "service_id", "jobs")
|
||||||
|
|
||||||
|
|
||||||
class ApiKeySchema(BaseSchema):
|
class ApiKeySchema(BaseSchema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.ApiKey
|
model = models.ApiKey
|
||||||
exclude = ("service", "secret")
|
exclude = ("service", "secret")
|
||||||
|
|
||||||
|
|
||||||
class JobSchema(BaseSchema):
|
class JobSchema(BaseSchema):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Job
|
model = models.Job
|
||||||
|
|
||||||
|
|
||||||
class VerifyCodeSchema(BaseSchema):
|
class RequestVerifyCodeSchema(ma.Schema):
|
||||||
class Meta:
|
def verify_code_type(self):
|
||||||
model = models.VerifyCode
|
if self not in ['sms', 'email']:
|
||||||
exclude = ('user', "_code", "expiry_datetime", "code_used", "created_at")
|
raise ValidationError('Invalid code type')
|
||||||
|
|
||||||
|
code_type = fields.Str(required=True, validate=verify_code_type)
|
||||||
|
to = fields.Str(required=False)
|
||||||
|
|
||||||
|
|
||||||
user_schema = UserSchema()
|
user_schema = UserSchema()
|
||||||
@@ -83,4 +82,4 @@ api_keys_schema = ApiKeySchema(many=True)
|
|||||||
job_schema = JobSchema()
|
job_schema = JobSchema()
|
||||||
job_schema_load_json = JobSchema(load_json=True)
|
job_schema_load_json = JobSchema(load_json=True)
|
||||||
jobs_schema = JobSchema(many=True)
|
jobs_schema = JobSchema(many=True)
|
||||||
verify_code_schema = VerifyCodeSchema()
|
request_verify_code_schema = RequestVerifyCodeSchema()
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ from app.dao.users_dao import (
|
|||||||
)
|
)
|
||||||
from app.schemas import (
|
from app.schemas import (
|
||||||
user_schema, users_schema, service_schema, services_schema,
|
user_schema, users_schema, service_schema, services_schema,
|
||||||
verify_code_schema, user_schema_load_json)
|
request_verify_code_schema, user_schema_load_json)
|
||||||
from app import db, notify_alpha_client
|
from app import notify_alpha_client
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
|
|
||||||
|
|
||||||
@@ -126,24 +126,22 @@ def send_user_code(user_id):
|
|||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return jsonify(result="error", message="User not found"), 404
|
return jsonify(result="error", message="User not found"), 404
|
||||||
|
|
||||||
request_json = request.get_json()
|
verify_code, errors = request_verify_code_schema.load(request.get_json())
|
||||||
|
|
||||||
verify_code, errors = verify_code_schema.load(request_json)
|
|
||||||
if errors:
|
if errors:
|
||||||
return jsonify(result="error", message=errors), 400
|
return jsonify(result="error", message=errors), 400
|
||||||
|
|
||||||
from app.dao.users_dao import create_secret_code
|
from app.dao.users_dao import create_secret_code
|
||||||
secret_code = create_secret_code()
|
secret_code = create_secret_code()
|
||||||
create_user_code(user, secret_code, verify_code.code_type)
|
create_user_code(user, secret_code, verify_code.get('code_type'))
|
||||||
# TODO this will need to fixed up when we stop using
|
# TODO this will need to fixed up when we stop using
|
||||||
# notify_alpha_client
|
# notify_alpha_client
|
||||||
if verify_code.code_type == 'sms':
|
if verify_code.get('code_type') == 'sms':
|
||||||
mobile = user.mobile_number if 'to' not in request_json else request_json['to']
|
mobile = user.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
|
||||||
notify_alpha_client.send_sms(
|
notify_alpha_client.send_sms(
|
||||||
mobile_number=mobile,
|
mobile_number=mobile,
|
||||||
message=secret_code)
|
message=secret_code)
|
||||||
elif verify_code.code_type == 'email':
|
elif verify_code.get('code_type') == 'email':
|
||||||
email = user.email_address if 'to' not in request_json else request_json['to']
|
email = user.email_address if verify_code.get('to', None) is None else verify_code.get('to')
|
||||||
notify_alpha_client.send_email(
|
notify_alpha_client.send_email(
|
||||||
email,
|
email,
|
||||||
secret_code,
|
secret_code,
|
||||||
|
|||||||
@@ -348,3 +348,21 @@ def test_send_user_code_for_email_uses_optional_to_field(notify_api,
|
|||||||
'11111',
|
'11111',
|
||||||
'notify@digital.cabinet-office.gov.uk',
|
'notify@digital.cabinet-office.gov.uk',
|
||||||
'Verification code')
|
'Verification code')
|
||||||
|
|
||||||
|
|
||||||
|
def test_request_verify_code_schema_invalid_code_type(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
import json
|
||||||
|
from app.schemas import request_verify_code_schema
|
||||||
|
data = json.dumps({'code_type': 'not_sms'})
|
||||||
|
code, error = request_verify_code_schema.loads(data)
|
||||||
|
assert code == {}
|
||||||
|
assert error == {'code_type': ['Invalid code type']}
|
||||||
|
|
||||||
|
|
||||||
|
def test_request_verify_code_schema_with_to(notify_api, notify_db, notify_db_session, sample_user):
|
||||||
|
import json
|
||||||
|
from app.schemas import request_verify_code_schema
|
||||||
|
data = json.dumps({'code_type': 'sms', 'to': 'some@one.gov.uk'})
|
||||||
|
code, error = request_verify_code_schema.loads(data)
|
||||||
|
assert code == {'code_type': 'sms', 'to': 'some@one.gov.uk'}
|
||||||
|
assert error == {}
|
||||||
|
|||||||
Reference in New Issue
Block a user