Skip to content

Commit

Permalink
Merge pull request #4724 from mozilla/python-3-12-tests-MPP-3487
Browse files Browse the repository at this point in the history
MPP-3487 - Fix tests, handle missing sqlcommenter in Python 3.12,
  • Loading branch information
jwhitlock authored May 29, 2024
2 parents 0203fb9 + 76d5aad commit a754c66
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
9 changes: 2 additions & 7 deletions emails/tests/sns_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
class GrabKeyfileTest(TestCase):
@patch("emails.sns.urlopen")
def test_grab_keyfile_checks_cert_url_origin(self, mock_urlopen):
cert_url = (
"https://sns.us-east-1.amazonaws.com/"
"SimpleNotificationService-7ff5318490ec183fbaddaa2a969abfda.pem"
)
assert mock_urlopen.called_once_with(cert_url)

cert_url = "https://attacker.com/cert.pem"
with self.assertRaises(SuspiciousOperation):
cert_url = "https://attacker.com/cert.pem"
_grab_keyfile(cert_url)
mock_urlopen.assert_not_called()
11 changes: 10 additions & 1 deletion privaterelay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
except ImportError:
HAS_SILK = False

try:
import google.cloud.sqlcommenter # noqa: F401

HAS_SQLCOMMENTER = True
except ImportError:
HAS_SQLCOMMENTER = False

try:
from privaterelay.glean.server_events import GLEAN_EVENT_MOZLOG_TYPE
except ImportError:
Expand Down Expand Up @@ -378,9 +385,11 @@ def _get_initial_middleware() -> list[str]:
"waffle.middleware.WaffleMiddleware",
"privaterelay.middleware.AddDetectedCountryToRequestAndResponseHeaders",
"privaterelay.middleware.StoreFirstVisit",
"google.cloud.sqlcommenter.django.middleware.SqlCommenter",
]

if HAS_SQLCOMMENTER:
MIDDLEWARE.append("google.cloud.sqlcommenter.django.middleware.SqlCommenter")

ROOT_URLCONF = "privaterelay.urls"

TEMPLATES = [
Expand Down
1 change: 1 addition & 0 deletions privaterelay/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def log_extra(log_record: LogRecord) -> dict[str, Any]:
"relativeCreated",
"rid",
"stack_info",
"taskName",
"thread",
"threadName",
}
Expand Down
12 changes: 6 additions & 6 deletions privaterelay/tests/views_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Callable, Iterator
from copy import deepcopy
from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta
from pathlib import Path
from typing import Any, Literal
from unittest.mock import ANY, Mock, patch
Expand Down Expand Up @@ -350,7 +350,7 @@ def get_fxa_event_jwt(
"iss": "https://accounts.firefox.com/",
"sub": fxa_id,
"aud": client_id,
"iat": int(datetime.utcnow().timestamp()) + iat_skew,
"iat": int(datetime.now(UTC).timestamp()) + iat_skew,
"jti": str(uuid4()),
"events": {event_key: event_data},
}
Expand All @@ -369,7 +369,7 @@ def test_fxa_rp_events_password_change(
fxa_id=setup_fxa_rp_events.fxa_acct.uid,
client_id=setup_fxa_rp_events.app.client_id,
signing_key=setup_fxa_rp_events.key,
event_data={"changeTime": int(datetime.utcnow().timestamp()) - 100},
event_data={"changeTime": int(datetime.now(UTC).timestamp()) - 100},
)
auth_header = f"Bearer {event_jwt}"

Expand All @@ -395,7 +395,7 @@ def test_fxa_rp_events_password_change_slight_future_iat(
fxa_id=setup_fxa_rp_events.fxa_acct.uid,
client_id=setup_fxa_rp_events.app.client_id,
signing_key=setup_fxa_rp_events.key,
event_data={"changeTime": int(datetime.utcnow().timestamp()) - 100},
event_data={"changeTime": int(datetime.now(UTC).timestamp()) - 100},
iat_skew=3,
)
auth_header = f"Bearer {event_jwt}"
Expand Down Expand Up @@ -426,7 +426,7 @@ def test_fxa_rp_events_password_change_far_future_iat(
fxa_id=setup_fxa_rp_events.fxa_acct.uid,
client_id=setup_fxa_rp_events.app.client_id,
signing_key=setup_fxa_rp_events.key,
event_data={"changeTime": int(datetime.utcnow().timestamp()) - 100},
event_data={"changeTime": int(datetime.now(UTC).timestamp()) - 100},
iat_skew=10,
)
auth_header = f"Bearer {event_jwt}"
Expand Down Expand Up @@ -493,7 +493,7 @@ def test_fxa_rp_events_subscription_change(
event_data={
"capabilities": ["new_capability"],
"isActive": True,
"changeTime": int(datetime.utcnow().timestamp()) - 100,
"changeTime": int(datetime.now(UTC).timestamp()) - 100,
},
)
auth_header = f"Bearer {event_jwt}"
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module = [
"django_filters.*",
"django_ftl",
"django_ftl.bundles",
"google.*",
"google_measurement_protocol",
"googlecloudprofiler",
"ipware",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ drf-spectacular-sidecar==2024.5.1
glean_parser==14.1.2
google-measurement-protocol==1.1.0
google-cloud-profiler==4.1.0
google-cloud-sqlcommenter==2.0.0
google-cloud-sqlcommenter==2.0.0; python_version < '3.12'
gunicorn==22.0.0
jwcrypto==1.5.6
markus[datadog]==4.2.0
Expand Down

0 comments on commit a754c66

Please sign in to comment.