Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon committed Aug 31, 2023
1 parent db3f9d4 commit 3607692
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 65 deletions.
3 changes: 1 addition & 2 deletions samplesheets/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,7 @@ def clean(self):
irods_backend, self.project, self.instance, cleaned_data
)
if errors:
field, error = errors
self.add_error(field, error)
self.add_error(*errors)
return self.cleaned_data


Expand Down
3 changes: 1 addition & 2 deletions samplesheets/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,5 @@ def validate(self, attrs):
irods_backend, self.context['project'], self.instance, attrs
)
if errors:
field, error = errors
raise serializers.ValidationError('{}: {}'.format(field, error))
raise serializers.ValidationError('{}: {}'.format(*errors))
return attrs
38 changes: 18 additions & 20 deletions samplesheets/tests/test_permissions_api_taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
)


# Local constants
LABEL_CREATE = 'label'
LABEL_UPDATE = 'label_update'


# Base Classes and Mixins ------------------------------------------------------


Expand Down Expand Up @@ -157,7 +162,6 @@ class TestIrodsAccessTicketListAPIView(IrodsAccessTicketAPIViewTestBase):

def setUp(self):
super().setUp()
# Set up URLs
self.url = (
reverse(
'samplesheets:api_irods_ticket_list',
Expand All @@ -171,7 +175,7 @@ def setUp(self):
path=self.coll.path + '/ticket1',
user=self.user_owner,
ticket='ticket',
label='label',
label=LABEL_CREATE,
date_expires=(timezone.localtime() + timedelta(days=1)).isoformat(),
)

Expand Down Expand Up @@ -227,14 +231,13 @@ class TestIrodsAccessTicketRetrieveAPIView(IrodsAccessTicketAPIViewTestBase):

def setUp(self):
super().setUp()
# Set up URLs
self.ticket = self.make_irods_ticket(
study=self.study,
assay=self.assay,
path=self.coll.path + '/ticket1',
user=self.user_owner,
ticket='ticket',
label='label',
label=LABEL_CREATE,
date_expires=(timezone.localtime() + timedelta(days=1)).isoformat(),
)
self.url = reverse(
Expand Down Expand Up @@ -292,33 +295,31 @@ def test_get_archive(self):
class TestIrodsAccessTicketCreateAPIView(IrodsAccessTicketAPIViewTestBase):
"""Test permissions for IrodsAccessTicketCreateAPIView"""

def _delete_ticket(self):
"""Delete ticket created in test case"""
access_ticket = IrodsAccessTicket.objects.all().first()
ticket_str = access_ticket.ticket
self.irods_backend.delete_ticket(self.irods, ticket_str)
access_ticket.delete()

def setUp(self):
super().setUp()
# Set up URLs
self.url = reverse(
'samplesheets:api_irods_ticket_create',
kwargs={'project': self.project.sodar_uuid},
)
self.path = self.coll.path
self.label = 'label'
self.date_expires = (
timezone.localtime() + timedelta(days=1)
).isoformat()
# Set up post data
self.post_data = {
'assay': self.assay.pk,
'path': self.path,
'label': self.label,
'label': LABEL_CREATE,
'date_expires': self.date_expires,
}

def _delete_ticket(self):
"""Delete ticket created in test case"""
access_ticket = IrodsAccessTicket.objects.all().first()
ticket_str = access_ticket.ticket
self.irods_backend.delete_ticket(self.irods, ticket_str)
access_ticket.delete()

def test_create(self):
"""Test IrodsAccessTicketCreateAPIView POST"""
good_users = [
Expand Down Expand Up @@ -395,23 +396,21 @@ class TestIrodsAccessTicketUpdateAPIView(IrodsAccessTicketAPIViewTestBase):

def setUp(self):
super().setUp()
# Set up URLs
self.ticket = self.make_irods_ticket(
study=self.study,
assay=self.assay,
path=self.coll.path + '/ticket1',
user=self.user_owner,
ticket='ticket',
label='label',
label=LABEL_CREATE,
date_expires=(timezone.localtime() + timedelta(days=1)).isoformat(),
)
self.url = reverse(
'samplesheets:api_irods_ticket_update',
kwargs={'irodsaccessticket': self.ticket.sodar_uuid},
)
# Set up post data
self.label_update = 'label_update'
self.post_data = {'label': self.label_update}
self.post_data = {'label': LABEL_UPDATE}

def test_update(self):
"""Test IrodsAccessTicketUpdateAPIView PATCH"""
Expand Down Expand Up @@ -480,15 +479,14 @@ class TestIrodsAccessTicketDestroyAPIView(IrodsAccessTicketAPIViewTestBase):
def _create_irods_ticket(self):
# Create ticket in database and iRODS
ticket_str = 'ticket'
label = 'label'
# Create ticket in database and iRODS
self.ticket = self.make_irods_ticket(
study=self.study,
assay=self.assay,
path=self.coll.path,
user=self.user_owner,
ticket=ticket_str,
label=label,
label=LABEL_CREATE,
date_expires=timezone.localtime() + timedelta(days=1),
)
self.irods_backend.issue_ticket(
Expand Down
13 changes: 4 additions & 9 deletions samplesheets/tests/test_views_api_taskflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
)
from samplesheets.views_api import (
IRODS_QUERY_ERROR_MSG,
IRODS_TICKETS_NOT_FOUND_MSG,
)

from samplesheets.tests.test_io import SampleSheetIOMixin, SHEET_DIR
Expand Down Expand Up @@ -392,9 +391,8 @@ def test_get_no_tickets(self):
self.assertEqual(IrodsAccessTicket.objects.count(), 0)
with self.login(self.user_contrib):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 404)
expected = {'detail': IRODS_TICKETS_NOT_FOUND_MSG}
self.assertEqual(json.loads(response.content), expected)
self.assertEqual(response.status_code, 200)
self.assertEqual(json.loads(response.content), [])

def test_get_active(self):
"""Test GET IrodsAccessTicketListAPIView with active = True"""
Expand Down Expand Up @@ -761,10 +759,7 @@ def test_patch(self):
with self.login(self.user_contrib):
response = self.client.patch(
self.url,
{
'label': LABEL_UPDATE,
'date_expires': self.date_expires_update,
},
{'date_expires': self.date_expires_update},
)

self.assertEqual(response.status_code, 200)
Expand All @@ -773,7 +768,7 @@ def test_patch(self):
)
expected = {
'sodar_uuid': str(self.ticket.sodar_uuid),
'label': LABEL_UPDATE,
'label': self.label,
'ticket': self.ticket.ticket,
'assay': self.ticket.assay.pk,
'study': self.ticket.study.pk,
Expand Down
49 changes: 17 additions & 32 deletions samplesheets/views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,9 @@ class IrodsAccessTicketListAPIView(
**Methods:** ``GET``
**Query parameters:** ``active`` (boolean, default ``0``)
**Query parameters:**
**Returns:**
- ``tickets``: List of iRODS access tickets
- ``active`` (boolean, default ``0``)
"""

permission_required = 'samplesheets.edit_sheet'
Expand All @@ -346,24 +344,18 @@ def get_queryset(self):
active = bool(int(active))
if active:
tickets = [t for t in tickets if t.is_active()]
if not tickets:
raise NotFound(IRODS_TICKETS_NOT_FOUND_MSG)
return tickets


class IrodsAccessTicketRetrieveAPIView(
IrodsAccessTicketModifyMixin, SODARAPIBaseProjectMixin, RetrieveAPIView
IrodsAccessTicketModifyMixin, SODARAPIGenericProjectMixin, RetrieveAPIView
):
"""
Retrieve an iRODS access ticket for a project.
**URL:** ``/samplesheets/api/irods/ticket/retrieve/{IrodsAccessTicket.sodar_uuid}``
**Methods:** ``GET``
**Returns:**
- ``ticket``: iRODS access ticket
"""

lookup_field = 'sodar_uuid'
Expand All @@ -379,7 +371,7 @@ def get_object(self):


class IrodsAccessTicketCreateAPIView(
IrodsAccessTicketModifyMixin, SODARAPIBaseProjectMixin, CreateAPIView
IrodsAccessTicketModifyMixin, SODARAPIGenericProjectMixin, CreateAPIView
):
"""
Create an iRODS access ticket for a project.
Expand All @@ -393,10 +385,6 @@ class IrodsAccessTicketCreateAPIView(
- ``path``: iRODS path
- ``label``: Label (string)
- ``date_expires``: Expiration date (YYYY-MM-DDThh:mm:ssZ, optional)
**Returns:**
- ``ticket``: iRODS access ticket
"""

permission_required = 'samplesheets.edit_sheet'
Expand All @@ -411,22 +399,19 @@ def get_serializer_context(self):
def perform_create(self, serializer):
"""Override perform_create() to create IrodsAccessTicket"""
irods_backend = get_backend_api('omics_irods')
if serializer.is_valid():
try:
with irods_backend.get_session() as irods:
ticket = irods_backend.issue_ticket(
irods,
'read',
serializer.validated_data.get('path'),
ticket_str=build_secret(16),
expiry_date=serializer.validated_data.get(
'date_expires'
),
)
except Exception as ex:
raise ValidationError(
'{} {}'.format('Creating ' + IRODS_TICKET_EX_MSG + ':', ex)
try:
with irods_backend.get_session() as irods:
ticket = irods_backend.issue_ticket(
irods,
'read',
serializer.validated_data.get('path'),
ticket_str=build_secret(16),
expiry_date=serializer.validated_data.get('date_expires'),
)
except Exception as ex:
raise ValidationError(
'{} {}'.format('Creating ' + IRODS_TICKET_EX_MSG + ':', ex)
)

serializer.validated_data['ticket'] = ticket.ticket
serializer.save()
Expand Down Expand Up @@ -468,9 +453,9 @@ def get_object(self):

def perform_update(self, serializer):
"""Override perform_update() to update IrodsAccessTicket"""
serializer.save()
if not set(serializer.initial_data) & {'label', 'date_expires'}:
raise ValidationError(IRODS_TICKET_NO_UPDATE_FIELDS_MSG)
serializer.save()
# Create timeline event
self.create_timeline_event(serializer.instance, 'update')
# Add app alerts to owners/delegates
Expand Down

0 comments on commit 3607692

Please sign in to comment.