plain 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. plain/README.md +33 -0
  2. plain/__main__.py +5 -0
  3. plain/assets/README.md +56 -0
  4. plain/assets/__init__.py +6 -0
  5. plain/assets/finders.py +233 -0
  6. plain/assets/preflight.py +14 -0
  7. plain/assets/storage.py +916 -0
  8. plain/assets/utils.py +52 -0
  9. plain/assets/whitenoise/__init__.py +5 -0
  10. plain/assets/whitenoise/base.py +259 -0
  11. plain/assets/whitenoise/compress.py +189 -0
  12. plain/assets/whitenoise/media_types.py +137 -0
  13. plain/assets/whitenoise/middleware.py +197 -0
  14. plain/assets/whitenoise/responders.py +286 -0
  15. plain/assets/whitenoise/storage.py +178 -0
  16. plain/assets/whitenoise/string_utils.py +13 -0
  17. plain/cli/README.md +123 -0
  18. plain/cli/__init__.py +3 -0
  19. plain/cli/cli.py +439 -0
  20. plain/cli/formatting.py +61 -0
  21. plain/cli/packages.py +73 -0
  22. plain/cli/print.py +9 -0
  23. plain/cli/startup.py +33 -0
  24. plain/csrf/README.md +3 -0
  25. plain/csrf/middleware.py +466 -0
  26. plain/csrf/views.py +10 -0
  27. plain/debug.py +23 -0
  28. plain/exceptions.py +242 -0
  29. plain/forms/README.md +14 -0
  30. plain/forms/__init__.py +8 -0
  31. plain/forms/boundfield.py +58 -0
  32. plain/forms/exceptions.py +11 -0
  33. plain/forms/fields.py +1030 -0
  34. plain/forms/forms.py +297 -0
  35. plain/http/README.md +1 -0
  36. plain/http/__init__.py +51 -0
  37. plain/http/cookie.py +20 -0
  38. plain/http/multipartparser.py +743 -0
  39. plain/http/request.py +754 -0
  40. plain/http/response.py +719 -0
  41. plain/internal/__init__.py +0 -0
  42. plain/internal/files/README.md +3 -0
  43. plain/internal/files/__init__.py +3 -0
  44. plain/internal/files/base.py +161 -0
  45. plain/internal/files/locks.py +127 -0
  46. plain/internal/files/move.py +102 -0
  47. plain/internal/files/temp.py +79 -0
  48. plain/internal/files/uploadedfile.py +150 -0
  49. plain/internal/files/uploadhandler.py +254 -0
  50. plain/internal/files/utils.py +78 -0
  51. plain/internal/handlers/__init__.py +0 -0
  52. plain/internal/handlers/base.py +133 -0
  53. plain/internal/handlers/exception.py +145 -0
  54. plain/internal/handlers/wsgi.py +216 -0
  55. plain/internal/legacy/__init__.py +0 -0
  56. plain/internal/legacy/__main__.py +12 -0
  57. plain/internal/legacy/management/__init__.py +414 -0
  58. plain/internal/legacy/management/base.py +692 -0
  59. plain/internal/legacy/management/color.py +113 -0
  60. plain/internal/legacy/management/commands/__init__.py +0 -0
  61. plain/internal/legacy/management/commands/collectstatic.py +297 -0
  62. plain/internal/legacy/management/sql.py +67 -0
  63. plain/internal/legacy/management/utils.py +175 -0
  64. plain/json.py +40 -0
  65. plain/logs/README.md +24 -0
  66. plain/logs/__init__.py +5 -0
  67. plain/logs/configure.py +39 -0
  68. plain/logs/loggers.py +74 -0
  69. plain/logs/utils.py +46 -0
  70. plain/middleware/README.md +3 -0
  71. plain/middleware/__init__.py +0 -0
  72. plain/middleware/clickjacking.py +52 -0
  73. plain/middleware/common.py +87 -0
  74. plain/middleware/gzip.py +64 -0
  75. plain/middleware/security.py +64 -0
  76. plain/packages/README.md +41 -0
  77. plain/packages/__init__.py +4 -0
  78. plain/packages/config.py +259 -0
  79. plain/packages/registry.py +438 -0
  80. plain/paginator.py +187 -0
  81. plain/preflight/README.md +3 -0
  82. plain/preflight/__init__.py +38 -0
  83. plain/preflight/compatibility/__init__.py +0 -0
  84. plain/preflight/compatibility/django_4_0.py +20 -0
  85. plain/preflight/files.py +19 -0
  86. plain/preflight/messages.py +88 -0
  87. plain/preflight/registry.py +72 -0
  88. plain/preflight/security/__init__.py +0 -0
  89. plain/preflight/security/base.py +268 -0
  90. plain/preflight/security/csrf.py +40 -0
  91. plain/preflight/urls.py +117 -0
  92. plain/runtime/README.md +75 -0
  93. plain/runtime/__init__.py +61 -0
  94. plain/runtime/global_settings.py +199 -0
  95. plain/runtime/user_settings.py +353 -0
  96. plain/signals/README.md +14 -0
  97. plain/signals/__init__.py +5 -0
  98. plain/signals/dispatch/__init__.py +9 -0
  99. plain/signals/dispatch/dispatcher.py +320 -0
  100. plain/signals/dispatch/license.txt +35 -0
  101. plain/signing.py +299 -0
  102. plain/templates/README.md +20 -0
  103. plain/templates/__init__.py +6 -0
  104. plain/templates/core.py +24 -0
  105. plain/templates/jinja/README.md +227 -0
  106. plain/templates/jinja/__init__.py +22 -0
  107. plain/templates/jinja/defaults.py +119 -0
  108. plain/templates/jinja/extensions.py +39 -0
  109. plain/templates/jinja/filters.py +28 -0
  110. plain/templates/jinja/globals.py +19 -0
  111. plain/test/README.md +3 -0
  112. plain/test/__init__.py +16 -0
  113. plain/test/client.py +985 -0
  114. plain/test/utils.py +255 -0
  115. plain/urls/README.md +3 -0
  116. plain/urls/__init__.py +40 -0
  117. plain/urls/base.py +118 -0
  118. plain/urls/conf.py +94 -0
  119. plain/urls/converters.py +66 -0
  120. plain/urls/exceptions.py +9 -0
  121. plain/urls/resolvers.py +731 -0
  122. plain/utils/README.md +3 -0
  123. plain/utils/__init__.py +0 -0
  124. plain/utils/_os.py +52 -0
  125. plain/utils/cache.py +327 -0
  126. plain/utils/connection.py +84 -0
  127. plain/utils/crypto.py +76 -0
  128. plain/utils/datastructures.py +345 -0
  129. plain/utils/dateformat.py +329 -0
  130. plain/utils/dateparse.py +154 -0
  131. plain/utils/dates.py +76 -0
  132. plain/utils/deconstruct.py +54 -0
  133. plain/utils/decorators.py +90 -0
  134. plain/utils/deprecation.py +6 -0
  135. plain/utils/duration.py +44 -0
  136. plain/utils/email.py +12 -0
  137. plain/utils/encoding.py +235 -0
  138. plain/utils/functional.py +456 -0
  139. plain/utils/hashable.py +26 -0
  140. plain/utils/html.py +401 -0
  141. plain/utils/http.py +374 -0
  142. plain/utils/inspect.py +73 -0
  143. plain/utils/ipv6.py +46 -0
  144. plain/utils/itercompat.py +8 -0
  145. plain/utils/module_loading.py +69 -0
  146. plain/utils/regex_helper.py +353 -0
  147. plain/utils/safestring.py +72 -0
  148. plain/utils/termcolors.py +221 -0
  149. plain/utils/text.py +518 -0
  150. plain/utils/timesince.py +138 -0
  151. plain/utils/timezone.py +244 -0
  152. plain/utils/tree.py +126 -0
  153. plain/validators.py +603 -0
  154. plain/views/README.md +268 -0
  155. plain/views/__init__.py +18 -0
  156. plain/views/base.py +107 -0
  157. plain/views/csrf.py +24 -0
  158. plain/views/errors.py +25 -0
  159. plain/views/exceptions.py +4 -0
  160. plain/views/forms.py +76 -0
  161. plain/views/objects.py +229 -0
  162. plain/views/redirect.py +72 -0
  163. plain/views/templates.py +66 -0
  164. plain/wsgi.py +11 -0
  165. plain-0.1.0.dist-info/LICENSE +85 -0
  166. plain-0.1.0.dist-info/METADATA +51 -0
  167. plain-0.1.0.dist-info/RECORD +169 -0
  168. plain-0.1.0.dist-info/WHEEL +4 -0
  169. plain-0.1.0.dist-info/entry_points.txt +3 -0
