Skip to content

Commit 80518c2

Browse files
committed
Increase coverage
1 parent 115d030 commit 80518c2

File tree

4 files changed

+563
-0
lines changed

4 files changed

+563
-0
lines changed

tests/management/test_audit.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,110 @@ def test_create_event(self):
131131
verify=True,
132132
timeout=DEFAULT_TIMEOUT_SECONDS,
133133
)
134+
135+
def test_search_all_params_and_defaults(self):
136+
client = DescopeClient(
137+
self.dummy_project_id,
138+
self.public_key_dict,
139+
False,
140+
self.dummy_management_key,
141+
)
142+
143+
from_ts = datetime.fromtimestamp(1000)
144+
to_ts = datetime.fromtimestamp(2000)
145+
146+
with patch("requests.post") as mock_post:
147+
network_resp = mock.Mock()
148+
network_resp.ok = True
149+
# Include one record with missing fields to exercise defaults in converter
150+
network_resp.json.return_value = {
151+
"audits": [
152+
{
153+
"projectId": "p",
154+
"userId": "u",
155+
"action": "a",
156+
"externalIds": ["x"],
157+
"occurred": str(datetime.now().timestamp() * 1000),
158+
"device": "Desktop",
159+
"method": "otp",
160+
"geo": "US",
161+
"remoteAddress": "1.1.1.1",
162+
"tenants": ["t1"],
163+
"data": {"k": "v"},
164+
},
165+
{},
166+
]
167+
}
168+
mock_post.return_value = network_resp
169+
170+
resp = client.mgmt.audit.search(
171+
user_ids=["u1"],
172+
actions=["a1"],
173+
excluded_actions=["a0"],
174+
devices=["Mobile"],
175+
methods=["totp"],
176+
geos=["IL"],
177+
remote_addresses=["2.2.2.2"],
178+
login_ids=["e1"],
179+
tenants=["t"],
180+
no_tenants=True,
181+
text="hello",
182+
from_ts=from_ts,
183+
to_ts=to_ts,
184+
)
185+
186+
audits = resp["audits"]
187+
assert len(audits) == 2
188+
# Defaults for the second empty record
189+
assert audits[1]["projectId"] == ""
190+
assert audits[1]["userId"] == ""
191+
assert audits[1]["action"] == ""
192+
assert audits[1]["loginIds"] == []
193+
assert audits[1]["tenants"] == []
194+
assert audits[1]["data"] == {}
195+
196+
# Verify request body composed with all params
197+
args, kwargs = mock_post.call_args
198+
assert args[0] == f"{common.DEFAULT_BASE_URL}{MgmtV1.audit_search}"
199+
sent = kwargs["json"]
200+
assert sent["userIds"] == ["u1"]
201+
assert sent["actions"] == ["a1"]
202+
assert sent["excludedActions"] == ["a0"]
203+
assert sent["devices"] == ["Mobile"]
204+
assert sent["methods"] == ["totp"]
205+
assert sent["geos"] == ["IL"]
206+
assert sent["remoteAddresses"] == ["2.2.2.2"]
207+
assert sent["externalIds"] == ["e1"]
208+
assert sent["tenants"] == ["t"]
209+
assert sent["noTenants"] is True
210+
assert sent["text"] == "hello"
211+
assert sent["from"] == int(from_ts.timestamp() * 1000)
212+
assert sent["to"] == int(to_ts.timestamp() * 1000)
213+
214+
def test_create_event_minimal(self):
215+
client = DescopeClient(
216+
self.dummy_project_id,
217+
self.public_key_dict,
218+
False,
219+
self.dummy_management_key,
220+
)
221+
222+
with patch("requests.post") as mock_post:
223+
network_resp = mock.Mock()
224+
network_resp.ok = True
225+
network_resp.json.return_value = {}
226+
mock_post.return_value = network_resp
227+
client.mgmt.audit.create_event(
228+
action="a",
229+
type="info",
230+
actor_id="aid",
231+
tenant_id="tid",
232+
)
233+
# Only the mandatory keys should appear in json body
234+
sent = mock_post.call_args.kwargs["json"]
235+
assert sent == {
236+
"action": "a",
237+
"type": "info",
238+
"actorId": "aid",
239+
"tenantId": "tid",
240+
}

0 commit comments

Comments
 (0)