Skip to content

Commit

Permalink
Merge pull request #6413 from freedomofpress/delegate-tz-string-forma…
Browse files Browse the repository at this point in the history
…tting-to-datetime

Delegate date strings formatting to datetime.isoformat()
  • Loading branch information
legoktm authored Dec 20, 2024
2 parents f81a64b + da8f43f commit 0ba1208
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 6 deletions.
4 changes: 1 addition & 3 deletions securedrop/journalist_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

_insecure_views = ["main.login", "static"]
_insecure_api_views = ["api.get_token", "api.get_endpoints"]
# Timezone-naive datetime format expected by SecureDrop Client
API_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"


def get_logo_url(app: Flask) -> str:
Expand Down Expand Up @@ -67,7 +65,7 @@ class JSONEncoder(json.JSONEncoder):

def default(self, obj: "Any") -> "Any":
if isinstance(obj, datetime):
return obj.strftime(API_DATETIME_FORMAT)
return obj.isoformat()
super().default(obj)

app.json_encoder = JSONEncoder
Expand Down
3 changes: 1 addition & 2 deletions securedrop/tests/test_journalist_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

def assert_valid_timestamp(timestamp: str) -> None:
"""verify the timestamp is encoded in the format we want"""
dt_format = "%Y-%m-%dT%H:%M:%S.%fZ"
assert timestamp == datetime.strptime(timestamp, dt_format).strftime(dt_format)
assert timestamp == datetime.fromisoformat(timestamp).isoformat()


def test_unauthenticated_user_gets_all_endpoints(journalist_app):
Expand Down
2 changes: 1 addition & 1 deletion securedrop/tests/test_journalist_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def test_session_api_login(journalist_app, test_journo, redis):
# Then the expiration date returned in `get_token` response also conforms to the same rules
assert (
datetime.now(timezone.utc)
< datetime.strptime(resp.json["expiration"], "%Y-%m-%dT%H:%M:%S.%f%z")
< datetime.fromisoformat(resp.json["expiration"])
< (
datetime.now(timezone.utc)
+ timedelta(seconds=journalist_app.config["SESSION_LIFETIME"])
Expand Down

0 comments on commit 0ba1208

Please sign in to comment.