Skip to content

Commit

Permalink
update manager and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calellowitz committed Sep 16, 2023
1 parent 4a25497 commit 1d26622
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
19 changes: 10 additions & 9 deletions commcare_connect/users/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
class UserManager(DjangoUserManager):
"""Custom manager for the User model."""

def _create_user(self, email: str, password: str | None, **extra_fields):
def _create_user(self, username: str | None, email: str | None, password: str | None, **extra_fields):
"""
Create and save a user with the given email and password.
"""
if not email:
raise ValueError("The given email must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
if not username and not email:
raise ValueError("Username or email must be set")
if email:
email = self.normalize_email(email)
user = self.model(username=username, email=email, **extra_fields)
user.password = make_password(password)
user.save(using=self._db)
return user

def create_user(self, email: str, password: str | None = None, **extra_fields):
def create_user(self, username: str | None = None, email: str | None = None, password: str | None = None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)
return self._create_user(username, email, password, **extra_fields)

def create_superuser(self, email: str, password: str | None = None, **extra_fields):
def create_superuser(self, username: str | None = None, email: str | None = None, password: str | None = None, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)

Expand All @@ -31,4 +32,4 @@ def create_superuser(self, email: str, password: str | None = None, **extra_fiel
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")

return self._create_user(email, password, **extra_fields)
return self._create_user(username, email, password, **extra_fields)
2 changes: 1 addition & 1 deletion commcare_connect/users/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_add(self, admin_client):
assert User.objects.filter(email="[email protected]").exists()

def test_view_user(self, admin_client):
user = User.objects.get(email="admin@example.com")
user = User.objects.get(username="admin")
url = reverse("admin:users_user_change", kwargs={"object_id": user.pk})
response = admin_client.get(url)
assert response.status_code == 200
4 changes: 2 additions & 2 deletions commcare_connect/users/tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ def test_createsuperuser_command():
out = StringIO()
command_result = call_command(
"createsuperuser",
"--email",
"--username",
"[email protected]",
interactive=False,
stdout=out,
)

assert command_result is None
assert out.getvalue() == "Superuser created successfully.\n"
user = User.objects.get(email="[email protected]")
user = User.objects.get(username="[email protected]")
assert not user.has_usable_password()

0 comments on commit 1d26622

Please sign in to comment.