Django Create Session Variables in Unit Test

Came across this snippet some time ago but I can’t remember the source. I’ve since used it quite extensively.

def stuff_session(client, dictionary):
    from django.conf import settings
    from django.utils.importlib import import_module
    engine = import_module(settings.SESSION_ENGINE)
    store = engine.SessionStore()
    store.save()
    client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
    session = client.session
    session.update(dictionary)
    session.save()

To use simply

stuff_session(self.client, {'customer_session_id': '0ABAA842-7E77-2591-3B02-162716195AD6'})
Advertisements

One thought on “Django Create Session Variables in Unit Test

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s