Skip to content

Conversation

jnns
Copy link
Contributor

@jnns jnns commented May 19, 2020

Using the admin_client fixture with a custom user model that doesn't have a username field doesn't work currently. The fixture assumes that the user model always has a username field present. For this case, Django has User.USERNAME_FIELD and User.get_username().

Unfortunately, the existing test didn't catch this issue because a custom user model with User.username is used.

This pull request changes the test setup to use a custom user without username and makes admin_client use User.get_username().

There are a few merge requests and issues about this problem already. I hope this one meets the requirements to be merged. If not, please let me know and I see what I can do.

@jnns jnns force-pushed the admin-client-fix branch 2 times, most recently from d1b4335 to dcf4202 Compare May 19, 2020 20:40
jnns added 2 commits May 19, 2020 22:53
The previous test setup inherited from AbstractUser thus MyCustomUser
still had a username field.

The problems people are having when using the admin_client fixture in
combination with a custom user model are due to the username field
not being present.

This change accounts for the more realistic scenario.

See these tickets:
pytest-dev#246
pytest-dev#484
pytest-dev#748
Relying on `User.USERNAME_FIELD` is enough for light customization and
doesn't require `extra_arguments`.

Esoteric user models probably need a specially tailored manager anyway
and most likely a custom `django_user` fixture that goes with that.

See this ticket: 
pytest-dev#457
@jnns jnns force-pushed the admin-client-fix branch from dcf4202 to 94dcb49 Compare May 19, 2020 20:58
jnns added 2 commits May 19, 2020 23:15
"1 passed, 1 warning" wasn't matched before when there was a deprecation
warning.

Newer Django versions warn about the removal of `django.conf.urls.url` 
in favor of `django.urls.re_path` but Django 1.11 is also tested against 
and doesn't support `re_path` yet.
@jnns jnns force-pushed the admin-client-fix branch from 0590417 to 9f38109 Compare May 20, 2020 06:51
Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jnns! This looks good to me and fixes what seems to be a long-standing issue.

If you could do the following that would be great:

  1. Fix my one tiny comment.
  2. Rebase the PR on latest master.
  3. Add an entry to the changelog (docs/changelog.rst).
  4. Edit the PR to say "Closes #XXX" on old the old PRs/issues that this fixes, so they're automatically closed once merged.

Comment on lines +296 to +298
username_field: username,
"email": "admin@example.com",
"password": "password",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case username_field is email, it should override the email below, so should come after it (even though the values are the same, but just for good measure).

Suggested change
username_field: username,
"email": "admin@example.com",
"password": "password",
"email": "admin@example.com",
"password": "password",
username_field: username,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to review! I will add the requested changes and push again.

@bluetech
Copy link
Member

Hey @jnns, I am just about to release version 4.0.0 and want to include these changes, so I re-submitted this PR with the requested changes in #885. Thanks again!

@bluetech bluetech closed this Oct 16, 2020
@jnns jnns deleted the admin-client-fix branch October 16, 2020 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants