Code cleanup

Had some older code lying around
This commit is contained in:
zandercymatics 2023-08-25 08:34:13 -06:00
parent 026222327c
commit 0378f1570e
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 24 additions and 51 deletions

View file

@ -171,24 +171,14 @@ class DomainAdmin(ListHeaderAdmin):
return HttpResponseRedirect(reverse("domain", args=(obj.id,))) return HttpResponseRedirect(reverse("domain", args=(obj.id,)))
return super().response_change(request, obj) return super().response_change(request, obj)
# Sets domain_id as a context var def change_view(self, request, object_id):
def change_view(self, request, object_id, form_url="", extra_context=None): # If the analyst was recently editing
if "analyst_action" in request.session: if "analyst_action" in request.session:
# If an analyst performed an edit action,
# delete the session variable # delete the session variable
del request.session["analyst_action"] del request.session["analyst_action"]
# delete the associated location # delete the associated location
del request.session["analyst_action_location"] del request.session["analyst_action_location"]
return super().change_view(request, object_id)
extra_context = extra_context or {}
extra_context["domain_id"] = object_id
return super().change_view(
request,
object_id,
form_url,
extra_context=extra_context,
)
def has_change_permission(self, request, obj=None): def has_change_permission(self, request, obj=None):
# Fixes a bug wherein users which are only is_staff # Fixes a bug wherein users which are only is_staff
@ -196,7 +186,6 @@ class DomainAdmin(ListHeaderAdmin):
# but cannot access this page when it is a request of type POST. # but cannot access this page when it is a request of type POST.
if request.user.is_staff: if request.user.is_staff:
return True return True
return super().has_change_permission(request, obj) return super().has_change_permission(request, obj)

View file

