Skip to content

Commit bc23f0f

Browse files
committed
update: adds init tests
1 parent d2c9f3f commit bc23f0f

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

tests/plugins/plugins.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def init(
4949
all_plugins: List[SuperTokensPublicPlugin],
5050
sdk_version: str,
5151
):
52-
# TODO: Test this
53-
print(f"{identifier} init")
52+
PluginTestRecipe.init_calls.append(identifier)
5453

5554
return init
5655

@@ -78,6 +77,7 @@ def plugin_factory(
7877
override_functions: bool = False,
7978
override_apis: bool = False,
8079
deps: Optional[List[SuperTokensPlugin]] = None,
80+
add_init: bool = False,
8181
):
8282
override_map_obj: OverrideMap = {PluginTestRecipe.recipe_id: OverrideConfig()}
8383

@@ -90,24 +90,57 @@ def plugin_factory(
9090
identifier
9191
)
9292

93+
init_fn = None
94+
if add_init:
95+
init_fn = init_factory(identifier)
96+
9397
class Plugin(SuperTokensPlugin):
9498
id: str = identifier
9599
compatible_sdk_versions: Union[str, List[str]] = ["0.30.0"]
96100
override_map: Optional[OverrideMap] = override_map_obj
97-
init: Any = init_factory(identifier)
101+
init: Any = init_fn
98102
dependencies: Optional[SuperTokensPluginDependencies] = dependency_factory(deps)
99103

100104
return Plugin()
101105

102106

103-
Plugin1 = plugin_factory("plugin1", override_functions=True)
104-
Plugin2 = plugin_factory("plugin2", override_functions=True)
105-
Plugin3Dep1 = plugin_factory("plugin3dep1", override_functions=True, deps=[Plugin1])
107+
Plugin1 = plugin_factory(
108+
"plugin1",
109+
override_functions=True,
110+
add_init=True,
111+
)
112+
Plugin2 = plugin_factory(
113+
"plugin2",
114+
override_functions=True,
115+
add_init=True,
116+
)
117+
Plugin3Dep1 = plugin_factory(
118+
"plugin3dep1",
119+
override_functions=True,
120+
deps=[Plugin1],
121+
add_init=True,
122+
)
106123
Plugin3Dep2_1 = plugin_factory(
107-
"plugin3dep2_1", override_functions=True, deps=[Plugin2, Plugin1]
124+
"plugin3dep2_1",
125+
override_functions=True,
126+
deps=[Plugin2, Plugin1],
127+
add_init=True,
128+
)
129+
Plugin4Dep1 = plugin_factory(
130+
"plugin4dep1",
131+
override_functions=True,
132+
deps=[Plugin1],
133+
add_init=True,
134+
)
135+
Plugin4Dep2 = plugin_factory(
136+
"plugin4dep2",
137+
override_functions=True,
138+
deps=[Plugin2],
139+
add_init=True,
108140
)
109-
Plugin4Dep1 = plugin_factory("plugin4dep1", override_functions=True, deps=[Plugin1])
110-
Plugin4Dep2 = plugin_factory("plugin4dep2", override_functions=True, deps=[Plugin2])
111141
Plugin4Dep3__2_1 = plugin_factory(
112-
"plugin4dep3__2_1", override_functions=True, deps=[Plugin3Dep2_1]
142+
"plugin4dep3__2_1",
143+
override_functions=True,
144+
deps=[Plugin3Dep2_1],
145+
add_init=True,
113146
)

tests/plugins/recipe.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
class PluginTestRecipe(RecipeModule):
2828
__instance: Optional["PluginTestRecipe"] = None
29+
init_calls: List[str] = []
2930
recipe_id = "plugin_test"
3031

3132
config: NormalizedPluginTestConfig
@@ -94,6 +95,7 @@ def func(app_info: AppInfo, plugins: List[OverrideMap]):
9495
@staticmethod
9596
def reset():
9697
PluginTestRecipe.__instance = None
98+
PluginTestRecipe.init_calls = []
9799

98100
def get_all_cors_headers(self) -> List[str]:
99101
return []

