1

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?

Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • 1
    I have never used Userrena myself, but with some systems this might be due to user not clicking "Verify your email" link or some other extra limitations forced on the user account creation. – Mikko Ohtamaa Dec 24 '13 at 16:14
  • Sorry for responding so late. This was the clue I needed. – Bentley4 Dec 26 '13 at 11:24

2 Answers2

1

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

self.client.login(username=my_user.username, password='my_password')
Dragos C
  • 363
  • 3
  • 7
1

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
Bentley4
  • 10,678
  • 25
  • 83
  • 134