def test_marshmallow_load_nested_subfield_errors(app, db, es, test_data,
                                                 search_url, search_class):
    """Test loading nested subfield errors."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestSchemaNested)
    }

    with app.test_client() as client:
        HEADERS = [('Accept', 'application/json'),
                   ('Content-Type', 'application/json')]

        # Create record
        missing_top_most_required_field = dict(nested_field=[{}],
                                               non_list=dict(random='a',
                                                             id='b'))
        res = client.post(search_url,
                          data=json.dumps(missing_top_most_required_field),
                          headers=HEADERS)
        assert res.status_code == 400
        response_json = json.loads(res.data.decode('utf-8'))

        # Error order is not deterministic from marshmallow
        def has_error(field, parents):
            for error in response_json['errors']:
                if error['field'] == field and error['parents'] == parents:
                    return True
            return False

        assert len(response_json['errors']) == 2
        assert has_error(field='title', parents=['non_list'])
        assert has_error(field='sample', parents=['nested_field', 0])
def test_marshmallow_load(app, db, es, test_data, search_url, search_class):
    """Test marshmallow loader."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestMetadataSchema)
    }

    with app.test_client() as client:
        HEADERS = [
            ('Accept', 'application/json'),
            ('Content-Type', 'application/json'),
            ('If-Match', '"0"'),
        ]

        # Create record
        req_data = test_data[0]
        res = client.post(search_url,
                          data=json.dumps(req_data),
                          headers=HEADERS)
        assert res.status_code == 201

        # Check that the returned response matches the stored data
        original_res_data = get_json(res)
        model_record = RecordMetadata.query.one()
        assert original_res_data['metadata'] == model_record.json

        # Try to modify the "control_number"
        req_data = deepcopy(original_res_data['metadata'])
        req_data['control_number'] = 42
        req_url = original_res_data['links']['self']
        res = client.put(req_url, data=json.dumps(req_data), headers=HEADERS)
        res_data = get_json(res)
        model_record = RecordMetadata.query.one()
        assert res_data['metadata'] == original_res_data['metadata']
        assert res_data['metadata'] == model_record.json
def test_marshmallow_load(app, db, es, test_data, search_url, search_class):
    """Test marshmallow loader."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestMetadataSchema)}

    with app.test_client() as client:
        HEADERS = [
            ('Accept', 'application/json'),
            ('Content-Type', 'application/json')
        ]

        # Create record
        req_data = test_data[0]
        res = client.post(
            search_url, data=json.dumps(req_data), headers=HEADERS)
        assert res.status_code == 201

        # Check that the returned response matches the stored data
        original_res_data = get_json(res)
        model_record = RecordMetadata.query.one()
        assert original_res_data['metadata'] == model_record.json

        # Try to modify the "control_number"
        req_data = deepcopy(original_res_data['metadata'])
        req_data['control_number'] = 42
        req_url = original_res_data['links']['self']
        res = client.put(req_url, data=json.dumps(req_data), headers=HEADERS)
        res_data = get_json(res)
        model_record = RecordMetadata.query.one()
        assert res_data['metadata'] == original_res_data['metadata']
        assert res_data['metadata'] == model_record.json
def test_marshmallow_load_errors(app, db, es, test_data, search_url,
                                 search_class):
    """Test marshmallow loader errors."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestSchema)
    }

    with app.test_client() as client:
        HEADERS = [('Accept', 'application/json'),
                   ('Content-Type', 'application/json')]

        # Create record
        incomplete_data = dict(test_data[0])
        del incomplete_data['title']
        res = client.post(search_url,
                          data=json.dumps(incomplete_data),
                          headers=HEADERS)
        assert res.status_code == 400
def test_marshmallow_load_errors(app, db, es, test_data, search_url,
                                 search_class):
    """Test marshmallow loader errors."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestSchema)}

    with app.test_client() as client:
        HEADERS = [
            ('Accept', 'application/json'),
            ('Content-Type', 'application/json')
        ]

        # Create record
        incomplete_data = dict(test_data[0])
        del incomplete_data['title']
        res = client.post(
            search_url, data=json.dumps(incomplete_data), headers=HEADERS)
        assert res.status_code == 400
def test_marshmallow_load_nested_errors(app, db, es, test_data, search_url,
                                        search_class):
    """Test loading nested errors."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestSchemaNested)
    }

    with app.test_client() as client:
        HEADERS = [('Accept', 'application/json'),
                   ('Content-Type', 'application/json')]

        # Create record
        missing_top_most_required_field = dict(nested="test")
        res = client.post(search_url,
                          data=json.dumps(missing_top_most_required_field),
                          headers=HEADERS)
        assert res.status_code == 400
        response_json = json.loads(res.data.decode('utf-8'))
        assert response_json['errors'][0]['field'] == 'nested_field'
def test_marshmallow_load_nested_errors(app, db, es, test_data, search_url,
                                        search_class):
    """Test loading nested errors."""
    app.config['RECORDS_REST_DEFAULT_LOADERS'] = {
        'application/json': marshmallow_loader(_TestSchemaNested)}

    with app.test_client() as client:
        HEADERS = [
            ('Accept', 'application/json'),
            ('Content-Type', 'application/json')
        ]

        # Create record
        missing_top_most_required_field = dict(nested="test")
        res = client.post(
            search_url, data=json.dumps(missing_top_most_required_field),
            headers=HEADERS)
        assert res.status_code == 400
        response_json = json.loads(res.data.decode('utf-8'))
        assert response_json['errors'][0]['field'] == 'nested_field'
Beispiel #8
0
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Deposit loaders."""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import DepositMetadataSchemaV1

json_v1 = marshmallow_loader(DepositMetadataSchemaV1)

__all__ = ('json_v1', )
Beispiel #9
0
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2019 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Document loaders."""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import DocumentMetadataSchemaV1

json_v1 = marshmallow_loader(DocumentMetadataSchemaV1)

__all__ = ('json_v1', )
Beispiel #10
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# My site is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the MetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import MetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(MetadataSchemaV1)

__all__ = (
    'json_v1',
)
Beispiel #11
0
# Swiss Open Access Repository
# Copyright (C) 2021 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Loaders for projects."""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import ProjectMetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(ProjectMetadataSchemaV1)

__all__ = (
    'json_v1',
)
Beispiel #12
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2018 NU,FSM,GHSL.
#
# CD2H Data Model is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the MetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import marshmallow_loader

from ..marshmallow import RecordSchemaV1

json_v1 = marshmallow_loader(RecordSchemaV1)

__all__ = ('json_v1', )
Beispiel #13
0
    updated = fields.Str(dump_only=True)
    links = fields.Dict(dump_only=True)
    id = PersistentIdentifier()

    # @post_dump
    # def dump_metadata(self, source, **kwargs):
    #     if source['metadata']['source_type'] == SourceType.JOURNAL.value:
    #         print('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsss')
    #         print(source)
    #         print('SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSsss')
    #         source['metadata'] = journal_data_schema.dump(source['metadata'])
    #     return source


source_data_schema_many = SourceDataSchemaV1(many=True)
source_data_schema = SourceDataSchemaV1(many=False)

source_loader_v1 = marshmallow_loader(SourceDataSchemaV1)

# Serializers
# ===========
#: JSON serializer definition.
source_v1 = JSONSerializer(SourceSchemaV1, replace_refs=True)

# Records-REST serializers
# ========================
#: JSON record serializer for individual records.
source_v1_response = record_responsify(source_v1, 'application/json')
#: JSON record serializer for search results.
source_v1_search = search_responsify(source_v1, 'application/json')
Beispiel #14
0
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2021 RERO
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Loaders."""

from invenio_records_rest.loaders.marshmallow import marshmallow_loader

from ..schemas import RecordMetadataSchema

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(RecordMetadataSchema)

__all__ = ('json_v1', )
Beispiel #15
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# My site is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.

"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the InstitutionMetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import InstitutionMetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(InstitutionMetadataSchemaV1)

__all__ = (
    'json_v1',
)
#
# Copyright (C) 2018 CERN.
#
# test-access is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the MetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import \
    json_patch_loader, marshmallow_loader

from test_access.records.loaders.myrecord import MyRecordSchemaV1
from ..marshmallow import MetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(MetadataSchemaV1)
my_record_loader = marshmallow_loader(MyRecordSchemaV1)

__all__ = (
    'json_v1',
    'my_record_loader',
)
Beispiel #17
0
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the OrganisationMetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import OrganisationMetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(OrganisationMetadataSchemaV1)

__all__ = ('json_v1', )
Beispiel #18
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 CERN.
#
# My site is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.
"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the MetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import MetadataSchemaV1, AuthorMetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(MetadataSchemaV1)
author_v1 = marshmallow_loader(AuthorMetadataSchemaV1)

__all__ = (
    'json_v1',
    'author_v1',
)
Beispiel #19
0
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 EUDAT.
#
# B2SHARE is free software; you can redistribute it and/or modify it under
# the terms of the MIT License; see LICENSE file for more details.
"""Loaders.

This file contains sample loaders that can be used to deserialize input data in
an application level data structure. The marshmallow_loader() method can be
parameterized with different schemas for the record metadata. In the provided
json_v1 instance, it uses the MetadataSchemaV1, defining the
PersistentIdentifier field.
"""

from __future__ import absolute_import, print_function

from invenio_records_rest.loaders.marshmallow import json_patch_loader, \
    marshmallow_loader

from ..marshmallow import MetadataSchemaV1

#: JSON loader using Marshmallow for data validation.
json_v1 = marshmallow_loader(MetadataSchemaV1)

__all__ = ('json_v1', )
Beispiel #20
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# This file is part of CERN Search.
# Copyright (C) 2018-2019 CERN.
#
# CERN Search is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

from cern_search_rest_api.modules.cernsearch.marshmallow import CSASRecordSchemaV1
from invenio_records_rest.loaders.marshmallow import marshmallow_loader

csas_loader = marshmallow_loader(CSASRecordSchemaV1)

__all__ = ('csas_loader', )