|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParseCredentialArgs(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + toolName string |
| 14 | + input string |
| 15 | + expectedName string |
| 16 | + expectedAlias string |
| 17 | + expectedArgs map[string]string |
| 18 | + wantErr bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "empty", |
| 22 | + toolName: "", |
| 23 | + expectedName: "", |
| 24 | + expectedAlias: "", |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "tool name only", |
| 28 | + toolName: "myCredentialTool", |
| 29 | + expectedName: "myCredentialTool", |
| 30 | + expectedAlias: "", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "tool name and alias", |
| 34 | + toolName: "myCredentialTool as myAlias", |
| 35 | + expectedName: "myCredentialTool", |
| 36 | + expectedAlias: "myAlias", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "tool name with one arg", |
| 40 | + toolName: "myCredentialTool with value1 as arg1", |
| 41 | + expectedName: "myCredentialTool", |
| 42 | + expectedAlias: "", |
| 43 | + expectedArgs: map[string]string{ |
| 44 | + "arg1": "value1", |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "tool name with two args", |
| 49 | + toolName: "myCredentialTool with value1 as arg1 and value2 as arg2", |
| 50 | + expectedName: "myCredentialTool", |
| 51 | + expectedAlias: "", |
| 52 | + expectedArgs: map[string]string{ |
| 53 | + "arg1": "value1", |
| 54 | + "arg2": "value2", |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "tool name with alias and one arg", |
| 59 | + toolName: "myCredentialTool as myAlias with value1 as arg1", |
| 60 | + expectedName: "myCredentialTool", |
| 61 | + expectedAlias: "myAlias", |
| 62 | + expectedArgs: map[string]string{ |
| 63 | + "arg1": "value1", |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "tool name with alias and two args", |
| 68 | + toolName: "myCredentialTool as myAlias with value1 as arg1 and value2 as arg2", |
| 69 | + expectedName: "myCredentialTool", |
| 70 | + expectedAlias: "myAlias", |
| 71 | + expectedArgs: map[string]string{ |
| 72 | + "arg1": "value1", |
| 73 | + "arg2": "value2", |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "tool name with quoted args", |
| 78 | + toolName: `myCredentialTool with "value one" as arg1 and "value two" as arg2`, |
| 79 | + expectedName: "myCredentialTool", |
| 80 | + expectedAlias: "", |
| 81 | + expectedArgs: map[string]string{ |
| 82 | + "arg1": "value one", |
| 83 | + "arg2": "value two", |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "tool name with arg references", |
| 88 | + toolName: `myCredentialTool with ${var1} as arg1 and ${var2} as arg2`, |
| 89 | + input: `{"var1": "value1", "var2": "value2"}`, |
| 90 | + expectedName: "myCredentialTool", |
| 91 | + expectedAlias: "", |
| 92 | + expectedArgs: map[string]string{ |
| 93 | + "arg1": "value1", |
| 94 | + "arg2": "value2", |
| 95 | + }, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "tool name with alias but no 'as' (invalid)", |
| 99 | + toolName: "myCredentialTool myAlias", |
| 100 | + wantErr: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "tool name with 'as' but no alias (invalid)", |
| 104 | + toolName: "myCredentialTool as", |
| 105 | + wantErr: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "tool with 'with' but no args (invalid)", |
| 109 | + toolName: "myCredentialTool with", |
| 110 | + wantErr: true, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "tool with args but no 'with' (invalid)", |
| 114 | + toolName: "myCredentialTool value1 as arg1", |
| 115 | + wantErr: true, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "tool with trailing 'and' (invalid)", |
| 119 | + toolName: "myCredentialTool with value1 as arg1 and", |
| 120 | + wantErr: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "tool with quoted arg but the quote is unterminated (invalid)", |
| 124 | + toolName: `myCredentialTool with "value one" as arg1 and "value two as arg2`, |
| 125 | + wantErr: true, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "invalid input", |
| 129 | + toolName: "myCredentialTool", |
| 130 | + input: `{"asdf":"asdf"`, |
| 131 | + wantErr: true, |
| 132 | + }, |
| 133 | + } |
| 134 | + |
| 135 | + for _, tt := range tests { |
| 136 | + t.Run(tt.name, func(t *testing.T) { |
| 137 | + originalName, alias, args, err := ParseCredentialArgs(tt.toolName, tt.input) |
| 138 | + if tt.wantErr { |
| 139 | + require.Error(t, err, "expected an error but got none") |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + require.NoError(t, err, "did not expect an error but got one") |
| 144 | + require.Equal(t, tt.expectedName, originalName, "unexpected original name") |
| 145 | + require.Equal(t, tt.expectedAlias, alias, "unexpected alias") |
| 146 | + require.Equal(t, len(tt.expectedArgs), len(args), "unexpected number of args") |
| 147 | + |
| 148 | + for k, v := range tt.expectedArgs { |
| 149 | + assert.Equal(t, v, args[k], "unexpected value for args[%s]", k) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments