Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
Merge pull request #565 from CSCfi/CSCFAIRMETA-435-Drafts-OAI-PMH-API…
Browse files Browse the repository at this point in the history
…-Should-only-list-published-datasets

CSCFAIRMETA-435: [ADD] Drafts-OAI-PMH-API-Should-only-list-published-…
  • Loading branch information
Katri Tegel authored Apr 15, 2020
2 parents 52a255f + fbff5af commit a65eef9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/metax_api/api/oaipmh/base/metax_oai_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def _get_filtered_records_data(self, verb, metadata_prefix, set, cursor, batch_s
data_catalog__catalog_json__identifier__in=settings.OAI['SET_MAPPINGS'][set])
else:
query_set = query_set.filter(data_catalog__catalog_json__identifier__in=self._get_default_set_filter())
query_set = query_set.filter(state='published')

data = []
for record in query_set:
if verb == 'ListRecords':
Expand Down Expand Up @@ -440,6 +442,8 @@ def getRecord(self, metadataPrefix, identifier):
if metadataPrefix == OAI_DC_URNRESOLVER_MDPREFIX:
raise BadArgumentError('Invalid metadataPrefix value. It can be only used with ListRecords verb')
record = CatalogRecord.objects.get(identifier__exact=identifier)
if record.state == 'draft':
raise IdDoesNotExistError("No record with identifier %s is available." % identifier)
except CatalogRecord.DoesNotExist:
try:
record = DataCatalog.objects.get(catalog_json__identifier__exact=identifier)
Expand All @@ -455,4 +459,4 @@ def getRecord(self, metadataPrefix, identifier):
raise NoRecordsMatchError

return (common.Header('', identifier, self._get_header_timestamp(record), ['metax'], False),
common.Metadata('', metadata), None)
common.Metadata('', metadata), None)
62 changes: 58 additions & 4 deletions src/metax_api/tests/api/oaipmh/minimal_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def setUp(self):
# some cr that has publisher set...
cr = CatalogRecord.objects.filter(research_dataset__publisher__isnull=False).first()
self.identifier = cr.identifier
self.id = cr.id
self.preferred_identifier = cr.preferred_identifier
self._use_http_authorization()

Expand All @@ -62,10 +63,15 @@ def _get_single_result(self, data, xpath):
results = self._get_results(data, xpath)
return results[0]

# VERB: Identity
def _set_dataset_as_draft(self, cr_id):
cr = CatalogRecord.objects.get(pk=cr_id)
cr.state = 'draft'
cr.force_save()

def test_identity(self):
response = self.client.get('/oai/?verb=Identity')
# VERB: Identify

def test_identify(self):
response = self.client.get('/oai/?verb=Identify')
self.assertEqual(response.status_code, status.HTTP_200_OK)

# VERB: ListMetadataFormats
Expand Down Expand Up @@ -115,9 +121,23 @@ def test_list_identifiers(self):
errors = self._get_results(response.content, '//o:error[@code="badArgument"]')
self.assertTrue(len(errors) == 1, response.content)

def test_list_identifiers_for_drafts(self):
''' Tests that drafts are not returned from ListIdentifiers '''
ms = settings.OAI['BATCH_SIZE']
allRecords = CatalogRecord.objects.filter(
data_catalog__catalog_json__identifier__in=MetaxOAIServer._get_default_set_filter())[:ms]

self._set_dataset_as_draft(25)
self._set_dataset_as_draft(26)

# headers should be reduced when some datasets are set as drafts
response = self.client.get('/oai/?verb=ListIdentifiers&metadataPrefix=oai_dc')
self.assertEqual(response.status_code, status.HTTP_200_OK)
headers = self._get_results(response.content, '//o:header')
self.assertFalse(len(headers) == len(allRecords), len(headers))

def test_list_identifiers_from_datacatalogs_set(self):
allRecords = DataCatalog.objects.all()[:settings.OAI['BATCH_SIZE']]

response = self.client.get('/oai/?verb=ListIdentifiers&metadataPrefix=oai_dc&set=datacatalogs')
self.assertEqual(response.status_code, status.HTTP_200_OK)
records = self._get_results(response.content, '//o:header')
Expand Down Expand Up @@ -145,6 +165,20 @@ def test_list_records(self):
records = self._get_results(response.content, '//o:record')
self.assertTrue(len(records) == len(allRecords))

def test_list_records_for_drafts(self):
''' Tests that drafts are not returned from ListRecords '''
ms = settings.OAI['BATCH_SIZE']
allRecords = CatalogRecord.objects.filter(
data_catalog__catalog_json__identifier__in=MetaxOAIServer._get_default_set_filter())[:ms]

self._set_dataset_as_draft(25)
self._set_dataset_as_draft(26)

response = self.client.get('/oai/?verb=ListRecords&metadataPrefix=oai_fairdata_datacite')
self.assertEqual(response.status_code, status.HTTP_200_OK)
records = self._get_results(response.content, '//o:record')
self.assertFalse(len(records) == len(allRecords))

def test_list_records_urnresolver_from_datacatalogs_set(self):
response = self.client.get('/oai/?verb=ListRecords&metadataPrefix=oai_dc_urnresolver&set=datacatalogs')
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down Expand Up @@ -267,6 +301,26 @@ def test_get_record(self):
'//o:record/o:header/o:identifier[text()="%s"]' % self.identifier)
self.assertTrue(len(identifiers) == 1, response.content)

def test_get_record_for_drafts(self):
''' Tests that GetRecord doesn't return drafts '''

response = self.client.get(
'/oai/?verb=GetRecord&identifier=%s&metadataPrefix=oai_dc' % self.identifier)
self.assertEqual(response.status_code, status.HTTP_200_OK)
identifiers = self._get_results(response.content,
'//o:record/o:header/o:identifier[text()="%s"]' % self.identifier)
self.assertTrue(len(identifiers) == 1, response.content)

# Set same dataset as draft
self._set_dataset_as_draft(self.id)

response = self.client.get(
'/oai/?verb=GetRecord&identifier=%s&metadataPrefix=oai_dc' % self.identifier)
self.assertEqual(response.status_code, status.HTTP_200_OK)
identifiers = self._get_results(response.content,
'//o:record/o:header/o:identifier[text()="%s"]' % self.identifier)
self.assertTrue(len(identifiers) == 0, response.content)

def test_get_record_non_existing(self):
response = self.client.get('/oai/?verb=GetRecord&identifier=urn:non:existing&metadataPrefix=oai_dc')
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down

0 comments on commit a65eef9

Please sign in to comment.