Skip to content

Commit

Permalink
Merge pull request #409 from harunurhan/stricter-file-url
Browse files Browse the repository at this point in the history
literature: stricter file url validation
  • Loading branch information
harunurhan committed Oct 28, 2020
2 parents ab35f92 + 13a4b9f commit 9c4a478
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -103,6 +103,9 @@ AUTHORS

node_modules

# pipenv
.python-version

# Backup files
*~

Expand Down
6 changes: 4 additions & 2 deletions inspire_schemas/records/hep.yml
Expand Up @@ -812,7 +812,8 @@ properties:
form is ``/files/bucket/key``. It can temprorarily be
the url to download the document from, until actually
downloaded.
format: uri-reference
format: 'uri-references'
pattern: ^(https?://|/api/files/).*
minLength: 1
type: string
required:
Expand Down Expand Up @@ -972,7 +973,8 @@ properties:
Relative URL to the file containing the figure. Its
form is ``/files/bucket/key``.
format: uri-reference
format: 'uri-reference'
pattern: ^(https?://|/api/files/).*
minLength: 1
type: string
required:
Expand Down
60 changes: 52 additions & 8 deletions tests/unit/test_literature_builder.py
Expand Up @@ -23,6 +23,7 @@
# as an Intergovernmental Organization or submit itself to any jurisdiction.

import pytest
from jsonschema import ValidationError

from inspire_schemas.builders.literature import LiteratureBuilder, is_citeable
from inspire_schemas.utils import load_schema, validate
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_add_figure():
label='label',
material='publication',
source='source',
url='url',
url='https://www.example.com/url',
description='description',
filename='filename',
original_url='http://www.example.com/original_url'
Expand All @@ -147,7 +148,7 @@ def test_add_figure():
'label': 'label',
'material': 'publication',
'source': 'source',
'url': 'url',
'url': 'https://www.example.com/url',
'filename': 'filename',
'original_url': 'http://www.example.com/original_url'
},
Expand All @@ -173,7 +174,7 @@ def test_add_figure_inspire_next():
label='label',
material='publication',
source='source',
url='url',
url='/api/files/a1/123',
description='description',
original_url='http://www.example.com/original_url'
)
Expand All @@ -185,7 +186,7 @@ def test_add_figure_inspire_next():
'label': 'label',
'material': 'publication',
'source': 'source',
'url': 'url',
'url': '/api/files/a1/123',
'original_url': 'http://www.example.com/original_url'
},
]
Expand Down Expand Up @@ -224,6 +225,27 @@ def test_add_figure_fails_on_duplicated_key():
)


def test_add_figure_fails_on_non_file_api_relative_url():
schema = load_schema('hep')
subschema = schema['properties']['figures']

builder = LiteratureBuilder('test')

with pytest.raises(ValidationError):
builder.add_figure(
'key',
caption='caption',
label='label',
material='publication',
source='source',
url='/not/api/url/for/files',
description='description',
original_url='http://www.example.com/original_url'
)
result = builder.record
validate(result['figures'], subschema)


def test_add_document():
schema = load_schema('hep')
subschema = schema['properties']['documents']
Expand All @@ -238,7 +260,7 @@ def test_add_document():
material='preprint',
original_url='http://www.example.com/original_url',
source='source',
url='url',
url='https://www.example.com/url',
filename='filename'
)

Expand All @@ -251,7 +273,7 @@ def test_add_document():
'material': 'preprint',
'original_url': 'http://www.example.com/original_url',
'source': 'source',
'url': 'url',
'url': 'https://www.example.com/url',
'filename': 'filename'
},
]
Expand All @@ -278,7 +300,7 @@ def test_add_document_inspire_next():
material='preprint',
original_url='http://www.example.com/original_url',
source='source',
url='url',
url='/api/files/a1/123',
)

expected = [
Expand All @@ -290,7 +312,7 @@ def test_add_document_inspire_next():
'material': 'preprint',
'original_url': 'http://www.example.com/original_url',
'source': 'source',
'url': 'url',
'url': '/api/files/a1/123',
},
]
result = builder.record
Expand Down Expand Up @@ -327,6 +349,28 @@ def test_add_document_fails_on_existing_key():
)


def test_add_document_fails_on_non_file_api_relative_url():
schema = load_schema('hep')
subschema = schema['properties']['documents']

builder = LiteratureBuilder('test')

with pytest.raises(ValidationError):
builder.add_document(
'key',
description='description',
fulltext=True,
hidden=True,
material='preprint',
original_url='http://www.example.com/original_url',
source='source',
url='/not/api/url/for/files',
filename='filename'
)
result = builder.record
validate(result['documents'], subschema)


def test_make_author():
schema = load_schema('hep')
subschema = schema['properties']['authors']
Expand Down

0 comments on commit 9c4a478

Please sign in to comment.