mirror of
https://github.com/GSA/notifications-api.git
synced 2026-05-06 17:18:43 -04:00
Add a schema to validate a single user attr 'strictly'
This commit is contained in:
@@ -105,6 +105,41 @@ class UserSchema(BaseSchema):
|
||||
strict = True
|
||||
|
||||
|
||||
class UserUpdateAttributeSchema(BaseSchema):
|
||||
|
||||
class Meta:
|
||||
model = models.User
|
||||
exclude = (
|
||||
"updated_at", "created_at", "user_to_service",
|
||||
"_password", "verify_codes")
|
||||
strict = True
|
||||
|
||||
@validates('name')
|
||||
def validate_name(self, value):
|
||||
if not value:
|
||||
raise ValidationError('Invalid name')
|
||||
|
||||
@validates('email_address')
|
||||
def validate_email_address(self, value):
|
||||
try:
|
||||
validate_email_address(value)
|
||||
except InvalidEmailError as e:
|
||||
raise ValidationError(e.message)
|
||||
|
||||
@validates('mobile_number')
|
||||
def validate_mobile_number(self, value):
|
||||
try:
|
||||
validate_phone_number(value)
|
||||
except InvalidPhoneError as error:
|
||||
raise ValidationError('Invalid phone number: {}'.format(error))
|
||||
|
||||
@validates_schema(pass_original=True)
|
||||
def check_unknown_fields(self, data, original_data):
|
||||
for key in original_data:
|
||||
if key not in self.fields:
|
||||
raise ValidationError('Unknown field name {}'.format(key))
|
||||
|
||||
|
||||
class ProviderDetailsSchema(BaseSchema):
|
||||
class Meta:
|
||||
model = models.ProviderDetails
|
||||
@@ -529,6 +564,7 @@ class UnarchivedTemplateSchema(BaseSchema):
|
||||
|
||||
user_schema = UserSchema()
|
||||
user_schema_load_json = UserSchema(load_json=True)
|
||||
user_update_schema_load_json = UserUpdateAttributeSchema(load_json=True, partial=True)
|
||||
service_schema = ServiceSchema()
|
||||
service_schema_load_json = ServiceSchema(load_json=True)
|
||||
detailed_service_schema = DetailedServiceSchema()
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import pytest
|
||||
|
||||
|
||||
def test_job_schema_doesnt_return_notifications(sample_notification_with_job):
|
||||
from app.schemas import job_schema
|
||||
|
||||
@@ -22,3 +25,34 @@ def test_notification_schema_adds_api_key_name(sample_notification_with_api_key)
|
||||
|
||||
data = notification_with_template_schema.dump(sample_notification_with_api_key).data
|
||||
assert data['key_name'] == 'Test key'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('user_attribute, user_value', [
|
||||
('name', 'New User'),
|
||||
('email_address', 'newuser@mail.com'),
|
||||
('mobile_number', '+4407700900460')
|
||||
])
|
||||
def test_user_schema_accepts_valid_attributes(user_attribute, user_value):
|
||||
update_dict = {
|
||||
user_attribute: user_value
|
||||
}
|
||||
from app.schemas import user_update_schema_load_json
|
||||
|
||||
data, errors = user_update_schema_load_json.load(update_dict)
|
||||
assert not errors
|
||||
|
||||
|
||||
@pytest.mark.parametrize('user_attribute, user_value', [
|
||||
('name', None),
|
||||
('name', ''),
|
||||
('email_address', 'bademail@...com'),
|
||||
('mobile_number', '+44077009')
|
||||
])
|
||||
def test_user_schema_rejects_invalid_attributes(user_attribute, user_value):
|
||||
from app.schemas import user_update_schema_load_json
|
||||
update_dict = {
|
||||
user_attribute: user_value
|
||||
}
|
||||
|
||||
with pytest.raises(Exception):
|
||||
data, errors = user_update_schema_load_json.load(update_dict)
|
||||
|
||||
Reference in New Issue
Block a user