Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump FastAPI and Starlette, add httpx #867

Merged
merged 5 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ctms/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def __init__(
super().__init__(flows=flows, scheme_name=scheme_name, auto_error=True)

async def __call__(self, request: Request) -> Optional[str]:
authorization: str = request.headers.get("Authorization")
authorization: Optional[str] = request.headers.get("Authorization")

# TODO: Try combining these lines after FastAPI 0.61.2 / mypy update
scheme_param = get_authorization_scheme_param(authorization)
Expand Down
75 changes: 59 additions & 16 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ authors = ["CTMS Reviewers <@mozilla-it/ctms-reviewers>"]
[tool.poetry.dependencies]
# These packages are mandatory and form the core of this package’s distribution.
python = "^3.12.1"
fastapi = "^0.84.0"
starlette = "^0.19.1"
fastapi = "^0.108.0"
starlette = "^0.32.0"
requests = "^2.31.0"
uvicorn = {extras = ["standard"], version = "^0.25.0"}
pydantic = {extras = ["email"], version = "^1.10.13"}
Expand Down Expand Up @@ -52,6 +52,7 @@ types-requests = "^2.31.0"
factory-boy = "^3.3.0"
pytest-factoryboy = "^2.5.1"
backoff = "^2.2.1"
httpx = "^0.26.0"

[tool.pytest.ini_options]
testpaths = [
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def _add(
query_fields = {"primary_email": contact_fixture.email.primary_email}
sample = contact_fixture.copy(deep=True)
sample = modifier(sample)
resp = client.post("/ctms", sample.json())
resp = client.post("/ctms", content=sample.json())
assert resp.status_code == code, resp.text
if check_redirect:
assert resp.headers["location"] == f"/ctms/{sample.email.email_id}"
Expand Down Expand Up @@ -740,7 +740,7 @@ def _add(
new_default_fields = new_default_fields or set()
sample = contact.copy(deep=True)
sample = modifier(sample)
resp = client.put(f"/ctms/{sample.email.email_id}", sample.json())
resp = client.put(f"/ctms/{sample.email.email_id}", content=sample.json())
assert resp.status_code == code, resp.text
saved = get_contacts_by_any_id(dbsession, **query_fields)
assert len(saved) == stored_contacts
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/routers/contacts/test_api_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ def test_create_without_trace(client):

def test_create_with_non_json_is_error(client):
"""When non-JSON is posted /ctms, a 422 is returned"""
data = "this is not JSON"
data = b"this is not JSON"
with capture_logs() as cap_logs:
resp = client.post("/ctms", data=data)
resp = client.post("/ctms", content=data)
assert resp.status_code == 422
assert (
resp.json()["detail"][0]["msg"] == "Expecting value: line 1 column 1 (char 0)"
)
assert resp.json()["detail"][0]["msg"] == "JSON decode error"
assert len(cap_logs) == 1
assert "trace" not in cap_logs[0]

Expand Down
10 changes: 4 additions & 6 deletions tests/unit/routers/contacts/test_api_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_create_or_update_basic_id_is_different(client, minimal_contact):

# This id is different from the one in the contact
resp = client.put(
"/ctms/d16c4ec4-caa0-4bf2-a06f-1bbf07bf03c7", minimal_contact.json()
"/ctms/d16c4ec4-caa0-4bf2-a06f-1bbf07bf03c7", content=minimal_contact.json()
)
assert resp.status_code == 422, resp.text

Expand Down Expand Up @@ -150,13 +150,11 @@ def test_put_replace_no_trace(client, minimal_contact):
def test_put_with_not_json_is_error(client, dbsession):
"""Calling PUT with a text body is a 422 validation error."""
email_id = str(uuid4())
data = "make a contact please"
data = b"make a contact please"
with capture_logs() as caplogs:
resp = client.put(f"/ctms/{email_id}", data=data)
resp = client.put(f"/ctms/{email_id}", content=data)
assert resp.status_code == 422
assert (
resp.json()["detail"][0]["msg"] == "Expecting value: line 1 column 1 (char 0)"
)
assert resp.json()["detail"][0]["msg"] == "JSON decode error"
assert len(caplogs) == 1
assert "trace" not in caplogs[0]

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def test_post_token_header(anon_client, test_token_settings, client_id_and_secre
client_id, client_secret = client_id_and_secret
resp = anon_client.post(
"/token",
{"grant_type": "client_credentials"},
data={"grant_type": "client_credentials"},
auth=HTTPBasicAuth(client_id, client_secret),
)
assert resp.status_code == 200
Expand All @@ -87,7 +87,7 @@ def test_post_token_form_data(anon_client, test_token_settings, client_id_and_se
client_id, client_secret = client_id_and_secret
resp = anon_client.post(
"/token",
{
data={
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_post_token_succeeds_refresh_grant(
client_id, client_secret = client_id_and_secret
resp = anon_client.post(
"/token",
{
data={
"grant_type": "refresh_token",
"refresh_token": None,
"client_id": client_id,
Expand All @@ -137,7 +137,7 @@ def test_post_token_fails_wrong_grant(anon_client, client_id_and_secret):
"""If grant_type is omitted, getting a token fails."""
resp = anon_client.post(
"/token",
{"grant_type": "password"},
data={"grant_type": "password"},
auth=HTTPBasicAuth(*client_id_and_secret),
)
assert resp.status_code == 422
Expand All @@ -153,7 +153,7 @@ def test_post_token_fails_wrong_grant(anon_client, client_id_and_secret):
def test_post_token_fails_no_credentials(anon_client):
"""If no credentials are passed, token generation fails."""
with capture_logs() as caplog:
resp = anon_client.post("/token", {"grant_type": "client_credentials"})
resp = anon_client.post("/token", data={"grant_type": "client_credentials"})
assert resp.status_code == 400
assert resp.json() == {"detail": "Incorrect username or password"}
assert caplog[0]["token_fail"] == "No credentials"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_token_request_log(anon_client, client_id_and_secret):
with capture_logs() as cap_logs:
resp = anon_client.post(
"/token",
{"grant_type": "client_credentials"},
data={"grant_type": "client_credentials"},
auth=HTTPBasicAuth(client_id, client_secret),
cookies={"csrftoken": "0WzT-base64-string"},
)
Expand Down