Skip to content

Repoint handler tests at the WithSecurity constructors - #396

Open
dpage wants to merge 1 commit into
mainfrom
repoint-handler-tests
Open

Repoint handler tests at the WithSecurity constructors#396
dpage wants to merge 1 commit into
mainfrom
repoint-handler-tests

Conversation

@dpage

@dpage dpage commented Aug 7, 2026

Copy link
Copy Markdown
Member

The follow-up to #395, covering the one piece I deliberately left out of
the mechanical pass.

Why this needed its own PR

NewConnectionHandler and NewNotificationChannelHandler are
unreachable from the server binary: cmd/mcp-server/handlers.go wires
both handlers through the ...WithSecurity variants. They survived #395
because 49 test call sites still used the shorter forms, and deleting
the constructors there would have taken out test suites covering live
handler code, which is exactly the failure mode #395 was tightened to
avoid.

Why the rewrite is safe

The two constructor pairs are identical apart from a single line:

  • short form: hostValidator: DefaultHostValidator()
  • WithSecurity: hostValidator: NewHostValidator(allowInternal, allowedHosts, blockedHosts)

and DefaultHostValidator() is defined as exactly:

func DefaultHostValidator() *HostValidator {
    return NewHostValidator(false, nil, nil)
}

So rewriting NewConnectionHandler(a, b, c) to
NewConnectionHandlerWithSecurity(a, b, c, false, nil, nil) is
precisely equivalent. No test changes behaviour, and nothing needed
reinterpreting: every field the short constructor set is set identically
by the longer one. That is what makes this mechanical rather than a
judgement call, which is the thing I wasn't willing to assume without
checking.

With the short forms gone, DefaultHostValidator had no production
caller either, so it goes too. Its two incidental uses in
host_validation_test.go (in TestHostValidator_ValidatePort and
TestHostValidator_InternalNetworksList, which use it only as a
convenient default) now call NewHostValidator(false, nil, nil)
directly. TestDefaultHostValidator is removed along with the function
it existed to test.

Scope

Lines
Production Go removed 31 (3 functions)
Tests −67 / +51 (net −16)
Changelog +12

49 call sites rewritten across 7 test files. No test was deleted except
TestDefaultHostValidator.

Verification

Relationship to #395

Cut from main, not stacked on remove-dead-code, because a branch
based on a non-main branch triggers no CI workflows at all. I verified
the two changesets touch entirely disjoint files (52 files in #395,
11 here, zero overlap), so they can merge in either order without
conflict.

Still outstanding after this

The 42 remaining dead functions whose tests need editing rather than
deleting, and the three test helpers (NewTestClient,
NewTestDatastoreWithSecret, NewTestDatastore) that compile into the
shipped binaries but are used across package boundaries by tests, so
they want extracting into a test-only package rather than deleting.

Summary by CodeRabbit

  • Removed

    • Removed legacy connection and notification-channel constructors, along with the default host-validation helper.
    • These APIs are replaced by security-aware configuration options for handler creation and explicit host-validator configuration.
  • Documentation

    • Added an Unreleased changelog entry describing the removed APIs and migration path.
  • Tests

    • Updated validation, routing, authentication, and RBAC coverage to use the current security-aware configuration without changing expected behavior.

The follow-up to the dead-code removal. NewConnectionHandler and
NewNotificationChannelHandler are unreachable from the server binary,
because cmd/mcp-server wires both handlers through the WithSecurity
variants instead. They survived the mechanical pass because 49 test call
sites still used the shorter forms, and deleting them there would have
taken out tests covering live handler code.

The two constructor pairs are identical apart from one line: the short
form sets hostValidator to DefaultHostValidator(), and the WithSecurity
form sets it to NewHostValidator(allowInternal, allowedHosts,
blockedHosts). Since DefaultHostValidator() is defined as exactly
NewHostValidator(false, nil, nil), rewriting a three-argument call to
pass an additional (false, nil, nil) is precisely equivalent, and no
test changes behaviour as a result.

With the short forms gone, DefaultHostValidator had no production
caller either, so it goes too. Its two incidental uses in
host_validation_test.go now call NewHostValidator(false, nil, nil)
directly, and TestDefaultHostValidator is removed along with the
function it existed to test.

