Skip to content

Commit 5ff887f

Browse files
committed
add documentation for new fixtures
Update helpers.rst with corrections from @blueyed
1 parent 67f2225 commit 5ff887f

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/helpers.rst

+52
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,39 @@ Example
121121
response = my_view(request)
122122
assert response.status_code == 200
123123

124+
``rf_user``, ``rf_admin``, ``rf_unauth``
125+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126+
127+
Additional instances of RequestFactory, which mimic the behavior of these
128+
two middlewares:
129+
130+
- `django.contrib.sessions.middleware.SessionMiddleware`_
131+
- `django.contrib.auth.middleware.AuthenticationMiddleware`_
132+
133+
This will add two attributes to the ``request`` object,
134+
``request.user`` and ``request.session``.
135+
136+
- ``request.session`` is an in-memory `SessionBase`_ object.
137+
- ``request.user`` depends on the fixture being used:
138+
139+
- ``rf_unauth``: an `AnonymousUser`_
140+
- ``rf_user``: a normal user with no additional privileges
141+
- ``rf_admin``: an admin user
142+
143+
::
144+
145+
from myapp.views import my_view
146+
147+
def test_details(rf_unauth):
148+
request = rf_unauth.get('/customer/details')
149+
response = my_view(request)
150+
assert response.status_code == 200
151+
152+
.. _django.contrib.sessions.middleware.SessionMiddleware: https://docs.djangoproject.com/en/2.0/ref/middleware/#django.contrib.sessions.middleware.SessionMiddleware
153+
.. _django.contrib.auth.middleware.AuthenticationMiddleware: https://docs.djangoproject.com/en/2.0/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware
154+
.. _SessionBase: https://docs.djangoproject.com/en/2.0/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase
155+
.. _AnonymousUser: https://docs.djangoproject.com/en/2.0/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser
156+
124157
``client`` - ``django.test.Client``
125158
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126159

@@ -167,6 +200,20 @@ Example
167200
Using the `admin_client` fixture will cause the test to automatically be marked for database use (no need to specify the
168201
``django_db`` mark).
169202

203+
``user_client`` - ``django.test.Client`` logged in as normal user
204+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205+
206+
Same as ``admin_client``, but for a regular user.
207+
208+
Example
209+
"""""""
210+
211+
::
212+
213+
def test_an_admin_view(user_client):
214+
response = user_client.get('/admin/')
215+
assert response.status_code == 403
216+
170217
``admin_user`` - a admin user (superuser)
171218
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172219

@@ -177,6 +224,11 @@ Using the `admin_user` fixture will cause the test to automatically be marked fo
177224
``django_db`` mark).
178225

179226

227+
``django_user`` - a normal user
228+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229+
230+
Same as ``admin_user``, but for a regular user.
231+
180232
``django_user_model``
181233
~~~~~~~~~~~~~~~~~~~~~
182234

0 commit comments

Comments
 (0)