Code

Opened 7 years ago

Closed 7 years ago

#5979 closed (fixed)

Django tests fail if SITE_ID is not 1.

Reported by: toddobryan Owned by: nobody
Component: Testing framework Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by gwilson)

The following tests fail if the SITE_ID is set to 2, for example. They work fine if it's 1.

======================================================================
ERROR: Request a logout after logging in
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 315, in test_logout
    self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

======================================================================
ERROR: Request a page that is protected with @login_required
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 245, in test_view_with_login
    self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_view/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

======================================================================
ERROR: Request a page that is protected with @login_required(redirect_field_name='redirect_to')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 277, in test_view_with_login_and_custom_redirect
    self.assertRedirects(response, 'http://testserver/accounts/login/?redirect_to=/test_client/login_protected_view_custom_redirect/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

======================================================================
ERROR: Request a page that is protected with a @login_required method
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 261, in test_view_with_method_login
    self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/login_protected_method_view/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

======================================================================
ERROR: Request a page that is protected with a @permission_required method
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 339, in test_view_with_method_permissions
    self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_method_view/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

======================================================================
ERROR: Request a page that is protected with @permission_required
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tobryan1/workspace/django/tests/modeltests/test_client/models.py", line 322, in test_view_with_permissions
    self.assertRedirects(response, 'http://testserver/accounts/login/?next=/test_client/permission_protected_view/')
  File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 98, in assertRedirects
    redirect_response = response.client.get(path, QueryDict(query))
  File "/usr/lib/python2.5/site-packages/django/test/client.py", line 219, in get
    return self.request(**r)
  File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 81, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python2.5/site-packages/django/contrib/auth/views.py", line 32, in login
    current_site = Site.objects.get_current()
  File "/usr/lib/python2.5/site-packages/django/contrib/sites/models.py", line 22, in get_current
    current_site = self.get(pk=sid)
  File "/usr/lib/python2.5/site-packages/django/db/models/manager.py", line 69, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 263, in get
    raise self.model.DoesNotExist, "%s matching query does not exist." % self.model._meta.object_name
DoesNotExist: Site matching query does not exist.

----------------------------------------------------------------------
Ran 214 tests in 245.089s

FAILED (errors=6)

Attachments (1)

test_siteid_overwrite_r6720.patch (530 bytes) - added by gav 7 years ago.
Forces SITE_ID to 1 for unit tests, patch against r6720.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 7 years ago by gwilson

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

fixed description formatting

comment:2 Changed 7 years ago by gav

  • Has patch set

Attaching a patch that just forces the SITE_ID to 1.

This works nicely in every case except where we may want to test multiple sites. For someone who wants to test multiple sites (whether for django.contrib.sites testing directly, or for a module that depends on its behavior), they will have to do special things in the particular test's setup methods anyway, including overwriting settings.SITE_ID via code multiple times to test the scenario. Therefore, I think it's fine to leave any complicated SITE_ID (and associated database content population) to the particular tests that actually need to test multiple sites.

Also, to fix this across this test framework in a way that would allow any random SITE_ID would add at least a few lines of clutter to django.test.simple.run_tests (since this is where DB construction/destruction is done, it would only execute once) or somewhere in django.test.client.*, which would be more reset-safe but gets run multiple times during the testing, and would therefore be inefficient and probably misplaced. Neither of those sound worth it for something that doesn't give the 99% case any benefit in testing, and for which there's an easy workaround (add proper Site construction to your specific test case).

Changed 7 years ago by gav

Forces SITE_ID to 1 for unit tests, patch against r6720.

comment:3 Changed 7 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

comment:4 Changed 7 years ago by mtredinnick

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

(In [6814]) Fixed #5979 -- Always use SITE_ID=1 when running Django's core tests. This
makes things much easier. Thanks, George Vilches.

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.