1

I have a view to create new users in my django project.

I am applying the @sensitive_post_parameters decorator to that view to make sure the password isn't logged if there is an unhandled exception or something like that (as indicated in the comments in the source code https://docs.djangoproject.com/en/2.0/_modules/django/views/decorators/debug/).

When I proceed to test the view, I would like to make sure that this protection of the sensitive information is still in place (that I didn't delete the decorator to the function by mistake or something).

I am aware, since the decorator is applied to my function, I can't test it directly from the view tests.

But, for example, with the @login_required decorator, I can test its effects with assertRedirects (as explained here How to test if a view is decorated with "login_required" (Django)).

I have been searching for a way to do that, but I can't find one that works.

I thought of something like this:

def test_senstive_post_parameters(self):
    request = RequestFactory().post('create_user', data={})
    my_sensitive_parameters = ['password']
    self.assertEqual(
        request.sensitive_post_parameters,
        my_senstive_parameters
    )

but that gives me an

AttributeError: 'WSGIRequest' object has no attribute 'sensitive_post_parameters'

Any help would be appreciated. Even it is telling me I shouldn't be attempting to test this, though I would really like to, as it is seems like an important behaviour that I should make sure remains in my code as it is later modified.

Ana Isabel
  • 13
  • 3

1 Answers1

1

You have created a request using RequestFactory, but you have not actually used it. To test the effect of your view you need to import the view and call it.

from myapp.views import create_user

def test_senstive_post_parameters(self):
    request = RequestFactory().post('create_user', data={})
    response = create_user(request)
    my_sensitive_parameters = ['password']
    self.assertEqual(
        request.sensitive_post_parameters,
        my_senstive_parameters
    )
Alasdair
  • 298,606
  • 55
  • 578
  • 516