fastlifeweb 0.16.4__py3-none-any.whl → 0.17.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 (34) hide show
  1. fastlife/adapters/jinjax/renderer.py +44 -15
  2. fastlife/adapters/jinjax/widget_factory/__init__.py +1 -0
  3. fastlife/adapters/jinjax/widget_factory/base.py +38 -0
  4. fastlife/adapters/jinjax/widget_factory/bool_builder.py +43 -0
  5. fastlife/adapters/jinjax/widget_factory/emailstr_builder.py +46 -0
  6. fastlife/adapters/jinjax/widget_factory/enum_builder.py +47 -0
  7. fastlife/adapters/jinjax/widget_factory/factory.py +165 -0
  8. fastlife/adapters/jinjax/widget_factory/literal_builder.py +52 -0
  9. fastlife/adapters/jinjax/widget_factory/model_builder.py +64 -0
  10. fastlife/adapters/jinjax/widget_factory/secretstr_builder.py +47 -0
  11. fastlife/adapters/jinjax/widget_factory/sequence_builder.py +58 -0
  12. fastlife/adapters/jinjax/widget_factory/set_builder.py +80 -0
  13. fastlife/adapters/jinjax/widget_factory/simpletype_builder.py +47 -0
  14. fastlife/adapters/jinjax/widget_factory/union_builder.py +90 -0
  15. fastlife/adapters/jinjax/widgets/hidden.py +2 -0
  16. fastlife/adapters/jinjax/widgets/model.py +2 -0
  17. fastlife/components/Form.jinja +12 -0
  18. fastlife/config/configurator.py +15 -15
  19. fastlife/config/exceptions.py +2 -0
  20. fastlife/config/resources.py +2 -2
  21. fastlife/config/settings.py +2 -0
  22. fastlife/middlewares/reverse_proxy/x_forwarded.py +7 -8
  23. fastlife/services/policy.py +1 -1
  24. fastlife/services/translations.py +12 -6
  25. fastlife/shared_utils/resolver.py +58 -1
  26. fastlife/testing/dom.py +140 -0
  27. fastlife/testing/form.py +204 -0
  28. fastlife/testing/session.py +67 -0
  29. fastlife/testing/testclient.py +4 -387
  30. {fastlifeweb-0.16.4.dist-info → fastlifeweb-0.17.0.dist-info}/METADATA +6 -6
  31. {fastlifeweb-0.16.4.dist-info → fastlifeweb-0.17.0.dist-info}/RECORD +33 -18
  32. fastlife/adapters/jinjax/widgets/factory.py +0 -525
  33. {fastlifeweb-0.16.4.dist-info → fastlifeweb-0.17.0.dist-info}/LICENSE +0 -0
  34. {fastlifeweb-0.16.4.dist-info → fastlifeweb-0.17.0.dist-info}/WHEEL +0 -0
