[test] Add tests for cmd.resolveGuardPolicyOverride#2171
Draft
github-actions[bot] wants to merge 1 commit intomainfrom
Draft
[test] Add tests for cmd.resolveGuardPolicyOverride#2171github-actions[bot] wants to merge 1 commit intomainfrom
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
Adds comprehensive unit tests for the resolveGuardPolicyOverride function in internal/cmd/root.go. This function had 0% direct test coverage despite being a critical security function that determines which guard policy to apply based on CLI flags and environment variables. Tests cover all 6+ execution branches: - No override (nil returned when no flags or env vars set) - CLI --guard-policy-json with valid JSON (source=cli) - CLI --guard-policy-json with invalid JSON (error path) - CLI --guard-policy-json with whitespace only (falls through to AllowOnly) - CLI --allowonly-scope-public with min-integrity - CLI --allowonly-scope-owner with optional repo - CLI repo without owner validation error - CLI min-integrity only without scope (error path) - CLI invalid min-integrity value (error path) - Env MCP_GATEWAY_GUARD_POLICY_JSON valid (source=env) - Env MCP_GATEWAY_GUARD_POLICY_JSON invalid (error path) - Env MCP_GATEWAY_GUARD_POLICY_JSON whitespace (treated as unset) - Env AllowOnly scope variants (public, owner, owner+repo) - Env repo without owner error path - Env min-integrity only without scope error path - CLI takes precedence over env vars - MCP_GATEWAY_GUARD_POLICY_JSON takes precedence over AllowOnly env vars Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
4 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Test Coverage Improvement:
resolveGuardPolicyOverrideFunction Analyzed
internal/cmdresolveGuardPolicyOverrideWhy This Function?
resolveGuardPolicyOverrideininternal/cmd/root.gois a critical security function that determines which guard policy to apply at startup. It had zero direct test coverage despite having multiple complex branches that interact with:--guard-policy-json,--allowonly-scope-*,--allowonly-min-integrity)MCP_GATEWAY_GUARD_POLICY_JSON,MCP_GATEWAY_ALLOWONLY_*)config.ParseGuardPolicyJSONandconfig.BuildAllowOnlyPolicylogicIncorrectly resolved guard policies could silently weaken security enforcement (DIFC), making this a high-value test target.
Tests Added
--guard-policy-jsonwith valid JSON → source="cli"--guard-policy-jsonwith invalid JSON → error path--guard-policy-jsonwith whitespace-only → falls through to AllowOnly path--allowonly-scope-public+ min-integrity → AllowOnly policy with repos="public"--allowonly-scope-owner+ min-integrity → AllowOnly policy with owner scope--allowonly-scope-owner+--allowonly-scope-repo→ combined scopeMCP_GATEWAY_GUARD_POLICY_JSONvalid → source="env"MCP_GATEWAY_GUARD_POLICY_JSONinvalid → errorMCP_GATEWAY_GUARD_POLICY_JSONwhitespace-only → treated as unset, skippedMCP_GATEWAY_ALLOWONLY_SCOPE_PUBLIC→ AllowOnly policy from envMCP_GATEWAY_ALLOWONLY_SCOPE_OWNER+ min-integrity → AllowOnly policyMCP_GATEWAY_ALLOWONLY_SCOPE_OWNER+SCOPE_REPO→ combined scopeMCP_GATEWAY_GUARD_POLICY_JSONtakes precedence over AllowOnly env varsTest Design
The tests use a
makeGuardPolicyTestCmd()helper that:cobra.Commandwith all 5 guard-policy flags registeredcmd.Flags().Set(name, value)to simulate CLI flag changes (which marks flags asChanged())This accurately mirrors how cobra processes real CLI input, ensuring
cmd.Flags().Changed()returns correct values.Generated by Test Coverage Improver
Next run could target:
server.resolveGuardPolicy(guards config path),mcp.initializeHTTPSession(error branches)Warning
The following domains were blocked by the firewall during workflow execution:
proxy.golang.orgreleaseassets.githubusercontent.comTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.