Commit 7e35bf19 by Will Daly

Merge pull request #1846 from MITx/feature/zoldak/test-user-profile-factory

Convert login tests to use factories
parents dce0b53c bb1134b8
from django.test import TestCase from django.test import TestCase
from django.test.client import Client from django.test.client import Client
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.contrib.auth.models import User from factories import UserFactory, RegistrationFactory, UserProfileFactory
from student.models import Registration, UserProfile
import json import json
class LoginTest(TestCase): class LoginTest(TestCase):
''' '''
Test student.views.login_user() view Test student.views.login_user() view
''' '''
def setUp(self): def setUp(self):
# Create one user and save it to the database # Create one user and save it to the database
self.user = User.objects.create_user('test', 'test@edx.org', 'test_password') self.user = UserFactory.build(username='test', email='test@edx.org')
self.user.is_active = True self.user.set_password('test_password')
self.user.save() self.user.save()
# Create a registration for the user # Create a registration for the user
Registration().register(self.user) registration = RegistrationFactory(user=self.user)
registration.register(self.user)
registration.activate()
# Create a profile for the user # Create a profile for the user
UserProfile(user=self.user).save() UserProfileFactory(user=self.user)
# Create the test client # Create the test client
self.client = Client() self.client = Client()
...@@ -42,19 +43,17 @@ class LoginTest(TestCase): ...@@ -42,19 +43,17 @@ class LoginTest(TestCase):
response = self._login_response(unicode_email, 'test_password') response = self._login_response(unicode_email, 'test_password')
self._assert_response(response, success=True) self._assert_response(response, success=True)
def test_login_fail_no_user_exists(self): def test_login_fail_no_user_exists(self):
response = self._login_response('not_a_user@edx.org', 'test_password') response = self._login_response('not_a_user@edx.org', 'test_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value='Email or password is incorrect') value='Email or password is incorrect')
def test_login_fail_wrong_password(self): def test_login_fail_wrong_password(self):
response = self._login_response('test@edx.org', 'wrong_password') response = self._login_response('test@edx.org', 'wrong_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value='Email or password is incorrect') value='Email or password is incorrect')
def test_login_not_activated(self): def test_login_not_activated(self):
# De-activate the user # De-activate the user
self.user.is_active = False self.user.is_active = False
self.user.save() self.user.save()
...@@ -62,8 +61,7 @@ class LoginTest(TestCase): ...@@ -62,8 +61,7 @@ class LoginTest(TestCase):
# Should now be unable to login # Should now be unable to login
response = self._login_response('test@edx.org', 'test_password') response = self._login_response('test@edx.org', 'test_password')
self._assert_response(response, success=False, self._assert_response(response, success=False,
value="This account has not been activated") value="This account has not been activated")
def test_login_unicode_email(self): def test_login_unicode_email(self):
unicode_email = u'test@edx.org' + unichr(40960) unicode_email = u'test@edx.org' + unichr(40960)
...@@ -95,13 +93,13 @@ class LoginTest(TestCase): ...@@ -95,13 +93,13 @@ class LoginTest(TestCase):
try: try:
response_dict = json.loads(response.content) response_dict = json.loads(response.content)
except ValueError: except ValueError:
self.fail("Could not parse response content as JSON: %s" self.fail("Could not parse response content as JSON: %s"
% str(response.content)) % str(response.content))
if success is not None: if success is not None:
self.assertEqual(response_dict['success'], success) self.assertEqual(response_dict['success'], success)
if value is not None: if value is not None:
msg = ("'%s' did not contain '%s'" % msg = ("'%s' did not contain '%s'" %
(str(response_dict['value']), str(value))) (str(response_dict['value']), str(value)))
self.assertTrue(value in response_dict['value'], msg) self.assertTrue(value in response_dict['value'], msg)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment