Skip to content

Commit

Permalink
Merge 361ff14 into e7f4be3
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiaraBi committed Sep 14, 2018
2 parents e7f4be3 + 361ff14 commit bf30087
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
73 changes: 73 additions & 0 deletions tests/unit/records/test_serializers_json.py
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
#
# This file is part of Zenodo.
# Copyright (C) 2018 CERN.
#
# Zenodo is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Zenodo 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Zenodo; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Unit test for Zenodo json serializer."""

import json

import pytest
from flask import url_for
from helpers import login_user_via_session
from invenio_indexer.api import RecordIndexer
from invenio_search import current_search

from zenodo.modules.records.models import AccessRight


@pytest.mark.parametrize('user_info, bucket_link, files', [
# anonymous user
(None, None, 0),
# owner
(dict(email='info@zenodo.org', password='tester'),
'http://localhost/files/22222222-2222-2222-2222-222222222222', 1),
# not owner
(dict(email='test@zenodo.org', password='tester2'), None, 0),
# admin user
(dict(email='admin@zenodo.org', password='admin'),
'http://localhost/files/22222222-2222-2222-2222-222222222222', 1),
])
def test_closed_access_record_serializer(api, db, es, users, json_headers,
record_with_files_creation,
user_info, bucket_link, files):
_, record, record_url = record_with_files_creation
record['access_right'] = AccessRight.CLOSED
record.commit()
db.session.commit()
indexer = RecordIndexer()
indexer.index(record)
current_search.flush_and_refresh(index='records')

with api.test_request_context():
with api.test_client() as client:
if user_info:
# Login as user
login_user_via_session(client, email=user_info['email'])
res = client.get(
url_for('invenio_records_rest.recid_item',
pid_value=record['recid']),
headers=json_headers
)
r = json.loads(res.data.decode('utf-8'))
assert r['links'].get('bucket', None) == bucket_link
assert len(r.get('files', [])) == files
7 changes: 6 additions & 1 deletion zenodo/modules/records/serializers/json.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
#
# This file is part of Zenodo.
# Copyright (C) 2016 CERN.
# Copyright (C) 2016-2018 CERN.
#
# Zenodo is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -57,6 +57,8 @@ def preprocess_record(self, pid, record, links_factory=None):
if not has_request_context() or has_read_files_permission(
current_user, record):
result['files'] = record['_files']
else:
del result['metadata']['_buckets']

# Serialize PID versioning as related identifiers
pv = PIDVersioning(child=pid)
Expand All @@ -78,6 +80,9 @@ def preprocess_search_hit(self, pid, record_hit, links_factory=None,
result['files'] = record_hit['_source']['_files']
elif '_files' in record_hit:
result['files'] = record_hit['_files']
else:
# delete the bucket if no files
del result['metadata']['_buckets']
return result

def dump(self, obj, context=None):
Expand Down

0 comments on commit bf30087

Please sign in to comment.