tests/plugins/test_plugins.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
PluginRouteHandlerFunctionOkResponse,
1616
SuperTokensPlugin,
1717
)
18+
from supertokens_python.post_init_callbacks import PostSTInitCallbacks
1819
from supertokens_python.supertokens import SupertokensPublicConfig
1920

2021
from tests.utils import outputs, reset
@@ -41,9 +42,11 @@
4142
def setup_and_teardown():
4243
reset()
4344
PluginTestRecipe.reset()
45+
PostSTInitCallbacks.reset()
4446
yield
4547
reset()
4648
PluginTestRecipe.reset()
49+
PostSTInitCallbacks.reset()
4750

4851

4952
def recipe_factory(override_functions: bool = False, override_apis: bool = False):
@@ -214,36 +217,41 @@ def test_overrides(
214217

215218
# TODO: Figure out a way to add circular dependencies and test them
216219
@mark.parametrize(
217-
("plugins", "recipe_expectation", "api_expectation"),
220+
("plugins", "recipe_expectation", "api_expectation", "init_expectation"),
218221
[
219222
param(
220223
[Plugin1, Plugin1],
221224
outputs(["plugin1", "original"]),
222225
outputs(["original"]),
226+
outputs(["plugin1"]),
223227
id="1,1 => 1",
224228
),
225229
param(
226230
[Plugin1, Plugin2],
227231
outputs(["plugin2", "plugin1", "original"]),
228232
outputs(["original"]),
233+
outputs(["plugin1", "plugin2"]),
229234
id="1,2 => 2,1",
230235
),
231236
param(
232237
[Plugin3Dep1],
233238
outputs(["plugin3dep1", "plugin1", "original"]),
234239
outputs(["original"]),
240+
outputs(["plugin1", "plugin3dep1"]),
235241
id="3->1 => 3,1",
236242
),
237243
param(
238244
[Plugin3Dep2_1],
239245
outputs(["plugin3dep2_1", "plugin1", "plugin2", "original"]),
240246
outputs(["original"]),
247+
outputs(["plugin2", "plugin1", "plugin3dep2_1"]),
241248
id="3->(2,1) => 3,2,1",
242249
),
243250
param(
244251
[Plugin3Dep1, Plugin4Dep2],
245252
outputs(["plugin4dep2", "plugin2", "plugin3dep1", "plugin1", "original"]),
246253
outputs(["original"]),
254+
outputs(["plugin1", "plugin3dep1", "plugin2", "plugin4dep2"]),
247255
id="3->1,4->2 => 4,2,3,1",
248256
),
249257
param(
@@ -252,20 +260,23 @@ def test_overrides(
252260
["plugin4dep3__2_1", "plugin3dep2_1", "plugin1", "plugin2", "original"]
253261
),
254262
outputs(["original"]),
263+
outputs(["plugin2", "plugin1", "plugin3dep2_1", "plugin4dep3__2_1"]),
255264
id="4->3->(2,1) => 4,3,1,2",
256265
),
257266
param(
258267
[Plugin3Dep1, Plugin4Dep1],
259268
outputs(["plugin4dep1", "plugin3dep1", "plugin1", "original"]),
260269
outputs(["original"]),
270+
outputs(["plugin1", "plugin3dep1", "plugin4dep1"]),
261271
id="3->1,4->1 => 4,3,1",
262272
),
263273
],
264274
)
265-
def test_depdendencies(
275+
def test_depdendencies_and_init(
266276
plugins: List[SuperTokensPlugin],
267277
recipe_expectation: Any,
268278
api_expectation: Any,
279+
init_expectation: Any,
269280
):
270281
partial_init(
271282
recipe_list=[
@@ -298,8 +309,10 @@ def test_depdendencies(
298309
message="msg",
299310
)
300311

312+
with init_expectation as expected_stack:
313+
assert PluginTestRecipe.init_calls == expected_stack
314+
301315

302-
# TODO: Add tests for init, recipe config override
303316
def test_st_config_override():
304317
plugin = plugin_factory("plugin1", override_functions=False, override_apis=False)
305318

0 commit comments

Comments
 (0)