Search code examples
djangodjango-testingdjango-usersdjango-userena

Logging in manually created userena user fails, why?


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?


Solution

  • 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