diff --git a/censys/asm/logbook.py b/censys/asm/logbook.py index d5f16bfc..b85e6bba 100644 --- a/censys/asm/logbook.py +++ b/censys/asm/logbook.py @@ -4,6 +4,8 @@ from collections.abc import Iterator from typing import Optional, Union +from censys.common.utils import format_rfc3339 + from .api import CensysAsmAPI @@ -14,13 +16,13 @@ class Logbook(CensysAsmAPI): def get_cursor( self, - start: Optional[Union[datetime.datetime, int]] = None, + start: Optional[Union[str, datetime.datetime, int]] = None, filters: Optional[list[str]] = None, ) -> str: """Requests a logbook cursor. Args: - start ([datetime.datetime, int]): Optional; Timestamp or event ID to begin searching. + start ([str, datetime.datetime, int]): Optional; Timestamp or event ID. Naive datetimes use UTC. filters (list): Optional; List of filters applied to logbook search results. Returns: @@ -71,13 +73,13 @@ class Filters: def format_data( - start: Optional[Union[datetime.datetime, int]] = None, + start: Optional[Union[str, datetime.datetime, int]] = None, filters: Optional[list[str]] = None, ) -> dict: """Formats cursor request data into a start date/id and filter list. Args: - start ([datetime.datetime, int]): Optional; Timestamp or event ID to begin searching. + start ([str, datetime.datetime, int]): Optional; Timestamp or event ID. Naive datetimes use UTC. filters (list): Optional; List of filters applied to logbook search results. Returns: @@ -89,6 +91,10 @@ def format_data( data["filter"] = {"type": filters} if isinstance(start, int): data["idFrom"] = start + elif isinstance(start, datetime.datetime): + if start.utcoffset() is not None: + start = start.astimezone(datetime.timezone.utc) + data["dateFrom"] = format_rfc3339(start) elif start: data["dateFrom"] = start diff --git a/tests/asm/test_events.py b/tests/asm/test_events.py index 4727f1df..900ac03e 100644 --- a/tests/asm/test_events.py +++ b/tests/asm/test_events.py @@ -1,10 +1,12 @@ +import datetime import unittest import pytest +import responses from pytest_mock import MockerFixture from censys.asm.client import AsmClient -from censys.asm.logbook import Filters +from censys.asm.logbook import Filters, Logbook from .utils import ( RESOURCE_PAGING_RESULTS, @@ -24,6 +26,33 @@ TEST_START_ID = 20712 +@pytest.mark.parametrize( + ("start", "expected"), + [ + (datetime.datetime(2020, 10, 29, 19, 26, 34, 371000), {"dateFrom": "2020-10-29T19:26:34.371000Z"}), + ( + datetime.datetime( + 2020, 10, 30, 0, 56, 34, 371000, tzinfo=datetime.timezone(datetime.timedelta(hours=5, minutes=30)) + ), + {"dateFrom": "2020-10-29T19:26:34.371000Z"}, + ), + (TEST_START_DATE, {"dateFrom": TEST_START_DATE}), + (0, {"idFrom": 0}), + (None, {}), + ], +) +@responses.activate +def test_logbook_cursor_serializes_start(start, expected): + body = {"filter": {"type": [Filters.HOST]}, **expected} + responses.add( + responses.POST, + EVENTS_CURSOR_URL, + json={"cursor": TEST_CURSOR}, + match=[responses.matchers.json_params_matcher(body)], + ) + assert Logbook(api_key="test-api-key").get_cursor(start=start, filters=[Filters.HOST]) == TEST_CURSOR + + class EventsUnitTests(unittest.TestCase): """Unit tests for Events API."""