Skip to content

Commit

Permalink
add study plugin view tests (#1999, #2021)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Oct 9, 2024
1 parent 167ee6b commit 6a0c589
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Added
- Assay app unit tests (#1980)
- Missing assay plugin ``__init__.py`` files (#2014)
- Study plugin override via ISA-Tab comments (#1885)
- Token auth support in study plugin IGV XML serving views (#1999, #2021)

Changed
-------
Expand Down
3 changes: 2 additions & 1 deletion irodsbackend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def get(self, request, *args, **kwargs):
@fallback_to_auth_basic
class BasicAuthView(View):
"""
View for verifying login credentials for local users in iRODS.
View for verifying login credentials for local users in iRODS. Allows using
Knox token in place of password.
Should only be used in local development and testing situations or when an
external LDAP/AD login is not available.
Expand Down
57 changes: 57 additions & 0 deletions samplesheets/studyapps/cancer/tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Permission tests for the cancer study app"""

# NOTE: Basic auth tested in test_views

import os

from django.urls import reverse

from samplesheets.models import GenericMaterial
from samplesheets.tests.test_io import SHEET_DIR
from samplesheets.tests.test_permissions import SamplesheetsPermissionTestBase


# Local constants
SHEET_PATH = os.path.join(SHEET_DIR, 'bih_cancer.zip')
SOURCE_ID = 'normal1'


class TestIGVSessionFileRenderView(SamplesheetsPermissionTestBase):
"""Tests for IGVSessionFileRenderView permissions"""

def setUp(self):
super().setUp()
self.investigation = self.import_isa_from_file(SHEET_PATH, self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.source = GenericMaterial.objects.get(
study=self.study, name=SOURCE_ID
)
self.url = reverse(
'samplesheets.studyapps.cancer:igv',
kwargs={'genericmaterial': self.source.sodar_uuid},
)

def test_get(self):
"""Test IGVSessionFileRenderView GET"""
good_users = [
self.superuser,
self.user_owner_cat, # Inherited
self.user_delegate_cat, # Inherited
self.user_contributor_cat, # Inherited
self.user_guest_cat, # Inherited
self.user_owner,
self.user_delegate,
self.user_contributor,
self.user_guest,
]
bad_users = [self.user_finder_cat, self.user_no_roles]
self.assert_response(self.url, good_users, 200)
self.assert_response(self.url, bad_users, 302)
self.assert_response(self.url, self.anonymous, 401)
# Test public project
self.project.set_public()
self.assert_response(
self.url, [self.user_finder_cat, self.user_no_roles], 200
)
self.assert_response(self.url, self.anonymous, 401)
103 changes: 103 additions & 0 deletions samplesheets/studyapps/cancer/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
"""View tests for the cancer study app"""

# NOTE: We don't need to add files in iRODS to test this view

import base64
import os

from django.urls import reverse

# Projectroles dependency
from projectroles.tests.test_views_api import (
SODARAPIViewTestMixin,
EMPTY_KNOX_TOKEN,
)

from samplesheets.models import GenericMaterial
from samplesheets.tests.test_io import SHEET_DIR
from samplesheets.tests.test_models import SampleSheetModelMixin
from samplesheets.tests.test_views import SamplesheetsViewTestBase


# Local constants
SHEET_PATH = os.path.join(SHEET_DIR, 'bih_cancer.zip')
SOURCE_ID = 'normal1'


class TestIGVSessionFileRenderView(
SODARAPIViewTestMixin,
SampleSheetModelMixin,
SamplesheetsViewTestBase,
):
"""Tests for cancer plugin IGVSessionFileRenderView"""

@staticmethod
def _get_auth_header(username, password):
"""Return basic auth header"""
credentials = base64.b64encode(
'{}:{}'.format(username, password).encode('utf-8')
).strip()
return {
'HTTP_AUTHORIZATION': 'Basic {}'.format(credentials.decode('utf-8'))
}

def setUp(self):
super().setUp()
self.investigation = self.import_isa_from_file(SHEET_PATH, self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.source = GenericMaterial.objects.get(
study=self.study, name=SOURCE_ID
)
self.url = reverse(
'samplesheets.studyapps.cancer:igv',
kwargs={'genericmaterial': self.source.sodar_uuid},
)

def test_get(self):
"""Test IGVSessionFileRenderView GET"""
with self.login(self.user_contributor):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get('Content-Disposition'),
'attachment; filename="{}.case.igv.xml"'.format(SOURCE_ID),
)
# NOTE: XML forming tested in TestGetIGVXML

def test_get_basic_auth(self):
"""Test GET with basic auth"""
response = self.client.get(
self.url,
**self._get_auth_header(self.user_contributor.username, 'password')
)
self.assertEqual(response.status_code, 200)

def test_get_token(self):
"""Test GET with Knox token"""
knox_token = self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(self.user_contributor.username, knox_token)
)
self.assertEqual(response.status_code, 200)

def test_get_token_invalid(self):
"""Test GET with invalid Knox token"""
self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(
self.user_contributor.username, EMPTY_KNOX_TOKEN
)
)
self.assertEqual(response.status_code, 401)

def test_get_token_wrong_user(self):
"""Test GET with Knox token and wrong user"""
knox_token = self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(self.user_delegate.username, knox_token)
)
self.assertEqual(response.status_code, 401)
57 changes: 57 additions & 0 deletions samplesheets/studyapps/germline/tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Permission tests for the germline study app"""

# NOTE: Basic auth tested in test_views

import os

from django.urls import reverse

from samplesheets.models import GenericMaterial
from samplesheets.tests.test_io import SHEET_DIR
from samplesheets.tests.test_permissions import SamplesheetsPermissionTestBase


# Local constants
SHEET_PATH = os.path.join(SHEET_DIR, 'bih_germline.zip')
SOURCE_ID = 'p1'


class TestIGVSessionFileRenderView(SamplesheetsPermissionTestBase):
"""Tests for IGVSessionFileRenderView permissions"""

def setUp(self):
super().setUp()
self.investigation = self.import_isa_from_file(SHEET_PATH, self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.source = GenericMaterial.objects.get(
study=self.study, name=SOURCE_ID
)
self.url = reverse(
'samplesheets.studyapps.germline:igv',
kwargs={'genericmaterial': self.source.sodar_uuid},
)

def test_get(self):
"""Test IGVSessionFileRenderView GET"""
good_users = [
self.superuser,
self.user_owner_cat, # Inherited
self.user_delegate_cat, # Inherited
self.user_contributor_cat, # Inherited
self.user_guest_cat, # Inherited
self.user_owner,
self.user_delegate,
self.user_contributor,
self.user_guest,
]
bad_users = [self.user_finder_cat, self.user_no_roles]
self.assert_response(self.url, good_users, 200)
self.assert_response(self.url, bad_users, 302)
self.assert_response(self.url, self.anonymous, 401)
# Test public project
self.project.set_public()
self.assert_response(
self.url, [self.user_finder_cat, self.user_no_roles], 200
)
self.assert_response(self.url, self.anonymous, 401)
104 changes: 104 additions & 0 deletions samplesheets/studyapps/germline/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
"""View tests for the germline study app"""

# NOTE: We don't need to add files in iRODS to test this view

import base64
import os

from django.urls import reverse

# Projectroles dependency
from projectroles.tests.test_views_api import (
SODARAPIViewTestMixin,
EMPTY_KNOX_TOKEN,
)

from samplesheets.models import GenericMaterial
from samplesheets.tests.test_io import SHEET_DIR
from samplesheets.tests.test_models import SampleSheetModelMixin
from samplesheets.tests.test_views import SamplesheetsViewTestBase


# Local constants
SHEET_PATH = os.path.join(SHEET_DIR, 'bih_germline.zip')
SOURCE_ID = 'p1'
FAMILY_ID = 'FAM_p1'


class TestIGVSessionFileRenderView(
SODARAPIViewTestMixin,
SampleSheetModelMixin,
SamplesheetsViewTestBase,
):
"""Tests for germline plugin IGVSessionFileRenderView"""

@staticmethod
def _get_auth_header(username, password):
"""Return basic auth header"""
credentials = base64.b64encode(
'{}:{}'.format(username, password).encode('utf-8')
).strip()
return {
'HTTP_AUTHORIZATION': 'Basic {}'.format(credentials.decode('utf-8'))
}

def setUp(self):
super().setUp()
self.investigation = self.import_isa_from_file(SHEET_PATH, self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.source = GenericMaterial.objects.get(
study=self.study, name=SOURCE_ID
)
self.url = reverse(
'samplesheets.studyapps.germline:igv',
kwargs={'genericmaterial': self.source.sodar_uuid},
)

def test_get(self):
"""Test IGVSessionFileRenderView GET"""
with self.login(self.user_contributor):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.get('Content-Disposition'),
'attachment; filename="{}.pedigree.igv.xml"'.format(FAMILY_ID),
)
# NOTE: XML forming tested in TestGetIGVXML

def test_get_basic_auth(self):
"""Test GET with basic auth"""
response = self.client.get(
self.url,
**self._get_auth_header(self.user_contributor.username, 'password')
)
self.assertEqual(response.status_code, 200)

def test_get_token(self):
"""Test GET with Knox token"""
knox_token = self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(self.user_contributor.username, knox_token)
)
self.assertEqual(response.status_code, 200)

def test_get_token_invalid(self):
"""Test GET with invalid Knox token"""
self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(
self.user_contributor.username, EMPTY_KNOX_TOKEN
)
)
self.assertEqual(response.status_code, 401)

def test_get_token_wrong_user(self):
"""Test GET with Knox token and wrong user"""
knox_token = self.get_token(self.user_contributor)
response = self.client.get(
self.url,
**self._get_auth_header(self.user_delegate.username, knox_token)
)
self.assertEqual(response.status_code, 401)

0 comments on commit 6a0c589

Please sign in to comment.