|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMaskSensitiveArgs(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + args []string |
| 13 | + expected []string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "no sensitive flags", |
| 17 | + args: []string{"codacy-cli", "analyze", "--tool", "eslint"}, |
| 18 | + expected: []string{"codacy-cli", "analyze", "--tool", "eslint"}, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "api token with space", |
| 22 | + args: []string{"codacy-cli", "init", "--api-token", "secret123", "--tool", "eslint"}, |
| 23 | + expected: []string{"codacy-cli", "init", "--api-token", "***", "--tool", "eslint"}, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "api token with equals", |
| 27 | + args: []string{"codacy-cli", "init", "--api-token=secret123", "--tool", "eslint"}, |
| 28 | + expected: []string{"codacy-cli", "init", "--api-token=***", "--tool", "eslint"}, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "repository token at end with space", |
| 32 | + args: []string{"codacy-cli", "init", "--repository-token", "secret123"}, |
| 33 | + expected: []string{"codacy-cli", "init", "--repository-token", "***"}, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "repository token at end with equals", |
| 37 | + args: []string{"codacy-cli", "init", "--repository-token=secret123"}, |
| 38 | + expected: []string{"codacy-cli", "init", "--repository-token=***"}, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "project token at start with space", |
| 42 | + args: []string{"codacy-cli", "--project-token", "secret123", "analyze"}, |
| 43 | + expected: []string{"codacy-cli", "--project-token", "***", "analyze"}, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "project token at start with equals", |
| 47 | + args: []string{"codacy-cli", "--project-token=secret123", "analyze"}, |
| 48 | + expected: []string{"codacy-cli", "--project-token=***", "analyze"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "multiple tokens mixed format", |
| 52 | + args: []string{"codacy-cli", "--api-token=secret1", "--project-token", "secret2"}, |
| 53 | + expected: []string{"codacy-cli", "--api-token=***", "--project-token", "***"}, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "token flag at end without value", |
| 57 | + args: []string{"codacy-cli", "analyze", "--api-token"}, |
| 58 | + expected: []string{"codacy-cli", "analyze", "--api-token"}, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "empty value after equals", |
| 62 | + args: []string{"codacy-cli", "--api-token="}, |
| 63 | + expected: []string{"codacy-cli", "--api-token=***"}, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + for _, tt := range tests { |
| 68 | + t.Run(tt.name, func(t *testing.T) { |
| 69 | + masked := maskSensitiveArgs(tt.args) |
| 70 | + assert.Equal(t, tt.expected, masked, "masked arguments should match expected") |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments