Skip to content

Commit 7edef74

Browse files
when querying sessions, properly use timestamp in milliseconds
1 parent c44731e commit 7edef74

2 files changed

Lines changed: 99 additions & 5 deletions

File tree

src/_incydr_sdk/sessions/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def get_page(
7676

7777
# Parse timestamps
7878
if start_time and not isinstance(start_time, (int, float)):
79-
start_time = parse_ts_to_posix_ts(start_time)
79+
start_time = parse_ts_to_posix_ts(start_time) * 1000
8080
if end_time and not isinstance(end_time, (int, float)):
81-
end_time = parse_ts_to_posix_ts(end_time)
81+
end_time = parse_ts_to_posix_ts(end_time) * 1000
8282

8383
if states and not isinstance(states, List):
8484
states = [states]
@@ -253,9 +253,9 @@ def update_state_by_criteria(
253253

254254
# Parse timestamps
255255
if start_time and not isinstance(start_time, (int, float)):
256-
start_time = parse_ts_to_posix_ts(start_time)
256+
start_time = parse_ts_to_posix_ts(start_time) * 1000
257257
if end_time and not isinstance(end_time, (int, float)):
258-
end_time = parse_ts_to_posix_ts(end_time)
258+
end_time = parse_ts_to_posix_ts(end_time) * 1000
259259

260260
if states and not isinstance(states, List):
261261
states = [states]

tests/test_sessions.py

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222

2323
TEST_SESSION_ID = "123-session-1"
2424
DATETIME_INSTANT = datetime(2024, 1, 1, tzinfo=timezone.utc)
25-
POSIX_TS = int(DATETIME_INSTANT.timestamp())
25+
POSIX_TS = int(DATETIME_INSTANT.timestamp()) * 1000
26+
START_DATE = "2024-12-19"
27+
START_TIMESTAMP = 1734566400000 # in ms
28+
END_DATE = "2024-12-20"
29+
END_TIMESTAMP = 1734652800000 # in ms
2630

2731
TEST_SESSION = {
2832
"actorId": TEST_SESSION_ID,
@@ -157,6 +161,49 @@ def test_get_page_when_custom_params_returns_expected_data(httpserver_auth: HTTP
157161
assert page.items[0].json() == json.dumps(TEST_SESSION)
158162
assert len(page.items) == 1 == page.total_count
159163

164+
def test_get_page_when_given_date_uses_correct_timestamp(httpserver_auth: HTTPServer):
165+
query = {
166+
"actor_id": "actor-id",
167+
"on_or_after": START_TIMESTAMP,
168+
"before": END_TIMESTAMP,
169+
"has_alerts": "false",
170+
"risk_indicators": ["risk-indicator"],
171+
"state": ["OPEN"],
172+
"severity": [3],
173+
"rule_id": ["rule-id"],
174+
"watchlist_id": ["watchlist-id"],
175+
"content_inspection_status": "PENDING",
176+
"order_by": "score",
177+
"sort_direction": "desc",
178+
"page_number": 2,
179+
"page_size": 10,
180+
}
181+
sessions_page = {"items": [TEST_SESSION], "totalCount": 1}
182+
httpserver_auth.expect_request(
183+
"/v1/sessions", method="GET", query_string=urlencode(query, doseq=True)
184+
).respond_with_json(sessions_page)
185+
186+
client = Client()
187+
page = client.sessions.v1.get_page(
188+
actor_id="actor-id",
189+
start_time=START_DATE,
190+
end_time=END_DATE,
191+
has_alerts=False,
192+
sort_key=SortKeys.SCORE,
193+
risk_indicators=["risk-indicator"],
194+
sort_dir=SortDirection.DESC,
195+
states=SessionStates.OPEN,
196+
severities=3,
197+
rule_ids="rule-id",
198+
watchlist_ids="watchlist-id",
199+
page_num=2,
200+
page_size=10,
201+
content_inspection_status=ContentInspectionStatuses.PENDING,
202+
)
203+
assert isinstance(page, SessionsPage)
204+
assert page.items[0].json() == json.dumps(TEST_SESSION)
205+
assert len(page.items) == 1 == page.total_count
206+
160207

161208
def test_iter_all_when_default_params_returns_expected_data(
162209
httpserver_auth: HTTPServer,
@@ -356,6 +403,53 @@ def test_update_state_by_criteria_makes_expected_calls(httpserver_auth: HTTPServ
356403
for response in responses:
357404
assert response.status_code == 200
358405

406+
def test_update_state_by_criteria_when_given_date_uses_correct_timestamp(httpserver_auth: HTTPServer):
407+
query = {
408+
"actor_id": "actor-id",
409+
"on_or_after": START_TIMESTAMP,
410+
"before": END_TIMESTAMP,
411+
"has_alerts": "false",
412+
"risk_indicators": ["risk-indicator"],
413+
"state": ["OPEN"],
414+
"severity": [3],
415+
"rule_id": ["rule-id"],
416+
"watchlist_id": ["watchlist-id"],
417+
"content_inspection_status": "PENDING",
418+
}
419+
420+
token = "123-token"
421+
httpserver_auth.expect_request(
422+
"/v1/sessions/change-states",
423+
query_string=urlencode(query, doseq=True),
424+
method="POST",
425+
json={"continuationToken": None, "newState": "CLOSED"},
426+
).respond_with_json({"continuationToken": token})
427+
httpserver_auth.expect_request(
428+
"/v1/sessions/change-states",
429+
query_string=urlencode(query, doseq=True),
430+
method="POST",
431+
json={"continuationToken": token, "newState": "CLOSED"},
432+
).respond_with_json({"continuationToken": None})
433+
434+
client = Client()
435+
responses = client.sessions.v1.update_state_by_criteria(
436+
new_state=SessionStates.CLOSED,
437+
actor_id="actor-id",
438+
start_time=START_DATE,
439+
end_time=END_DATE,
440+
has_alerts=False,
441+
risk_indicators=["risk-indicator"],
442+
states=SessionStates.OPEN,
443+
severities=3,
444+
rule_ids="rule-id",
445+
watchlist_ids="watchlist-id",
446+
content_inspection_status=ContentInspectionStatuses.PENDING,
447+
)
448+
assert responses[0].json()["continuationToken"] == token
449+
assert responses[1].json()["continuationToken"] is None
450+
for response in responses:
451+
assert response.status_code == 200
452+
359453

360454
# ************************************************ CLI ************************************************
361455

0 commit comments

Comments
 (0)