def test_success(self): # The success case. user = User.objects.get(username='******') data = { 'old_password': '******', 'new_password1': 'abc123', 'new_password2': 'abc123', } form = PasswordChangeForm(user, data) self.assertTrue(form.is_valid())
def change_password(request): user = request.user form_password = PasswordChangeForm(user, request.POST or None, prefix="password") if form_password.is_valid(): form_password.save() request.flash["message"] = unicode(_("Password successfully saved.")) request.flash["severity"] = "success" return HttpResponseRedirect(reverse("account_password")) return render_to_response( "store_admin/account/password.html", {"form_password": form_password}, RequestContext(request) )
def test_incorrect_password(self): user = User.objects.get(username='******') data = { 'old_password': '******', 'new_password1': 'abc123', 'new_password2': 'abc123', } form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual(form["old_password"].errors, [force_unicode(form.error_messages['password_incorrect'])])
def test_incorrect_password(self): user = User.objects.get(username='******') data = { 'old_password': '******', 'new_password1': 'abc123', 'new_password2': 'abc123', } form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual( form["old_password"].errors, [force_unicode(form.error_messages['password_incorrect'])])
def test_password_verification(self): # The two new passwords do not match. user = User.objects.get(username='******') data = { 'old_password': '******', 'new_password1': 'abc123', 'new_password2': 'abc', } form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual(form["new_password2"].errors, [force_unicode(form.error_messages['password_mismatch'])])
def test_password_verification(self): # The two new passwords do not match. user = User.objects.get(username='******') data = { 'old_password': '******', 'new_password1': 'abc123', 'new_password2': 'abc', } form = PasswordChangeForm(user, data) self.assertFalse(form.is_valid()) self.assertEqual( form["new_password2"].errors, [force_unicode(form.error_messages['password_mismatch'])])
def change_password(request): user = request.user form_password = PasswordChangeForm(user, request.POST or None, prefix="password") if form_password.is_valid(): form_password.save() request.flash['message'] = unicode(_("Password successfully saved.")) request.flash['severity'] = "success" return HttpResponseRedirect(reverse('account_password')) return render_to_response('store_admin/account/password.html', {'form_password': form_password}, RequestContext(request))
def change_username_password(request): user = request.user form_username = UsernameChangeForm(request.POST or None, prefix="username", instance=user) form_password = PasswordChangeForm(user, request.POST or None, prefix="password") if form_password.is_valid() or form_username.is_valid(): if form_password.is_valid(): form_password.save() request.flash['message'] = unicode(_("Password successfully saved.")) request.flash['severity'] = "success" return HttpResponseRedirect(reverse('username_password')) if form_username.is_valid(): form_username.save() request.flash['message'] = unicode(_("Username successfully saved.")) request.flash['severity'] = "success" return HttpResponseRedirect(reverse('username_password')) return render_to_response('store_admin/account/username_password.html', {'form_password': form_password, 'form_username': form_username}, RequestContext(request))
def test_field_order(self): # Regression test - check the order of fields: user = User.objects.get(username='******') self.assertEqual( PasswordChangeForm(user, {}).fields.keys(), ['old_password', 'new_password1', 'new_password2'])