@@ -0,0 +1,204 @@
1
+ """Class utilities to access to the web form."""
2
+
3
+ from typing import TYPE_CHECKING, Any
4
+
5
+ from multidict import MultiDict
6
+
7
+ if TYPE_CHECKING:
8
+ from .dom import Element # coverage: ignore
9
+ from .testclient import WebResponse, WebTestClient # coverage: ignore
10
+
11
+
12
+ class WebForm:
13
+ """
14
+ Handle html form.
15
+
16
+ Form are filled out and submit with methods and try to avoid invalid
17
+ usage, such as selecting an option that don't exists is not possible here.
18
+ Again, no javascript is executed here, but htmx attribute `hx-post` and `hx-target`
19
+ are read while submiting to simulate it.
20
+ """
21
+
22
+ def __init__(self, client: "WebTestClient", origin: str, form: "Element"):
23
+ self._client = client
24
+ self._form = form
25
+ self._origin = origin
26
+ self._formfields: dict[str, Element] = {}
27
+ self._formdata: MultiDict[str] = MultiDict()
28
+ inputs = self._form.by_node_name("input")
29
+ for input in inputs:
30
+ self._formfields[input.attrs["name"]] = input
31
+ if input.attrs.get("type") == "checkbox" and "checked" not in input.attrs:
32
+ continue
33
+ self._formdata.add(input.attrs["name"], input.attrs.get("value", ""))
34
+
35
+ selects = self._form.by_node_name("select")
36
+ for select in selects:
37
+ fieldname = select.attrs["name"]
38
+ self._formfields[fieldname] = select
39
+ options = select.by_node_name("option")
40
+ if "multiple" in select.attrs:
41
+ for option in options:
42
+ if "selected" in option.attrs:
43
+ self._formdata.add(
44
+ fieldname, option.attrs.get("value", option.text)
45
+ )
46
+ else:
47
+ if options:
48
+ self._formdata[fieldname] = options[0].attrs.get(
49
+ "value", options[0].text
50
+ )
51
+ for option in options:
52
+ if "selected" in option.attrs:
53
+ self._formdata[fieldname] = option.attrs.get(
54
+ "value", option.text
55
+ )
56
+ break
57
+
58
+ # field textearea...
59
+
60
+ def set(self, fieldname: str, value: str) -> Any:
61
+ """
62
+ Set a value to an input field.
63
+
64
+ It works for checkbox and radio as well.
65
+ Checkbox may contains many values.
66
+ Options of select can't be set with this method, the select method must
67
+ be used instead.
68
+ """
69
+ if fieldname not in self._formfields:
70
+ raise ValueError(f'"{fieldname}" does not exists')
71
+ if self._formfields[fieldname].node_name == "select":
72
+ raise ValueError(f'"{fieldname}" is a <select>, use select() instead')
73
+
74
+ if self._formfields[fieldname].attrs.get("type") == "checkbox":
75
+ self._formdata.add(fieldname, value)
76
+ return
77
+
78
+ if self._formfields[fieldname].attrs.get("type") == "radio":
79
+ radio = self._form.by_node_name(
80
+ "input", attrs={"type": "radio", "value": value}
81
+ )
82
+ if not radio:
83
+ raise ValueError(
84
+ f'radio "{fieldname}" does not contains {value} option'
85
+ )
86
+
87
+ self._formdata[fieldname] = value
88
+
89
+ def unset(self, fieldname: str, value: str) -> Any:
90
+ """Unset an element. Only works with checkbox."""
91
+ if fieldname not in self._formfields:
92
+ raise ValueError(f'"{fieldname}" does not exists')
93
+ if self._formfields[fieldname].node_name != "input":
94
+ raise ValueError(f'"{fieldname}" is not a checkbox')
95
+ if self._formfields[fieldname].attrs.get("type") != "checkbox":
96
+ raise ValueError(f'"{fieldname}" is not a checkbox')
97
+ values = self._formdata.popall(fieldname)
98
+ if value not in values:
99
+ raise ValueError(f'"{value}" not in "{fieldname}"')
100
+ for val in values:
101
+ if val != value:
102
+ self._formdata[fieldname] = val
103
+
104
+ def select(self, fieldname: str, value: str) -> Any:
105
+ """
106
+ Select an option, if multiple, value is added, otherwise, value is replaced.
107
+ """
108
+ if fieldname not in self._formfields:
109
+ raise ValueError(f'"{fieldname}" does not exists')
110
+ field = self._formfields[fieldname]
111
+ if field.node_name != "select":
112
+ raise ValueError(f"{fieldname} is a {field!r}, " "use set() instead")
113
+
114
+ for option in field.by_node_name("option"):
115
+ if option.text == value.strip():
116
+ if "multiple" in field.attrs:
117
+ self._formdata.add(fieldname, value)
118
+ else:
119
+ self._formdata[fieldname] = option.attrs.get("value", option.text)
120
+ break
121
+ else:
122
+ raise ValueError(f'No option {value} in <select name="{fieldname}">')
123
+
124
+ def unselect(self, fieldname: str, value: str) -> Any:
125
+ """
126
+ Unselect an option if multiple, otherwise an exception is raised.
127
+ """
128
+ if fieldname not in self._formfields:
129
+ raise ValueError(f'"{fieldname}" does not exists')
130
+ field = self._formfields[fieldname]
131
+
132
+ if field.node_name != "select":
133
+ raise ValueError(
134
+ f"{fieldname} is a {self._formfields[fieldname]!r}, "
135
+ "use unset() for checkbox instead"
136
+ )
137
+ if "multiple" not in field.attrs:
138
+ raise ValueError("only <select multiple> support unselect")
139
+
140
+ for option in self._formfields[fieldname].by_node_name("option"):
141
+ if option.text == value.strip():
142
+ values = self._formdata.popall(fieldname)
143
+ if value not in values:
144
+ raise ValueError(f'"{value}" not selected in "{fieldname}"')
145
+ for val in values:
146
+ if val != value:
147
+ self._formdata[fieldname] = val
148
+ break
149
+ else:
150
+ raise ValueError(f'No option {value} in <select name="{fieldname}">')
151
+
152
+ def button(self, text: str, position: int = 0) -> "WebForm":
153
+ """
154
+ Simmulate a click on a button using the text of the button,
155
+
156
+ and eventually a position. The button return the form and the submit()
157
+ should be called directly.
158
+
159
+ This is used in order to inject the value of the button in the form, usually
160
+ done while many actions are available on a form.
161
+
162
+ ::
163
+
164
+ form.button("Go").submit()
165
+
166
+ """
167
+ buttons = self._form.get_all_by_text(text, node_name="button")
168
+ if position >= len(buttons):
169
+ pos = ""
170
+ if position > 0:
171
+ pos = f" at position {position}"
172
+ raise ValueError(f'Button "{text}" not found{pos}')
173
+ button = buttons[position]
174
+ if "name" in button.attrs:
175
+ self._formdata[button.attrs["name"]] = button.attrs.get("value", "")
176
+ return self
177
+
178
+ def submit(self, follow_redirects: bool = True) -> "WebResponse":
179
+ """
180
+ Submit the form as it has been previously filled out.
181
+ """
182
+ headers: dict[str, str] = {}
183
+ target = (
184
+ self._form.attrs.get("hx-post")
185
+ or self._form.attrs.get("post")
186
+ or self._origin
187
+ )
188
+ if "hx-post" in self._form.attrs:
189
+ if hx_target := self._form.hx_target:
190
+ headers["HX-Target"] = hx_target
191
+
192
+ return self._client.post(
193
+ target,
194
+ data=self._formdata,
195
+ headers=headers,
196
+ follow_redirects=follow_redirects,
197
+ )
198
+
199
+ def __contains__(self, key: str) -> bool:
200
+ """Test if a field exists in the form."""
201
+ return key in self._formdata
202
+
203
+ def __repr__(self) -> str:
204
+ return repr(self._formdata)
@@ -0,0 +1,67 @@
1
+ """Session store for the webtest client."""
2
+
3
+ import time
4
+ from collections.abc import Mapping
5
+ from http.cookiejar import Cookie
6
+ from typing import TYPE_CHECKING, Any
7
+
8
+ import httpx
9
+
10
+ if TYPE_CHECKING:
11
+ from .testclient import WebTestClient # coverage: ignore
12
+
13
+
14
+ CookieTypes = httpx._types.CookieTypes # type: ignore
15
+ Cookies = httpx._models.Cookies # type: ignore
16
+
17
+
18
+ class Session(dict[str, Any]):
19
+ """Manipulate the session of the WebTestClient browser."""
20
+
21
+ def __init__(self, client: "WebTestClient"):
22
+ self.client = client
23
+ self.srlz = client.session_serializer
24
+ self.settings = self.client.settings
25
+ data: Mapping[str, Any]
26
+ cookie_name = self.settings.session_cookie_name
27
+ self.has_session = cookie_name in self.client.cookies
28
+ if self.has_session:
29
+ data, error = self.srlz.deserialize(
30
+ self.client.cookies[cookie_name].encode("utf-8")
31
+ )
32
+ if error:
33
+ self.has_session = False
34
+ else:
35
+ data = {}
36
+ super().__init__(data)
37
+
38
+ def __setitem__(self, __key: Any, __value: Any) -> None:
39
+ """Initialize a value in the session of the client in order to test."""
40
+ super().__setitem__(__key, __value)
41
+ settings = self.settings
42
+ data = self.serialize()
43
+ self.client.cookies.jar.set_cookie(
44
+ Cookie(
45
+ version=0,
46
+ name=settings.session_cookie_name,
47
+ value=data,
48
+ port=None,
49
+ port_specified=False,
50
+ domain=f".{settings.session_cookie_domain}",
51
+ domain_specified=True,
52
+ domain_initial_dot=True,
53
+ path="/",
54
+ path_specified=True,
55
+ secure=False,
56
+ expires=int(time.time() + settings.session_duration.total_seconds()),
57
+ discard=False,
58
+ comment=None,
59
+ comment_url=None,
60
+ rest={"HttpOnly": None, "SameSite": "lax"}, # type: ignore
61
+ rfc2109=False,
62
+ )
63
+ )
64
+
65
+ def serialize(self) -> str:
66
+ """Serialize the session"""
67
+ return self.srlz.serialize(self).decode("utf-8")
@@ -1,9 +1,6 @@
1
1
  """Testing your application."""
