Skip to content

test: add comprehensive Vitest unit test coverage for allowlist and sql-macros (#71) - #351

Open
Gubaarev wants to merge 1 commit into
outerbase:mainfrom
Gubaarev:test/coverage-allowlist-and-macros
Open

Gubaarev wants to merge 1 commit into
outerbase:mainfrom
Gubaarev:test/coverage-allowlist-and-macros

Conversation

@Gubaarev

Copy link
Copy Markdown

Summary

This PR adds 21 focused, meaningful Vitest unit tests covering two previously untested modules in the codebase:

  1. src/allowlist/index.ts (Query Allowlist security feature)
  2. plugins/sql-macros/index.ts (SQL Macros plugin)

Closes #71
/claim #71

Test Coverage Details

1. src/allowlist/index.test.ts (8 tests)

  • Feature Toggle & Administrative Bypass:
    • Immediate true return when isEnabled is false without calling database.
    • Administrative bypass when config.role === "admin".
  • Validation & Edge Cases:
    • Proper return of Error("No SQL provided for allowlist check") when input SQL is empty.
    • Graceful handling of loadAllowlist database failure and rejection fallback.
  • AST Matching & Normalization:
    • Exact match testing with trailing semicolon normalization (normalizeSQL).
    • AST structural match testing with WHERE clause conditions.
    • Audit logging of rejected queries into tmp_allowlist_rejections via addRejectedQuery.
    • Resilience against addRejectedQuery database insertion failure.

2. plugins/sql-macros/index.test.ts (13 tests)

  • Initialization & Registration:
    • Plugin name starbasedb:sql-macros and options setup.
    • Middleware registration and config capturing from StarbaseContext.
  • preventSelectStar:
    • Rejection of SELECT * for non-admin users with specific error message.
    • Allowance of explicit column queries.
    • Administrative bypass for config.role === "admin".
    • Pass-through for non-SELECT statements (INSERT/UPDATE/DELETE).
    • Syntax error resilience returning original SQL.
  • $_exclude Column Macro:
    • Pass-through when dataSource is missing or non-internal.
    • Pass-through for pragma_table_info queries.
    • Replacement of $_exclude with remaining table schema columns for single excluded column.
    • Replacement of $_exclude for multiple excluded columns.
    • Normal query pass-through without $_exclude.

Verification

  • Run pnpm test src/allowlist/index.test.ts plugins/sql-macros/index.test.ts: 21 / 21 tests passed (100%).
  • Checked formatting with pnpm exec prettier --check.
  • Zero modifications to production source code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve test coverage with Vitest

1 participant