Django

Code

Changeset 8497

Show
Ignore:
Timestamp:
08/23/08 13:20:49 (3 months ago)
Author:
mtredinnick
Message:

Tests for password change process. Thanks, Mike Richardson. Fixed #8402.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r8474 r8497  
    331331    rhettg@gmail.com 
    332332    ricardojbarrios@gmail.com 
     333    Mike Richardson 
    333334    Matt Riggott 
    334335    Henrique Romano <onaiort@gmail.com> 
  • django/trunk/django/contrib/auth/tests/__init__.py

    r8162 r8497  
    11from django.contrib.auth.tests.basic import BASIC_TESTS 
    2 from django.contrib.auth.tests.views import PasswordResetTest 
     2from django.contrib.auth.tests.views import PasswordResetTest, ChangePasswordTest 
    33from django.contrib.auth.tests.forms import FORM_TESTS 
    44from django.contrib.auth.tests.tokens import TOKEN_GENERATOR_TESTS 
     5 
     6# The password for the fixture data users is 'password' 
    57 
    68__test__ = { 
     
    810    'PASSWORDRESET_TESTS': PasswordResetTest, 
    911    'FORM_TESTS': FORM_TESTS, 
    10     'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS 
     12    'TOKEN_GENERATOR_TESTS': TOKEN_GENERATOR_TESTS, 
     13    'CHANGEPASSWORD_TESTS': ChangePasswordTest, 
    1114} 
  • django/trunk/django/contrib/auth/tests/views.py

    r8162 r8497  
    11 
     2import os 
    23import re 
     4 
     5from django.conf import settings 
    36from django.contrib.auth.models import User 
    47from django.test import TestCase 
     
    811    fixtures = ['authtestdata.json'] 
    912    urls = 'django.contrib.auth.urls' 
    10      
     13 
    1114    def test_email_not_found(self): 
    1215        "Error is raised if the provided email address isn't currently registered" 
     
    1619        self.assertContains(response, "That e-mail address doesn't have an associated user account") 
    1720        self.assertEquals(len(mail.outbox), 0) 
    18      
     21 
    1922    def test_email_found(self): 
    2023        "Email is sent if a valid email address is provided for password reset" 
     
    4043        response = self.client.get(path) 
    4144        # redirect to a 'complete' page: 
    42         self.assertEquals(response.status_code, 200)  
     45        self.assertEquals(response.status_code, 200) 
    4346        self.assert_("Please enter your new password" in response.content) 
    4447 
     
    5053 
    5154        response = self.client.get(path) 
    52         self.assertEquals(response.status_code, 200)  
     55        self.assertEquals(response.status_code, 200) 
    5356        self.assert_("The password reset link was invalid" in response.content) 
    5457 
     
    6164        response = self.client.post(path, {'new_password1': 'anewpassword', 
    6265                                           'new_password2':' anewpassword'}) 
    63         # Check the password has not been changed  
     66        # Check the password has not been changed 
    6467        u = User.objects.get(email='staffmember@example.com') 
    6568        self.assert_(not u.check_password("anewpassword")) 
     
    7073                                           'new_password2': 'anewpassword'}) 
    7174        # It redirects us to a 'complete' page: 
    72         self.assertEquals(response.status_code, 302)  
    73         # Check the password has been changed  
     75        self.assertEquals(response.status_code, 302) 
     76        # Check the password has been changed 
    7477        u = User.objects.get(email='staffmember@example.com') 
    7578        self.assert_(u.check_password("anewpassword")) 
     
    7780        # Check we can't use the link again 
    7881        response = self.client.get(path) 
    79         self.assertEquals(response.status_code, 200)  
     82        self.assertEquals(response.status_code, 200) 
    8083        self.assert_("The password reset link was invalid" in response.content) 
    8184 
     
    8790        self.assert_("The two password fields didn't match" in response.content) 
    8891 
     92 
     93class ChangePasswordTest(TestCase): 
     94    fixtures = ['authtestdata.json'] 
     95    urls = 'django.contrib.auth.urls' 
     96 
     97    def setUp(self): 
     98        self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS 
     99        settings.TEMPLATE_DIRS = ( 
     100            os.path.join( 
     101                os.path.dirname(__file__), 
     102                'templates' 
     103            ) 
     104        ,) 
     105 
     106    def tearDown(self): 
     107        settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS 
     108 
     109    def login(self, password='password'): 
     110        response = self.client.post('/login/', { 
     111            'username': 'testclient', 
     112            'password': password 
     113            } 
     114        ) 
     115        self.assertEquals(response.status_code, 302) 
     116        self.assert_(response['Location'].endswith('/accounts/profile/')) 
     117 
     118    def fail_login(self, password='password'): 
     119        response = self.client.post('/login/', { 
     120            'username': 'testclient', 
     121            'password': password 
     122            } 
     123        ) 
     124        self.assertEquals(response.status_code, 200) 
     125        self.assert_("Please enter a correct username and password. Note that both fields are case-sensitive." in response.content) 
     126 
     127    def logout(self): 
     128        response = self.client.get('/logout/') 
     129 
     130    def test_password_change_fails_with_invalid_old_password(self): 
     131        self.login() 
     132        response = self.client.post('/password_change/', { 
     133            'old_password': 'donuts', 
     134            'new_password1': 'password1', 
     135            'new_password2': 'password1', 
     136            } 
     137        ) 
     138        self.assertEquals(response.status_code, 200) 
     139        self.assert_("Your old password was entered incorrectly. Please enter it again." in response.content) 
     140 
     141    def test_password_change_fails_with_mismatched_passwords(self): 
     142        self.login() 
     143        response = self.client.post('/password_change/', { 
     144            'old_password': 'password', 
     145            'new_password1': 'password1', 
     146            'new_password2': 'donuts', 
     147            } 
     148        ) 
     149        self.assertEquals(response.status_code, 200) 
     150        self.assert_("The two password fields didn't match." in response.content) 
     151 
     152    def test_password_change_succeeds(self): 
     153        self.login() 
     154        response = self.client.post('/password_change/', { 
     155            'old_password': 'password', 
     156            'new_password1': 'password1', 
     157            'new_password2': 'password1', 
     158            } 
     159        ) 
     160        self.assertEquals(response.status_code, 302) 
     161        self.assert_(response['Location'].endswith('/password_change/done/')) 
     162        self.fail_login() 
     163        self.login(password='password1') 
     164 
  • django/trunk/django/contrib/auth/urls.py

    r8172 r8497  
    66 
    77urlpatterns = patterns('', 
     8    (r'^login/$', 'django.contrib.auth.views.login'), 
    89    (r'^logout/$', 'django.contrib.auth.views.logout'), 
    910    (r'^password_change/$', 'django.contrib.auth.views.password_change'),