Skip to content

Commit

Permalink
Merge pull request #211 from UW-GAC/feature/anvil-cdsa-group-error-me…
Browse files Browse the repository at this point in the history
…ssage

Show an error message when auditing and CDSA group does not exist in app
  • Loading branch information
amstilp authored Jul 6, 2023
2 parents 7953646 + 6d0ef90 commit c5471a2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
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

0 comments on commit c5471a2

Please sign in to comment.