Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions tests/test_audit_logs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from datetime import datetime

import pytest
from pydantic import ValidationError

from workos.audit_logs import AuditLogEvent, AuditLogs
from workos.exceptions import AuthenticationException, BadRequestException
from workos.types.audit_logs.audit_log_event_response import AuditLogEventResponse


class _TestSetup:
Expand Down Expand Up @@ -84,7 +86,9 @@ def test_succeeds(self, capture_and_mock_http_client_request):
"organization_id": organization_id,
"event": event,
}
assert response is None
assert response is not None
assert response.success is True
assert isinstance(response, AuditLogEventResponse)

def test_sends_idempotency_key(
self, mock_audit_log_event, capture_and_mock_http_client_request
Expand All @@ -104,7 +108,35 @@ def test_sends_idempotency_key(
)

assert request_kwargs["headers"]["idempotency-key"] == idempotency_key
assert response is None
assert response is not None
assert response.success is True

def test_auto_generates_idempotency_key(
self, mock_audit_log_event, capture_and_mock_http_client_request
):
"""Test that idempotency key is auto-generated when not provided."""
organization_id = "org_123456789"

request_kwargs = capture_and_mock_http_client_request(
self.http_client, {"success": True}, 200
)

response = self.audit_logs.create_event(
organization_id=organization_id,
event=mock_audit_log_event,
# No idempotency_key provided
)

# Assert header exists and has a non-empty value
assert "idempotency-key" in request_kwargs["headers"]
idempotency_key = request_kwargs["headers"]["idempotency-key"]
assert idempotency_key and idempotency_key.strip()
# Assert the auto-generated key has the correct prefix
assert idempotency_key.startswith("workos-python-")
# Assert the key has the expected UUID format after the prefix
assert len(idempotency_key) > len("workos-python-")
assert response is not None
assert response.success is True

def test_throws_unauthorized_exception(
self, mock_audit_log_event, mock_http_client_with_response
Expand Down Expand Up @@ -152,6 +184,63 @@ def test_throws_badrequest_excpetion(
== "Audit Log could not be processed due to missing or incorrect data."
)

def test_handles_missing_success_field(
self, mock_audit_log_event, mock_http_client_with_response
):
"""Test that schema validation fails when response is missing required fields."""
organization_id = "org_123456789"

# Mock response missing the 'success' field
mock_http_client_with_response(
self.http_client,
{}, # Empty response
200,
)

with pytest.raises(ValidationError):
self.audit_logs.create_event(
organization_id=organization_id,
event=mock_audit_log_event,
)

def test_handles_invalid_success_type(
self, mock_audit_log_event, mock_http_client_with_response
):
"""Test that schema validation fails when response has incorrect field types."""
organization_id = "org_123456789"

# Mock response with wrong type for 'success' field (non-coercible value)
mock_http_client_with_response(
self.http_client,
{"success": ["invalid", "list"]}, # List instead of boolean
200,
)

with pytest.raises(ValidationError):
self.audit_logs.create_event(
organization_id=organization_id,
event=mock_audit_log_event,
)

def test_handles_malformed_json_response(
self, mock_audit_log_event, mock_http_client_with_response
):
"""Test that schema validation fails when response is completely malformed."""
organization_id = "org_123456789"

# Mock response with unexpected structure
mock_http_client_with_response(
self.http_client,
{"unexpected": "data", "structure": 123},
200,
)

with pytest.raises(ValidationError):
self.audit_logs.create_event(
organization_id=organization_id,
event=mock_audit_log_event,
)

class TestCreateExport(_TestSetup):
def test_succeeds(self, mock_http_client_with_response):
organization_id = "org_123456789"
Expand Down
Loading