|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +// `mcpproxy access suggest` (#1700): the query it sends, what it prints, and |
| 12 | +// that it never applies anything. |
| 13 | + |
| 14 | +const suggestResponse = `{ |
| 15 | + "since":"2026-10-19T12:00:00Z","until":"2026-10-20T12:00:00Z","window":"24h0m0s", |
| 16 | + "blocked_calls":9,"already_fixed":1,"profile_removed":0,"non_profile_blocked":2,"unattributed":0, |
| 17 | + "suggestions":[ |
| 18 | + {"profile":"research","tool":"github:create_issue","server":"github","block_reason":"profile_tier","count":5, |
| 19 | + "tokens":["ci-bot","dev-bot"],"clients":[],"first_seen":"2026-10-20T08:00:00Z","last_seen":"2026-10-20T11:00:00Z", |
| 20 | + "suggestion":"allow","fixes":[{"step":"tier_cap","action":"allow_in_profile","target":"research","label":"Allow github:create_issue in research"}]}, |
| 21 | + {"profile":"research","tool":"github:search_code","server":"github","block_reason":"profile_unannotated","count":2, |
| 22 | + "tokens":["ci-bot"],"clients":[],"first_seen":"2026-10-20T08:00:00Z","last_seen":"2026-10-20T09:00:00Z", |
| 23 | + "suggestion":"classify","needs_input":["tier"],"note":"The tool declares no tier. Choose read, write or destructive; no tier is assumed.", |
| 24 | + "fixes":[{"step":"tier_cap","action":"classify_in_profile","target":"research","label":"Classify github:search_code in research"}]}, |
| 25 | + {"profile":"research","tool":"github:delete_repo","server":"github","block_reason":"profile_rule","count":1, |
| 26 | + "tokens":["ci-bot"],"clients":[],"first_seen":"2026-10-20T08:00:00Z","last_seen":"2026-10-20T08:00:00Z", |
| 27 | + "suggestion":"none","note":"Denied by an explicit rule in the profile. Edit tools.deny if this access is intended.","fixes":[]}, |
| 28 | + {"profile":"research","tool":"code_execution","block_reason":"profile_code_execution","count":1, |
| 29 | + "tokens":["ci-bot"],"clients":[],"first_seen":"2026-10-20T08:00:00Z","last_seen":"2026-10-20T08:00:00Z", |
| 30 | + "suggestion":"none","warning":"Setting code_execution on profile research lets scripts call every upstream tool the profile can reach.","fixes":[]} |
| 31 | + ], |
| 32 | + "patches":[{"profile":"research","changes":["tools.allow += github:create_issue"], |
| 33 | + "proposed":{"name":"research","servers":["github"],"max_tier":"read","tools":{"allow":["github:create_issue"]}}}] |
| 34 | +}` |
| 35 | + |
| 36 | +func TestAccessSuggest_DefaultQuery(t *testing.T) { |
| 37 | + rec := newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 38 | + _, _, err := runCLI(t, GetAccessCommand, "table", "suggest") |
| 39 | + require.NoError(t, err) |
| 40 | + q, _ := url.ParseQuery(rec.only(t).RawQuery) |
| 41 | + require.Equal(t, url.Values{"since": {"24h"}}, q, "the default window is 24h and no patch is asked for") |
| 42 | +} |
| 43 | + |
| 44 | +func TestAccessSuggest_FlagsMapToTheQuery(t *testing.T) { |
| 45 | + rec := newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 46 | + _, _, err := runCLI(t, GetAccessCommand, "table", "suggest", "--since", "7d", "--profile", "research", "--token", "ci-bot", "--patch") |
| 47 | + require.NoError(t, err) |
| 48 | + q, _ := url.ParseQuery(rec.only(t).RawQuery) |
| 49 | + require.Equal(t, url.Values{"since": {"7d"}, "profile": {"research"}, "token": {"ci-bot"}, "patch": {"true"}}, q) |
| 50 | +} |
| 51 | + |
| 52 | +func TestAccessSuggest_TableShowsCommandsNotesAndWarnings(t *testing.T) { |
| 53 | + newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 54 | + out, _, err := runCLI(t, GetAccessCommand, "table", "suggest") |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + last := time.Date(2026, 10, 20, 11, 0, 0, 0, time.UTC).Local().Format("2006-01-02 15:04") |
| 58 | + require.Contains(t, out, "Profile refusals in the last 24h0m0s: 9") |
| 59 | + require.Contains(t, out, "github:create_issue") |
| 60 | + require.Contains(t, out, "ci-bot,dev-bot") |
| 61 | + require.Contains(t, out, last) |
| 62 | + require.Contains(t, out, "mcpproxy profile update research --add-allow github:create_issue") |
| 63 | + // A tier is never filled in for the operator. |
| 64 | + require.Contains(t, out, "mcpproxy profile classify research github:search_code <read|write|destructive>") |
| 65 | + require.Contains(t, out, "Denied by an explicit rule") |
| 66 | + require.NotContains(t, out, "--add-allow github:delete_repo", "a deny rule is never offered for widening") |
| 67 | + require.Contains(t, out, "WARNING: Setting code_execution on profile research") |
| 68 | + require.Contains(t, out, "Not listed: 1 group(s) already reachable; 2 blocked call(s) not caused by a profile") |
| 69 | + require.NotContains(t, out, "Proposed profile", "the patch is opt-in") |
| 70 | +} |
| 71 | + |
| 72 | +func TestAccessSuggest_PatchPrintsTheProposedProfileDocument(t *testing.T) { |
| 73 | + newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 74 | + out, _, err := runCLI(t, GetAccessCommand, "table", "suggest", "--patch") |
| 75 | + require.NoError(t, err) |
| 76 | + require.Contains(t, out, "Proposed profile research (tools.allow += github:create_issue):") |
| 77 | + require.Contains(t, out, `"allow": [`) |
| 78 | + require.Contains(t, out, `"max_tier": "read"`) |
| 79 | +} |
| 80 | + |
| 81 | +func TestAccessSuggest_JSONIsData(t *testing.T) { |
| 82 | + newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 83 | + out, _, err := runCLI(t, GetAccessCommand, "json", "suggest") |
| 84 | + require.NoError(t, err) |
| 85 | + require.Equal(t, indented(t, suggestResponse), out) |
| 86 | +} |
| 87 | + |
| 88 | +func TestAccessSuggest_NothingToSuggest(t *testing.T) { |
| 89 | + newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(`{"window":"24h0m0s","suggestions":[],"blocked_calls":0}`)}) |
| 90 | + out, _, err := runCLI(t, GetAccessCommand, "table", "suggest") |
| 91 | + require.NoError(t, err) |
| 92 | + require.Contains(t, out, "Nothing to suggest.") |
| 93 | +} |
| 94 | + |
| 95 | +func TestAccessSuggest_RefusalIsExit1(t *testing.T) { |
| 96 | + newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": refuse(400, `{"success":false,"error":"invalid since \"soon\": use a duration such as 90m, 24h or 7d"}`)}) |
| 97 | + _, _, err := runCLI(t, GetAccessCommand, "table", "suggest", "--since", "soon") |
| 98 | + require.Error(t, err) |
| 99 | + require.Equal(t, ExitCodeGeneralError, classifyError(err)) |
| 100 | + require.Contains(t, err.Error(), "invalid since") |
| 101 | +} |
| 102 | + |
| 103 | +func TestAccessSuggest_OnlyReadsFromTheDaemon(t *testing.T) { |
| 104 | + rec := newRESTRecorder(t, map[string]cannedResponse{"GET /api/v1/access/suggest": okResp(suggestResponse)}) |
| 105 | + _, _, err := runCLI(t, GetAccessCommand, "table", "suggest", "--patch") |
| 106 | + require.NoError(t, err) |
| 107 | + for _, r := range rec.all() { |
| 108 | + require.Equal(t, "GET", r.Method, "suggest never writes a profile") |
| 109 | + } |
| 110 | +} |
0 commit comments