|
| 1 | +package httpapi_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/coder/coder/coderd/httpapi" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestSearchWorkspace(t *testing.T) { |
| 11 | + t.Parallel() |
| 12 | + testCases := []struct { |
| 13 | + Name string |
| 14 | + Query string |
| 15 | + Expected map[string]string |
| 16 | + ExpectedErrorContains string |
| 17 | + }{ |
| 18 | + { |
| 19 | + Name: "Empty", |
| 20 | + Query: "", |
| 21 | + Expected: map[string]string{}, |
| 22 | + }, |
| 23 | + { |
| 24 | + Name: "Owner/Name", |
| 25 | + Query: "Foo/Bar", |
| 26 | + Expected: map[string]string{ |
| 27 | + "owner": "Foo", |
| 28 | + "name": "Bar", |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + Name: "Name", |
| 33 | + Query: "workspace-name", |
| 34 | + Expected: map[string]string{ |
| 35 | + "name": "workspace-name", |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "Name+Param", |
| 40 | + Query: "workspace-name template:docker", |
| 41 | + Expected: map[string]string{ |
| 42 | + "name": "workspace-name", |
| 43 | + "template": "docker", |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + Name: "OnlyParams", |
| 48 | + Query: "name:workspace-name template:docker owner:alice", |
| 49 | + Expected: map[string]string{ |
| 50 | + "owner": "alice", |
| 51 | + "name": "workspace-name", |
| 52 | + "template": "docker", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + Name: "QuotedParam", |
| 57 | + Query: `name:workspace-name template:"docker template" owner:alice`, |
| 58 | + Expected: map[string]string{ |
| 59 | + "owner": "alice", |
| 60 | + "name": "workspace-name", |
| 61 | + "template": "docker template", |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "QuotedKey", |
| 66 | + Query: `"spaced key":"spaced value"`, |
| 67 | + Expected: map[string]string{ |
| 68 | + "spaced key": "spaced value", |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + // This will not return an error |
| 73 | + Name: "ExtraKeys", |
| 74 | + Query: `foo:bar`, |
| 75 | + Expected: map[string]string{ |
| 76 | + "foo": "bar", |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + // Quotes keep elements together |
| 81 | + Name: "QuotedSpecial", |
| 82 | + Query: `name:"workspace:name"`, |
| 83 | + Expected: map[string]string{ |
| 84 | + "name": "workspace:name", |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + Name: "QuotedMadness", |
| 89 | + Query: `"key:is:wild/a/b/c":"foo:bar/baz/zoo:zonk"`, |
| 90 | + Expected: map[string]string{ |
| 91 | + "key:is:wild/a/b/c": "foo:bar/baz/zoo:zonk", |
| 92 | + }, |
| 93 | + }, |
| 94 | + |
| 95 | + // Failures |
| 96 | + { |
| 97 | + Name: "ExtraSlashes", |
| 98 | + Query: `foo/bar/baz`, |
| 99 | + ExpectedErrorContains: "can only contain 1 '/'", |
| 100 | + }, |
| 101 | + { |
| 102 | + Name: "ExtraColon", |
| 103 | + Query: `owner:name:extra`, |
| 104 | + ExpectedErrorContains: "can only contain 1 ':'", |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, c := range testCases { |
| 109 | + c := c |
| 110 | + t.Run(c.Name, func(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + values, err := httpapi.WorkspaceSearchQuery(c.Query) |
| 113 | + if c.ExpectedErrorContains != "" { |
| 114 | + require.Error(t, err, "expected error") |
| 115 | + require.ErrorContains(t, err, c.ExpectedErrorContains) |
| 116 | + } else { |
| 117 | + require.NoError(t, err, "expected no error") |
| 118 | + require.Equal(t, c.Expected, values, "expected values") |
| 119 | + } |
| 120 | + |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
0 commit comments