Skip to content

Commit 0fbba9b

Browse files
committed
Errbit | Fixing permissions | handling already queued errors for indexing
1 parent fdcbca3 commit 0fbba9b

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

core/collections/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,7 @@ class CollectionVersionProcessingView(CollectionBaseView, ConceptContainerProces
11641164

11651165
class CollectionVersionExportView(ConceptContainerExportMixin, CollectionVersionBaseView):
11661166
entity = 'Collection'
1167-
permission_classes = (CanViewConceptDictionary,)
1167+
permission_classes = (CanViewConceptDictionary, IsAuthenticated)
11681168
serializer_class = CollectionVersionExportSerializer
11691169

11701170
def handle_export_version(self):

core/common/mixins.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pydash import compact, get
1717
from rest_framework import status
1818
from rest_framework.mixins import ListModelMixin, CreateModelMixin
19+
from rest_framework.permissions import IsAuthenticated
1920
from rest_framework.response import Response
2021

2122
from core.common.constants import HEAD, ACCESS_TYPE_NONE, INCLUDE_FACETS, \
@@ -954,7 +955,7 @@ def save_as_new_version(self, user, **kwargs): # pylint: disable=too-many-branc
954955

955956

956957
class ConceptContainerExportMixin:
957-
permission_classes = (CanViewConceptDictionaryVersion, )
958+
permission_classes = (CanViewConceptDictionaryVersion, IsAuthenticated)
958959

959960
def get_object(self):
960961
queryset = self.get_queryset()
@@ -1033,7 +1034,7 @@ def delete(self, request, *args, **kwargs): # pylint: disable=unused-argument
10331034
class ConceptContainerProcessingMixin:
10341035
def get_permissions(self):
10351036
if self.request.method == 'POST':
1036-
return [HasOwnership(), ]
1037+
return [HasOwnership(), IsAuthenticated()]
10371038

10381039
return [CanViewConceptDictionary(), ]
10391040

core/integration_tests/tests_sources.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,20 @@ def test_post_202_version(self, s3_has_path_mock, export_source_mock):
11301130
s3_has_path_mock.assert_called_once_with("users/username/username_source1_v1.")
11311131
export_source_mock.apply_async.assert_called_once_with((self.source_v1.id,), queue='default', task_id=ANY)
11321132

1133+
@patch('core.sources.views.export_source')
1134+
@patch('core.services.storages.cloud.aws.S3.has_path')
1135+
def test_post_401_version_anonymous(self, s3_has_path_mock, export_source_mock):
1136+
export_source_mock.__name__ = 'export_source'
1137+
s3_has_path_mock.return_value = False
1138+
response = self.client.post(
1139+
self.source_v1.uri + 'export/',
1140+
format='json'
1141+
)
1142+
1143+
self.assertEqual(response.status_code, 401)
1144+
s3_has_path_mock.assert_not_called()
1145+
export_source_mock.apply_async.assert_not_called()
1146+
11331147
@patch('core.sources.views.export_source')
11341148
@patch('core.services.storages.cloud.aws.S3.exists')
11351149
def test_post_409_head(self, s3_exists_mock, export_source_mock):
@@ -1208,6 +1222,20 @@ def test_delete_204(self, s3_remove_mock, has_export_mock, export_path_mock):
12081222
self.assertEqual(response.status_code, 204)
12091223
s3_remove_mock.assert_called_once_with('v1/export/path')
12101224

1225+
@patch('core.sources.models.Source.version_export_path', new_callable=PropertyMock)
1226+
@patch('core.sources.models.Source.has_export')
1227+
@patch('core.services.storages.cloud.aws.S3.remove')
1228+
def test_delete_401_anonymous(self, s3_remove_mock, has_export_mock, export_path_mock):
1229+
has_export_mock.return_value = True
1230+
export_path_mock.return_value = 'v1/export/path'
1231+
response = self.client.delete(
1232+
self.source_v1.uri + 'export/',
1233+
format='json'
1234+
)
1235+
1236+
self.assertEqual(response.status_code, 401)
1237+
s3_remove_mock.assert_not_called()
1238+
12111239

12121240
class ExportSourceTaskTest(OCLAPITestCase):
12131241
@patch('core.common.utils.get_export_service')

core/sources/models.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import uuid
22
from collections import OrderedDict
33

4+
from celery_once import AlreadyQueued
45
from dirtyfields import DirtyFieldsMixin
56
from django.conf import settings
67
from django.contrib.postgres.fields import ArrayField
@@ -484,13 +485,19 @@ def index_mappings_async(self, user):
484485
user = user or self.updated_by
485486

486487
task = Task.new(queue='indexing', user=user, name=index_source_mappings.__name__)
487-
index_source_mappings.apply_async((self.id,), queue='indexing', persist_args=True, task_id=task.id)
488+
try:
489+
index_source_mappings.apply_async((self.id,), queue='indexing', persist_args=True, task_id=task.id)
490+
except AlreadyQueued:
491+
pass
488492

489493
def index_concepts_async(self, user):
490494
user = user or self.updated_by
491495

492496
task = Task.new(queue='indexing', user=user, name=index_source_concepts.__name__)
493-
index_source_concepts.apply_async((self.id,), queue='indexing', persist_args=True, task_id=task.id)
497+
try:
498+
index_source_concepts.apply_async((self.id,), queue='indexing', persist_args=True, task_id=task.id)
499+
except AlreadyQueued:
500+
pass
494501

495502
def get_export_task(self):
496503
return Task.find(name__iendswith='export_source', args__contains=[self.id])

core/sources/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ class SourceVersionProcessingView(SourceBaseView, ConceptContainerProcessingMixi
530530

531531
class SourceVersionExportView(ConceptContainerExportMixin, SourceVersionBaseView):
532532
entity = 'Source'
533-
permission_classes = (CanViewConceptDictionary,)
533+
permission_classes = (CanViewConceptDictionary, IsAuthenticated)
534534
serializer_class = SourceVersionExportSerializer
535535

536536
def handle_export_version(self):

0 commit comments

Comments
 (0)