Code

Opened 6 years ago

Last modified 9 months ago

#7836 new Bug

django.test.client._session should check for session middleware instead

Reported by: trevor Owned by: nobody
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: charette.s@…, vlastimil@…, unai@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The django test client class allows you to use self.client.session only if you have 'django.contrib.sessions' in your INSTALLED_APPS setting. That is, it assumes you are using sessions if and only if you have the sessions app installed.

This assumption breaks down when you are using the sessions middleware independently of the sessions app, for instance if you are using a cache backend for sessions.

I propose that django.test.client._session check for the sessions middleware class in MIDDLEWARE_CLASSES instead of the sessions app in INSTALLED_APPS to make it easier for people using the session middleware independently of the sessions app to use self.client.sesssion in tests.

Attachments (0)

Change History (14)

comment:1 Changed 6 years ago by trevor

  • Component changed from Uncategorized to Unit test system
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 6 years ago by programmerq

  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 4 years ago by mtredinnick

  • Triage Stage changed from Design decision needed to Accepted

This looks like a reasonable request.

comment:4 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 3 years ago by ramiro

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

In [16386]:

Fixed #7836 -- Modified strategy used by the test client to check for session functionality so it is useful in environments where the DB-based session backend isn't being used. Thanks trevor for the report.

comment:6 Changed 2 years ago by ramiro

In [17739]:

Reverted r16386 because it replaced a brittle method with another not less
arbitrary method when the test client checks for the presence of the bundled
session backends+session middleware combination.

We will revisit the issue soon, probably to make these checks even less strict.

Refs #7836, #16605

comment:7 Changed 2 years ago by ramiro

  • Easy pickings unset
  • Resolution fixed deleted
  • Status changed from closed to reopened
  • UI/UX unset

comment:8 Changed 2 years ago by charettes

  • Cc charette.s@… added

comment:9 Changed 16 months ago by vzima

  • Cc vlastimil@… added

comment:10 Changed 16 months ago by aaugustin

  • Status changed from reopened to new

comment:11 follow-up: Changed 9 months ago by unaizalakain

I'm probably missing something, where's the need of checking if some kind of session middleware exists? If not present, wouldn't {} be returned anyways?

BTW, shouldn't it be better if the engine module import happen outside _session and instead of checking for if coockie if cookie is not None is checked?

Last edited 9 months ago by unaizalakain (previous) (diff)

comment:12 Changed 9 months ago by unaizalakain

  • Cc unai@… added

comment:13 in reply to: ↑ 11 Changed 9 months ago by vzima

Replying to unaizalakain:

I'm probably missing something, where's the need of checking if some kind of session middleware exists? If not present, wouldn't {} be returned anyways?

In application, the attribute request.session is created by SessionMiddleware. If missing you will get AttributeError.
In tests, you get either empty dictionary or SessionStore.

The problem here is no error is indicated by client when you don't have sessions installed, but client.login does not do a valid login. This is problem which is not quite easy to find.

BTW, shouldn't it be better if the engine module import happen outside _session and instead of checking for if coockie if cookie is not None is checked?

It wouldn't. You can change settings for the test and you want the test to use yours.

comment:14 Changed 9 months ago by unaizalakain

What if _sesion arises an exception if no subclass of the session middleware is found?

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


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

 
Note: See TracTickets for help on using tickets.