|
| 1 | +/* |
| 2 | + * SonarQube CLI |
| 3 | + * Copyright (C) 2026 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +// Integration tests for `sonar hook claude-prompt-submit`. |
| 22 | +// Runs the actual binary with real stdin to exercise scanText (stdinData path) in process.ts. |
| 23 | +// |
| 24 | +// Note: hardcoded token below is an intentional test fixture for the secret scanner. |
| 25 | +// sonar-ignore-next-line S6769 |
| 26 | + |
| 27 | +import { afterEach, beforeEach, describe, expect, it } from 'bun:test'; |
| 28 | +import { TestHarness } from '../../harness'; |
| 29 | + |
| 30 | +// Hardcoded test token — intentional fixture for secret detection, not a real credential |
| 31 | +// sonar-ignore-next-line S6769 |
| 32 | +const GITHUB_TEST_TOKEN = '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'; |
| 33 | + |
| 34 | +// Unreachable server — binary handles connection-refused gracefully and proceeds with scan |
| 35 | +const FAKE_SERVER = 'http://localhost:19999'; |
| 36 | + |
| 37 | +describe('sonar hook claude-prompt-submit', () => { |
| 38 | + let harness: TestHarness; |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + harness = await TestHarness.create(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + await harness.dispose(); |
| 46 | + }); |
| 47 | + |
| 48 | + it( |
| 49 | + 'exits 0 and outputs block JSON when prompt contains a secret', |
| 50 | + async () => { |
| 51 | + harness.state().withSecretsBinaryInstalled(); |
| 52 | + harness.withAuth(FAKE_SERVER, 'fake-token'); |
| 53 | + |
| 54 | + const result = await harness.run('hook claude-prompt-submit', { |
| 55 | + stdin: JSON.stringify({ prompt: `my token is ${GITHUB_TEST_TOKEN}` }), |
| 56 | + }); |
| 57 | + |
| 58 | + expect(result.exitCode).toBe(0); |
| 59 | + const blockLine = result.stdout |
| 60 | + .split('\n') |
| 61 | + .map((l) => l.trim()) |
| 62 | + .find((l) => l.startsWith('{') && l.includes('"block"')); |
| 63 | + expect(blockLine).toBeDefined(); |
| 64 | + const output = JSON.parse(blockLine ?? '{}'); |
| 65 | + expect(output.decision).toBe('block'); |
| 66 | + expect(output.reason).toContain('secrets'); |
| 67 | + }, |
| 68 | + { timeout: 30000 }, |
| 69 | + ); |
| 70 | + |
| 71 | + it( |
| 72 | + 'exits 0 and outputs nothing when prompt contains no secrets', |
| 73 | + async () => { |
| 74 | + harness.state().withSecretsBinaryInstalled(); |
| 75 | + harness.withAuth(FAKE_SERVER, 'fake-token'); |
| 76 | + |
| 77 | + const result = await harness.run('hook claude-prompt-submit', { |
| 78 | + stdin: JSON.stringify({ prompt: 'please help me refactor this function' }), |
| 79 | + }); |
| 80 | + |
| 81 | + expect(result.exitCode).toBe(0); |
| 82 | + expect(result.stdout).not.toContain('"block"'); |
| 83 | + }, |
| 84 | + { timeout: 30000 }, |
| 85 | + ); |
| 86 | + |
| 87 | + it( |
| 88 | + 'exits 0 and outputs nothing when stdin is invalid JSON', |
| 89 | + async () => { |
| 90 | + harness.state().withSecretsBinaryInstalled(); |
| 91 | + harness.withAuth(FAKE_SERVER, 'fake-token'); |
| 92 | + |
| 93 | + const result = await harness.run('hook claude-prompt-submit', { |
| 94 | + stdin: 'not valid json {{', |
| 95 | + }); |
| 96 | + |
| 97 | + expect(result.exitCode).toBe(0); |
| 98 | + expect(result.stdout).not.toContain('"block"'); |
| 99 | + }, |
| 100 | + { timeout: 15000 }, |
| 101 | + ); |
| 102 | + |
| 103 | + it( |
| 104 | + 'exits 0 and outputs nothing when prompt field is absent', |
| 105 | + async () => { |
| 106 | + harness.state().withSecretsBinaryInstalled(); |
| 107 | + harness.withAuth(FAKE_SERVER, 'fake-token'); |
| 108 | + |
| 109 | + const result = await harness.run('hook claude-prompt-submit', { |
| 110 | + stdin: JSON.stringify({ tool_name: 'Read' }), |
| 111 | + }); |
| 112 | + |
| 113 | + expect(result.exitCode).toBe(0); |
| 114 | + expect(result.stdout).not.toContain('"block"'); |
| 115 | + }, |
| 116 | + { timeout: 15000 }, |
| 117 | + ); |
| 118 | + |
| 119 | + it( |
| 120 | + 'exits 0 and outputs nothing when not authenticated', |
| 121 | + async () => { |
| 122 | + harness.state().withSecretsBinaryInstalled(); |
| 123 | + // no withAuth — no active connection |
| 124 | + |
| 125 | + const result = await harness.run('hook claude-prompt-submit', { |
| 126 | + stdin: JSON.stringify({ prompt: `my token is ${GITHUB_TEST_TOKEN}` }), |
| 127 | + }); |
| 128 | + |
| 129 | + expect(result.exitCode).toBe(0); |
| 130 | + expect(result.stdout).not.toContain('"block"'); |
| 131 | + }, |
| 132 | + { timeout: 15000 }, |
| 133 | + ); |
| 134 | + |
| 135 | + it( |
| 136 | + 'exits 0 and outputs nothing when secrets binary is not installed', |
| 137 | + async () => { |
| 138 | + harness.withAuth(FAKE_SERVER, 'fake-token'); |
| 139 | + |
| 140 | + const result = await harness.run('hook claude-prompt-submit', { |
| 141 | + stdin: JSON.stringify({ prompt: `my token is ${GITHUB_TEST_TOKEN}` }), |
| 142 | + }); |
| 143 | + |
| 144 | + expect(result.exitCode).toBe(0); |
| 145 | + expect(result.stdout).not.toContain('"block"'); |
| 146 | + }, |
| 147 | + { timeout: 15000 }, |
| 148 | + ); |
| 149 | +}); |
0 commit comments