django-webtest, user authentication and view decorators

695 views Asked by At

I'm using django-webtest (v1.5.6) to test a decorator is limiting access to a view to authenticated users.

My view is simply:

@active_account_required
def homepage(request):
    return render(request, 'accounts/account_homepage.html', {
        'user': request.user,
        })  

The active_account_required decorator is:

def active_account_required(function = None):
    """ 
    Check the user is both logged in and has a valid, activated user account.

    If a user tries to access a view protected by this decorator, they will be
    redirected accordingly.

    See http://passingcuriosity.com/2009/writing-view-decorators-for-django/
    """

    def _dec(view_func):
        def _view(request, *args, **kwargs):
            if request.user.is_anonymous():
                return HttpResponseRedirect(reverse_lazy('auth_login'))
            if not request.user.get_profile().is_activated():
                return HttpResponseRedirect(reverse_lazy('registration_activation_incomplete'))
            return view_func(request, *args, **kwargs)

        _view.__name__ = view_func.__name__
        _view.__dict__ = view_func.__dict__
        _view.__doc__ = view_func.__doc__

        return _view

    if function is None:
        return _dec
    else:
        return _dec(function)

My test method is

class AccountViewsTests(WebTest):
    def test_activated_user_can_access_account_homepage(self):
        """ 
        Test an activated user can access the account homepage
        """
        user = G(User)
        user.get_profile().datetime_activated = timezone.now()
        res = self.app.get(reverse('account_homepage'), user = user)
        pdb.set_trace()
        self.assertTemplateUsed(res, 'accounts/account_homepage.html',
                'Activated account did not access account homepage correctly')

(The user object is created using the G function from django-dynamic-fixture)

When running the test, the decorator is preventing access to the homepage view.

You can see I'm using pdb to inspect the objects. User is a valid user object that should pass all the tests in the active_account_required decorator:

(Pdb) user
<User: 2>
(Pdb) user.is_anonymous()
False
(Pdb) user.get_profile().is_activated()
True

Despite the user being correct, the response from self.app.get(reverse('account_homepage'), user = user) is a 302 redirect to the registration_activation_incomplete URL as per the decorator code:

(Pdb) res
<302 FOUND text/html location: http://localhost:80/accounts/registration/activate/incomplete/ no body>

It appears the user object is not being sent correctly in the WebTest request, but this matches the django-webtest documentation. I've also tried passing the user in by username as user='2' but get the same result.

Any ideas?

1

There are 1 answers

0
edkay On

Oops - the problem is that I simply forgot to save my user profile after setting the activation timestamp!

Changing the test code to:

user = G(User)
user.get_profile().datetime_activated = timezone.now()
user.get_profile().save()
res = self.app.get(reverse('account_homepage'), user = user)

i.e. adding user.get_profile().save() got it working :)

Sorry for the noise.