Skip to content

Instantly share code, notes, and snippets.

@reidrac
Created May 27, 2012 15:19
Show Gist options
  • Save reidrac/2814685 to your computer and use it in GitHub Desktop.
Save reidrac/2814685 to your computer and use it in GitHub Desktop.
Dajango tests using sessions without the Django internal user system
from django.test.client import Client
from django.test import TestCase
from django.conf import settings
from django.utils.importlib import import_module
from project.app.models import User
class MyTestCase(TestCase):
def setUp(self):
settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
engine = import_module(settings.SESSION_ENGINE)
store = engine.SessionStore()
store.save()
self.session = store
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key
self.user = User.objects.create(name='user', email='user@mail.dom')
session = self.session
session['user'] = self.user
session.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment