Beispiel #1
0
    def test_valid_token(self):
        """
        WHEN a user changes his or her password with a valid token
        THEN the new password is set
        """
        token = self.a_user.profile.generate_token(TokenType.RESET)
        is_password_changed = Profile.reset_password(token, OTHER_PASSWORD)

        user = User.objects.get(pk=self.a_user.pk)
        self.assertTrue(is_password_changed)
        self.assertTrue(user.check_password(OTHER_PASSWORD))
Beispiel #2
0
    def test_expired_token(self):
        """
        WHEN a user changes his or her password with an invalid token
        THEN the password remains the same
        """
        token = self.a_user.profile.generate_token(TokenType.RESET, 0.5)
        time.sleep(1)
        is_password_changed = Profile.reset_password(token, OTHER_PASSWORD)

        user = User.objects.get(pk=self.a_user.pk)
        self.assertFalse(is_password_changed)
        self.assertFalse(user.check_password(OTHER_PASSWORD))
Beispiel #3
0
    def test_invalid_token(self):
        """
        GIVEN an invalid password reset token
        WHEN resetting the user's password
        THEN the second user's password remains the same
        """
        token = self.a_user.profile.generate_token(TokenType.RESET) + "a"

        is_password_changed = Profile.reset_password(token, OTHER_PASSWORD)

        user = User.objects.get(pk=self.a_user.pk)
        self.assertFalse(is_password_changed)
        self.assertFalse(user.check_password(OTHER_PASSWORD))
Beispiel #4
0
def password_reset_view(request, token):
    if request.method == "POST":
        data = request.POST.copy()
        form = ResetPasswordForm(data=data)
        if form.is_valid():
            if Profile.reset_password(token, data["password1"]):
                messages.info(request, "Your password has been updated.")
                return redirect(reverse("app:auth.login"))
            else:
                return redirect(reverse("app:main.index"))

    form = ResetPasswordForm()
    args = dict(title="Reset Your Password", hide_nav=True, form=form)
    return render(request, "auth/reset_password.html", args)