Opened 8 years ago

Closed 8 months 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: 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.

Change History (15)

comment:1 Changed 8 years ago by Trevor Caira

Component: UncategorizedUnit test system

comment:2 Changed 8 years ago by Jeff Anderson

Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 6 years ago by Malcolm Tredinnick

Triage Stage: Design decision neededAccepted

This looks like a reasonable request.

comment:4 Changed 6 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:5 Changed 5 years ago by Ramiro Morales

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 Changed 5 years ago by Ramiro Morales

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

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

comment:8 Changed 4 years ago by Simon Charette

Cc: charette.s@… added

comment:9 Changed 4 years ago by Vlastimil Zíma

Cc: vlastimil@… added

comment:10 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:11 Changed 3 years ago by Unai Zalakain

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 3 years ago by Unai Zalakain (previous) (diff)

comment:12 Changed 3 years ago by Unai Zalakain

Cc: unai@… added

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

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

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

comment:15 Changed 8 months ago by Claude Paroz

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