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 protected]'})
self.assertRedirects(response, reverse('home'), 302, 200)
session = self.client.session
self.assertEqual(session['message'], 'Instruction have sent on your mail - [email protected]')
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 protected]')
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, https://mcmap.net/q/804434/-setting-a-session-variable-in-django-tests 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.