chg: minor change in testing

This commit is contained in:
thentgesMindee
2022-11-08 17:58:11 +01:00
parent 1d9badb4a0
commit 3ae7154271
2 changed files with 22 additions and 24 deletions

View File

@@ -337,16 +337,10 @@ class TestDecorators(TestSlowapi):
assert response.status_code == 200 if i < 6 else 429
@pytest.mark.parametrize(
"key_style, expected_key",
[
("url", "LIMITER/mock//t1/param_one/1/1/minute"),
(
"endpoint",
"LIMITER/mock/tests.test_fastapi_extension.t1_func/1/1/minute",
),
],
"key_style",
["url", "endpoint"],
)
def test_key_style(self, build_fastapi_app, key_style, expected_key):
def test_key_style(self, build_fastapi_app, key_style):
app, limiter = build_fastapi_app(key_func=lambda: "mock", key_style=key_style)
@app.get("/t1/{my_param}")
@@ -361,12 +355,17 @@ class TestDecorators(TestSlowapi):
# meaning it should not raise any RateLimitExceeded error.
if key_style == "url":
assert second_call.status_code == 200
# also assert that we counted only one request on the expected key
assert limiter._storage.get(expected_key) == 1
assert limiter._storage.get("LIMITER/mock//t1/param_one/1/1/minute") == 1
assert limiter._storage.get("LIMITER/mock//t1/param_two/1/1/minute") == 1
# However, with the `endpoint` key_style, it will use the function name (e.g: "t1_func")
# meaning it will raise a RateLimitExceeded error, because no matter the parameter value
# it will share the limitations.
elif key_style == "endpoint":
assert second_call.status_code == 429
# check that we counted 2 requests, even though we had a different value for "my_param"
assert limiter._storage.get(expected_key) == 2
assert (
limiter._storage.get(
"LIMITER/mock/tests.test_fastapi_extension.t1_func/1/1/minute"
)
== 2
)

View File

@@ -324,16 +324,10 @@ class TestDecorators(TestSlowapi):
assert "error" in response.json()
@pytest.mark.parametrize(
"key_style, expected_key",
[
("url", "LIMITER/mock//t1/param_one/1/1/minute"),
(
"endpoint",
"LIMITER/mock/tests.test_starlette_extension.t1_func/1/1/minute",
),
],
"key_style",
["url", "endpoint"],
)
def test_key_style(self, build_starlette_app, key_style, expected_key):
def test_key_style(self, build_starlette_app, key_style):
app, limiter = build_starlette_app(key_func=lambda: "mock", key_style=key_style)
@limiter.limit("1/minute")
@@ -349,12 +343,17 @@ class TestDecorators(TestSlowapi):
# meaning it should not raise any RateLimitExceeded error.
if key_style == "url":
assert second_call.status_code == 200
# also assert that we counted only one request on the expected key
assert limiter._storage.get(expected_key) == 1
assert limiter._storage.get("LIMITER/mock//t1/param_one/1/1/minute") == 1
assert limiter._storage.get("LIMITER/mock//t1/param_two/1/1/minute") == 1
# However, with the `endpoint` key_style, it will use the function name (e.g: "t1_func")
# meaning it will raise a RateLimitExceeded error, because no matter the parameter value
# it will share the limitations.
elif key_style == "endpoint":
assert second_call.status_code == 429
# check that we counted 2 requests, even though we had a different value for "my_param"
assert limiter._storage.get(expected_key) == 2
assert (
limiter._storage.get(
"LIMITER/mock/tests.test_starlette_extension.t1_func/1/1/minute"
)
== 2
)