forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
177 lines (147 loc) · 4.1 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package api
import (
"context"
"net/http"
"net/url"
"testing"
"github.com/puppetlabs/wash/plugin"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
type mockEntry struct {
plugin.EntryBase
}
func newMockEntry(name string) *mockEntry {
return &mockEntry{
EntryBase: plugin.NewEntry(name),
}
}
func (e *mockEntry) Schema() *plugin.EntrySchema {
return nil
}
type HelpersTestSuite struct {
suite.Suite
}
func (suite *HelpersTestSuite) SetupSuite() {
plugin.SetTestCache(newMockCache())
}
func (suite *HelpersTestSuite) TearDownSuite() {
plugin.UnsetTestCache()
}
type mockRoot struct {
plugin.EntryBase
mock.Mock
}
func (m *mockRoot) Init(map[string]interface{}) error {
return nil
}
func (m *mockRoot) List(ctx context.Context) ([]plugin.Entry, error) {
args := m.Called(ctx)
return args.Get(0).([]plugin.Entry), args.Error(1)
}
func (m *mockRoot) Schema() *plugin.EntrySchema {
return nil
}
func (m *mockRoot) ChildSchemas() []*plugin.EntrySchema {
return nil
}
func (m *mockRoot) WrappedTypes() plugin.SchemaMap {
return nil
}
func getRequest(ctx context.Context, path string) *http.Request {
return (&http.Request{URL: &url.URL{RawQuery: url.Values{"path": []string{path}}.Encode()}}).WithContext(ctx)
}
func (suite *HelpersTestSuite) TestGetEntryFromPath() {
reg := plugin.NewRegistry()
plug := &mockRoot{EntryBase: plugin.NewEntry("mine")}
plug.SetTestID("/mine")
suite.NoError(reg.RegisterPlugin(plug, map[string]interface{}{}))
ctx := context.WithValue(context.Background(), pluginRegistryKey, reg)
mountpoint := "/mountpoint"
ctx = context.WithValue(ctx, mountpointKey, mountpoint)
_, _, err := getEntryFromRequest(getRequest(ctx, "relative"))
suite.Error(relativePathResponse("relative"), err)
// TODO: Add tests for non-Wash entries (i.e. for apifs)
entry, path, err := getEntryFromRequest(getRequest(ctx, mountpoint))
if suite.Nil(err) {
suite.Equal(mountpoint, path)
suite.Equal(reg.Name(), plugin.Name(entry))
}
entry, path, err = getEntryFromRequest(getRequest(ctx, mountpoint+"/"))
if suite.Nil(err) {
suite.Equal(mountpoint+"/", path)
suite.Equal(reg.Name(), plugin.Name(entry))
}
entry, path, err = getEntryFromRequest(getRequest(ctx, mountpoint+"/mine"))
if suite.Nil(err) {
suite.Equal(mountpoint+"/mine", path)
suite.Equal(plug.Name(), plugin.Name(entry))
}
_, _, err = getEntryFromRequest(getRequest(ctx, mountpoint+"/yours"))
suite.Error(err)
file := newMockEntry("a file")
file.SetTestID("/mine/a file")
plug.On("List", mock.Anything).Return([]plugin.Entry{file}, nil)
entry, path, err = getEntryFromRequest(getRequest(ctx, mountpoint+"/mine/a file"))
if suite.Nil(err) {
suite.Equal(mountpoint+"/mine/a file", path)
suite.Equal(file.Name(), plugin.Name(entry))
}
plug.AssertExpectations(suite.T())
plug.On("List", mock.Anything).Return([]plugin.Entry{file}, nil)
_, _, err = getEntryFromRequest(getRequest(ctx, mountpoint+"/mine/a dir"))
suite.Error(err)
plug.AssertExpectations(suite.T())
}
func (suite *HelpersTestSuite) TestGetBoolParam() {
var u url.URL
for query, expect := range map[string]bool{"": false, "param=true": true, "param=false": false} {
u.RawQuery = query
val, err := getBoolParam(&u, "param")
suite.Nil(err)
suite.Equal(expect, val)
}
u.RawQuery = "param=other"
_, err := getBoolParam(&u, "param")
suite.Error(err)
}
func (suite *HelpersTestSuite) TestGetIntParam() {
var u url.URL
// tc => testCase
type tc struct {
query string
expectedVal int
expectedFound bool
expectedErrRegex string
}
tcs := []tc{
tc{
query: "",
expectedFound: false,
},
tc{
query: "param=10",
expectedVal: 10,
expectedFound: true,
},
tc{
query: "param=foo",
expectedErrRegex: ".*int.*",
},
}
for _, tc := range tcs {
u.RawQuery = tc.query
val, found, err := getIntParam(&u, "param")
if tc.expectedErrRegex != "" {
suite.Regexp(tc.expectedErrRegex, err)
} else {
suite.Equal(tc.expectedFound, found)
if found {
suite.Equal(tc.expectedVal, val)
}
}
}
}
func TestHelpers(t *testing.T) {
suite.Run(t, new(HelpersTestSuite))
}