2
2
 
3
- import re
4
- import time
5
- from collections.abc import Iterator, Mapping, MutableMapping, Sequence
6
- from http.cookiejar import Cookie
3
+ from collections.abc import Mapping, MutableMapping
7
4
  from typing import Any, Literal
8
5
  from urllib.parse import urlencode
9
6
 
@@ -16,336 +13,14 @@ from starlette.types import ASGIApp
16
13
  from fastlife.config.settings import Settings
17
14
  from fastlife.middlewares.session.serializer import AbsractSessionSerializer
18
15
  from fastlife.shared_utils.resolver import resolve
16
+ from fastlife.testing.dom import Element
17
+ from fastlife.testing.form import WebForm
18
+ from fastlife.testing.session import Session
19
19
 
20
20
  CookieTypes = httpx._types.CookieTypes # type: ignore
21
21
  Cookies = httpx._models.Cookies # type: ignore
22
22
 
23
23
 
24
- class Element:
25
- """Access to a dom element."""
26
-
27
- def __init__(self, client: "WebTestClient", tag: bs4.Tag):
28
- self._client = client
29
- self._tag = tag
30
-
31
- def click(self) -> "WebResponse":
32
- """Simulate a client to a a link. No javascript exectuted here."""
33
- return self._client.get(self._tag.attrs["href"])
34
-
35
- @property
36
- def node_name(self) -> str:
37
- """Get the node name of the dom element."""
38
- return self._tag.name
39
-
40
- @property
41
- def attrs(self) -> dict[str, str]:
42
- """Attributes of the element."""
43
- return self._tag.attrs
44
-
45
- @property
46
- def text(self) -> str:
47
- """
48
- Return the text of the element, with text of childs element.
49
-
50
- Note that the text is stripped for convenience but inner text may contains
51
- many spaces not manipulated here.
52
- """
53
- return self._tag.text.strip()
54
-
55
- @property
56
- def h1(self) -> "Element":
57
- """
58
- Return the h1 child element.
59
-
60
- Should be used on the html body element directly.
61
- """
62
- nodes = self.by_node_name("h1")
63
- assert len(nodes) == 1, f"Should have 1 <h1>, got {len(nodes)} in {self}"
64
- return nodes[0]
65
-
66
- @property
67
- def h2(self) -> Sequence["Element"]:
68
- """
69
- Return the h2 elements.
70
- """
71
- return self.by_node_name("h2")
72
-
73
- @property
74
- def form(self) -> "Element | None":
75
- """Get the form element of the web page."""
76
- return Element(self._client, self._tag.form) if self._tag.form else None
77
-
78
- @property
79
- def hx_target(self) -> str | None:
80
- """
81
- Return the hx-target of the element.
82
-
83
- It may be set on a parent. It also resolve special case "this" and return the id
84
- of the element.
85
- """
86
- el: bs4.Tag | None = self._tag
87
- while el:
88
- if "hx-target" in el.attrs:
89
- ret = el.attrs["hx-target"]
90
- if ret == "this":
91
- ret = el.attrs["id"]
92
- return ret
93
- el = el.parent
94
- return None
95
-
96
- def by_text(self, text: str, *, node_name: str | None = None) -> "Element | None":
97
- """Find the first element that match the text."""
98
- nodes = self.iter_all_by_text(text, node_name=node_name)
99
- return next(nodes, None)
100
-
101
- def iter_all_by_text(
102
- self, text: str, *, node_name: str | None = None
103
- ) -> "Iterator[Element]":
104
- """Return an iterator of all elements that match the text."""
105
- nodes = self._tag.find_all(string=re.compile(rf"\s*{text}\s*"))
106
- for node in nodes:
107
- if isinstance(node, bs4.NavigableString):
108
- node = node.parent
109
-
110
- if node_name:
111
- while node is not None:
112
- if node.name == node_name:
113
- yield Element(self._client, node)
114
- node = node.parent
115
- elif node:
116
- yield Element(self._client, node)
117
- return None
118
-
119
- def get_all_by_text(
120
- self, text: str, *, node_name: str | None = None
121
- ) -> "Sequence[Element]":
122
- """Return the list of all elements that match the text."""
123
- nodes = self.iter_all_by_text(text, node_name=node_name)
124
- return list(nodes)
125
-
126
- def by_label_text(self, text: str) -> "Element | None":
127
- """Return the element which is the target of the label having the given text."""
128
- label = self.by_text(text, node_name="label")
129
- assert label is not None
130
- assert label.attrs.get("for") is not None
131
- resp = self._tag.find(id=label.attrs["for"])
132
- assert not isinstance(resp, bs4.NavigableString)
133
- return Element(self._client, resp) if resp else None
134
-
135
- def by_node_name(
136
- self, node_name: str, *, attrs: dict[str, str] | None = None
137
- ) -> list["Element"]:
138
- """
139
- Return the list of elements with the given node_name.
140
-
141
- An optional set of attributes may given and must match if passed.
142
- """
143
- return [
144
- Element(self._client, e) for e in self._tag.find_all(node_name, attrs or {})
145
- ]
146
-
147
- def __repr__(self) -> str:
148
- return f"<{self.node_name}>"
149
-
150
- def __str__(self) -> str:
151
- return str(self._tag)
152
-
153
-
154
- class WebForm:
155
- """
156
- Handle html form.
157
-
158
- Form are filled out and submit with methods and try to avoid invalid
159
- usage, such as selecting an option that don't exists is not possible here.
160
- Again, no javascript is executed here, but htmx attribute `hx-post` and `hx-target`
161
- are read while submiting to simulate it.
162
- """
163
-
164
- def __init__(self, client: "WebTestClient", origin: str, form: Element):
165
- self._client = client
166
- self._form = form
167
- self._origin = origin
168
- self._formfields: dict[str, Element] = {}
169
- self._formdata: MultiDict[str] = MultiDict()
170
- inputs = self._form.by_node_name("input")
171
- for input in inputs:
172
- self._formfields[input.attrs["name"]] = input
173
- if input.attrs.get("type") == "checkbox" and "checked" not in input.attrs:
174
- continue
175
- self._formdata.add(input.attrs["name"], input.attrs.get("value", ""))
176
-
177
- selects = self._form.by_node_name("select")
178
- for select in selects:
179
- fieldname = select.attrs["name"]
180
- self._formfields[fieldname] = select
181
- options = select.by_node_name("option")
182
- if "multiple" in select.attrs:
183
- for option in options:
184
- if "selected" in option.attrs:
185
- self._formdata.add(
186
- fieldname, option.attrs.get("value", option.text)
187
- )
188
- else:
189
- if options:
190
- self._formdata[fieldname] = options[0].attrs.get(
191
- "value", options[0].text
192
- )
193
- for option in options:
194
- if "selected" in option.attrs:
195
- self._formdata[fieldname] = option.attrs.get(
196
- "value", option.text
197
- )
198
- break
199
-
200
- # field textearea...
201
-
202
- def set(self, fieldname: str, value: str) -> Any:
203
- """
204
- Set a value to an input field.
205
-
206
- It works for checkbox and radio as well.
207
- Checkbox may contains many values.
208
- Options of select can't be set with this method, the select method must
209
- be used instead.
210
- """
211
- if fieldname not in self._formfields:
212
- raise ValueError(f'"{fieldname}" does not exists')
213
- if self._formfields[fieldname].node_name == "select":
214
- raise ValueError(f'"{fieldname}" is a <select>, use select() instead')
215
-
216
- if self._formfields[fieldname].attrs.get("type") == "checkbox":
217
- self._formdata.add(fieldname, value)
218
- return
219
-
220
- if self._formfields[fieldname].attrs.get("type") == "radio":
221
- radio = self._form.by_node_name(
222
- "input", attrs={"type": "radio", "value": value}
223
- )
224
- if not radio:
225
- raise ValueError(
226
- f'radio "{fieldname}" does not contains {value} option'
227
- )
228
-
229
- self._formdata[fieldname] = value
230
-
231
- def unset(self, fieldname: str, value: str) -> Any:
232
- """Unset an element. Only works with checkbox."""
233
- if fieldname not in self._formfields:
234
- raise ValueError(f'"{fieldname}" does not exists')
235
- if self._formfields[fieldname].node_name != "input":
236
- raise ValueError(f'"{fieldname}" is not a checkbox')
237
- if self._formfields[fieldname].attrs.get("type") != "checkbox":
238
- raise ValueError(f'"{fieldname}" is not a checkbox')
239
- values = self._formdata.popall(fieldname)
240
- if value not in values:
241
- raise ValueError(f'"{value}" not in "{fieldname}"')
242
- for val in values:
243
- if val != value:
244
- self._formdata[fieldname] = val
245
-
246
- def select(self, fieldname: str, value: str) -> Any:
247
- """
248
- Select an option, if multiple, value is added, otherwise, value is replaced.
249
- """
250
- if fieldname not in self._formfields:
251
- raise ValueError(f'"{fieldname}" does not exists')
252
- field = self._formfields[fieldname]
253
- if field.node_name != "select":
254
- raise ValueError(f"{fieldname} is a {field!r}, " "use set() instead")
255
-
256
- for option in field.by_node_name("option"):
257
- if option.text == value.strip():
258
- if "multiple" in field.attrs:
259
- self._formdata.add(fieldname, value)
260
- else:
261
- self._formdata[fieldname] = option.attrs.get("value", option.text)
262
- break
263
- else:
264
- raise ValueError(f'No option {value} in <select name="{fieldname}">')
265
-
266
- def unselect(self, fieldname: str, value: str) -> Any:
267
- """
268
- Unselect an option if multiple, otherwise an exception is raised.
269
- """
270
- if fieldname not in self._formfields:
271
- raise ValueError(f'"{fieldname}" does not exists')
272
- field = self._formfields[fieldname]
273
-
274
- if field.node_name != "select":
275
- raise ValueError(
276
- f"{fieldname} is a {self._formfields[fieldname]!r}, "
277
- "use unset() for checkbox instead"
278
- )
279
- if "multiple" not in field.attrs:
280
- raise ValueError("only <select multiple> support unselect")
281
-
282
- for option in self._formfields[fieldname].by_node_name("option"):
283
- if option.text == value.strip():
284
- values = self._formdata.popall(fieldname)
285
- if value not in values:
286
- raise ValueError(f'"{value}" not selected in "{fieldname}"')
287
- for val in values:
288
- if val != value:
289
- self._formdata[fieldname] = val
290
- break
291
- else:
292
- raise ValueError(f'No option {value} in <select name="{fieldname}">')
293
-
294
- def button(self, text: str, position: int = 0) -> "WebForm":
295
- """
296
- Simmulate a click on a button using the text of the button,
297
-
298
- and eventually a position. The button return the form and the submit()
299
- should be called directly.
300
-
301
- This is used in order to inject the value of the button in the form, usually
302
- done while many actions are available on a form.
303
-
304
- ::
305
-
306
- form.button("Go").submit()
307
-
308
- """
309
- buttons = self._form.get_all_by_text(text, node_name="button")
310
- if position >= len(buttons):
311
- pos = ""
312
- if position > 0:
313
- pos = f" at position {position}"
314
- raise ValueError(f'Button "{text}" not found{pos}')
315
- button = buttons[position]
316
- if "name" in button.attrs:
317
- self._formdata[button.attrs["name"]] = button.attrs.get("value", "")
318
- return self
319
-
320
- def submit(self, follow_redirects: bool = True) -> "WebResponse":
321
- """
322
- Submit the form as it has been previously filled out.
323
- """
324
- headers: dict[str, str] = {}
325
- target = (
326
- self._form.attrs.get("hx-post")
327
- or self._form.attrs.get("post")
328
- or self._origin
329
- )
330
- if "hx-post" in self._form.attrs:
331
- if hx_target := self._form.hx_target:
332
- headers["HX-Target"] = hx_target
333
-
334
- return self._client.post(
335
- target,
336
- data=self._formdata,
337
- headers=headers,
338
- follow_redirects=follow_redirects,
339
- )
340
-
341
- def __contains__(self, key: str) -> bool:
342
- """Test if a field exists in the form."""
343
- return key in self._formdata
344
-
345
- def __repr__(self) -> str:
346
- return repr(self._formdata)
347
-
348
-
349
24
  class WebResponse:
