Commit f4c6df5f authored by Pablo Panero's avatar Pablo Panero
Browse files

marshmallow/tests: add _data field validation and tests

parent fc3b1b46
......@@ -17,7 +17,7 @@ from marshmallow import validates_schema, ValidationError
def has_and_needs_binary(original_data):
es_index, doc = default_record_to_index(original_data)
binary_index_list = current_app.config['SEARCH_DOC_PIPELINES']
if doc in binary_index_list and not original_data.get('b64'):
if doc in binary_index_list and not original_data.get("_data").get('b64'):
return False
return True
......@@ -27,7 +27,7 @@ class CSASRecordSchemaV1(RecordMetadataSchemaJSONV1):
@validates_schema(pass_original=True)
def add_unknown_fields(self, data, original_data):
if not original_data.get('_access'):
raise ValidationError('Missing field _access.')
raise ValidationError('Missing field _access')
delete = original_data.get('_access').get('delete')
if not delete or not isinstance(delete, list):
raise ValidationError('Missing or wrong type (not an array) in '
......@@ -40,7 +40,9 @@ class CSASRecordSchemaV1(RecordMetadataSchemaJSONV1):
if not owner or not isinstance(owner, list):
raise ValidationError('Missing or wrong type (not an array) in '
'field _access.owner')
if not original_data.get('_data'):
raise ValidationError('Missing field _data')
if not has_and_needs_binary(original_data):
raise ValidationError('Record to be index belongs to binary index '
'but does not contain the [b64] field')
return
\ No newline at end of file
return
......@@ -128,11 +128,35 @@ def test_access_fields_existence(endpoint, api_key):
"update": ["CernSearch-Administrators@cern.ch"],
"delete": ["CernSearch-Administrators@cern.ch"]
},
"title": "test_access_fields_existence",
"description": "No _access.owner field"
"_data": {
"title": "test_access_fields_existence",
"description": "No _access.owner field"
}
}
resp = requests.post('{endpoint}/api/records/'.format(endpoint=endpoint),
headers=HEADERS, data=json.dumps(body))
assert resp.status_code == 400
assert {"field": "_schema", "message": "Missing or wrong type (not an array) in field _access.owner"} in resp.json()['errors']
@pytest.mark.unit
def test_data_field_existence(endpoint, api_key):
HEADERS['Authorization'] = 'Bearer {credentials}'.format(credentials=api_key)
# Create test record without _data field
body = {
"_access": {
"owner": ["CernSearch-Administrators@cern.ch"],
"update": ["CernSearch-Administrators@cern.ch"],
"delete": ["CernSearch-Administrators@cern.ch"]
},
"title": "test_access_fields_existence",
"description": "No _access field"
}
resp = requests.post('{endpoint}/api/records/'.format(endpoint=endpoint),
headers=HEADERS, data=json.dumps(body))
assert resp.status_code == 400
assert {"field": "_schema", "message": "Missing field _data"} in resp.json()['errors']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment