Skip to content

Commit

Permalink
add UserInactiveError
Browse files Browse the repository at this point in the history
  • Loading branch information
eugapx committed Dec 6, 2023
1 parent 9b99683 commit 2370ec9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion df_auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .exceptions import (
UserDoesNotExistError,
UserInactiveError,
WrongOTPError,
)
from .settings import api_settings
Expand Down Expand Up @@ -58,7 +59,7 @@ def generate_challenge(

if not user:
user = User.objects.filter(
**{self.identity_field: kwargs.get(self.identity_field)}, is_active=True
**{self.identity_field: kwargs.get(self.identity_field)}
).first()
if not user:
if api_settings.OTP_AUTO_CREATE_ACCOUNT and api_settings.SIGNUP_ALLOWED:
Expand All @@ -75,6 +76,8 @@ def generate_challenge(
)
else:
raise UserDoesNotExistError()
if not user.is_active:
raise UserInactiveError()

device, _ = self.DeviceModel.objects.get_or_create(
user=user,
Expand Down
9 changes: 9 additions & 0 deletions df_auth/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class UserDoesNotExistError(DfAuthValidationError):
default_code = "user_does_not_exist"


class UserInactiveError(DfAuthValidationError):
"""
This exception is used when user already exists
"""

default_detail = _("Your account was deactivated. Please contact support")
default_code = "user_inactive"


class DeviceTakenError(DfAuthValidationError):
"""
This exception is used when device is already registered
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "django-df-auth"
version = "1.0.0"
version = "1.0.1"
description = "Opinionated Django REST auth endpoints for JWT authentication and social accounts."
readme = "README.md"
authors = [{name = "Apexive OSS", email = "open-source@apexive.com"}]
Expand Down
27 changes: 27 additions & 0 deletions tests/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,33 @@ def test_user_2fa_update(self) -> None:
self.assertTrue(response.data["is_required"])


class InactiveUserAPITest(APITestCase):
def setUp(self) -> None:
# Create a test user and set up any other objects you need
self.email = "test@te.st"
self.user = User.objects.create_user(
username="testuser", password="testpass", is_active=False, email=self.email
)
self.client = APIClient()

def test_inactive_user_request_otp(self) -> None:
email_device = EmailDevice.objects.create(
user=self.user,
name=self.email,
email=self.email,
confirmed=True,
)

response = self.client.post(
"/api/v1/auth/otp/",
{
"email": email_device.email,
},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json()["errors"][0]["code"], "user_inactive")


def test_create_superuser() -> None:
User.objects.create_superuser(
username="testuser",
Expand Down

0 comments on commit 2370ec9

Please sign in to comment.