コード例 #1
0
def forgot_password(request):
    """
    Actions when user forgot password.
    :param request: object
    :return: redirect to views.home() or 'loginsys/forgot.html' with form, message
    """
    message = None
    if request.method == 'POST':
        form = ForgotPasswordForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            user = User.objects.filter(email=email)
            salt = random_salt(len(user[0].username))
            code = signing.dumps([user[0].id, user[0].email, user[0].username],
                                 key=settings.SECRET_KEY, salt=salt)
            url = settings.SITE_URL + reverse('loginsys:reset', args=[code])
            send_email.apply_async(('Welcome', '<p>Hello</p><p><a href="{0}">Go to this link</a></p>'.
                                    format(url), [email]))
            store = ForgotPasswordLink()
            store.random_salt = salt
            store.user_link_id = user[0].id
            store.code_value = code
            store.save()
            forgot_password_salt_life.apply_async((user[0].id, salt), countdown=180)
            request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)
            return redirect(reverse('home'))
    else:
        form = ForgotPasswordForm()
    return render(request, 'loginsys/forgot.html', {'form': form, 'message': message})
コード例 #2
0
 def test_reset_password(self):
     """
     Test case for reset_password()
     :return: None
     """
     response = self.client.get(str(settings.SITE_URL + reverse('loginsys:reset', args=[123])))
     self.assertEqual(response.status_code, 302)
     self.assertEqual(self.client.session['message'], 'Time over. Link was blocked.')
     self.assertRedirects(response, reverse('home'), 302, 200)
     user = User.objects.get(username='******')
     salt = random_salt(len(user.username))
     code = signing.dumps([user.id, user.email, user.username],
                          key=settings.SECRET_KEY, salt=salt)
     store = ForgotPasswordLink()
     store.random_salt = salt
     store.user_link_id = user.id
     store.code_value = code
     store.save()
     new_code = str(code) + '1'
     response = self.client.get(str(settings.SITE_URL + reverse('loginsys:reset', args=[new_code])))
     self.assertEqual(response.status_code, 302)
     self.assertEqual(self.client.session['message'], 'Time over. Link was blocked.')
     self.assertRedirects(response, reverse('home'), 302, 200)
     response = self.client.get(str(settings.SITE_URL + reverse('loginsys:reset', args=[code])))
     self.assertEqual(response.status_code, 200)
     self.assertTemplateUsed(response, 'loginsys/reset.html')
     self.assertTemplateUsed(response, 'base.html')
     self.assertEqual(response.context['code'], code)
     self.assertEqual(response.context['form'].is_valid(), False)
     response = self.client.post(str(settings.SITE_URL + reverse('loginsys:reset', args=[code])),
                                 {'password': '******', 'repeat_password': '******'})
     self.assertEqual(response.status_code, 200)
     self.assertTemplateUsed(response, 'loginsys/reset.html')
     self.assertTemplateUsed(response, 'base.html')
     self.assertEqual(response.context['code'], code)
     self.assertEqual(response.context['form'].is_valid(), False)
     response = self.client.post(str(settings.SITE_URL + reverse('loginsys:reset', args=[code])),
                                 {'password': '******', 'repeat_password': '******'})
     self.assertEqual(response.status_code, 302)
     self.assertEqual(self.client.session['message'], 'Password has changed.')
     self.assertRedirects(response, reverse('home'), 302, 200)
     data = ForgotPasswordLink.objects.all()
     self.assertEqual(len(data), 0)