From a255234173bc6be13e3a28eb93794c65e2cc4439 Mon Sep 17 00:00:00 2001
From: Ethan Nguyen <etnguyen03@hotmail.com>
Date: Wed, 12 Apr 2023 22:31:17 -0500
Subject: [PATCH] test: modify for axes

See jazzband/django-axes#998
---
 tjdests/settings/__init__.py | 8 ++++++++
 tjdests/test.py              | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/tjdests/settings/__init__.py b/tjdests/settings/__init__.py
index 131ec30..f5a9e06 100644
--- a/tjdests/settings/__init__.py
+++ b/tjdests/settings/__init__.py
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/3.2/ref/settings/
 """
 import logging
 import os
+import sys
 from pathlib import Path
 from typing import List, Optional
 
@@ -46,6 +47,7 @@ INSTALLED_APPS = [
     "social_django",
     "django_extensions",
     "bootstrap_pagination",
+    "axes",
     "tjdests.apps.authentication",
     "tjdests.apps.destinations",
     "tjdests.apps.profile",
@@ -60,6 +62,7 @@ MIDDLEWARE = [
     "django.contrib.auth.middleware.AuthenticationMiddleware",
     "django.contrib.messages.middleware.MessageMiddleware",
     "django.middleware.clickjacking.XFrameOptionsMiddleware",
+    "axes.middleware.AxesMiddleware",
 ]
 
 ROOT_URLCONF = "tjdests.urls"
@@ -116,6 +119,7 @@ AUTH_PASSWORD_VALIDATORS = [
 AUTH_USER_MODEL = "authentication.User"
 
 AUTHENTICATION_BACKENDS = (
+    "axes.backends.AxesStandaloneBackend",
     "tjdests.apps.authentication.oauth.IonOauth2",
     "django.contrib.auth.backends.ModelBackend",
 )
@@ -163,6 +167,10 @@ MESSAGE_TAGS = {
     messages.ERROR: "danger",
 }
 
+TESTING = any("test" in arg for arg in sys.argv)
+if TESTING:
+    AXES_ENABLED = False
+
 # Override the following in secret.py
 SENIOR_GRAD_YEAR: int = -1
 BRANDING_NAME: str = "UNDEFINED"
diff --git a/tjdests/test.py b/tjdests/test.py
index 5aae985..a39add8 100644
--- a/tjdests/test.py
+++ b/tjdests/test.py
@@ -1,3 +1,4 @@
+from django.http import HttpRequest
 from django.test import TestCase
 
 from tjdests.apps.authentication.models import User
@@ -40,5 +41,7 @@ class TJDestsTestCase(TestCase):
                 "publish_data": publish_data,
             },
         )[0]
-        self.client.force_login(user)
+        user.set_password("hello123")
+        user.save()
+        self.client.login(username=username, password="hello123", request=HttpRequest())
         return user