@@ -121,6 +121,39 @@ Example
121
121
response = my_view(request)
122
122
assert response.status_code == 200
123
123
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
+
124
157
``client `` - ``django.test.Client ``
125
158
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126
159
@@ -167,6 +200,20 @@ Example
167
200
Using the `admin_client ` fixture will cause the test to automatically be marked for database use (no need to specify the
168
201
``django_db `` mark).
169
202
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
+
170
217
``admin_user `` - a admin user (superuser)
171
218
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172
219
@@ -177,6 +224,11 @@ Using the `admin_user` fixture will cause the test to automatically be marked fo
177
224
``django_db `` mark).
178
225
179
226
227
+ ``django_user `` - a normal user
228
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
229
+
230
+ Same as ``admin_user ``, but for a regular user.
231
+
180
232
``django_user_model ``
181
233
~~~~~~~~~~~~~~~~~~~~~
182
234
0 commit comments