Code

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#6923 closed (wontfix)

Contrib.auth.tests.basic.py does not delete test users 'testuser' and 'testuser2'

Reported by: anonymous Owned by: nobody
Component: Contrib apps Version: master
Severity: Keywords: auth
Cc: em@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

In django.django.contrib.auth.tests.basic.py there are tests for checking that the auth module works correctly. To do this two test users are created, but not deleted. This means that user tests that use the name "testuser" or "testuser2" will get incorrect password errors, and not log in. The test database should be empty.

Attachments (0)

Change History (6)

comment:1 Changed 6 years ago by anonymous

  • Cc em@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by devin

  • Resolution set to worksforme
  • Status changed from new to closed

I've tried reproducing this with both Doctests and Django TestCase, but in both cases, the test database contains no Users. Since test databases are created in the __call__ method of TestCase, it doesn't seem like this could happen for Django TestCase. I don't know about the Doctests, but I couldn't reproduce using a variety of database engines.

comment:3 Changed 6 years ago by Emil Stenström <em@…>

  • Resolution worksforme deleted
  • Status changed from closed to reopened

@devin: Sorry if I was unclear, this bug has to do with TestCase, not Doctests. The file it concerns is: http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/tests/basic.py?rev=7967 which clearly has two lines of type:

u = User.objects.create_user('testuser', 'test@example.com', 'testpw')

...but no corresponding line for deleting that user. It will of course be deleted after all tests are done, but that's too late. When I run "manage.py test" all tests are run, both the basic.py and my own, so "testuser" will be in the test database when my tests are run.

I hope this explanation warrants a reopening of the ticket, otherwise you may slap me.

comment:4 Changed 6 years ago by russellm

  • Resolution set to wontfix
  • Status changed from reopened to closed

This is a known limitation of using doctests. Essentially, you can't write a doctest or vanilla unit test and expect that the database is clean at the start of the test.

The only permanent solutions that exist involve flushing the database at the start or end of every doctest. Unfortunately, flushing is an expensive operation, so we don't want to do it unless we know we need to.

The workaround is to modify your test; Either modify your test conditions to check for deltas rather than absolutes (e.g., find the number of users at the start of the test, and after adding some users check that current count - initial count = expected) or manually flush the database at the start of your test. You can do this by calling:

>>> from django.core.management import call_command
>>> call_command('flush', verbosity=0, interactive=False)

at the start of your test.

This is somewhat related to #5624; this ticket calls for finding a way to associate fixtures with doctests. Part of this process may be to find a way to explicitly request a database flush without loading a fixture.

comment:5 Changed 6 years ago by Emil Stenström <em@…>

Wouldn't it be easier to delete the users you create in this specific test? Two lines of code:

u.delete()
u2.delete()

I've encountered one other user hitting the same wall when trying to name my own test user "testuser" and getting strange errors.

comment:6 Changed 6 years ago by russellm

Sure - deleting those two users will solve _this_ problem, _this_ time. However, it isn't a general solution. The general solution involves flushing the database, which is an expensive operation if it isn't required.

In your case, it obviously _is_ required. doctests make no guarantees about the state of the database at the start of the test, so you need to modify your test to guarantee the initially empty state of the database, using the manual call to flush I described previously.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.