Opened 16 years ago

Closed 9 years ago

#7836 closed Bug (fixed)

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

Reported by: Trevor Caira Owned by: nobody
Component: Testing framework Version: dev
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.

Change History (15)

comment:1 by Trevor Caira, 16 years ago

Component: UncategorizedUnit test system

comment:2 by Jeff Anderson, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:3 by Malcolm Tredinnick, 14 years ago

Triage Stage: Design decision neededAccepted

This looks like a reasonable request.

comment:4 by Luke Plant, 14 years ago

Severity: Normal
Type: Bug

comment:5 by Ramiro Morales, 14 years ago

Resolution: fixed
Status: newclosed

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 by Ramiro Morales, 13 years ago

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 by Ramiro Morales, 13 years ago

Easy pickings: unset
Resolution: fixed
Status: closedreopened
UI/UX: unset

comment:8 by Simon Charette, 13 years ago

Cc: charette.s@… added

comment:9 by Vlastimil Zíma, 12 years ago

Cc: vlastimil@… added

comment:10 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:11 by Unai Zalakain, 11 years ago

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?

Version 0, edited 11 years ago by Unai Zalakain (next)

comment:12 by Unai Zalakain, 11 years ago

Cc: unai@… added

in reply to:  11 comment:13 by Vlastimil Zíma, 11 years ago

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 by Unai Zalakain, 11 years ago

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

comment:15 by Claude Paroz, 9 years ago

Resolution: fixed
Status: newclosed

I think that the recent commit 21dd98a3 fixing #25699 is also fixing this issue.

Note: See TracTickets for help on using tickets.
Back to Top