From f1369bbf157fbd2c182bbebdeeb76b5ea3401ac2 Mon Sep 17 00:00:00 2001 From: gromdimon Date: Thu, 10 Aug 2023 15:35:37 +0200 Subject: [PATCH] fix: small cleanups --- .../tests/test_permissions_api_taskflow.py | 63 ++++++++++++------- samplesheets/tests/test_views_api_taskflow.py | 40 +++++++----- samplesheets/tests/test_views_taskflow.py | 2 +- samplesheets/urls.py | 2 +- samplesheets/views_api.py | 2 +- 5 files changed, 67 insertions(+), 42 deletions(-) diff --git a/samplesheets/tests/test_permissions_api_taskflow.py b/samplesheets/tests/test_permissions_api_taskflow.py index eec42bef..5587afdc 100644 --- a/samplesheets/tests/test_permissions_api_taskflow.py +++ b/samplesheets/tests/test_permissions_api_taskflow.py @@ -139,19 +139,14 @@ class TestIrodsAccessTicketAPIViewBase( def create_ticket(self): """Helper function to create a ticket""" - path = self.coll.path + '/ticket1' - ticket_str = 'ticket' - label = 'label' - date_expires = (timezone.localtime() + timedelta(days=1)).isoformat() - # Create ticket in database and iRODS ticket = self.make_irods_ticket( study=self.study, assay=self.assay, - path=path, + path=self.coll.path + '/ticket1', user=self.user_owner, - ticket=ticket_str, - label=label, - date_expires=date_expires, + ticket='ticket', + label='label', + date_expires=(timezone.localtime() + timedelta(days=1)).isoformat(), ) return ticket @@ -204,6 +199,11 @@ def test_get(self): self.assert_response_api(self.url, bad_users, 403) self.assert_response_api(self.url, self.anonymous, 401) + @override_settings(PROJECTROLES_ALLOW_ANONYMOUS=True) + def test_get_anon(self): + """Test get() with anonymous access""" + self.assert_response_api(self.url, self.anonymous, 401) + def test_get_archive(self): """Test get() with archived project""" self.project.set_archive() @@ -212,11 +212,11 @@ def test_get_archive(self): self.user_owner_cat, self.user_delegate_cat, self.user_contributor_cat, + self.user_guest_cat, + self.user_finder_cat, self.user_owner, self.user_delegate, self.user_contributor, - self.user_guest_cat, - self.user_finder_cat, self.user_guest, self.user_no_roles, ] @@ -258,6 +258,11 @@ def test_get(self): self.assert_response_api(self.url, bad_users, 403) self.assert_response_api(self.url, self.anonymous, 401) + @override_settings(PROJECTROLES_ALLOW_ANONYMOUS=True) + def test_get_anon(self): + """Test get() with anonymous access""" + self.assert_response_api(self.url, self.anonymous, 401) + def test_get_archive(self): """Test get() with archived project""" self.project.set_archive() @@ -266,11 +271,11 @@ def test_get_archive(self): self.user_owner_cat, self.user_delegate_cat, self.user_contributor_cat, + self.user_guest_cat, + self.user_finder_cat, self.user_owner, self.user_delegate, self.user_contributor, - self.user_guest_cat, - self.user_finder_cat, self.user_guest, self.user_no_roles, ] @@ -305,8 +310,8 @@ def setUp(self): def _delete_ticket(self): """Delete ticket created in setUp()""" access_ticket = IrodsAccessTicket.objects.all().first() - ticket = access_ticket.ticket - self.irods_backend.delete_ticket(self.irods, ticket) + ticket_str = access_ticket.ticket + self.irods_backend.delete_ticket(self.irods, ticket_str) access_ticket.delete() def test_create(self): @@ -341,6 +346,13 @@ def test_create(self): self.url, self.anonymous, 401, method='post', data=self.post_data ) + @override_settings(PROJECTROLES_ALLOW_ANONYMOUS=True) + def test_create_anon(self): + """Test post() with anonymous access""" + self.assert_response_api( + self.url, self.anonymous, 401, method='post', data=self.post_data + ) + def test_create_archive(self): """Test post() with archived project""" self.project.set_archive() @@ -349,11 +361,11 @@ def test_create_archive(self): self.user_owner_cat, self.user_delegate_cat, self.user_contributor_cat, + self.user_guest_cat, + self.user_finder_cat, self.user_owner, self.user_delegate, self.user_contributor, - self.user_guest_cat, - self.user_finder_cat, self.user_guest, self.user_no_roles, ] @@ -410,6 +422,13 @@ def test_update(self): self.url, self.anonymous, 401, method='put', data=self.post_data ) + @override_settings(PROJECTROLES_ALLOW_ANONYMOUS=True) + def test_update_anon(self): + """Test put() with anonymous access""" + self.assert_response_api( + self.url, self.anonymous, 401, method='put', data=self.post_data + ) + def test_update_archive(self): """Test put() with archived project""" self.project.set_archive() @@ -418,11 +437,11 @@ def test_update_archive(self): self.user_owner_cat, self.user_delegate_cat, self.user_contributor_cat, + self.user_guest_cat, + self.user_finder_cat, self.user_owner, self.user_delegate, self.user_contributor, - self.user_guest_cat, - self.user_finder_cat, self.user_guest, self.user_no_roles, ] @@ -437,7 +456,7 @@ def test_update_archive(self): ) -class TestIrodsAccessTicketDeleteAPIView(TestIrodsAccessTicketAPIViewBase): +class TestIrodsAccessTicketDestroyAPIView(TestIrodsAccessTicketAPIViewBase): """Test permissions for IrodsAccessTicketDeleteAPIView""" def create_irods_ticket(self): @@ -548,11 +567,11 @@ def test_get_archive(self): self.user_owner_cat, self.user_delegate_cat, self.user_contributor_cat, + self.user_guest_cat, + self.user_finder_cat, self.user_owner, self.user_delegate, self.user_contributor, - self.user_guest_cat, - self.user_finder_cat, self.user_guest, self.user_no_roles, ] diff --git a/samplesheets/tests/test_views_api_taskflow.py b/samplesheets/tests/test_views_api_taskflow.py index 9727521b..43992d12 100644 --- a/samplesheets/tests/test_views_api_taskflow.py +++ b/samplesheets/tests/test_views_api_taskflow.py @@ -15,8 +15,6 @@ from django.urls import reverse from django.utils import timezone -from appalerts.models import AppAlert - # Projectroles dependency from projectroles.models import SODAR_CONSTANTS from projectroles.plugins import get_backend_api @@ -66,6 +64,7 @@ IRODS_FILE_NAME2, INVALID_REDIS_URL, TICKET_STR, + TICKET_LABEL, ) # SODAR constants @@ -356,9 +355,9 @@ class TestIrodsAccessTicketListAPIView(TestIrodsAccessTicketAPIViewBase): def setUp(self): super().setUp() - self.ticket_str = 'ticket' + self.ticket_str = TICKET_STR self.path = self.coll.path - self.label = 'label' + self.label = TICKET_LABEL self.date_expires = None self.url = reverse( 'samplesheets:api_irods_ticket_list', @@ -465,9 +464,9 @@ class TestIrodsAccessTicketRetrieveAPIView(TestIrodsAccessTicketAPIViewBase): def setUp(self): super().setUp() - self.ticket_str = 'ticket' + self.ticket_str = TICKET_STR self.path = self.coll.path - self.label = 'label' + self.label = TICKET_LABEL self.date_expires = None self.ticket = self.make_irods_ticket( study=self.study, @@ -529,7 +528,7 @@ class TestIrodsAccessTicketCreateAPIView(TestIrodsAccessTicketAPIViewBase): def setUp(self): super().setUp() self.path = self.coll.path - self.label = 'label' + self.label = TICKET_LABEL self.date_expires = ( timezone.localtime() + timedelta(days=1) ).isoformat() @@ -572,7 +571,9 @@ def test_create(self): self.assertEqual(self.get_tl_event_count('create'), 1) self.assertEqual(self.get_app_alert_count('create'), 1) self.assertEqual( - AppAlert.objects.filter(alert_name='irods_ticket_create') + self.app_alert_model.objects.filter( + alert_name='irods_ticket_create' + ) .first() .user, self.user_delegate, @@ -614,7 +615,9 @@ def test_create_contributor(self): self.assertEqual(self.get_tl_event_count('create'), 1) self.assertEqual(self.get_app_alert_count('create'), 2) self.assertEqual( - AppAlert.objects.filter(alert_name='irods_ticket_create') + self.app_alert_model.objects.filter( + alert_name='irods_ticket_create' + ) .first() .user, self.user_delegate, @@ -710,14 +713,13 @@ def test_post_existing_ticket(self): self.assertEqual(self.get_app_alert_count('create'), 0) - class TestIrodsAccessTicketUpdateAPIView(TestIrodsAccessTicketAPIViewBase): """Tests for IrodsAccessTicketUpdateAPIView""" def setUp(self): super().setUp() - self.ticket_str = 'ticket' - self.label = 'label' + self.ticket_str = TICKET_STR + self.label = TICKET_LABEL self.date_expires = None self.ticket = self.make_irods_ticket( study=self.study, @@ -775,7 +777,9 @@ def test_update(self): self.assertEqual(self.get_tl_event_count('update'), 1) self.assertEqual(self.get_app_alert_count('update'), 2) self.assertEqual( - AppAlert.objects.filter(alert_name='irods_ticket_update') + self.app_alert_model.objects.filter( + alert_name='irods_ticket_update' + ) .first() .user, self.user_delegate, @@ -816,13 +820,13 @@ def test_update_invalid_date(self): self.assertEqual(self.get_app_alert_count('update'), 0) -class TestIrodsAccessTicketDeleteAPIView(TestIrodsAccessTicketAPIViewBase): +class TestIrodsAccessTicketDestroyAPIView(TestIrodsAccessTicketAPIViewBase): """Tests for IrodsAccessTicketDeleteAPIView""" def setUp(self): super().setUp() - self.ticket_str = 'ticket' - self.label = 'label' + self.ticket_str = TICKET_STR + self.label = TICKET_LABEL self.date_expires = ( timezone.localtime() + timedelta(days=1) ).isoformat() @@ -864,7 +868,9 @@ def test_delete(self): self.assertEqual(self.get_tl_event_count('delete'), 1) self.assertEqual(self.get_app_alert_count('delete'), 2) self.assertEqual( - AppAlert.objects.filter(alert_name='irods_ticket_delete') + self.app_alert_model.objects.filter( + alert_name='irods_ticket_delete' + ) .first() .user, self.user_delegate, diff --git a/samplesheets/tests/test_views_taskflow.py b/samplesheets/tests/test_views_taskflow.py index 3dfc5986..b1277bdb 100644 --- a/samplesheets/tests/test_views_taskflow.py +++ b/samplesheets/tests/test_views_taskflow.py @@ -80,9 +80,9 @@ SOURCE_ID = '0815' SAMPLE_ID = '0815-N1' INVALID_REDIS_URL = 'redis://127.0.0.1:6666/0' -TICKET_LABEL = 'TestTicket' TICKET_LABEL_UPDATED = 'TestTicketUpdated' TICKET_STR = 'q657xxx3i2x2b8vj' +TICKET_LABEL = 'TestTicket' IRODS_REQUEST_DESC_UPDATE = 'Updated' diff --git a/samplesheets/urls.py b/samplesheets/urls.py index 732dd4bf..17db4d1c 100644 --- a/samplesheets/urls.py +++ b/samplesheets/urls.py @@ -198,7 +198,7 @@ ), path( route='api/irods/ticket/delete/', - view=views_api.IrodsAccessTicketDeleteAPIView.as_view(), + view=views_api.IrodsAccessTicketDestroyAPIView.as_view(), name='api_irods_ticket_delete', ), path( diff --git a/samplesheets/views_api.py b/samplesheets/views_api.py index 348a275a..ca46678c 100644 --- a/samplesheets/views_api.py +++ b/samplesheets/views_api.py @@ -536,7 +536,7 @@ def put(self, request, *args, **kwargs): return Response(context, status=status.HTTP_200_OK) -class IrodsAccessTicketDeleteAPIView( +class IrodsAccessTicketDestroyAPIView( IrodsAccessTicketModifyMixin, SODARAPIBaseProjectMixin, DestroyAPIView ): """