Code

Opened 10 months ago

Closed 10 months ago

Last modified 10 months ago

#20609 closed Cleanup/optimization (fixed)

Document how to use request.user with RequestFactory

Reported by: michel@… Owned by: susan
Component: Documentation Version: 1.5
Severity: Normal Keywords:
Cc: timograham@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I had a lot of problems finding out why I cannot use request.user in my tests while using RequestFactory.

this line in the documentation (https://docs.djangoproject.com/en/1.4/topics/testing/#the-request-factory) seems to say it:

It does not support middleware. Session and authentication attributes must be supplied by the test itself if required for the view to function properly.

But it would be nice to have an example with a user added, like:

class TestCaseWithUser(TestCase):

def setUp(self):

# Every test needs access to the request factory.
self.factory = RequestFactory()
self.client = Client()
self.user_foo = User.objects.create_user('foo', 'foo@…', 'bar')

def tearDown(self):

# Delete those objects that are saved in setup
self.user_foo.delete()

def test_request_user(self):

self.client.login( username='foo', password='bar')
request = self.factory.post('/my/url/', {"somedata": "data"})
# add a user so in the view to test request.user works
request.user = self.user_foo
nt.assert_equal(request.user,self.user_foo)

Attachments (0)

Change History (6)

comment:1 Changed 10 months ago by timo

  • Cc timograham@… added
  • Easy pickings set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Documentation extension to Document how to use request.user with RequestFactory
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Cleanup/optimization

comment:2 Changed 10 months ago by susan

  • Owner changed from nobody to susan
  • Status changed from new to assigned

comment:3 Changed 10 months ago by susan

See https://github.com/django/django/pull/1319/files for PR. I edited the given doc patch. Please code review. Thanks.

comment:4 Changed 10 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 067e0424ce8a0e15e2faac3f9f9fabe9b7667c72:

Fixed #20609 -- Documented how to use request.user with RequestFactory

Thanks michel@ for the suggestion.

comment:5 Changed 10 months ago by Tim Graham <timograham@…>

In 75041d5ea3821912abf8b9d1cd5477228a78cb23:

[1.6.x] Fixed #20609 -- Documented how to use request.user with RequestFactory

Thanks michel@ for the suggestion.

Backport of 067e0424ce from master.

comment:6 Changed 10 months ago by Tim Graham <timograham@…>

In 583f340d7dd52c6182530fd4ca798d01c4d93609:

[1.5.x] Fixed #20609 -- Documented how to use request.user with RequestFactory

Thanks michel@ for the suggestion.

Backport of 75041d5ea3 from stable/1.5.x.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.