Skip to content

Commit

Permalink
Merge pull request #60 from censys/logbook-bugfix
Browse files Browse the repository at this point in the history
fix: fetch subsequent logbook pages correctly
  • Loading branch information
mbremerkamp authored Feb 11, 2021
2 parents 5bd7548 + 6fbe754 commit d27e95a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions censys/asm/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def _get_logbook_page(
while not end_of_events:
res = self._get(path, args=args)
end_of_events = res["endOfEvents"]
args = {"cursor": res["nextCursor"]}

for event in res["events"]:
yield event
2 changes: 1 addition & 1 deletion censys/asm/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_cursor(

def get_events(self, cursor: Optional[str] = None) -> Generator[dict, None, None]:
"""
Requests a logbook cursor.
Requests logbook events from inception or from the provided cursor.
Args:
cursor (str, optional): Logbook cursor.
Expand Down
11 changes: 9 additions & 2 deletions tests/asm/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
EVENTS_RESOURCE_TYPE = "events"

TEST_CURSOR = "eyJmaWx0ZXIiOnt9LCJzdGFydCI6MH0"
TEST_NEXT_CURSOR = "eyJmaWx0ZXIiOnt9LCJzdGFydCI6MjA3MTJ9"
TEST_START_DATE = "2020-10-29T19:26:34.371Z"
TEST_START_ID = 20712

Expand Down Expand Up @@ -108,9 +109,12 @@ def test_get_all_events(self, mock):
res = [event for event in events]

self.assertEqual(RESOURCE_PAGING_RESULTS, res)
mock.assert_called_with(
mock.assert_any_call(
EVENTS_URL, params={"cursor": None}, timeout=TEST_TIMEOUT
)
mock.assert_any_call(
EVENTS_URL, params={"cursor": TEST_NEXT_CURSOR}, timeout=TEST_TIMEOUT
)

@patch("censys.base.requests.Session.get")
def test_get_events_with_cursor(self, mock):
Expand All @@ -119,9 +123,12 @@ def test_get_events_with_cursor(self, mock):
res = [event for event in events]

self.assertEqual(RESOURCE_PAGING_RESULTS, res)
mock.assert_called_with(
mock.assert_any_call(
EVENTS_URL, params={"cursor": TEST_CURSOR}, timeout=TEST_TIMEOUT
)
mock.assert_any_call(
EVENTS_URL, params={"cursor": TEST_NEXT_CURSOR}, timeout=TEST_TIMEOUT
)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion tests/asm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def __init__(self, status_code, resource_type, page_number=1):
resource_type: self.get_resource(), # Return dummy resource generator
"pageNumber": 1,
"totalPages": 3, # Default to 3 pages of resources
"cursor": "eyJmaWx0ZXIiOnt9LCJzdGFydCI6MjA3MTJ9",
"nextCursor": "eyJmaWx0ZXIiOnt9LCJzdGFydCI6MjA3MTJ9",
"cursor": "eyJmaWx0ZXIiOnt9LCJzdGFydCI6MH0",
"endOfEvents": False,
}

Expand Down

0 comments on commit d27e95a

Please sign in to comment.