Skip to content

Commit

Permalink
Superusers must be active by default
Browse files Browse the repository at this point in the history
  • Loading branch information
tsenovilla committed Mar 2, 2024
1 parent 6ee0178 commit 443019b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
Binary file modified coverage/.coverage
Binary file not shown.
3 changes: 3 additions & 0 deletions extended_accounts_api/models/Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ def create_user(self, username, password=None, **extra_fields):
def create_superuser(self, username, password=None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True) ## Super users are active by default

if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")
if extra_fields.get("is_active") is not True:
raise ValueError("Superuser must have is_active=True.")

return self._create_user(username, password, **extra_fields)

Expand Down
8 changes: 8 additions & 0 deletions extended_accounts_api/models/tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def test_create_superuser_OK(self):
admin_user = Account.objects.create_superuser(**data)
self.assertTrue(admin_user.is_staff)
self.assertTrue(admin_user.is_superuser)
self.assertTrue(admin_user.is_active)

def test_create_superuser_KO_if_is_staff_manually_set_to_False_KO(self):
data = self.__modify_data(
Expand All @@ -121,6 +122,13 @@ def test_create_superuser_KO_if_is_superuser_manually_set_to_False_KO(self):
with self.assertRaises(ValueError):
Account.objects.create_superuser(is_superuser=False, **data)

def test_create_superuser_KO_if_is_active_manually_set_to_False_KO(self):
data = self.__modify_data(
{"username": "jdoe", "phone_number": 987654321, "email": "[email protected]"}
)
with self.assertRaises(ValueError):
Account.objects.create_superuser(is_active=False, **data)

## UPDATE TESTS

def test_update_account_OK(self):
Expand Down

0 comments on commit 443019b

Please sign in to comment.