Skip to content

Commit a3dc56d

Browse files
iamanikeevblueyed
authored andcommitted
admin_user fixture: handle "email" username_field (#676)
1 parent 0e375ec commit a3dc56d

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

pytest_django/fixtures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,16 @@ def admin_user(db, django_user_model, django_username_field):
252252
"""
253253
UserModel = django_user_model
254254
username_field = django_username_field
255+
username = "admin@example.com" if username_field == "email" else "admin"
255256

256257
try:
257-
user = UserModel._default_manager.get(**{username_field: "admin"})
258+
user = UserModel._default_manager.get(**{username_field: username})
258259
except UserModel.DoesNotExist:
259260
extra_fields = {}
260-
if username_field != "username":
261+
if username_field not in ("username", "email"):
261262
extra_fields[username_field] = "admin"
262263
user = UserModel._default_manager.create_superuser(
263-
"admin", "admin@example.com", "password", **extra_fields
264+
username, "admin@example.com", "password", **extra_fields
264265
)
265266
return user
266267

tests/test_fixtures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ def test_with_live_server(live_server):
469469
django_testdir.runpytest_subprocess("--liveserver=localhost:%s" % port)
470470

471471

472+
@pytest.mark.parametrize('username_field', ('email', 'identifier'))
472473
@pytest.mark.django_project(
473474
extra_settings="""
474475
AUTH_USER_MODEL = 'app.MyCustomUser'
@@ -482,7 +483,7 @@ def test_with_live_server(live_server):
482483
ROOT_URLCONF = 'tpkg.app.urls'
483484
"""
484485
)
485-
def test_custom_user_model(django_testdir):
486+
def test_custom_user_model(django_testdir, username_field):
486487
django_testdir.create_app_file(
487488
"""
488489
from django.contrib.auth.models import AbstractUser
@@ -491,8 +492,8 @@ def test_custom_user_model(django_testdir):
491492
class MyCustomUser(AbstractUser):
492493
identifier = models.CharField(unique=True, max_length=100)
493494
494-
USERNAME_FIELD = 'identifier'
495-
""",
495+
USERNAME_FIELD = '%s'
496+
""" % (username_field),
496497
"models.py",
497498
)
498499
django_testdir.create_app_file(

0 commit comments

Comments
 (0)