@ -14,10 +14,9 @@ from registrar.models import (
DomainInformation, DomainInformation,
User, User,
DomainInvitation, DomainInvitation,
Domain
) )
from registrar.models.domain import Domain
from .common import ( from .common import (
AuditedAdminMockData,
completed_application, completed_application,
generic_domain_object, generic_domain_object,
mock_user, mock_user,
@ -25,11 +24,8 @@ from .common import (
create_user, create_user,
multiple_unalphabetical_domain_objects, multiple_unalphabetical_domain_objects,
) )
from django.contrib.sessions.backends.db import SessionStore from django.contrib.sessions.backends.db import SessionStore
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.conf import settings from django.conf import settings
from unittest.mock import MagicMock from unittest.mock import MagicMock
import boto3_mocking # type: ignore import boto3_mocking # type: ignore
@ -651,7 +647,6 @@ class DomainSessionVariableTest(TestCase):
self.factory = RequestFactory() self.factory = RequestFactory()
self.admin = DomainAdmin(Domain, None) self.admin = DomainAdmin(Domain, None)
self.client = Client(HTTP_HOST="localhost:8080") self.client = Client(HTTP_HOST="localhost:8080")
self.superuser = create_superuser()
def test_session_variables_set_correctly(self): def test_session_variables_set_correctly(self):
"""Checks if session variables are being set correctly""" """Checks if session variables are being set correctly"""
@ -660,12 +655,7 @@ class DomainSessionVariableTest(TestCase):
self.client.login(username="superuser", password=p) self.client.login(username="superuser", password=p)
dummy_domain_information: DomainInformation = generic_domain_object("information", "session") dummy_domain_information: DomainInformation = generic_domain_object("information", "session")
request = self.factory.post( request = self.get_factory_post_edit_domain(dummy_domain_information.domain.pk)
reverse('admin:registrar_domain_change', args=(dummy_domain_information.domain.pk,)),
{'_edit_domain': 'true'},
follow=True
)
self.populate_session_values(request, dummy_domain_information.domain) self.populate_session_values(request, dummy_domain_information.domain)
self.assertEqual(request.session['analyst_action'], 'edit') self.assertEqual(request.session['analyst_action'], 'edit')
@ -676,19 +666,13 @@ class DomainSessionVariableTest(TestCase):
p = "adminpass" p = "adminpass"
self.client.login(username="superuser", password=p) self.client.login(username="superuser", password=p)
# We need to create multiple of these to ensure data is consistent across all of them dummy_domain_information_list: [DomainInformation] = multiple_unalphabetical_domain_objects("information")
dummy_domain_information_list: [DomainInformation] = multiple_unalphabetical_domain_objects("invitation")
for item in dummy_domain_information_list: for item in dummy_domain_information_list:
request = self.factory.post( request = self.get_factory_post_edit_domain(item.domain.pk)
reverse('admin:registrar_domain_change', args=(item.pk,)),
{'_edit_domain': 'true'},
follow=True
)
self.populate_session_values(request, item) self.populate_session_values(request, item)
self.assertEqual(request.session['analyst_action'], 'edit') self.assertEqual(request.session['analyst_action'], 'edit')
self.assertEqual(request.session['analyst_action_location'], item.pk) self.assertEqual(request.session['analyst_action_location'], item.domain.pk)
def test_session_variables_concurrent_requests(self): def test_session_variables_concurrent_requests(self):
""" Simulates two requests at once """ """ Simulates two requests at once """
@ -730,4 +714,14 @@ class DomainSessionVariableTest(TestCase):
request.user = self.client request.user = self.client
request.session = SessionStore() request.session = SessionStore()
request.session.create() request.session.create()
self.admin.response_change(request, domain_object) self.admin.response_change(request, domain_object)
def get_factory_post_edit_domain(self, primary_key):
"""Posts to registrar domain change
with the edit domain button 'clicked',
then returns the factory object"""
return self.factory.post(
reverse('admin:registrar_domain_change', args=(primary_key,)),
{'_edit_domain': 'true'},
follow=True
)

View file

@ -2,9 +2,7 @@
from django.contrib.auth.mixins import PermissionRequiredMixin from django.contrib.auth.mixins import PermissionRequiredMixin
from registrar.models import DomainApplication, DomainInvitation from registrar.models import DomainApplication, DomainInvitation, DomainInformation, UserDomainRole
from registrar.models import DomainInformation, UserDomainRole
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -34,27 +32,20 @@ class DomainPermission(PermissionsLoginMixin):
return False return False
pk = self.kwargs["pk"] pk = self.kwargs["pk"]
# If pk is none then something went very wrong... # If pk is none then something went very wrong...
if pk is None: if pk is None:
raise ValueError("Primary key is None") raise ValueError("Primary key is None")
# Checks if the creator is the user requesting this item
user_is_creator: bool = UserDomainRole.objects.filter(
user=self.request.user, domain__id=pk
).exists()
# user needs to have a role on the domain # user needs to have a role on the domain
if user_is_creator: if UserDomainRole.objects.filter(
user=self.request.user, domain__id=pk
).exists():
return True return True
# ticket 806 # ticket 806
requested_domain: DomainInformation = None requested_domain: DomainInformation = None
try: try:
requested_domain = DomainInformation.objects.get(id=pk) requested_domain = DomainInformation.objects.get(id=pk)
except DomainInformation.DoesNotExist: except DomainInformation.DoesNotExist:
# Q: While testing, I saw that, application-wide, if you go to a domain # Q: While testing, I saw that, application-wide, if you go to a domain
# that does not exist, for example, # that does not exist, for example,
@ -62,7 +53,7 @@ class DomainPermission(PermissionsLoginMixin):
# the page throws a 403 error, instead of a 404. # the page throws a 403 error, instead of a 404.
# Do we want it to throw a 404 instead? # Do we want it to throw a 404 instead?
# Basically, should this be Http404()? # Basically, should this be Http404()?
logger.warning(f"Domain with PK {pk} does not exist") logger.debug(f"Domain with PK {pk} does not exist")
return False return False
# Analysts may manage domains, when they are in these statuses: # Analysts may manage domains, when they are in these statuses:
@ -79,7 +70,6 @@ class DomainPermission(PermissionsLoginMixin):
) )
session = self.request.session session = self.request.session
# Check if the user is attempting a valid edit action. # Check if the user is attempting a valid edit action.
# If analyst_action is present, analyst_action_location will be present. # If analyst_action is present, analyst_action_location will be present.
# if it isn't, then it either suggests tampering # if it isn't, then it either suggests tampering