From fb45c607e501073f37c622ed43fd8d41345a66d5 Mon Sep 17 00:00:00 2001 From: doria <93405247+dr-frmr@users.noreply.github.com> Date: Wed, 13 Aug 2025 13:13:33 -0400 Subject: [PATCH] fix: refactor datetime handling in security and utility modules (#188) * fix: refactor datetime handling in security and utility modules - Updated `verify_jwt` function to utilize `parse_datetime_iso` for improved expiration time validation. - Enhanced `_validate_datetime_string` to prioritize timezone-aware formats and streamline parsing logic. - Modified `created_at` assignments in `ObservationContext` to use UTC timezone for consistency in timestamp handling. * fix: proper observation extraction - Replaced direct observation references with the `extract_observation_content` function to enhance clarity and maintainability in the `new_observations` list comprehension. * refactor: streamline datetime parsing in filter and formatting utilities - Removed redundant timezone-aware format handling from `_validate_datetime_string` in `filter.py`. - Enhanced `parse_datetime_iso` in `formatting.py` to ensure consistent conversion of 'Z' suffix to timezone-aware datetime objects, improving clarity and functionality. * refactor: update JWT verification and enhance datetime parsing - Changed `verify_jwt` function from asynchronous to synchronous for improved performance and simplicity. - Enhanced `parse_datetime_iso` to include comprehensive input validation and support for various timezone formats, ensuring consistent and secure datetime parsing. * chore: coderabbit --- src/deriver/deriver.py | 3 +- src/security.py | 17 +- src/utils/filter.py | 40 +-- src/utils/formatting.py | 51 +++- src/utils/shared_models.py | 6 +- tests/test_datetime_parsing.py | 533 +++++++++++++++++++++++++++++++++ 6 files changed, 608 insertions(+), 42 deletions(-) create mode 100644 tests/test_datetime_parsing.py diff --git a/src/deriver/deriver.py b/src/deriver/deriver.py index 437d8781..541782e7 100644 --- a/src/deriver/deriver.py +++ b/src/deriver/deriver.py @@ -16,6 +16,7 @@ from src.utils.clients import honcho_llm_call from src.utils.embedding_store import EmbeddingStore from src.utils.formatting import ( REASONING_LEVELS, + extract_observation_content, find_new_observations, format_context_for_prompt, format_new_turn_with_timestamp, @@ -451,7 +452,7 @@ class CertaintyReasoner: update_peer_card_start = time.perf_counter() # flatten new observations by level into a list new_observations = [ - observation + extract_observation_content(observation) for level in new_observations_by_level.values() for observation in level ] diff --git a/src/security.py b/src/security.py index a0e1a0bc..46dd2dce 100644 --- a/src/security.py +++ b/src/security.py @@ -8,7 +8,7 @@ from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from pydantic import BaseModel from src.config import settings -from src.utils.formatting import utc_now_iso +from src.utils.formatting import parse_datetime_iso, utc_now_iso from .exceptions import AuthenticationException @@ -80,7 +80,7 @@ def create_jwt(params: JWTParams) -> str: ) -async def verify_jwt(token: str) -> JWTParams: +def verify_jwt(token: str) -> JWTParams: """Verify a JWT and return the decoded parameters.""" params = JWTParams() @@ -94,12 +94,11 @@ async def verify_jwt(token: str) -> JWTParams: params.t = decoded["t"] if "exp" in decoded: params.exp = decoded["exp"] - if ( - params.exp - and datetime.datetime.fromisoformat(params.exp) - < datetime.datetime.now() - ): - raise AuthenticationException("JWT expired") + if params.exp: + exp_time = parse_datetime_iso(params.exp) + current_time = datetime.datetime.now(datetime.timezone.utc) + if exp_time < current_time: + raise AuthenticationException("JWT expired") if "ad" in decoded: params.ad = decoded["ad"] if "w" in decoded: @@ -170,7 +169,7 @@ async def auth( logger.warning("No access token provided") raise AuthenticationException("No access token provided") - jwt_params = await verify_jwt(credentials.credentials) + jwt_params = verify_jwt(credentials.credentials) # based on api operation, verify api key based on that key's permissions if jwt_params.ad: diff --git a/src/utils/filter.py b/src/utils/filter.py index 50867da1..8e91e2cc 100644 --- a/src/utils/filter.py +++ b/src/utils/filter.py @@ -533,8 +533,8 @@ def _validate_datetime_string(value: str) -> datetime.datetime | None: """ Safely validate and parse a datetime string to prevent SQL injection. - This function attempts to parse the datetime string using multiple common formats - to ensure it's a valid datetime before allowing it to be used in SQL queries. + This function prioritizes timezone-aware datetime formats and uses the + consistent parse_datetime_iso utility for proper timezone handling. Args: value: String value to validate as datetime @@ -545,28 +545,28 @@ def _validate_datetime_string(value: str) -> datetime.datetime | None: # Strip whitespace value = value.strip() - # Try to parse with various common datetime formats - datetime_formats = [ - "%Y-%m-%d %H:%M:%S", # 2024-01-01 12:00:00 - "%Y-%m-%d %H:%M:%S.%f", # 2024-01-01 12:00:00.123456 - "%Y-%m-%dT%H:%M:%S", # 2024-01-01T12:00:00 (ISO format) - "%Y-%m-%dT%H:%M:%S.%f", # 2024-01-01T12:00:00.123456 - "%Y-%m-%dT%H:%M:%SZ", # 2024-01-01T12:00:00Z (UTC) - "%Y-%m-%dT%H:%M:%S.%fZ", # 2024-01-01T12:00:00.123456Z - "%Y-%m-%d", # 2024-01-01 - ] - - for fmt in datetime_formats: - try: - return datetime.datetime.strptime(value, fmt) - except ValueError: - continue - - # Try fromisoformat as a fallback (Python 3.7+) with Z format support + # First try the standard ISO format with timezone info using our utility try: return parse_datetime_iso(value) except ValueError: pass + # Fallback to naive formats (assume UTC timezone for compatibility) + naive_formats = [ + "%Y-%m-%dT%H:%M:%S", # 2024-01-01T12:00:00 (ISO format, assume UTC) + "%Y-%m-%dT%H:%M:%S.%f", # 2024-01-01T12:00:00.123456 (assume UTC) + "%Y-%m-%d %H:%M:%S", # 2024-01-01 12:00:00 (assume UTC) + "%Y-%m-%d %H:%M:%S.%f", # 2024-01-01 12:00:00.123456 (assume UTC) + "%Y-%m-%d", # 2024-01-01 (assume UTC, start of day) + ] + + for fmt in naive_formats: + try: + parsed = datetime.datetime.strptime(value, fmt) + # Assume UTC timezone for naive datetimes + return parsed.replace(tzinfo=datetime.timezone.utc) + except ValueError: + continue + # Return None for invalid datetime - let the caller handle the error return None diff --git a/src/utils/formatting.py b/src/utils/formatting.py index e9c3b8ad..cb205e37 100644 --- a/src/utils/formatting.py +++ b/src/utils/formatting.py @@ -59,11 +59,10 @@ def utc_now_iso() -> str: def parse_datetime_iso(iso_string: str) -> datetime: """ - Parse ISO 8601 datetime string, handling both Z and +00:00 UTC formats. + Parse ISO 8601 datetime string, handling various timezone formats. - This function handles the fact that Python's fromisoformat() doesn't - directly support the 'Z' suffix, which is the standard ISO 8601 way - to represent UTC timezone. + This function properly handles Z suffix, timezone offsets, and naive timestamps. + It validates input and always returns a timezone-aware datetime object. Args: iso_string: ISO 8601 formatted datetime string @@ -71,15 +70,49 @@ def parse_datetime_iso(iso_string: str) -> datetime: Returns: datetime object with timezone information + Raises: + ValueError: If the input string is invalid or contains suspicious content + Example: >>> parse_datetime_iso('2023-01-01T12:00:00Z') datetime.datetime(2023, 1, 1, 12, 0, tzinfo=datetime.timezone.utc) - >>> parse_datetime_iso('2023-01-01T12:00:00+00:00') - datetime.datetime(2023, 1, 1, 12, 0, tzinfo=datetime.timezone.utc) + >>> parse_datetime_iso('2023-01-01T12:00:00+05:00') + datetime.datetime(2023, 1, 1, 12, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=18000))) """ - # Convert Z format to +00:00 format for Python's fromisoformat - normalized_string = iso_string.replace("Z", "+00:00") - return datetime.fromisoformat(normalized_string) + # Input validation - ensure type and reject suspicious content + if not iso_string: + raise ValueError("Invalid input: must be a non-empty string") + + iso_string = str(iso_string) + + # Security check - reject strings with null bytes or suspicious characters + if "\x00" in iso_string or "\r" in iso_string or "\n" in iso_string: + raise ValueError("Invalid input: contains null bytes or line breaks") + + # Check for non-printable unicode characters that could be used for attacks + if any(ord(c) < 32 and c not in "\t" for c in iso_string): + raise ValueError("Invalid input: contains non-printable characters") + + # Strip whitespace + iso_string = iso_string.strip() + if not iso_string: + raise ValueError("Invalid input: empty after stripping whitespace") + + # Handle Z suffix (convert to +00:00) + if iso_string.endswith(("Z", "z")): + iso_string = iso_string[:-1] + "+00:00" + + try: + # Try parsing with timezone info first + result = datetime.fromisoformat(iso_string) + + # If no timezone info, assume UTC + if result.tzinfo is None: + result = result.replace(tzinfo=timezone.utc) + + return result + except ValueError as e: + raise ValueError(f"Invalid ISO 8601 datetime format: {e}") from e @runtime_checkable diff --git a/src/utils/shared_models.py b/src/utils/shared_models.py index 522227cc..70f83a76 100644 --- a/src/utils/shared_models.py +++ b/src/utils/shared_models.py @@ -4,7 +4,7 @@ Shared Pydantic models used by both dialectic and deriver modules. from __future__ import annotations -from datetime import datetime +from datetime import datetime, timezone from enum import Enum from pydantic import BaseModel, Field @@ -147,7 +147,7 @@ class ObservationContext(BaseModel): obs = Observation( content=conclusion, metadata=explicit_metadata, - created_at=datetime.now(), + created_at=datetime.now(timezone.utc), ) context.add_observation(obs, ReasoningLevel.EXPLICIT) @@ -169,7 +169,7 @@ class ObservationContext(BaseModel): obs = Observation( content=structured_obs.conclusion, metadata=deductive_metadata, - created_at=datetime.now(), + created_at=datetime.now(timezone.utc), ) context.add_observation(obs, level) diff --git a/tests/test_datetime_parsing.py b/tests/test_datetime_parsing.py new file mode 100644 index 00000000..69ac2059 --- /dev/null +++ b/tests/test_datetime_parsing.py @@ -0,0 +1,533 @@ +""" +Comprehensive tests for datetime parsing functionality in utils/formatting.py. + +This test suite covers edge cases for timezone handling, format validation, +security considerations, and integration with the filtering system. +""" + +from datetime import datetime, timedelta, timezone +from typing import Any + +import pytest + +from src.utils.filter import ( + _validate_datetime_string, # pyright: ignore[reportPrivateUsage] +) +from src.utils.formatting import ( + format_datetime_utc, + parse_datetime_iso, + utc_now_iso, +) + + +class TestParseDatetimeISO: + """Test parse_datetime_iso function with various input formats and edge cases.""" + + def test_parse_z_suffix_format(self): + """Test parsing ISO strings with Z suffix (UTC indicator).""" + test_cases = [ + "2023-01-01T12:00:00Z", + "2023-12-31T23:59:59Z", + "2000-02-29T00:00:00Z", # Leap year + "1970-01-01T00:00:00Z", # Unix epoch + "2023-01-01T12:00:00.123456Z", # With microseconds + ] + + for iso_string in test_cases: + result = parse_datetime_iso(iso_string) + assert result.tzinfo == timezone.utc + assert isinstance(result, datetime) + + def test_parse_plus_zero_format(self): + """Test parsing ISO strings with +00:00 timezone indicator.""" + test_cases = [ + "2023-01-01T12:00:00+00:00", + "2023-12-31T23:59:59+00:00", + "2023-01-01T12:00:00.123456+00:00", + ] + + for iso_string in test_cases: + result = parse_datetime_iso(iso_string) + assert result.tzinfo == timezone.utc + assert isinstance(result, datetime) + + def test_parse_without_timezone(self): + """Test parsing ISO strings without timezone (assumes UTC).""" + test_cases = [ + "2023-01-01T12:00:00", + "2023-01-01T12:00:00.123456", + ] + + for iso_string in test_cases: + result = parse_datetime_iso(iso_string) + assert result.tzinfo == timezone.utc + assert isinstance(result, datetime) + + def test_equivalency_between_formats(self): + """Test that Z and +00:00 formats produce equivalent results.""" + base_time = "2023-01-01T12:00:00" + + result_z = parse_datetime_iso(base_time + "Z") + result_plus = parse_datetime_iso(base_time + "+00:00") + result_none = parse_datetime_iso(base_time) + + assert result_z == result_plus == result_none + assert all( + r.tzinfo == timezone.utc for r in [result_z, result_plus, result_none] + ) + + def test_microseconds_precision(self): + """Test handling of microseconds precision.""" + test_cases = [ + ("2023-01-01T12:00:00.000001Z", 1), # 1 microsecond + ("2023-01-01T12:00:00.123456Z", 123456), # Full precision + ("2023-01-01T12:00:00.999999Z", 999999), # Max microseconds + ] + + for iso_string, expected_microseconds in test_cases: + result = parse_datetime_iso(iso_string) + assert result.microsecond == expected_microseconds + + def test_edge_case_dates(self): + """Test parsing of edge case dates.""" + edge_cases = [ + "1900-01-01T00:00:00Z", # Very old date + "2100-12-31T23:59:59Z", # Far future date + "2000-02-29T12:00:00Z", # Leap year Feb 29 + "1999-02-28T12:00:00Z", # Non-leap year Feb 28 + "2023-01-01T00:00:00Z", # Start of day + "2023-12-31T23:59:59Z", # End of day + ] + + for iso_string in edge_cases: + result = parse_datetime_iso(iso_string) + assert result.tzinfo == timezone.utc + assert isinstance(result, datetime) + + def test_invalid_formats_raise_errors(self): + """Test that invalid datetime formats raise ValueError.""" + invalid_cases = [ + "not-a-date", + "2023-13-01T12:00:00Z", # Invalid month + "2023-01-32T12:00:00Z", # Invalid day + "2023-01-01T25:00:00Z", # Invalid hour + "2023-01-01T12:60:00Z", # Invalid minute + "2023-01-01T12:00:60Z", # Invalid second + "2023/01/01T12:00:00Z", # Wrong date separator + "", # Empty string + ] + + for invalid_input in invalid_cases: + with pytest.raises(ValueError): + parse_datetime_iso(invalid_input) + + def test_none_and_non_string_inputs(self): + """Test that None and non-string inputs raise errors.""" + invalid_inputs: list[Any] = [None, 123, [], {}] + + for invalid_input in invalid_inputs: + with pytest.raises(ValueError): + parse_datetime_iso(invalid_input) + + def test_permissive_formats_accepted(self): + """Test that some permissive formats are actually accepted by fromisoformat.""" + permissive_cases = [ + "2023-01-01 12:00:00", # Space separator (valid in ISO 8601) + "2023-01-01T12:00:00", # No timezone (gets UTC added) + ] + + for permissive_input in permissive_cases: + result = parse_datetime_iso(permissive_input) + assert isinstance(result, datetime) + assert result.tzinfo == timezone.utc + + def test_malicious_injection_attempts(self): + """Test that potential SQL injection attempts in datetime strings fail safely.""" + malicious_inputs = [ + "2023-01-01'; DROP TABLE messages; --", + "2023-01-01T12:00:00Z' OR 1=1", + "2023-01-01T12:00:00Z'; SELECT * FROM users; --", + "2023-01-01T12:00:00Z' UNION SELECT password FROM auth", + "2023-01-01T12:00:00Z", + "2023-01-01T12:00:00Z\r\n", # CRLF injection + "2023-01-01T12:00:00Z\x00", # Null byte + ] + + for malicious_input in malicious_inputs: + with pytest.raises(ValueError): + parse_datetime_iso(malicious_input) + + def test_unicode_and_special_characters(self): + """Test handling of unicode and special characters.""" + # All suspicious unicode characters should be rejected + unicode_cases = [ + "2023-01-01T12:00:00Z\u200b", # Zero-width space + "2023-01-01T12:00:00Z\ufeff", # Byte order mark + "2023\u200001\u200001T12:00:00Z", # Zero-width space in date + "2023-01-01T12:00:00Z\u0000", # Null unicode + ] + + for unicode_input in unicode_cases: + with pytest.raises(ValueError): + parse_datetime_iso(unicode_input) + + def test_whitespace_handling(self): + """Test handling of leading/trailing whitespace.""" + # Valid whitespace should be stripped and accepted + whitespace_cases = [ + " 2023-01-01T12:00:00Z ", # Leading/trailing spaces + "\t2023-01-01T12:00:00Z\t", # Tabs + " 2023-01-01T12:00:00Z", # Leading space only + "2023-01-01T12:00:00Z ", # Trailing space only + ] + + for whitespace_input in whitespace_cases: + result = parse_datetime_iso(whitespace_input) + assert isinstance(result, datetime) + assert result.tzinfo == timezone.utc + + # Invalid whitespace with newlines should be rejected + invalid_whitespace_cases = [ + "\n2023-01-01T12:00:00Z\n", # Newlines + "2023-01-01T12:00:00Z\r", # Carriage return + "2023-01-01T12:00:00Z\r\n", # CRLF + ] + + for invalid_input in invalid_whitespace_cases: + with pytest.raises(ValueError): + parse_datetime_iso(invalid_input) + + def test_timezone_offset_formats(self): + """Test parsing various timezone offset formats.""" + timezone_cases = [ + ("2023-01-01T12:00:00-05:00", timedelta(hours=-5)), # EST + ("2023-01-01T12:00:00+05:30", timedelta(hours=5, minutes=30)), # IST + ("2023-01-01T12:00:00-08:00", timedelta(hours=-8)), # PST + ("2023-01-01T12:00:00+00:00", timedelta(0)), # UTC explicit + ] + + for iso_string, expected_offset in timezone_cases: + result = parse_datetime_iso(iso_string) + assert isinstance(result, datetime) + assert result.tzinfo is not None + assert result.utcoffset() == expected_offset + + +class TestFormatDatetimeUTC: + """Test format_datetime_utc function for consistent output formatting.""" + + def test_format_utc_datetime(self): + """Test formatting UTC datetime objects.""" + dt = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc) + result = format_datetime_utc(dt) + assert result == "2023-01-01T12:00:00Z" + + def test_format_naive_datetime_assumes_utc(self): + """Test that naive datetimes are assumed to be UTC.""" + dt = datetime(2023, 1, 1, 12, 0, 0) # No timezone + result = format_datetime_utc(dt) + assert result == "2023-01-01T12:00:00Z" + + def test_format_non_utc_converts_to_utc(self): + """Test that non-UTC timezones are converted to UTC.""" + # EST is UTC-5 + est = timezone(timedelta(hours=-5)) + dt = datetime(2023, 1, 1, 7, 0, 0, tzinfo=est) # 7 AM EST + result = format_datetime_utc(dt) + assert result == "2023-01-01T12:00:00Z" # Should be 12 PM UTC + + def test_format_with_microseconds(self): + """Test formatting datetimes with microseconds.""" + dt = datetime(2023, 1, 1, 12, 0, 0, 123456, tzinfo=timezone.utc) + result = format_datetime_utc(dt) + assert result == "2023-01-01T12:00:00.123456Z" + + def test_roundtrip_consistency(self): + """Test that format -> parse -> format is consistent.""" + original_dt = datetime(2023, 1, 1, 12, 30, 45, 123456, tzinfo=timezone.utc) + + # Format to string + formatted = format_datetime_utc(original_dt) + + # Parse back to datetime + parsed = parse_datetime_iso(formatted) + + # Format again + reformatted = format_datetime_utc(parsed) + + assert formatted == reformatted + assert original_dt == parsed + + +class TestUTCNowISO: + """Test utc_now_iso function for current time generation.""" + + def test_returns_valid_iso_string(self): + """Test that utc_now_iso returns a valid ISO string.""" + result = utc_now_iso() + + # Should be parseable + parsed = parse_datetime_iso(result) + assert isinstance(parsed, datetime) + assert parsed.tzinfo == timezone.utc + + def test_returns_recent_time(self): + """Test that utc_now_iso returns a recent time (within last few seconds).""" + before = datetime.now(timezone.utc) + result_str = utc_now_iso() + after = datetime.now(timezone.utc) + + result = parse_datetime_iso(result_str) + + assert before <= result <= after + + def test_format_consistency(self): + """Test that utc_now_iso uses consistent Z format.""" + result = utc_now_iso() + assert result.endswith("Z") + assert "+00:00" not in result + + +class TestFilterDatetimeValidation: + """Test _validate_datetime_string function used in API filters.""" + + def test_validate_valid_formats(self): + """Test validation of valid datetime formats.""" + timezone_aware_cases = [ + "2023-01-01T12:00:00Z", + "2023-01-01T12:00:00+00:00", + "2023-01-01T12:00:00", + "2023-01-01 12:00:00", + ] + + for valid_input in timezone_aware_cases: + result = _validate_datetime_string(valid_input) + assert result is not None + assert isinstance(result, datetime) + assert result.tzinfo == timezone.utc # Should have UTC timezone + + # Date-only format should also be timezone-aware + date_only_result = _validate_datetime_string("2023-01-01") + assert date_only_result is not None + assert isinstance(date_only_result, datetime) + assert date_only_result.tzinfo == timezone.utc # Should assume UTC + + def test_validate_invalid_formats_return_none(self): + """Test that invalid formats return None rather than raising exceptions.""" + invalid_cases = [ + "not-a-date", + "2023-13-01", # Invalid month + "2023-01-32", # Invalid day + "", + "2023-01-01'; DROP TABLE messages; --", # SQL injection attempt + ] + + for invalid_input in invalid_cases: + result = _validate_datetime_string(invalid_input) + assert result is None + + def test_validate_strips_whitespace(self): + """Test that validation strips leading/trailing whitespace.""" + whitespace_input = " 2023-01-01T12:00:00Z " + result = _validate_datetime_string(whitespace_input) + assert result is not None + assert isinstance(result, datetime) + + def test_validate_security_against_injection(self): + """Test that validation protects against injection attacks.""" + injection_attempts = [ + "2023-01-01'; DROP TABLE messages; --", + "2023-01-01 OR 1=1", + "'; SELECT * FROM users; --", + "2023-01-01' UNION SELECT password FROM auth", + ] + + for injection_attempt in injection_attempts: + result = _validate_datetime_string(injection_attempt) + assert result is None # Should reject malicious input + + +class TestDatetimeEdgeCasesIntegration: + """Integration tests for datetime handling across the system.""" + + def test_leap_year_handling(self): + """Test handling of leap year dates.""" + leap_year_cases = [ + "2000-02-29T12:00:00Z", # Leap year (divisible by 400) + "2004-02-29T12:00:00Z", # Leap year (divisible by 4) + "1900-02-28T12:00:00Z", # Not leap year (divisible by 100, not 400) + ] + + for case in leap_year_cases: + result = parse_datetime_iso(case) + assert isinstance(result, datetime) + + # Should be able to format back + formatted = format_datetime_utc(result) + assert isinstance(formatted, str) + + def test_daylight_saving_transition(self): + """Test handling of daylight saving time transitions.""" + # Create datetime during DST transition (these are complex edge cases) + # Spring forward: 2023-03-12 2:00 AM EST becomes 3:00 AM EDT + # Fall back: 2023-11-05 2:00 AM EDT becomes 1:00 AM EST + + # Since our system normalizes to UTC, DST transitions shouldn't affect us + # But we should test edge cases around the transition times + spring_forward = "2023-03-12T07:00:00Z" # 2 AM EST = 7 AM UTC + fall_back = "2023-11-05T06:00:00Z" # 1 AM EST = 6 AM UTC + + for transition_time in [spring_forward, fall_back]: + result = parse_datetime_iso(transition_time) + assert result.tzinfo == timezone.utc + + def test_year_boundary_handling(self): + """Test handling of year boundary dates.""" + boundary_cases = [ + "1999-12-31T23:59:59Z", # Last second of millennium + "2000-01-01T00:00:00Z", # First second of millennium + "2023-12-31T23:59:59.999999Z", # Last microsecond of year + "2024-01-01T00:00:00.000000Z", # First microsecond of year + ] + + for boundary_case in boundary_cases: + result = parse_datetime_iso(boundary_case) + assert isinstance(result, datetime) + assert result.tzinfo == timezone.utc + + def test_time_precision_limits(self): + """Test handling of time precision edge cases.""" + precision_cases = [ + "2023-01-01T12:00:00.000000Z", # No microseconds + "2023-01-01T12:00:00.000001Z", # Minimum microseconds + "2023-01-01T12:00:00.999999Z", # Maximum microseconds + "2023-01-01T12:00:00.123Z", # Partial microseconds (should pad) + ] + + for precision_case in precision_cases: + result = parse_datetime_iso(precision_case) + assert isinstance(result, datetime) + + # Should be able to format and parse back consistently + formatted = format_datetime_utc(result) + reparsed = parse_datetime_iso(formatted) + assert result == reparsed + + def test_extreme_date_values(self): + """Test handling of extreme date values within reasonable bounds.""" + # Python datetime has limits: 1-01-01 to 9999-12-31 + extreme_cases = [ + "0001-01-01T00:00:00Z", # Minimum year + "9999-12-31T23:59:59Z", # Maximum year + "1970-01-01T00:00:00Z", # Unix epoch start + "2038-01-19T03:14:07Z", # 32-bit timestamp limit (2^31 - 1 seconds) + ] + + for extreme_case in extreme_cases: + result = parse_datetime_iso(extreme_case) + assert isinstance(result, datetime) + assert result.tzinfo == timezone.utc + + def test_consistency_across_functions(self): + """Test that all datetime functions work consistently together.""" + # Generate a current time + now_str = utc_now_iso() + + # Parse it + now_dt = parse_datetime_iso(now_str) + + # Format it back + reformatted = format_datetime_utc(now_dt) + + # Should be parseable by filter validation + validated = _validate_datetime_string(reformatted) + + assert validated is not None + assert validated.tzinfo == timezone.utc + assert abs((validated - now_dt).total_seconds()) < 1 # Should be very close + + +class TestTimezoneHandlingEdgeCases: + """Test edge cases specifically related to timezone handling.""" + + def test_timezone_info_preservation(self): + """Test that timezone information is correctly handled and converted.""" + # Test various timezone inputs + utc_dt = datetime(2023, 1, 1, 12, 0, 0, tzinfo=timezone.utc) + naive_dt = datetime(2023, 1, 1, 12, 0, 0) # No timezone + + # Both should format to the same UTC string when naive assumes UTC + utc_formatted = format_datetime_utc(utc_dt) + naive_formatted = format_datetime_utc(naive_dt) + + assert utc_formatted == naive_formatted == "2023-01-01T12:00:00Z" + + def test_non_utc_timezone_conversion(self): + """Test conversion from non-UTC timezones.""" + # Create a datetime in EST (UTC-5) + est = timezone(timedelta(hours=-5)) + est_dt = datetime(2023, 1, 1, 7, 0, 0, tzinfo=est) + + # When formatted as UTC, should be 5 hours later + utc_formatted = format_datetime_utc(est_dt) + assert utc_formatted == "2023-01-01T12:00:00Z" + + # When parsed back, should be equivalent UTC time + parsed_utc = parse_datetime_iso(utc_formatted) + assert parsed_utc.tzinfo == timezone.utc + assert parsed_utc.hour == 12 # 7 AM EST = 12 PM UTC + + def test_timezone_edge_cases_around_midnight(self): + """Test timezone conversions that cross date boundaries.""" + # 1 AM EST on Jan 1 = 6 AM UTC on Jan 1 + est = timezone(timedelta(hours=-5)) + est_dt = datetime(2023, 1, 1, 1, 0, 0, tzinfo=est) + utc_formatted = format_datetime_utc(est_dt) + assert utc_formatted == "2023-01-01T06:00:00Z" + + # 11 PM EST on Dec 31 = 4 AM UTC on Jan 1 (crosses date boundary) + est_dt_late = datetime(2022, 12, 31, 23, 0, 0, tzinfo=est) + utc_formatted_late = format_datetime_utc(est_dt_late) + assert utc_formatted_late == "2023-01-01T04:00:00Z" # Next day in UTC + + +class TestErrorHandlingAndRecovery: + """Test error handling and graceful recovery scenarios.""" + + def test_partial_datetime_parsing_fallback(self): + """Test fallback behavior for partial datetime strings.""" + # The filter validation function has fallback formats + time_cases = [ + "2023-01-01 12:00:00", # Space separator + "2023-01-01T12:00:00", # No timezone + ] + + for partial_case in time_cases: + result = _validate_datetime_string(partial_case) + assert result is not None + assert result.tzinfo == timezone.utc # Should assume UTC + + # Date-only should also be timezone-aware with UTC assumption + date_result = _validate_datetime_string("2023-01-01") + assert date_result is not None + assert date_result.tzinfo == timezone.utc # Should assume UTC + + def test_graceful_degradation_on_errors(self): + """Test that datetime parsing fails gracefully without crashing.""" + error_cases: list[Any] = [ + None, + 123, # Not a string + [], # Wrong type + {}, # Wrong type + ] + + for error_case in error_cases: + # parse_datetime_iso should raise appropriate errors + if error_case is None or not isinstance(error_case, str): + with pytest.raises(ValueError): + parse_datetime_iso(error_case) + + # Filter validation should return None for invalid types + if isinstance(error_case, str) or error_case is None: + result = _validate_datetime_string(error_case or "") + assert result is None