Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show an error message when auditing and CDSA group does not exist in app #211

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# ------------------------------------------------------------------------------
ANVIL_API_SERVICE_ACCOUNT_FILE = "foo"
ANVIL_DATA_ACCESS_GROUP_PREFIX = "TEST_PRIMED"
ANVIL_CDSA_GROUP_NAME = "TEST_PRIMED_CDSA"

# template tests require debug to be set
# get the last templates entry and set debug option
Expand Down
33 changes: 27 additions & 6 deletions primed/cdsa/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3352,9 +3352,8 @@ def setUp(self):
codename=AnVILProjectManagerAccess.VIEW_PERMISSION_CODENAME
)
)
self.anvil_cdsa_group = ManagedGroupFactory.create(
name=settings.ANVIL_CDSA_GROUP_NAME
)
# Create the test group.
self.anvil_cdsa_group = ManagedGroupFactory.create(name="TEST_PRIMED_CDSA")

def get_url(self, *args):
"""Get the url for the view being tested."""
Expand Down Expand Up @@ -3506,6 +3505,18 @@ def test_context_error_table_has_access(self):
)
self.assertIsNotNone(table.rows[0].get_cell_value("action"))

@override_settings(ANVIL_CDSA_GROUP_NAME="FOOBAR")
def test_anvil_cdsa_group_does_not_exist(self):
"""The view redirects with a message if the CDSA group does not exist in the app."""
self.client.force_login(self.user)
response = self.client.get(self.get_url())
self.assertRedirects(response, reverse("anvil_consortium_manager:index"))
# Check messages.
messages = [m.message for m in get_messages(response.wsgi_request)]
self.assertEqual(len(messages), 1)
self.assertIn("FOOBAR", str(messages[0]))
self.assertIn("does not exist", str(messages[0]))


class CDSAWorkspaceAuditTest(TestCase):
"""Tests for the SignedAgreementAudit view."""
Expand All @@ -3520,9 +3531,7 @@ def setUp(self):
codename=AnVILProjectManagerAccess.VIEW_PERMISSION_CODENAME
)
)
self.anvil_cdsa_group = ManagedGroupFactory.create(
name=settings.ANVIL_CDSA_GROUP_NAME
)
self.anvil_cdsa_group = ManagedGroupFactory.create(name="TEST_PRIMED_CDSA")

def get_url(self, *args):
"""Get the url for the view being tested."""
Expand Down Expand Up @@ -3681,6 +3690,18 @@ def test_context_error_table_has_access(self):
)
self.assertIsNotNone(table.rows[0].get_cell_value("action"))

@override_settings(ANVIL_CDSA_GROUP_NAME="FOOBAR")
def test_anvil_cdsa_group_does_not_exist(self):
"""The view redirects with a message if the CDSA group does not exist in the app."""
self.client.force_login(self.user)
response = self.client.get(self.get_url())
self.assertRedirects(response, reverse("anvil_consortium_manager:index"))
# Check messages.
messages = [m.message for m in get_messages(response.wsgi_request)]
self.assertEqual(len(messages), 1)
self.assertIn("FOOBAR", str(messages[0]))
self.assertIn("does not exist", str(messages[0]))


class StudyRecordsList(TestCase):
"""Tests for the StudyRecordsList view."""
Expand Down
57 changes: 44 additions & 13 deletions primed/cdsa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from django.core.exceptions import ValidationError
from django.db import transaction
from django.forms import inlineformset_factory
from django.http import Http404
from django.http import Http404, HttpResponseRedirect
from django.urls import reverse
from django.views.generic import DetailView, FormView, TemplateView
from django_tables2 import SingleTableView

Expand Down Expand Up @@ -294,33 +295,63 @@ class SignedAgreementAudit(AnVILConsortiumManagerViewRequired, TemplateView):
"""View to show audit results for `SignedAgreements`."""

template_name = "cdsa/signedagreement_audit.html"
ERROR_CDSA_GROUP_DOES_NOT_EXIST = (
"""The CDSA group "{}" does not exist in the app."""
)

def get(self, request, *args, **kwargs):
try:
self.audit = signed_agreement_audit.SignedAgreementAccessAudit()
except models.ManagedGroup.DoesNotExist:
messages.error(
self.request,
self.ERROR_CDSA_GROUP_DOES_NOT_EXIST.format(
settings.ANVIL_CDSA_GROUP_NAME
),
)
return HttpResponseRedirect(reverse("anvil_consortium_manager:index"))
return super().get(request, *args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Run the audit on all SignedAgreements.
audit = signed_agreement_audit.SignedAgreementAccessAudit()
audit.run_audit()
context["verified_table"] = audit.get_verified_table()
context["errors_table"] = audit.get_errors_table()
context["needs_action_table"] = audit.get_needs_action_table()
context["audit"] = audit
self.audit.run_audit()
context["verified_table"] = self.audit.get_verified_table()
context["errors_table"] = self.audit.get_errors_table()
context["needs_action_table"] = self.audit.get_needs_action_table()
context["audit"] = self.audit
return context


class CDSAWorkspaceAudit(AnVILConsortiumManagerViewRequired, TemplateView):
"""View to show audit results for `CDSAWorkspaces`."""

template_name = "cdsa/cdsaworkspace_audit.html"
ERROR_CDSA_GROUP_DOES_NOT_EXIST = (
"""The CDSA group "{}" does not exist in the app."""
)

def get(self, request, *args, **kwargs):
try:
self.audit = workspace_audit.WorkspaceAccessAudit()
except models.ManagedGroup.DoesNotExist:
messages.error(
self.request,
self.ERROR_CDSA_GROUP_DOES_NOT_EXIST.format(
settings.ANVIL_CDSA_GROUP_NAME
),
)
return HttpResponseRedirect(reverse("anvil_consortium_manager:index"))
return super().get(request, *args, **kwargs)

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Run the audit on all SignedAgreements.
audit = workspace_audit.WorkspaceAccessAudit()
audit.run_audit()
context["verified_table"] = audit.get_verified_table()
context["errors_table"] = audit.get_errors_table()
context["needs_action_table"] = audit.get_needs_action_table()
context["audit"] = audit
self.audit.run_audit()
context["verified_table"] = self.audit.get_verified_table()
context["errors_table"] = self.audit.get_errors_table()
context["needs_action_table"] = self.audit.get_needs_action_table()
context["audit"] = self.audit
return context


Expand Down