Example #1
0
    def test_UserPasswordChangeForm_save(self):
        # password hash before save
        ret_old = User.objects.get(username='******')
        old_pass = ret_old.password

        # Update new password
        user_password_change_data = {
            'old_password': '******',
            'new_password1': 'pass2',
            'new_password2': 'pass2'
        }
        user_password_change_form = UserPasswordChangeForm(
            self.user, data=user_password_change_data)

        # run is_valid to get cleaned_data attributes.
        self.assertTrue(user_password_change_form.is_valid())

        user_password_change_form.save()

        # Also try to get from database after it's saved
        ret_new = User.objects.get(username='******')
        new_pass = ret_new.password

        # Check result
        self.assertIsInstance(ret_new, User)
        self.assertNotEqual(old_pass, new_pass)
Example #2
0
    def test_UserPasswordChangeForm_clean_old_password(self):
        user_password_change_data = {'old_password': '******', 'new_password1': 'pass2', 'new_password2': 'pass2'}
        user_password_change_form = UserPasswordChangeForm(self.user, data=user_password_change_data)

        self.assertTrue(user_password_change_form.is_valid())

        ret = user_password_change_form.clean_old_password()

        self.assertEqual('glass_onion', ret)
Example #3
0
    def test_UserPasswordChangeForm_clean_new_password2(self, mock_vp):
        user_password_change_data = {'old_password': '******', 'new_password1': 'pass2', 'new_password2': 'pass2'}
        user_password_change_form = UserPasswordChangeForm(self.user, data=user_password_change_data)

        self.assertTrue(user_password_change_form.is_valid())

        ret = user_password_change_form.clean_new_password2()
        self.assertEqual('pass2', ret)

        mock_vp.assert_called_with('pass2')
Example #4
0
    def test_UserPasswordChangeForm_clean_new_password2_diff(self):
        user_password_change_data = {'old_password': '******', 'new_password1': 'pass1', 'new_password2': 'pass2'}
        user_password_change_form = UserPasswordChangeForm(self.user, data=user_password_change_data)

        # run is_valid to get cleaned_data
        self.assertFalse(user_password_change_form.is_valid())

        # is_valid removes new_password2 because it's different from pass1, we update here to run the test
        user_password_change_form.cleaned_data['new_password2'] = 'pass2'

        self.assertRaises(forms.ValidationError, user_password_change_form.clean_new_password2)
Example #5
0
    def test_UserPasswordChangeForm_clean_old_password_invalid(self):
        user_password_change_data = {'old_password': '******', 'new_password1': 'pass2', 'new_password2': 'pass2'}
        user_password_change_form = UserPasswordChangeForm(self.user, data=user_password_change_data)

        # is_valid to get cleaned_data
        self.assertFalse(user_password_change_form.is_valid())

        # is_valid removes old_password, add it back for testing
        user_password_change_form.cleaned_data['old_password'] = '******'

        self.assertRaises(forms.ValidationError,  user_password_change_form.clean_old_password)
Example #6
0
def change_password(request, username=None):
    """
    Handle change password request.
    """
    # Get the user object from model
    request_user = request.user

    # Users are not allowed to make changes to other users settings
    if request_user.username != username:
        messages.warning(request, "You are not allowed to change other users' settings.")
        return redirect('user:profile', username=request_user.username)

    # Handle form
    if request.method == 'POST' and 'change-password-submit' in request.POST:
        # Create a form populated with request data
        form = UserPasswordChangeForm(user=request.user, data=request.POST)

        if form.is_valid():
            # Validate the old and new passwords
            form.clean_old_password()
            form.clean_new_password2()

            # If no exceptions raised to here, then the old password is valid and the new passwords match.
            # Save the new passwords to the database.
            form.save()

            # Return to the settings page
            return redirect('user:settings', username=username)

        else:
            pass

    else:
        # Create a form populated with data from the instance user
        form = UserPasswordChangeForm(user=request_user)

    # Create template context object
    context = {'form': form}

    return render(request, 'tethys_portal/user/change_password.html', context)
Example #7
0
def change_password(request, username=None):
    """
    Handle change password request.
    """
    # Get the user object from model
    request_user = request.user

    # Users are not allowed to make changes to other users settings
    if request_user.username != username:
        messages.warning(request, "You are not allowed to change other users' settings.")
        return redirect('user:profile', username=request_user.username)

    # Handle form
    if request.method == 'POST' and 'change-password-submit' in request.POST:
        # Create a form populated with request data
        form = UserPasswordChangeForm(user=request.user, data=request.POST)

        if form.is_valid():
            # Validate the old and new passwords
            form.clean_old_password()
            form.clean_new_password2()

            # If no exceptions raised to here, then the old password is valid and the new passwords match.
            # Save the new passwords to the database.
            form.save()

            # Return to the settings page
            return redirect('user:settings', username=username)

    else:
        # Create a form populated with data from the instance user
        form = UserPasswordChangeForm(user=request_user)

    # Create template context object
    context = {'form': form}

    return render(request, 'tethys_portal/user/change_password.html', context)