350
25
  """Represent an http response made by the WebTestClient browser."""
351
26
 
@@ -419,64 +94,6 @@ class WebResponse:
419
94
  return self.html.by_node_name(node_name, attrs=attrs)
420
95
 
421
96
 
422
- class Session(dict[str, Any]):
423
- """Manipulate the session of the WebTestClient browser."""
424
-
425
- def __init__(self, client: "WebTestClient"):
426
- self.client = client
427
- self.srlz = client.session_serializer
428
- self.settings = self.client.settings
429
- data: Mapping[str, Any]
430
- cookie_name = self.settings.session_cookie_name
431
- self.has_session = cookie_name in self.client.cookies
432
- if self.has_session:
433
- data, error = self.srlz.deserialize(
434
- self.client.cookies[cookie_name].encode("utf-8")
435
- )
436
- if error:
437
- self.has_session = False
438
- else:
439
- data = {}
440
- super().__init__(data)
441
-
442
- def __setitem__(self, __key: Any, __value: Any) -> None:
443
- """Initialize a value in the session of the client in order to test."""
444
- super().__setitem__(__key, __value)
445
- settings = self.settings
446
- data = self.serialize()
447
- self.client.cookies.jar.set_cookie(
448
- Cookie(
449
- version=0,
450
- name=settings.session_cookie_name,
451
- value=data,
452
- port=None,
453
- port_specified=False,
454
- domain=f".{settings.session_cookie_domain}",
455
- domain_specified=True,
456
- domain_initial_dot=True,
457
- path="/",
458
- path_specified=True,
459
- secure=False,
460
- expires=int(time.time() + settings.session_duration.total_seconds()),
461
- discard=False,
462
- comment=None,
463
- comment_url=None,
464
- rest={"HttpOnly": None, "SameSite": "lax"}, # type: ignore
465
- rfc2109=False,
466
- )
467
- )
468
- # this does not work
469
- # self.client.cookies.set(
470
- # settings.session_cookie_name,
471
- # data,
472
- # settings.session_cookie_domain,
473
- # settings.session_cookie_path,
474
- # )
475
-
476
- def serialize(self) -> str:
477
- return self.srlz.serialize(self).decode("utf-8")
478
-
479
-
480
97
  class WebTestClient:
481
98
  """The fake browser used for testing purpose."""
482
99