Skip to content
Open
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
41 changes: 37 additions & 4 deletions coldfront/core/allocation/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -27,6 +27,7 @@
ResourceFactory,
UserFactory,
)
from coldfront.core.utils.common import import_from_settings

logging.disable(logging.CRITICAL)

Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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.")
8 changes: 4 additions & 4 deletions coldfront/core/allocation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a nitpick: import_from_settings is typically used to declare a constant at the module level. Sometimes it's used to declare a constant in another scope, and other times it's used to set an object attribute. Very rarely is it used in any other way.

$ rg '= import_from_settings' | wc -l
175
$ rg '^[A-Z_]+ = import_from_settings' | wc -l
136
$ rg '^\s+self\.[a-zA-Z_]+ = import_from_settings' | wc -l
26
$ rg '^\s+[a-zA-Z_]+ = import_from_settings' | wc -l
10
$ rg --no-filename '= import_from_settings' | grep -vP '^\s*(self\.)?[a-zA-Z_]+ = import_from_settings' | wc -l
3
$ rg --no-filename '= import_from_settings' | grep -vP '^\s*(self\.)?[a-zA-Z_]+ = import_from_settings'
    context["SYSTEM_MONITOR_DISPLAY_XDMOD_LINK"] = import_from_settings("SYSTEM_MONITOR_DISPLAY_XDMOD_LINK", None)
    context["SYSTEM_MONITOR_DISPLAY_MORE_STATUS_INFO_LINK"] = import_from_settings(
# ALLOCATION_ATTRIBUTE_VIEW_LIST = import_from_settings(

note: I have an open PR that removes these two SYSTEM_MONITOR_* examples.

return False
if self.request.user.is_superuser:
return True
Expand All @@ -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,
}
Expand All @@ -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
Expand Down