Skip to content

Commit

Permalink
add documentation for new fixtures
Browse files Browse the repository at this point in the history
Update helpers.rst with corrections from @blueyed
  • Loading branch information
tony committed Jan 18, 2018
1 parent 67f2225 commit 5ff887f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions docs/helpers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,39 @@ Example
response = my_view(request)
assert response.status_code == 200

``rf_user``, ``rf_admin``, ``rf_unauth``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Additional instances of RequestFactory, which mimic the behavior of these
two middlewares:

- `django.contrib.sessions.middleware.SessionMiddleware`_
- `django.contrib.auth.middleware.AuthenticationMiddleware`_

This will add two attributes to the ``request`` object,
``request.user`` and ``request.session``.

- ``request.session`` is an in-memory `SessionBase`_ object.
- ``request.user`` depends on the fixture being used:

- ``rf_unauth``: an `AnonymousUser`_
- ``rf_user``: a normal user with no additional privileges
- ``rf_admin``: an admin user

::

from myapp.views import my_view

def test_details(rf_unauth):
request = rf_unauth.get('/customer/details')
response = my_view(request)
assert response.status_code == 200

.. _django.contrib.sessions.middleware.SessionMiddleware: https://docs.djangoproject.com/en/2.0/ref/middleware/#django.contrib.sessions.middleware.SessionMiddleware
.. _django.contrib.auth.middleware.AuthenticationMiddleware: https://docs.djangoproject.com/en/2.0/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware
.. _SessionBase: https://docs.djangoproject.com/en/2.0/topics/http/sessions/#django.contrib.sessions.backends.base.SessionBase
.. _AnonymousUser: https://docs.djangoproject.com/en/2.0/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser

``client`` - ``django.test.Client``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

``user_client`` - ``django.test.Client`` logged in as normal user
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same as ``admin_client``, but for a regular user.

Example
"""""""

::

def test_an_admin_view(user_client):
response = user_client.get('/admin/')
assert response.status_code == 403

``admin_user`` - a admin user (superuser)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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


``django_user`` - a normal user
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Same as ``admin_user``, but for a regular user.

``django_user_model``
~~~~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit 5ff887f

Please sign in to comment.