Skip to content

Conversation

csbarnet
Copy link
Contributor

This PR addresses the issue reported here: #791
It also adds tests for this view.

A couple of things worth noting:
Although the main change was the change from 'request.is_ajax' to 'request.get("x-requested-with") == "XMLHttpRequest"', a couple of changes were made to enable testing.

I changed the way ALLOCATION_ACCOUNT_ENABLED was accessed from settings to enable testing. Originally, this value is assigned to a constant at the top of the file, so that it is only evaluated on import. However, this view is only accessible when this setting is assigned the non-default value. Using 'import_from_settings' in the view allows us to inject different values for the setting at test time using the 'override_settings' annotation.

I also changed the setup in the test for creation of the pi user to enable request.user.userprofile.is_pi to return True for a PI user.

…aced 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 <Christopher.Barnett@tufts.edu>

update creation of pi user in test setup to set userprofile.is_pi to True

Signed-off-by: Chris Barnett <Christopher.Barnett@tufts.edu>
@csbarnet csbarnet force-pushed the bug-fix-allocation-account branch from 727c8d0 to 39fd83a Compare October 2, 2025 14:06
@csbarnet csbarnet force-pushed the bug-fix-allocation-account branch from 8e23054 to 39fd83a Compare October 9, 2025 17:41
"""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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants