From 5799abb2bf71fc95aa61effb710ee607f7e0bf85 Mon Sep 17 00:00:00 2001 From: Ethan Nguyen Date: Wed, 12 Apr 2023 22:36:28 -0500 Subject: [PATCH] add django-axes (#177) * build(deps): add django-axes * test: modify for axes See jazzband/django-axes#998 --- Pipfile | 3 ++- Pipfile.lock | 24 ++++++++++++++++++++---- tjdests/settings/__init__.py | 8 ++++++++ tjdests/test.py | 5 ++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/Pipfile b/Pipfile index 46de5bd..f5bcf0d 100644 --- a/Pipfile +++ b/Pipfile @@ -16,6 +16,7 @@ psycopg2 = "~=2.9.5" pygments = "~=2.14.0" social-auth-app-django = "~=5.0.0" whitenoise = "~=6.4.0" +django-axes = "~=5.41.0" [dev-packages] autopep8 = "~=2.0.2" @@ -33,4 +34,4 @@ types-Markdown = "~=3.4.2.7" # To avoid CI failures types-bleach = "~=6.0.0.2" # To avoid CI failures [requires] -python_version = "3.9" \ No newline at end of file +python_version = "3.9" diff --git a/Pipfile.lock b/Pipfile.lock index 0716590..835837f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "19bc5651835946f1e4fe7e7834590b4a0f861cbd6d820ade3e92a8bb929eb002" + "sha256": "3f3be7067d7f370e0286f22340b6f4cba7774cf28f894b70b0e181d3cc3b36f0" }, "pipfile-spec": 6, "requires": { @@ -253,6 +253,14 @@ "index": "pypi", "version": "==3.2.18" }, + "django-axes": { + "hashes": [ + "sha256:7e73c6cfe8e2390e60e5a189976833838206ab7fc04dc9b5c3f27fe589364eb3", + "sha256:cd2b6350d47168b4fc8d37cd159855bc0d55e7b1e6469d76708bc456bc377212" + ], + "index": "pypi", + "version": "==5.41.0" + }, "django-bootstrap-pagination": { "hashes": [ "sha256:47e742679cf109e12f10ae09d5263433f94440818cf7b023e90a3f5252849639", @@ -277,6 +285,14 @@ "index": "pypi", "version": "==3.2.1" }, + "django-ipware": { + "hashes": [ + "sha256:4fa5607ee85e12ee5e158bc7569ff1e134fb1579681aa1ff3f0ed04be21be153", + "sha256:80b52a3f571a371519cc552798f1015b934dd5dd7738bfad87e101e861bd21b8" + ], + "markers": "python_version >= '3.7'", + "version": "==5.0.0" + }, "executing": { "hashes": [ "sha256:0314a69e37426e3608aada02473b4161d4caf5a4b244d1d0c48072b8fee7bacc", @@ -805,11 +821,11 @@ }, "packaging": { "hashes": [ - "sha256:714ac14496c3e68c99c29b00845f7a2b85f3bb6f1078fd9f72fd20f0570002b2", - "sha256:b6ad297f8907de0fa2fe1ccbd26fdaf387f5f47c7275fedf8cce89f99446cf97" + "sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61", + "sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f" ], "markers": "python_version >= '3.7'", - "version": "==23.0" + "version": "==23.1" }, "pathspec": { "hashes": [ 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