From 39fd83aca31e913b4b61a919523377f7b92a9a37 Mon Sep 17 00:00:00 2001 From: Chris Barnett Date: Fri, 12 Sep 2025 19:30:47 -0400 Subject: [PATCH] replaced removed method is_ajax from AllocationAccountCreateView replaced ALLOCATION_ACCOUNT_ENABLED constant with call to settings to enable testing updated user test for pi status to enable testing created tests for AllocationAccountCreateView Signed-off-by: Chris Barnett update creation of pi user in test setup to set userprofile.is_pi to True Signed-off-by: Chris Barnett --- coldfront/core/allocation/tests/test_views.py | 41 +++++++++++++++++-- coldfront/core/allocation/views.py | 8 ++-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/coldfront/core/allocation/tests/test_views.py b/coldfront/core/allocation/tests/test_views.py index 4180a31d8..7aa9bdee3 100644 --- a/coldfront/core/allocation/tests/test_views.py +++ b/coldfront/core/allocation/tests/test_views.py @@ -5,7 +5,7 @@ import logging from http import HTTPStatus -from django.test import TestCase +from django.test import TestCase, override_settings from django.urls import reverse from coldfront.core.allocation.models import ( @@ -27,6 +27,7 @@ ResourceFactory, UserFactory, ) +from coldfront.core.utils.common import import_from_settings logging.disable(logging.CRITICAL) @@ -39,8 +40,10 @@ class AllocationViewBaseTest(TestCase): @classmethod def setUpTestData(cls): """Test Data setup for all allocation view tests.""" + pi_user = UserFactory() + pi_user.userprofile.is_pi = True AllocationStatusChoiceFactory(name="New") - cls.project = ProjectFactory(status=ProjectStatusChoiceFactory(name="Active")) + cls.project = ProjectFactory(pi=pi_user, status=ProjectStatusChoiceFactory(name="Active")) cls.allocation = AllocationFactory(project=cls.project) cls.allocation.resources.add(ResourceFactory(name="holylfs07/tier1")) # create allocation user that belongs to project @@ -52,8 +55,8 @@ def setUpTestData(cls): cls.proj_nonallocation_user = proj_nonallocation_user.user cls.admin_user = UserFactory(is_staff=True, is_superuser=True) manager_role = ProjectUserRoleChoiceFactory(name="Manager") - pi_user = ProjectUserFactory(user=cls.project.pi, project=cls.project, role=manager_role) - cls.pi_user = pi_user.user + ProjectUserFactory(user=pi_user, project=cls.project, role=manager_role) + cls.pi_user = pi_user # make a quota TB allocation attribute AllocationAttributeFactory( allocation=cls.allocation, @@ -373,3 +376,33 @@ def setUp(self): def test_allocationnotecreateview_access(self): self.allocation_access_tstbase(self.url) + + +@override_settings(ALLOCATION_ACCOUNT_ENABLED=True) +class AllocationAccountCreateViewTest(AllocationViewBaseTest): + """Tests for the AllocationAccountCreateView""" + + def setUp(self): + self.url = "/allocation/add-allocation-account/" + + def test_allocationaccountcreateview_access(self): + self.assertTrue(import_from_settings("ALLOCATION_ACCOUNT_ENABLED", False)) + self.allocation_access_tstbase(self.url) + utils.test_user_can_access(self, self.pi_user, self.url) + + def test_allocationaccountcreateview_get_form(self): + self.client.force_login(self.pi_user, backend=BACKEND) + response = self.client.get(self.url) + self.assertContains(response, "Add account names that can be associated with allocations") + + def test_allocationaccountcreateview_post_form(self): + self.client.force_login(self.pi_user, backend=BACKEND) + valid_data = {"name": "deptCE1234"} + response = self.client.post(self.url, data=valid_data, follow=True) + self.assertContains(response, "deptCE1234") + + def test_allocationaccountcreateview_post_invalid_form(self): + self.client.force_login(self.pi_user, backend=BACKEND) + invalid_data = {"name": ""} + response = self.client.post(self.url, data=invalid_data, follow=True) + self.assertContains(response, "This field is required.") diff --git a/coldfront/core/allocation/views.py b/coldfront/core/allocation/views.py index 841858292..ad4d13e1a 100644 --- a/coldfront/core/allocation/views.py +++ b/coldfront/core/allocation/views.py @@ -1640,7 +1640,7 @@ class AllocationAccountCreateView(LoginRequiredMixin, UserPassesTestMixin, Creat def test_func(self): """UserPassesTestMixin Tests""" - if not ALLOCATION_ACCOUNT_ENABLED: + if not import_from_settings("ALLOCATION_ACCOUNT_ENABLED", False): return False if self.request.user.is_superuser: return True @@ -1652,14 +1652,14 @@ def test_func(self): def form_invalid(self, form): response = super().form_invalid(form) - if self.request.is_ajax(): + if self.request.headers.get("x-requested-with") == "XMLHttpRequest": return JsonResponse(form.errors, status=400) return response def form_valid(self, form): form.instance.user = self.request.user response = super().form_valid(form) - if self.request.is_ajax(): + if self.request.headers.get("x-requested-with") == "XMLHttpRequest": data = { "pk": self.object.pk, } @@ -1678,7 +1678,7 @@ class AllocationAccountListView(LoginRequiredMixin, UserPassesTestMixin, ListVie def test_func(self): """UserPassesTestMixin Tests""" - if not ALLOCATION_ACCOUNT_ENABLED: + if not import_from_settings("ALLOCATION_ACCOUNT_ENABLED", False): return False if self.request.user.is_superuser: return True