Created
April 9, 2012 14:52
-
-
Save bcattle/2344020 to your computer and use it in GitHub Desktop.
Testing of user creation, includes session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnonymousSessionTestCase(TestCase): | |
def setUp(self): | |
# Initialize session | |
# see https://code.djangoproject.com/ticket/10899 | |
# Apaprently, need to do this because nobody's logged in | |
# http://www.gregaker.net/2011/aug/29/testing_django_views_that_rely_on_session_variables/ | |
# https://code.djangoproject.com/ticket/11475 | |
from django.conf import settings | |
engine = import_module(settings.SESSION_ENGINE) | |
store = engine.SessionStore() | |
store.save() | |
self.client.cookies[settings.SESSION_COOKIE_NAME] = store.session_key | |
self.session = self.client.session | |
class TestGetEmailAndPasswordView(AnonymousSessionTestCase): | |
def setUp(self): | |
super(TestGetEmailAndPasswordView, self).setUp() | |
# Put an invite in the session | |
invite = ApplicantInvitation(email='chuck.norris@gmail.com') | |
invite.save() | |
self.session['invite'] = invite | |
self.session.save() | |
def test_get_email_and_password_view(self): | |
""" | |
Tests that get_email_and_password view | |
correctly creates a user and redirects | |
""" | |
response = self.client.post(reverse('get_email_and_pw'), | |
{'email': 'chuck.norris@gmail.com', | |
'password1': 'asdf', | |
'password2': 'asdf',}) | |
# Check that we redirect | |
self.assertRedirects(response, reverse('get_blacklist')) | |
# Check that user was created | |
user = User.objects.get(email__iexact='chuck.norris@gmail.com') | |
self.assertTrue(user) | |
# Check that invite was disabled | |
invite = ApplicantInvitation.objects.get(email__iexact='chuck.norris@gmail.com') | |
self.assertTrue(invite.is_used) | |
# Check that user is logged in | |
response2 = self.client.get(response['Location']) | |
self.assertEqual(self.client.session['_auth_user_id'], user.pk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment