-
Notifications
You must be signed in to change notification settings - Fork 50
FEAT: Add session audit context API (set_audit_context / get_audit_context) #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kapilsamant
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
kapilsamant:feature/session-metadata-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+310
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| """ | ||
| Tests for the session metadata / auditing API (set_audit_context / get_audit_context). | ||
|
|
||
| Functions: | ||
| - test_set_and_get_audit_context: Set named fields and verify local cache. | ||
| - test_audit_context_server_roundtrip: Verify values are readable via SESSION_CONTEXT(). | ||
| - test_audit_context_extra_keys: Test arbitrary extra key-value pairs. | ||
| - test_audit_context_merge: Successive calls merge, not replace. | ||
| - test_audit_context_empty_call: Calling with no arguments is a no-op. | ||
| - test_audit_context_clear_value: Setting a key to "" clears it server-side. | ||
| - test_audit_context_read_only: read_only=True prevents subsequent changes. | ||
| - test_audit_context_closed_connection: Raises InterfaceError when connection is closed. | ||
| - test_audit_context_key_too_long: Raises ProgrammingError for oversized keys. | ||
| - test_audit_context_value_too_long: Raises ProgrammingError for oversized values. | ||
| - test_audit_context_non_string_value: Raises ProgrammingError for non-string values. | ||
| """ | ||
|
|
||
| import pytest | ||
| from mssql_python import connect | ||
| from mssql_python.exceptions import InterfaceError, ProgrammingError, DatabaseError | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def audit_conn(conn_str): | ||
| """Dedicated connection for audit context tests (module-scoped fixtures | ||
| would share session state, so we create a fresh connection per test).""" | ||
| conn = connect(conn_str) | ||
| yield conn | ||
| conn.close() | ||
|
|
||
|
|
||
| class TestAuditContext: | ||
| """Tests for Connection.set_audit_context / get_audit_context.""" | ||
|
|
||
| def test_set_and_get_audit_context(self, audit_conn): | ||
| """Named fields are reflected in the local cache.""" | ||
| audit_conn.set_audit_context( | ||
| application="BillingAPI", | ||
| module="InvoiceProcessor", | ||
| action="GenerateInvoice", | ||
| user_id="123", | ||
| ) | ||
| ctx = audit_conn.get_audit_context() | ||
| assert ctx["application_name"] == "BillingAPI" | ||
| assert ctx["module_name"] == "InvoiceProcessor" | ||
| assert ctx["action_name"] == "GenerateInvoice" | ||
| assert ctx["user_id"] == "123" | ||
|
|
||
| def test_audit_context_server_roundtrip(self, audit_conn): | ||
| """Values set via set_audit_context are readable with SESSION_CONTEXT().""" | ||
| audit_conn.set_audit_context(application="RoundTrip", user_id="42") | ||
| cursor = audit_conn.cursor() | ||
| try: | ||
| cursor.execute("SELECT SESSION_CONTEXT(N'application_name')") | ||
| row = cursor.fetchone() | ||
| assert row[0] == "RoundTrip" | ||
|
|
||
| cursor.execute("SELECT SESSION_CONTEXT(N'user_id')") | ||
| row = cursor.fetchone() | ||
| assert row[0] == "42" | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| def test_audit_context_extra_keys(self, audit_conn): | ||
| """Arbitrary extra keys are stored via sp_set_session_context.""" | ||
| audit_conn.set_audit_context(tenant_id="ACME", correlation_id="abc-def") | ||
| ctx = audit_conn.get_audit_context() | ||
| assert ctx["tenant_id"] == "ACME" | ||
| assert ctx["correlation_id"] == "abc-def" | ||
|
|
||
| # Verify server-side | ||
| cursor = audit_conn.cursor() | ||
| try: | ||
| cursor.execute("SELECT SESSION_CONTEXT(N'tenant_id')") | ||
| assert cursor.fetchone()[0] == "ACME" | ||
| finally: | ||
| cursor.close() | ||
|
|
||
| def test_audit_context_merge(self, audit_conn): | ||
| """Successive calls merge values, not replace.""" | ||
| audit_conn.set_audit_context(application="App1") | ||
| audit_conn.set_audit_context(module="Mod1") | ||
| ctx = audit_conn.get_audit_context() | ||
| assert ctx["application_name"] == "App1" | ||
| assert ctx["module_name"] == "Mod1" | ||
|
|
||
| def test_audit_context_overwrite(self, audit_conn): | ||
| """A second call with the same key overwrites the previous value.""" | ||
| audit_conn.set_audit_context(action="First") | ||
| audit_conn.set_audit_context(action="Second") | ||
| assert audit_conn.get_audit_context()["action_name"] == "Second" | ||
|
|
||
| def test_audit_context_empty_call(self, audit_conn): | ||
| """Calling with no arguments is a silent no-op.""" | ||
| audit_conn.set_audit_context() | ||
| assert audit_conn.get_audit_context() == {} | ||
|
|
||
| def test_audit_context_clear_value(self, audit_conn): | ||
| """Setting a key to '' clears it (sends NULL to the server).""" | ||
| audit_conn.set_audit_context(user_id="99") | ||
| audit_conn.set_audit_context(user_id="") | ||
| assert "user_id" not in audit_conn.get_audit_context() | ||
|
|
||
| def test_audit_context_read_only(self, audit_conn): | ||
| """read_only=True makes the key immutable for the session.""" | ||
| audit_conn.set_audit_context(action="Locked", read_only=True) | ||
| # Attempting to change a read-only key should raise a DatabaseError | ||
| # from SQL Server (error 15664). | ||
| with pytest.raises(DatabaseError): | ||
| audit_conn.set_audit_context(action="Changed") | ||
|
|
||
| def test_audit_context_closed_connection_set(self, audit_conn): | ||
| """set_audit_context raises InterfaceError on a closed connection.""" | ||
| audit_conn.close() | ||
| with pytest.raises(InterfaceError): | ||
| audit_conn.set_audit_context(application="X") | ||
|
|
||
| def test_audit_context_closed_connection_get(self, audit_conn): | ||
| """get_audit_context raises InterfaceError on a closed connection.""" | ||
| audit_conn.close() | ||
| with pytest.raises(InterfaceError): | ||
| audit_conn.get_audit_context() | ||
|
|
||
| def test_audit_context_key_too_long(self, audit_conn): | ||
| """Keys longer than 128 characters are rejected.""" | ||
| with pytest.raises(ProgrammingError): | ||
| audit_conn.set_audit_context(**{"x" * 200: "v"}) | ||
|
|
||
| def test_audit_context_value_too_long(self, audit_conn): | ||
| """Values longer than 4000 characters are rejected.""" | ||
| with pytest.raises(ProgrammingError): | ||
| audit_conn.set_audit_context(user_id="v" * 4001) | ||
|
|
||
| def test_audit_context_non_string_value(self, audit_conn): | ||
| """Non-string values are rejected with ProgrammingError.""" | ||
| with pytest.raises(ProgrammingError): | ||
| audit_conn.set_audit_context(user_id=123) # type: ignore[arg-type] | ||
|
kapilsamant marked this conversation as resolved.
|
||
|
|
||
| def test_get_audit_context_returns_copy(self, audit_conn): | ||
| """get_audit_context returns a copy, not the internal dict.""" | ||
| audit_conn.set_audit_context(application="Copy") | ||
| ctx = audit_conn.get_audit_context() | ||
| ctx["application_name"] = "Mutated" | ||
| assert audit_conn.get_audit_context()["application_name"] == "Copy" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.