samedi 1 août 2015

How to test django session in django unittest?

I have created view:

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})

I have write message in session:

request.session["message"] = 'Instruction have sent on your mail - {0}'.format(email)

Then I have tested view:

def test_forgot_password(self):
    response = self.client.post(reverse('loginsys:forgot'), {'email': 'email@project.com'})
    self.assertRedirects(response, reverse('home'), 302, 200)
    session = self.client.session
    self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com')

And I have got error: Error Traceback (most recent call last): File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 105, in test_forgot_password self.assertEqual(session['message'], 'Instruction have sent on your mail - email@project.com') File "/home/maxim/SSHKeyStoreVE/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 48, in getitem return self._session[key] KeyError: 'message'

So I don't know what is the problem. I find some examples and make like there, but it doesn't help: Django testing stored session data in tests, How do I modify the session in the Django test framework, http://ift.tt/1ME9Z7X (error: Error Traceback (most recent call last): File "/home/maxim/PycharmProjects/SSHKeyStore/loginsys/tests.py", line 22, in setUp self.client.session = self.session AttributeError: can't set attribute) + I have got message: RemovedInDjango19Warning: django.utils.importlib will be removed in Django 1.9. return f(*args, **kwds) This is bad.

Please help me to fined solution and understand the problem ( may be solution is here Using session object in Django unit test but I didn't understand).

Thanks.

Aucun commentaire:

Enregistrer un commentaire