سؤال

I want to be able to log in a manually created Userena user for testing:

From my_app/test/test_views.py

import django.test
from userena.models import UserenaSignup


class MyViewTestCase(django.test.TestCase):
    def test_login(self):
        my_user = UserenaSignup.objects.create_user(username="my_name", email="my_name@gmail.com", password="my_password")
        log_in = self.client.login(username=my_user.username, password=my_user.password, email=my_user.email)
        import pdb; pdb.set_trace() 

In pdb:

>>> log_in
False

Why does logging in fail?

هل كانت مفيدة؟

المحلول 2

As a reference:

How to create a userena user manually

>>> my_user = UserenaSignup.objects.create_user(username="my_name", email="my_name@gmail.com", password="my_password")

# A userena user that hasn't activated his account with an account confirmation link
# has False for is_active
>>> my_user.is_active
False

How to activate an inactive userena user

>>> active_my_user = UserenaSignup.objects.activate_user(my_user.userena_signup.activation_key)
>>> active_my_user.is_active
True

I found this bit of code here, in the tests of Userena.

How to login a userena user manually

Logging in failed because only a user that has True for is_active can log in. So after activating your user (see above) you log in as follows in a test:

self.client.login(username=active_my_user.username, password="my_password", email= active_my_user.email)
#True

نصائح أخرى

I think that my_user.password is crypted. Try this:

self.client.login(username=my_user.username, password='my_password')
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top