*,
grant_type: Annotated[
Union[str, None],
- Form(pattern="password"),
+ Form(pattern="^password$"),
Doc(
"""
The OAuth2 spec says it is required and MUST be the fixed string
self,
grant_type: Annotated[
str,
- Form(pattern="password"),
+ Form(pattern="^password$"),
Doc(
"""
The OAuth2 spec says it is required and MUST be the fixed string
+import pytest
from dirty_equals import IsDict
from fastapi import Depends, FastAPI, Security
from fastapi.security import OAuth2, OAuth2PasswordRequestFormStrict
)
-def test_strict_login_incorrect_grant_type():
+@pytest.mark.parametrize(
+ argnames=["grant_type"],
+ argvalues=[
+ pytest.param("incorrect", id="incorrect value"),
+ pytest.param("passwordblah", id="password with suffix"),
+ pytest.param("blahpassword", id="password with prefix"),
+ ],
+)
+def test_strict_login_incorrect_grant_type(grant_type: str):
response = client.post(
"/login",
- data={"username": "johndoe", "password": "secret", "grant_type": "incorrect"},
+ data={"username": "johndoe", "password": "secret", "grant_type": grant_type},
)
assert response.status_code == 422
assert response.json() == IsDict(
{
"type": "string_pattern_mismatch",
"loc": ["body", "grant_type"],
- "msg": "String should match pattern 'password'",
- "input": "incorrect",
- "ctx": {"pattern": "password"},
+ "msg": "String should match pattern '^password$'",
+ "input": grant_type,
+ "ctx": {"pattern": "^password$"},
}
]
}
"detail": [
{
"loc": ["body", "grant_type"],
- "msg": 'string does not match regex "password"',
+ "msg": 'string does not match regex "^password$"',
"type": "value_error.str.regex",
- "ctx": {"pattern": "password"},
+ "ctx": {"pattern": "^password$"},
}
]
}
"properties": {
"grant_type": {
"title": "Grant Type",
- "pattern": "password",
+ "pattern": "^password$",
"type": "string",
},
"username": {"title": "Username", "type": "string"},
from typing import Optional
+import pytest
from dirty_equals import IsDict
from fastapi import Depends, FastAPI, Security
from fastapi.security import OAuth2, OAuth2PasswordRequestFormStrict
)
-def test_strict_login_incorrect_grant_type():
+@pytest.mark.parametrize(
+ argnames=["grant_type"],
+ argvalues=[
+ pytest.param("incorrect", id="incorrect value"),
+ pytest.param("passwordblah", id="password with suffix"),
+ pytest.param("blahpassword", id="password with prefix"),
+ ],
+)
+def test_strict_login_incorrect_grant_type(grant_type: str):
response = client.post(
"/login",
- data={"username": "johndoe", "password": "secret", "grant_type": "incorrect"},
+ data={"username": "johndoe", "password": "secret", "grant_type": grant_type},
)
assert response.status_code == 422
assert response.json() == IsDict(
{
"type": "string_pattern_mismatch",
"loc": ["body", "grant_type"],
- "msg": "String should match pattern 'password'",
- "input": "incorrect",
- "ctx": {"pattern": "password"},
+ "msg": "String should match pattern '^password$'",
+ "input": grant_type,
+ "ctx": {"pattern": "^password$"},
}
]
}
"detail": [
{
"loc": ["body", "grant_type"],
- "msg": 'string does not match regex "password"',
+ "msg": 'string does not match regex "^password$"',
"type": "value_error.str.regex",
- "ctx": {"pattern": "password"},
+ "ctx": {"pattern": "^password$"},
}
]
}
"properties": {
"grant_type": {
"title": "Grant Type",
- "pattern": "password",
+ "pattern": "^password$",
"type": "string",
},
"username": {"title": "Username", "type": "string"},
from typing import Optional
+import pytest
from dirty_equals import IsDict
from fastapi import Depends, FastAPI, Security
from fastapi.security import OAuth2, OAuth2PasswordRequestFormStrict
)
-def test_strict_login_incorrect_grant_type():
+@pytest.mark.parametrize(
+ argnames=["grant_type"],
+ argvalues=[
+ pytest.param("incorrect", id="incorrect value"),
+ pytest.param("passwordblah", id="password with suffix"),
+ pytest.param("blahpassword", id="password with prefix"),
+ ],
+)
+def test_strict_login_incorrect_grant_type(grant_type: str):
response = client.post(
"/login",
- data={"username": "johndoe", "password": "secret", "grant_type": "incorrect"},
+ data={"username": "johndoe", "password": "secret", "grant_type": grant_type},
)
assert response.status_code == 422
assert response.json() == IsDict(
{
"type": "string_pattern_mismatch",
"loc": ["body", "grant_type"],
- "msg": "String should match pattern 'password'",
- "input": "incorrect",
- "ctx": {"pattern": "password"},
+ "msg": "String should match pattern '^password$'",
+ "input": grant_type,
+ "ctx": {"pattern": "^password$"},
}
]
}
"detail": [
{
"loc": ["body", "grant_type"],
- "msg": 'string does not match regex "password"',
+ "msg": 'string does not match regex "^password$"',
"type": "value_error.str.regex",
- "ctx": {"pattern": "password"},
+ "ctx": {"pattern": "^password$"},
}
]
}
"properties": {
"grant_type": {
"title": "Grant Type",
- "pattern": "password",
+ "pattern": "^password$",
"type": "string",
},
"username": {"title": "Username", "type": "string"},
{
"title": "Grant Type",
"anyOf": [
- {"pattern": "password", "type": "string"},
+ {"pattern": "^password$", "type": "string"},
{"type": "null"},
],
}
# TODO: remove when deprecating Pydantic v1
{
"title": "Grant Type",
- "pattern": "password",
+ "pattern": "^password$",
"type": "string",
}
),
{
"title": "Grant Type",
"anyOf": [
- {"pattern": "password", "type": "string"},
+ {"pattern": "^password$", "type": "string"},
{"type": "null"},
],
}
# TODO: remove when deprecating Pydantic v1
{
"title": "Grant Type",
- "pattern": "password",
+ "pattern": "^password$",
"type": "string",
}
),