Server coverage is 55.5%, matching main exactly. The only failing tests
are the two pre-existing vector(3) fixture failures from #337, which
reproduce identically on unmodified main.

This branch is cut from main rather than stacked on the dead-code
branch, so CI runs against it. The two changesets touch entirely
disjoint files and can merge in either order.
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The change removes superseded handler constructors and DefaultHostValidator. Test call sites now use security-aware constructors or explicit host-validator configuration. The changelog records the removed APIs.

Changes

API constructor migration

Layer / File(s) Summary
Remove legacy APIs
server/src/internal/api/connection_handlers.go, server/src/internal/api/notification_channel_handlers.go, server/src/internal/api/host_validation.go, server/src/internal/api/host_validation_test.go, docs/changelog.md
Removes the superseded constructors and DefaultHostValidator. Validator tests construct NewHostValidator(false, nil, nil) directly.
Migrate connection handler tests
server/src/internal/api/connection_handlers_test.go, server/src/internal/api/issue269_connection_name_test.go, server/src/internal/api/query_handlers_test.go, server/src/internal/api/rbac_integration_test.go, server/src/internal/api/rbac_issue233_connections_test.go, server/src/internal/api/rbac_issue35_test.go
Updates connection-handler and RBAC tests to use NewConnectionHandlerWithSecurity with explicit security arguments.
Migrate notification handler tests
server/src/internal/api/notification_channel_handlers_test.go, server/src/internal/api/rbac_integration_test.go
Updates notification-channel tests to use NewNotificationChannelHandlerWithSecurity with explicit security arguments.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: updating handler tests to use the WithSecurity constructors.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch repoint-handler-tests

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics -6 complexity · 2 duplication

Metric Results
Complexity -6
Duplication 2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
server/src/internal/api/connection_handlers_test.go (1)

29-31: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Rename the migrated constructor test.

Line 29 now tests NewConnectionHandlerWithSecurity, but the test name and failure message still refer to the removed NewConnectionHandler. Use a unique name because TestNewConnectionHandlerWithSecurity already exists.

Proposed update
-func TestNewConnectionHandler(t *testing.T) {
+func TestNewConnectionHandlerWithSecurity_DefaultConfig(t *testing.T) {
 	handler := NewConnectionHandlerWithSecurity(nil, nil, nil, false, nil, nil)
 	if handler == nil {
-		t.Fatal("NewConnectionHandler returned nil")
+		t.Fatal("NewConnectionHandlerWithSecurity returned nil")
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/src/internal/api/connection_handlers_test.go` around lines 29 - 31,
Rename the migrated constructor test to a unique name that accurately identifies
NewConnectionHandlerWithSecurity, and update its nil-check failure message to
reference NewConnectionHandlerWithSecurity instead of the removed
NewConnectionHandler. Ensure it does not conflict with the existing
TestNewConnectionHandlerWithSecurity.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@server/src/internal/api/connection_handlers_test.go`:
- Around line 29-31: Rename the migrated constructor test to a unique name that
accurately identifies NewConnectionHandlerWithSecurity, and update its nil-check
failure message to reference NewConnectionHandlerWithSecurity instead of the
removed NewConnectionHandler. Ensure it does not conflict with the existing
TestNewConnectionHandlerWithSecurity.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f7c50f55-c8cf-43f7-83dc-2bb3b6a425dc

📥 Commits

Reviewing files that changed from the base of the PR and between 19c645d and 5279861.

📒 Files selected for processing (12)
  • docs/changelog.md
  • server/src/internal/api/connection_handlers.go
  • server/src/internal/api/connection_handlers_test.go
  • server/src/internal/api/host_validation.go
  • server/src/internal/api/host_validation_test.go
  • server/src/internal/api/issue269_connection_name_test.go
  • server/src/internal/api/notification_channel_handlers.go
  • server/src/internal/api/notification_channel_handlers_test.go
  • server/src/internal/api/query_handlers_test.go
  • server/src/internal/api/rbac_integration_test.go
  • server/src/internal/api/rbac_issue233_connections_test.go
  • server/src/internal/api/rbac_issue35_test.go
💤 Files with no reviewable changes (3)
  • server/src/internal/api/host_validation.go
  • server/src/internal/api/notification_channel_handlers.go
  • server/src/internal/api/connection_handlers.go

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.

1 participant