-
Notifications
You must be signed in to change notification settings - Fork 359
[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
Open
Vlad0n20
wants to merge
2
commits into
CenterForOpenScience:feature/es2-consolidation
Choose a base branch
from
Vlad0n20:feature/ENG-9824
base: feature/es2-consolidation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+143
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
|
|
||
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Just curious as I'm not familiar with this functionality yet. Shouldn't we call
validate_required_metadataduringsavecall to prevent saving cedar template without cedar metadata record existence?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.
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.