atugushev / django-password-session

A reusable Django app that invalidates all active sessions after change password
MIT License
10 stars 1 forks source link

Error when testing the application #1

Closed mauricioabreu closed 10 years ago

mauricioabreu commented 10 years ago

I am facing a problem running my tests. All tests that rely on logged in users fail and output the following traceback:

  File "/vagrant/env/lib/python2.6/site-packages/password_session/handlers.py", line 18, in update_session_auth_hash
    if request.user == user:
AttributeError: 'HttpRequest' object has no attribute 'user'

My tests have setUp function (TestCase) that login the user.

self.client.login(username=self.user.email, password='aaa123')
atugushev commented 10 years ago

Hey, @mauricioabreu! Thank you for the report. Could you shoot me a whole testcase?

mauricioabreu commented 10 years ago

Of course:

I am using:

Here is an example:

class TestListUserSearch(TestCase):

    def setUp(self):
        self.user = mommy_recipes.make_user('brand')
        self.brand = CustomUserAccount.objects.get(user=self.user).account
        self.client.login(username=self.user.email,
                          password='aaa123')

    def test_list_users_with_filter_by_email(self):
        user = mommy.make(CustomUserAccount, account=self.brand).user
        user.save()

        url = reverse('user:user_list',
                      args=[self.brand.id]) + '?q={0}'.format(user.email)
        response = self.client.get(url)
        self.assertTemplateUsed(response, 'user/user_list.html')
        self.assertEqual(len(response.context['objects']), 1)

Every test I write setUp outputs errors:

AttributeError: 'HttpRequest' object has no attribute 'user'
atugushev commented 10 years ago

Couldn't reproduce it. I think you may have a special case with settings of test project or something like that.

It would be better if you sent me a pull request including a test to reproduce the problem. Otherwise, i cannot help you, because without a code i will not find anything.

mauricioabreu commented 10 years ago

Attached #2 Can you check my test? This way you can reproduce what I am talking about. Thanks!

mauricioabreu commented 10 years ago

When running the application itself those errors don't happen.

atugushev commented 10 years ago

Thank you for the pull request.

After diving into the problem i found that django.test.client.login() is not processing midlewares, where request.user is set.

I've found a commit https://github.com/django/django/commit/4fdd51b73240bf9c8d9472fcc45df699f0714755, that solve that problem, but it's fixed in django 1.7.

I'll try to find some solution.

mauricioabreu commented 10 years ago

I amo willing to provide a fix plus some tests. Tell me if you need some help. Thanks.

atugushev commented 10 years ago

It seems everything is ok!