-
Notifications
You must be signed in to change notification settings - Fork 357
[ENG-9824] - Add required metadata validation #11691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import pytest | ||
| from faker import Faker | ||
| from django.core.exceptions import ValidationError | ||
|
|
||
| from osf.models import CedarMetadataRecord, CedarMetadataTemplate | ||
| from osf_tests.factories import AuthUserFactory, PreprintFactory, PreprintProviderFactory | ||
|
|
||
| fake = Faker() | ||
|
|
||
| VALID_JSONSCHEMA = { | ||
| '$schema': 'http://json-schema.org/draft-07/schema#', | ||
| 'type': 'object', | ||
| 'properties': { | ||
| 'title': {'type': 'string'}, | ||
| }, | ||
| 'required': ['title'], | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def user(): | ||
| return AuthUserFactory() | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def provider(): | ||
| return PreprintProviderFactory() | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def cedar_template(): | ||
| return CedarMetadataTemplate.objects.create( | ||
| schema_name=fake.bs(), | ||
| cedar_id=fake.md5(), | ||
| template_version=1, | ||
| template=VALID_JSONSCHEMA, | ||
| active=True, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def preprint(user, provider): | ||
| return PreprintFactory(creator=user, provider=provider) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| class TestValidateRequiredMetadata: | ||
|
|
||
| def test_no_required_template_passes(self, provider, preprint): | ||
| assert provider.required_metadata_template is None | ||
| provider.validate_required_metadata(preprint) | ||
|
|
||
| def test_missing_record_raises(self, provider, cedar_template, preprint): | ||
| provider.required_metadata_template = cedar_template | ||
| provider.save() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious as I'm not familiar with this functionality yet. Shouldn't we call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method validates a specific osf_obj (preprint/node) against the provider's required template — not the provider itself. It's intended to be called at publish/submission time for a given object (e.g. before a preprint goes public). Calling it in Provider.save() wouldn't make sense since there's no single object to validate there — a provider may have thousands of associated preprints. |
||
|
|
||
| with pytest.raises(ValidationError, match='published CEDAR metadata record'): | ||
| provider.validate_required_metadata(preprint) | ||
|
|
||
| def test_unpublished_record_raises(self, provider, cedar_template, preprint): | ||
| provider.required_metadata_template = cedar_template | ||
| provider.save() | ||
|
|
||
| CedarMetadataRecord.objects.create( | ||
| guid=preprint.guids.first(), | ||
| template=cedar_template, | ||
| metadata={'title': 'My Preprint'}, | ||
| is_published=False, | ||
| ) | ||
|
|
||
| with pytest.raises(ValidationError, match='published CEDAR metadata record'): | ||
| provider.validate_required_metadata(preprint) | ||
|
|
||
| def test_published_valid_record_passes(self, provider, cedar_template, preprint): | ||
| provider.required_metadata_template = cedar_template | ||
| provider.save() | ||
|
|
||
| CedarMetadataRecord.objects.create( | ||
| guid=preprint.guids.first(), | ||
| template=cedar_template, | ||
| metadata={'title': 'My Preprint'}, | ||
| is_published=True, | ||
| ) | ||
|
|
||
| provider.validate_required_metadata(preprint) | ||
|
|
||
| def test_published_invalid_record_raises(self, provider, cedar_template, preprint): | ||
| provider.required_metadata_template = cedar_template | ||
| provider.save() | ||
|
|
||
| CedarMetadataRecord.objects.create( | ||
| guid=preprint.guids.first(), | ||
| template=cedar_template, | ||
| metadata={'title': 123}, | ||
| is_published=True, | ||
| ) | ||
|
|
||
| with pytest.raises(ValidationError): | ||
| provider.validate_required_metadata(preprint) | ||
|
|
||
| def test_record_for_wrong_template_raises(self, provider, cedar_template, preprint): | ||
| provider.required_metadata_template = cedar_template | ||
| provider.save() | ||
|
|
||
| other_template = CedarMetadataTemplate.objects.create( | ||
| schema_name=fake.bs(), | ||
| cedar_id=fake.md5(), | ||
| template_version=1, | ||
| template=VALID_JSONSCHEMA, | ||
| active=True, | ||
| ) | ||
| CedarMetadataRecord.objects.create( | ||
| guid=preprint.guids.first(), | ||
| template=other_template, | ||
| metadata={'title': 'My Preprint'}, | ||
| is_published=True, | ||
| ) | ||
|
|
||
| with pytest.raises(ValidationError, match='published CEDAR metadata record'): | ||
| provider.validate_required_metadata(preprint) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this import hasn't any dependencies and we can move it to the top of the file