Skip to content

Commit 040c856

Browse files
committed
test: post to protected endpoint expecting fails
1 parent 7d0c36a commit 040c856

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tests/cookie_body.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,45 @@ def get_configs():
7777
assert response.json() == {"detail": "Missing Cookie: `fastapi-csrf-token`."}
7878

7979

80+
@mark.parametrize(
81+
"csrf_settings",
82+
(
83+
(
84+
("cookie_secure", True),
85+
("secret_key", "secret"),
86+
("token_key", "csrf-token"),
87+
("token_location", "body"),
88+
),
89+
),
90+
)
91+
def test_submit_csrf_token_in_body_and_cookies_secure_but_using_http(
92+
csrf_settings: Tuple[Tuple[str, str], ...], test_client: TestClient
93+
):
94+
### Load config ###
95+
@CsrfProtect.load_config
96+
def get_configs():
97+
return csrf_settings
98+
99+
### Generate token ###
100+
response = test_client.get("/gen-token")
101+
assert response.status_code == 200
102+
103+
### Asserts that `cookie_token` is present
104+
cookie_token: Optional[str] = test_client.cookies.get("fastapi-csrf-token", None)
105+
assert cookie_token is not None
106+
107+
### Extract `csrf_token` from response to be set as next request's body ###
108+
csrf_token: Optional[str] = response.json().get("csrf_token", None)
109+
payload: Dict[str, str] = {"csrf-token": csrf_token} if csrf_token is not None else {}
110+
111+
### Post to protected endpoint but fails because TestClients defaults to http ###
112+
response = test_client.post("/protected", data=payload)
113+
114+
### Assertions ###
115+
assert response.status_code == 400
116+
assert response.json() == {"detail": "Missing Cookie: `fastapi-csrf-token`."}
117+
118+
80119
@mark.parametrize(
81120
"csrf_settings",
82121
(

tests/cookie_header.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,44 @@ def get_configs():
7878
assert response.json() == {"detail": "Missing Cookie: `fastapi-csrf-token`."}
7979

8080

81+
@mark.parametrize(
82+
"csrf_settings",
83+
(
84+
(
85+
("cookie_secure", True),
86+
("secret_key", "secret"),
87+
("token_location", "header"),
88+
),
89+
),
90+
)
91+
def test_submit_csrf_token_in_headers_and_cookies_secure_but_using_http(
92+
csrf_settings: Tuple[Tuple[str, str], ...], test_client: TestClient
93+
):
94+
### Load config ###
95+
@CsrfProtect.load_config
96+
def get_configs():
97+
return csrf_settings
98+
99+
### Generate token ###
100+
response = test_client.get("/gen-token")
101+
assert response.status_code == 200
102+
103+
### Asserts that `cookie_token` is present
104+
cookie_token: Optional[str] = test_client.cookies.get("fastapi-csrf-token", None)
105+
assert cookie_token is not None
106+
107+
### Extract `csrf_token` from response to be set as next request's body ###
108+
csrf_token: Optional[str] = response.json().get("csrf_token", None)
109+
payload: Dict[str, str] = {"csrf-token": csrf_token} if csrf_token is not None else {}
110+
111+
### Post to protected endpoint but fails because TestClients defaults to http ###
112+
response = test_client.post("/protected", data=payload)
113+
114+
### Assertions ###
115+
assert response.status_code == 400
116+
assert response.json() == {"detail": "Missing Cookie: `fastapi-csrf-token`."}
117+
118+
81119
@mark.parametrize(
82120
"csrf_settings",
83121
(

0 commit comments

Comments
 (0)