plain/test/client.py ADDED
@@ -0,0 +1,985 @@
1
+ import json
2
+ import mimetypes
3
+ import os
4
+ import sys
5
+ from copy import copy
6
+ from functools import partial
7
+ from http import HTTPStatus
8
+ from http.cookies import SimpleCookie
9
+ from importlib import import_module
10
+ from io import BytesIO, IOBase
11
+ from urllib.parse import unquote_to_bytes, urljoin, urlparse, urlsplit
12
+
13
+ from plain.http import HttpHeaders, HttpRequest, QueryDict
14
+ from plain.internal.handlers.base import BaseHandler
15
+ from plain.internal.handlers.wsgi import WSGIRequest
16
+ from plain.json import PlainJSONEncoder
17
+ from plain.runtime import settings
18
+ from plain.signals import got_request_exception, request_started
19
+ from plain.test.utils import ContextList
20
+ from plain.urls import resolve
21
+ from plain.utils.encoding import force_bytes
22
+ from plain.utils.functional import SimpleLazyObject
23
+ from plain.utils.http import urlencode
24
+ from plain.utils.itercompat import is_iterable
25
+ from plain.utils.regex_helper import _lazy_re_compile
26
+
27
+ __all__ = (
28
+ "Client",
29
+ "RedirectCycleError",
30
+ "RequestFactory",
31
+ "encode_file",
32
+ "encode_multipart",
33
+ )
34
+
35
+
36
+ BOUNDARY = "BoUnDaRyStRiNg"
37
+ MULTIPART_CONTENT = "multipart/form-data; boundary=%s" % BOUNDARY
38
+ CONTENT_TYPE_RE = _lazy_re_compile(r".*; charset=([\w-]+);?")
39
+ # Structured suffix spec: https://tools.ietf.org/html/rfc6838#section-4.2.8
40
+ JSON_CONTENT_TYPE_RE = _lazy_re_compile(r"^application\/(.+\+)?json")
41
+
42
+
43
+ class RedirectCycleError(Exception):
44
+ """The test client has been asked to follow a redirect loop."""
45
+
46
+ def __init__(self, message, last_response):
47
+ super().__init__(message)
48
+ self.last_response = last_response
49
+ self.redirect_chain = last_response.redirect_chain
50
+
51
+
52
+ class FakePayload(IOBase):
53
+ """
54
+ A wrapper around BytesIO that restricts what can be read since data from
55
+ the network can't be sought and cannot be read outside of its content
56
+ length. This makes sure that views can't do anything under the test client
57
+ that wouldn't work in real life.
58
+ """
59
+
60
+ def __init__(self, initial_bytes=None):
61
+ self.__content = BytesIO()
62
+ self.__len = 0
63
+ self.read_started = False
64
+ if initial_bytes is not None:
65
+ self.write(initial_bytes)
66
+
67
+ def __len__(self):
68
+ return self.__len
69
+
70
+ def read(self, size=-1, /):
71
+ if not self.read_started:
72
+ self.__content.seek(0)
73
+ self.read_started = True
74
+ if size == -1 or size is None:
75
+ size = self.__len
76
+ assert (
77
+ self.__len >= size
78
+ ), "Cannot read more than the available bytes from the HTTP incoming data."
79
+ content = self.__content.read(size)
80
+ self.__len -= len(content)
81
+ return content
82
+
83
+ def readline(self, size=-1, /):
84
+ if not self.read_started:
85
+ self.__content.seek(0)
86
+ self.read_started = True
87
+ if size == -1 or size is None:
88
+ size = self.__len
89
+ assert (
90
+ self.__len >= size
91
+ ), "Cannot read more than the available bytes from the HTTP incoming data."
92
+ content = self.__content.readline(size)
93
+ self.__len -= len(content)
94
+ return content
95
+
96
+ def write(self, b, /):
97
+ if self.read_started:
98
+ raise ValueError("Unable to write a payload after it's been read")
99
+ content = force_bytes(b)
100
+ self.__content.write(content)
101
+ self.__len += len(content)
102
+
103
+
104
+ def conditional_content_removal(request, response):
105
+ """
106
+ Simulate the behavior of most web servers by removing the content of
107
+ responses for HEAD requests, 1xx, 204, and 304 responses. Ensure
108
+ compliance with RFC 9112 Section 6.3.
109
+ """
110
+ if 100 <= response.status_code < 200 or response.status_code in (204, 304):
111
+ if response.streaming:
112
+ response.streaming_content = []
113
+ else:
114
+ response.content = b""
115
+ if request.method == "HEAD":
116
+ if response.streaming:
117
+ response.streaming_content = []
118
+ else:
119
+ response.content = b""
120
+ return response
121
+
122
+
123
+ class ClientHandler(BaseHandler):
124
+ """
125
+ An HTTP Handler that can be used for testing purposes. Use the WSGI
126
+ interface to compose requests, but return the raw Response object with
127
+ the originating WSGIRequest attached to its ``wsgi_request`` attribute.
128
+ """
129
+
130
+ def __init__(self, enforce_csrf_checks=True, *args, **kwargs):
131
+ self.enforce_csrf_checks = enforce_csrf_checks
132
+ super().__init__(*args, **kwargs)
133
+
134
+ def __call__(self, environ):
135
+ # Set up middleware if needed. We couldn't do this earlier, because
136
+ # settings weren't available.
137
+ if self._middleware_chain is None:
138
+ self.load_middleware()
139
+
140
+ request_started.send(sender=self.__class__, environ=environ)
141
+ request = WSGIRequest(environ)
142
+ # sneaky little hack so that we can easily get round
143
+ # CsrfViewMiddleware. This makes life easier, and is probably
144
+ # required for backwards compatibility with external tests against
145
+ # admin views.
146
+ request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
147
+
148
+ # Request goes through middleware.
149
+ response = self.get_response(request)
150
+
151
+ # Simulate behaviors of most web servers.
152
+ conditional_content_removal(request, response)
153
+
154
+ # Attach the originating request to the response so that it could be
155
+ # later retrieved.
156
+ response.wsgi_request = request
157
+
158
+ # Emulate a WSGI server by calling the close method on completion.
159
+ response.close()
160
+
161
+ return response
162
+
163
+
164
+ def store_rendered_templates(store, signal, sender, template, context, **kwargs):
165
+ """
166
+ Store templates and contexts that are rendered.
167
+
168
+ The context is copied so that it is an accurate representation at the time
169
+ of rendering.
170
+ """
171
+ store.setdefault("templates", []).append(template)
172
+ if "context" not in store:
173
+ store["context"] = ContextList()
174
+ store["context"].append(copy(context))
175
+
176
+
177
+ def encode_multipart(boundary, data):
178
+ """
179
+ Encode multipart POST data from a dictionary of form values.
180
+
181
+ The key will be used as the form data name; the value will be transmitted
182
+ as content. If the value is a file, the contents of the file will be sent
183
+ as an application/octet-stream; otherwise, str(value) will be sent.
184
+ """
185
+ lines = []
186
+
187
+ def to_bytes(s):
188
+ return force_bytes(s, settings.DEFAULT_CHARSET)
189
+
190
+ # Not by any means perfect, but good enough for our purposes.
191
+ def is_file(thing):
192
+ return hasattr(thing, "read") and callable(thing.read)
193
+
194
+ # Each bit of the multipart form data could be either a form value or a
195
+ # file, or a *list* of form values and/or files. Remember that HTTP field
196
+ # names can be duplicated!
197
+ for key, value in data.items():
198
+ if value is None:
199
+ raise TypeError(
200
+ "Cannot encode None for key '%s' as POST data. Did you mean "
201
+ "to pass an empty string or omit the value?" % key
202
+ )
203
+ elif is_file(value):
204
+ lines.extend(encode_file(boundary, key, value))
205
+ elif not isinstance(value, str) and is_iterable(value):
206
+ for item in value:
207
+ if is_file(item):
208
+ lines.extend(encode_file(boundary, key, item))
209
+ else:
210
+ lines.extend(
211
+ to_bytes(val)
212
+ for val in [
213
+ "--%s" % boundary,
214
+ 'Content-Disposition: form-data; name="%s"' % key,
215
+ "",
216
+ item,
217
+ ]
218
+ )
219
+ else:
220
+ lines.extend(
221
+ to_bytes(val)
222
+ for val in [
223
+ "--%s" % boundary,
224
+ 'Content-Disposition: form-data; name="%s"' % key,
225
+ "",
226
+ value,
227
+ ]
228
+ )
229
+
230
+ lines.extend(
231
+ [
232
+ to_bytes("--%s--" % boundary),
233
+ b"",
234
+ ]
235
+ )
236
+ return b"\r\n".join(lines)
237
+
238
+
239
+ def encode_file(boundary, key, file):
240
+ def to_bytes(s):
241
+ return force_bytes(s, settings.DEFAULT_CHARSET)
242
+
243
+ # file.name might not be a string. For example, it's an int for
244
+ # tempfile.TemporaryFile().
245
+ file_has_string_name = hasattr(file, "name") and isinstance(file.name, str)
246
+ filename = os.path.basename(file.name) if file_has_string_name else ""
247
+
248
+ if hasattr(file, "content_type"):
249
+ content_type = file.content_type
250
+ elif filename:
251
+ content_type = mimetypes.guess_type(filename)[0]
252
+ else:
253
+ content_type = None
254
+
255
+ if content_type is None:
256
+ content_type = "application/octet-stream"
257
+ filename = filename or key
258
+ return [
259
+ to_bytes("--%s" % boundary),
260
+ to_bytes(
261
+ f'Content-Disposition: form-data; name="{key}"; filename="{filename}"'
262
+ ),
263
+ to_bytes("Content-Type: %s" % content_type),
264
+ b"",
265
+ to_bytes(file.read()),
266
+ ]
267
+
268
+
269
+ class RequestFactory:
270
+ """
271
+ Class that lets you create mock Request objects for use in testing.
272
+
273
+ Usage:
274
+
275
+ rf = RequestFactory()
276
+ get_request = rf.get('/hello/')
277
+ post_request = rf.post('/submit/', {'foo': 'bar'})
278
+
279
+ Once you have a request object you can pass it to any view function,
280
+ just as if that view had been hooked up using a URLconf.
281
+ """
282
+
283
+ def __init__(self, *, json_encoder=PlainJSONEncoder, headers=None, **defaults):
284
+ self.json_encoder = json_encoder
285
+ self.defaults = defaults
286
+ self.cookies = SimpleCookie()
287
+ self.errors = BytesIO()
288
+ if headers:
289
+ self.defaults.update(HttpHeaders.to_wsgi_names(headers))
290
+
291
+ def _base_environ(self, **request):
292
+ """
293
+ The base environment for a request.
294
+ """
295
+ # This is a minimal valid WSGI environ dictionary, plus:
296
+ # - HTTP_COOKIE: for cookie support,
297
+ # - REMOTE_ADDR: often useful, see #8551.
298
+ # See https://www.python.org/dev/peps/pep-3333/#environ-variables
299
+ return {
300
+ "HTTP_COOKIE": "; ".join(
301
+ sorted(
302
+ f"{morsel.key}={morsel.coded_value}"
303
+ for morsel in self.cookies.values()
304
+ )
305
+ ),
306
+ "PATH_INFO": "/",
307
+ "REMOTE_ADDR": "127.0.0.1",
308
+ "REQUEST_METHOD": "GET",
309
+ "SCRIPT_NAME": "",
310
+ "SERVER_NAME": "testserver",
311
+ "SERVER_PORT": "80",
312
+ "SERVER_PROTOCOL": "HTTP/1.1",
313
+ "wsgi.version": (1, 0),
314
+ "wsgi.url_scheme": "http",
315
+ "wsgi.input": FakePayload(b""),
316
+ "wsgi.errors": self.errors,
317
+ "wsgi.multiprocess": True,
318
+ "wsgi.multithread": False,
319
+ "wsgi.run_once": False,
320
+ **self.defaults,
321
+ **request,
322
+ }
323
+
324
+ def request(self, **request):
325
+ "Construct a generic request object."
326
+ return WSGIRequest(self._base_environ(**request))
327
+
328
+ def _encode_data(self, data, content_type):
329
+ if content_type is MULTIPART_CONTENT:
330
+ return encode_multipart(BOUNDARY, data)
331
+ else:
332
+ # Encode the content so that the byte representation is correct.
333
+ match = CONTENT_TYPE_RE.match(content_type)
334
+ if match:
335
+ charset = match[1]
336
+ else:
337
+ charset = settings.DEFAULT_CHARSET
338
+ return force_bytes(data, encoding=charset)
339
+
340
+ def _encode_json(self, data, content_type):
341
+ """
342
+ Return encoded JSON if data is a dict, list, or tuple and content_type
343
+ is application/json.
344
+ """
345
+ should_encode = JSON_CONTENT_TYPE_RE.match(content_type) and isinstance(
346
+ data, dict | list | tuple
347
+ )
348
+ return json.dumps(data, cls=self.json_encoder) if should_encode else data
349
+
350
+ def _get_path(self, parsed):
351
+ path = parsed.path
352
+ # If there are parameters, add them
353
+ if parsed.params:
354
+ path += ";" + parsed.params
355
+ path = unquote_to_bytes(path)
356
+ # Replace the behavior where non-ASCII values in the WSGI environ are
357
+ # arbitrarily decoded with ISO-8859-1.
358
+ # Refs comment in `get_bytes_from_wsgi()`.
359
+ return path.decode("iso-8859-1")
360
+
361
+ def get(self, path, data=None, secure=False, *, headers=None, **extra):
362
+ """Construct a GET request."""
363
+ data = {} if data is None else data
364
+ return self.generic(
365
+ "GET",
366
+ path,
367
+ secure=secure,
368
+ headers=headers,
369
+ **{
370
+ "QUERY_STRING": urlencode(data, doseq=True),
371
+ **extra,
372
+ },
373
+ )
374
+
375
+ def post(
376
+ self,
377
+ path,
378
+ data=None,
379
+ content_type=MULTIPART_CONTENT,
380
+ secure=False,
381
+ *,
382
+ headers=None,
383
+ **extra,
384
+ ):
385
+ """Construct a POST request."""
386
+ data = self._encode_json({} if data is None else data, content_type)
387
+ post_data = self._encode_data(data, content_type)
388
+
389
+ return self.generic(
390
+ "POST",
391
+ path,
392
+ post_data,
393
+ content_type,
394
+ secure=secure,
395
+ headers=headers,
396
+ **extra,
397
+ )
398
+
399
+ def head(self, path, data=None, secure=False, *, headers=None, **extra):
400
+ """Construct a HEAD request."""
401
+ data = {} if data is None else data
402
+ return self.generic(
403
+ "HEAD",
404
+ path,
405
+ secure=secure,
406
+ headers=headers,
407
+ **{
408
+ "QUERY_STRING": urlencode(data, doseq=True),
409
+ **extra,
410
+ },
411
+ )
412
+
413
+ def trace(self, path, secure=False, *, headers=None, **extra):
414
+ """Construct a TRACE request."""
415
+ return self.generic("TRACE", path, secure=secure, headers=headers, **extra)
416
+
417
+ def options(
418
+ self,
419
+ path,
420
+ data="",
421
+ content_type="application/octet-stream",
422
+ secure=False,
423
+ *,
424
+ headers=None,
425
+ **extra,
426
+ ):
427
+ "Construct an OPTIONS request."
428
+ return self.generic(
429
+ "OPTIONS", path, data, content_type, secure=secure, headers=headers, **extra
430
+ )
431
+
432
+ def put(
433
+ self,
434
+ path,
435
+ data="",
436
+ content_type="application/octet-stream",
437
+ secure=False,
438
+ *,
439
+ headers=None,
440
+ **extra,
441
+ ):
442
+ """Construct a PUT request."""
443
+ data = self._encode_json(data, content_type)
444
+ return self.generic(
445
+ "PUT", path, data, content_type, secure=secure, headers=headers, **extra
446
+ )
447
+
448
+ def patch(
449
+ self,
450
+ path,
451
+ data="",
452
+ content_type="application/octet-stream",
453
+ secure=False,
454
+ *,
455
+ headers=None,
456
+ **extra,
457
+ ):
458
+ """Construct a PATCH request."""
459
+ data = self._encode_json(data, content_type)
460
+ return self.generic(
461
+ "PATCH", path, data, content_type, secure=secure, headers=headers, **extra
462
+ )
463
+
464
+ def delete(
465
+ self,
466
+ path,
467
+ data="",
468
+ content_type="application/octet-stream",
469
+ secure=False,
470
+ *,
471
+ headers=None,
472
+ **extra,
473
+ ):
474
+ """Construct a DELETE request."""
475
+ data = self._encode_json(data, content_type)
476
+ return self.generic(
477
+ "DELETE", path, data, content_type, secure=secure, headers=headers, **extra
478
+ )
479
+
480
+ def generic(
481
+ self,
482
+ method,
483
+ path,
484
+ data="",
485
+ content_type="application/octet-stream",
486
+ secure=False,
487
+ *,
488
+ headers=None,
489
+ **extra,
490
+ ):
491
+ """Construct an arbitrary HTTP request."""
492
+ parsed = urlparse(str(path)) # path can be lazy
493
+ data = force_bytes(data, settings.DEFAULT_CHARSET)
494
+ r = {
495
+ "PATH_INFO": self._get_path(parsed),
496
+ "REQUEST_METHOD": method,
497
+ "SERVER_PORT": "443" if secure else "80",
498
+ "wsgi.url_scheme": "https" if secure else "http",
499
+ }
500
+ if data:
501
+ r.update(
502
+ {
503
+ "CONTENT_LENGTH": str(len(data)),
504
+ "CONTENT_TYPE": content_type,
505
+ "wsgi.input": FakePayload(data),
506
+ }
507
+ )
508
+ if headers:
509
+ extra.update(HttpHeaders.to_wsgi_names(headers))
510
+ r.update(extra)
511
+ # If QUERY_STRING is absent or empty, we want to extract it from the URL.
512
+ if not r.get("QUERY_STRING"):
513
+ # WSGI requires latin-1 encoded strings. See get_path_info().
514
+ query_string = parsed[4].encode().decode("iso-8859-1")
515
+ r["QUERY_STRING"] = query_string
516
+ return self.request(**r)
517
+
518
+
519
+ class ClientMixin:
520
+ """
521
+ Mixin with common methods between Client and AsyncClient.
522
+ """
523
+
524
+ def store_exc_info(self, **kwargs):
525
+ """Store exceptions when they are generated by a view."""
526
+ self.exc_info = sys.exc_info()
527
+
528
+ def check_exception(self, response):
529
+ """
530
+ Look for a signaled exception, clear the current context exception
531
+ data, re-raise the signaled exception, and clear the signaled exception
532
+ from the local cache.
533
+ """
534
+ response.exc_info = self.exc_info
535
+ if self.exc_info:
536
+ _, exc_value, _ = self.exc_info
537
+ self.exc_info = None
538
+ if self.raise_request_exception:
539
+ raise exc_value
540
+
541
+ @property
542
+ def session(self):
543
+ """Return the current session variables."""
544
+ engine = import_module(settings.SESSION_ENGINE)
545
+ cookie = self.cookies.get(settings.SESSION_COOKIE_NAME)
546
+ if cookie:
547
+ return engine.SessionStore(cookie.value)
548
+ session = engine.SessionStore()
549
+ session.save()
550
+ self.cookies[settings.SESSION_COOKIE_NAME] = session.session_key
551
+ return session
552
+
553
+ def login(self, **credentials):
554
+ """
555
+ Set the Factory to appear as if it has successfully logged into a site.
556
+
557
+ Return True if login is possible or False if the provided credentials
558
+ are incorrect.
559
+ """
560
+ from plain.auth import authenticate
561
+
562
+ user = authenticate(**credentials)
563
+ if user:
564
+ self._login(user)
565
+ return True
566
+ return False
567
+
568
+ def force_login(self, user):
569
+ self._login(user)
570
+
571
+ def _login(self, user):
572
+ from plain.auth import login
573
+
574
+ # Create a fake request to store login details.
575
+ request = HttpRequest()
576
+ if self.session:
577
+ request.session = self.session
578
+ else:
579
+ engine = import_module(settings.SESSION_ENGINE)
580
+ request.session = engine.SessionStore()
581
+ login(request, user)
582
+ # Save the session values.
583
+ request.session.save()
584
+ # Set the cookie to represent the session.
585
+ session_cookie = settings.SESSION_COOKIE_NAME
586
+ self.cookies[session_cookie] = request.session.session_key
587
+ cookie_data = {
588
+ "max-age": None,
589
+ "path": "/",
590
+ "domain": settings.SESSION_COOKIE_DOMAIN,
591
+ "secure": settings.SESSION_COOKIE_SECURE or None,
592
+ "expires": None,
593
+ }
594
+ self.cookies[session_cookie].update(cookie_data)
595
+
596
+ def logout(self):
597
+ """Log out the user by removing the cookies and session object."""
598
+ from plain.auth import get_user, logout
599
+
600
+ request = HttpRequest()
601
+ if self.session:
602
+ request.session = self.session
603
+ request.user = get_user(request)
604
+ else:
605
+ engine = import_module(settings.SESSION_ENGINE)
606
+ request.session = engine.SessionStore()
607
+ logout(request)
608
+ self.cookies = SimpleCookie()
609
+
610
+ def _parse_json(self, response, **extra):
611
+ if not hasattr(response, "_json"):
612
+ if not JSON_CONTENT_TYPE_RE.match(response.get("Content-Type")):
613
+ raise ValueError(
614
+ 'Content-Type header is "%s", not "application/json"'
615
+ % response.get("Content-Type")
616
+ )
617
+ response._json = json.loads(
618
+ response.content.decode(response.charset), **extra
619
+ )
620
+ return response._json
621
+
622
+
623
+ class Client(ClientMixin, RequestFactory):
624
+ """
625
+ A class that can act as a client for testing purposes.
626
+
627
+ It allows the user to compose GET and POST requests, and
628
+ obtain the response that the server gave to those requests.
629
+ The server Response objects are annotated with the details
630
+ of the contexts and templates that were rendered during the
631
+ process of serving the request.
632
+
633
+ Client objects are stateful - they will retain cookie (and
634
+ thus session) details for the lifetime of the Client instance.
635
+
636
+ This is not intended as a replacement for Twill/Selenium or
637
+ the like - it is here to allow testing against the
638
+ contexts and templates produced by a view, rather than the
639
+ HTML rendered to the end-user.
640
+ """
641
+
642
+ def __init__(
643
+ self,
644
+ enforce_csrf_checks=False,
645
+ raise_request_exception=True,
646
+ *,
647
+ headers=None,
648
+ **defaults,
649
+ ):
650
+ super().__init__(headers=headers, **defaults)
651
+ self.handler = ClientHandler(enforce_csrf_checks)
652
+ self.raise_request_exception = raise_request_exception
653
+ self.exc_info = None
654
+ self.extra = None
655
+ self.headers = None
656
+
657
+ def request(self, **request):
658
+ """
659
+ Make a generic request. Compose the environment dictionary and pass
660
+ to the handler, return the result of the handler. Assume defaults for
661
+ the query environment, which can be overridden using the arguments to
662
+ the request.
663
+ """
664
+ environ = self._base_environ(**request)
665
+
666
+ # Curry a data dictionary into an instance of the template renderer
667
+ # callback function.
668
+ data = {}
669
+ partial(store_rendered_templates, data)
670
+ "template-render-%s" % id(request)
671
+ # signals.template_rendered.connect(on_template_render, dispatch_uid=signal_uid)
672
+ # Capture exceptions created by the handler.
673
+ exception_uid = "request-exception-%s" % id(request)
674
+ got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
675
+ try:
676
+ response = self.handler(environ)
677
+ finally:
678
+ # signals.template_rendered.disconnect(dispatch_uid=signal_uid)
679
+ got_request_exception.disconnect(dispatch_uid=exception_uid)
680
+ # Check for signaled exceptions.
681
+ self.check_exception(response)
682
+ # Save the client and request that stimulated the response.
683
+ response.client = self
684
+ response.request = request
685
+ # Add any rendered template detail to the response.
686
+ response.templates = data.get("templates", [])
687
+ response.context = data.get("context")
688
+ response.json = partial(self._parse_json, response)
689
+ # Attach the ResolverMatch instance to the response.
690
+ urlconf = getattr(response.wsgi_request, "urlconf", None)
691
+ response.resolver_match = SimpleLazyObject(
692
+ lambda: resolve(request["PATH_INFO"], urlconf=urlconf),
693
+ )
694
+ # Flatten a single context. Not really necessary anymore thanks to the
695
+ # __getattr__ flattening in ContextList, but has some edge case
696
+ # backwards compatibility implications.
697
+ if response.context and len(response.context) == 1:
698
+ response.context = response.context[0]
699
+ # Update persistent cookie data.
700
+ if response.cookies:
701
+ self.cookies.update(response.cookies)
702
+ return response
703
+
704
+ def get(
705
+ self,
706
+ path,
707
+ data=None,
708
+ follow=False,
709
+ secure=False,
710
+ *,
711
+ headers=None,
712
+ **extra,
713
+ ):
714
+ """Request a response from the server using GET."""
715
+ self.extra = extra
716
+ self.headers = headers
717
+ response = super().get(path, data=data, secure=secure, headers=headers, **extra)
718
+ if follow:
719
+ response = self._handle_redirects(
720
+ response, data=data, headers=headers, **extra
721
+ )
722
+ return response
723
+
724
+ def post(
725
+ self,
726
+ path,
727
+ data=None,
728
+ content_type=MULTIPART_CONTENT,
729
+ follow=False,
730
+ secure=False,
731
+ *,
732
+ headers=None,
733
+ **extra,
734
+ ):
735
+ """Request a response from the server using POST."""
736
+ self.extra = extra
737
+ self.headers = headers
738
+ response = super().post(
739
+ path,
740
+ data=data,
741
+ content_type=content_type,
742
+ secure=secure,
743
+ headers=headers,
744
+ **extra,
745
+ )
746
+ if follow:
747
+ response = self._handle_redirects(
748
+ response, data=data, content_type=content_type, headers=headers, **extra
749
+ )
750
+ return response
751
+
752
+ def head(
753
+ self,
754
+ path,
755
+ data=None,
756
+ follow=False,
757
+ secure=False,
758
+ *,
759
+ headers=None,
760
+ **extra,
761
+ ):
762
+ """Request a response from the server using HEAD."""
763
+ self.extra = extra
764
+ self.headers = headers
765
+ response = super().head(
766
+ path, data=data, secure=secure, headers=headers, **extra
767
+ )
768
+ if follow:
769
+ response = self._handle_redirects(
770
+ response, data=data, headers=headers, **extra
771
+ )
772
+ return response
773
+
774
+ def options(
775
+ self,
776
+ path,
777
+ data="",
778
+ content_type="application/octet-stream",
779
+ follow=False,
780
+ secure=False,
781
+ *,
782
+ headers=None,
783
+ **extra,
784
+ ):
785
+ """Request a response from the server using OPTIONS."""
786
+ self.extra = extra
787
+ self.headers = headers
788
+ response = super().options(
789
+ path,
790
+ data=data,
791
+ content_type=content_type,
792
+ secure=secure,
793
+ headers=headers,
794
+ **extra,
795
+ )
796
+ if follow:
797
+ response = self._handle_redirects(
798
+ response, data=data, content_type=content_type, headers=headers, **extra
799
+ )
800
+ return response
801
+
802
+ def put(
803
+ self,
804
+ path,
805
+ data="",
806
+ content_type="application/octet-stream",
807
+ follow=False,
808
+ secure=False,
809
+ *,
810
+ headers=None,
811
+ **extra,
812
+ ):
813
+ """Send a resource to the server using PUT."""
814
+ self.extra = extra
815
+ self.headers = headers
816
+ response = super().put(
817
+ path,
818
+ data=data,
819
+ content_type=content_type,
820
+ secure=secure,
821
+ headers=headers,
822
+ **extra,
823
+ )
824
+ if follow:
825
+ response = self._handle_redirects(
826
+ response, data=data, content_type=content_type, headers=headers, **extra
827
+ )
828
+ return response
829
+
830
+ def patch(
831
+ self,
832
+ path,
833
+ data="",
834
+ content_type="application/octet-stream",
835
+ follow=False,
836
+ secure=False,
837
+ *,
838
+ headers=None,
839
+ **extra,
840
+ ):
841
+ """Send a resource to the server using PATCH."""
842
+ self.extra = extra
843
+ self.headers = headers
844
+ response = super().patch(
845
+ path,
846
+ data=data,
847
+ content_type=content_type,
848
+ secure=secure,
849
+ headers=headers,
850
+ **extra,
851
+ )
852
+ if follow:
853
+ response = self._handle_redirects(
854
+ response, data=data, content_type=content_type, headers=headers, **extra
855
+ )
856
+ return response
857
+
858
+ def delete(
859
+ self,
860
+ path,
861
+ data="",
862
+ content_type="application/octet-stream",
863
+ follow=False,
864
+ secure=False,
865
+ *,
866
+ headers=None,
867
+ **extra,
868
+ ):
869
+ """Send a DELETE request to the server."""
870
+ self.extra = extra
871
+ self.headers = headers
872
+ response = super().delete(
873
+ path,
874
+ data=data,
875
+ content_type=content_type,
876
+ secure=secure,
877
+ headers=headers,
878
+ **extra,
879
+ )
880
+ if follow:
881
+ response = self._handle_redirects(
882
+ response, data=data, content_type=content_type, headers=headers, **extra
883
+ )
884
+ return response
885
+
886
+ def trace(
887
+ self,
888
+ path,
889
+ data="",
890
+ follow=False,
891
+ secure=False,
892
+ *,
893
+ headers=None,
894
+ **extra,
895
+ ):
896
+ """Send a TRACE request to the server."""
897
+ self.extra = extra
898
+ self.headers = headers
899
+ response = super().trace(
900
+ path, data=data, secure=secure, headers=headers, **extra
901
+ )
902
+ if follow:
903
+ response = self._handle_redirects(
904
+ response, data=data, headers=headers, **extra
905
+ )
906
+ return response
907
+
908
+ def _handle_redirects(
909
+ self,
910
+ response,
911
+ data="",
912
+ content_type="",
913
+ headers=None,
914
+ **extra,
915
+ ):
916
+ """
917
+ Follow any redirects by requesting responses from the server using GET.
918
+ """
919
+ response.redirect_chain = []
920
+ redirect_status_codes = (
921
+ HTTPStatus.MOVED_PERMANENTLY,
922
+ HTTPStatus.FOUND,
923
+ HTTPStatus.SEE_OTHER,
924
+ HTTPStatus.TEMPORARY_REDIRECT,
925
+ HTTPStatus.PERMANENT_REDIRECT,
926
+ )
927
+ while response.status_code in redirect_status_codes:
928
+ response_url = response.url
929
+ redirect_chain = response.redirect_chain
930
+ redirect_chain.append((response_url, response.status_code))
931
+
932
+ url = urlsplit(response_url)
933
+ if url.scheme:
934
+ extra["wsgi.url_scheme"] = url.scheme
935
+ if url.hostname:
936
+ extra["SERVER_NAME"] = url.hostname
937
+ if url.port:
938
+ extra["SERVER_PORT"] = str(url.port)
939
+
940
+ path = url.path
941
+ # RFC 3986 Section 6.2.3: Empty path should be normalized to "/".
942
+ if not path and url.netloc:
943
+ path = "/"
944
+ # Prepend the request path to handle relative path redirects
945
+ if not path.startswith("/"):
946
+ path = urljoin(response.request["PATH_INFO"], path)
947
+
948
+ if response.status_code in (
949
+ HTTPStatus.TEMPORARY_REDIRECT,
950
+ HTTPStatus.PERMANENT_REDIRECT,
951
+ ):
952
+ # Preserve request method and query string (if needed)
953
+ # post-redirect for 307/308 responses.
954
+ request_method = response.request["REQUEST_METHOD"].lower()
955
+ if request_method not in ("get", "head"):
956
+ extra["QUERY_STRING"] = url.query
957
+ request_method = getattr(self, request_method)
958
+ else:
959
+ request_method = self.get
960
+ data = QueryDict(url.query)
961
+ content_type = None
962
+
963
+ response = request_method(
964
+ path,
965
+ data=data,
966
+ content_type=content_type,
967
+ follow=False,
968
+ headers=headers,
969
+ **extra,
970
+ )
971
+ response.redirect_chain = redirect_chain
972
+
973
+ if redirect_chain[-1] in redirect_chain[:-1]:
974
+ # Check that we're not redirecting to somewhere we've already
975
+ # been to, to prevent loops.
976
+ raise RedirectCycleError(
977
+ "Redirect loop detected.", last_response=response
978
+ )
979
+ if len(redirect_chain) > 20:
980
+ # Such a lengthy chain likely also means a loop, but one with
981
+ # a growing path, changing view, or changing query argument;
982
+ # 20 is the value of "network.http.redirection-limit" from Firefox.
983
+ raise RedirectCycleError("Too many redirects.", last_response=response)
984
+
985
+ return response