-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathconstraint_test.go
More file actions
325 lines (253 loc) · 8.26 KB
/
Copy pathconstraint_test.go
File metadata and controls
325 lines (253 loc) · 8.26 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package fiber
import (
"regexp"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ConstraintMatchConstraint_NilHandler(t *testing.T) {
t.Parallel()
t.Run("resolves handler from Name", func(t *testing.T) {
t.Parallel()
c := &Constraint{
Name: ConstraintMinLen,
Data: []string{"3"},
}
require.True(t, c.matchConstraint("hello"))
require.False(t, c.matchConstraint("hi"))
})
t.Run("returns true for unknown constraint name", func(t *testing.T) {
t.Parallel()
c := &Constraint{
Name: "unknownConstraint",
Data: []string{"5"},
}
require.True(t, c.matchConstraint("anything"))
})
t.Run("resolves datetime handler", func(t *testing.T) {
t.Parallel()
c := &Constraint{
Name: ConstraintDatetime,
Data: []string{"2006-01-02"},
}
require.True(t, c.matchConstraint("2024-01-15"))
require.False(t, c.matchConstraint("not-a-date"))
})
}
func Test_ConstraintAnalyze_MissingArgs(t *testing.T) {
t.Parallel()
testCases := []struct {
handler ConstraintAnalyzer
name string
}{
{datetimeConstraintType{}, "datetime"},
{minLenConstraintType{}, "minLen"},
{maxLenConstraintType{}, "maxLen"},
{lenConstraintType{}, "len"},
{minConstraintType{}, "min"},
{maxConstraintType{}, "max"},
{regexConstraintType{}, "regex"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
_, err := tc.handler.Analyze([]string{})
require.Error(t, err)
})
}
}
func Test_ConstraintAnalyze_BetweenLen(t *testing.T) {
t.Parallel()
t.Run("missing second arg", func(t *testing.T) {
t.Parallel()
_, err := betweenLenConstraintType{}.Analyze([]string{"1"})
require.Error(t, err)
})
}
func Test_ConstraintAnalyze_Range(t *testing.T) {
t.Parallel()
t.Run("missing second arg", func(t *testing.T) {
t.Parallel()
_, err := rangeConstraintType{}.Analyze([]string{"1"})
require.Error(t, err)
})
}
func Test_ConstraintAnalyze_Regex(t *testing.T) {
t.Parallel()
t.Run("invalid pattern", func(t *testing.T) {
t.Parallel()
_, err := regexConstraintType{}.Analyze([]string{"("})
require.Error(t, err)
})
}
func Test_ConstraintExecute_IntConstraint(t *testing.T) {
t.Parallel()
handler := intConstraintType{}
require.True(t, handler.Execute("42", nil))
require.False(t, handler.Execute("abc", nil))
}
func Test_ConstraintExecute_BoolConstraint(t *testing.T) {
t.Parallel()
handler := boolConstraintType{}
require.True(t, handler.Execute("true", nil))
require.True(t, handler.Execute("false", nil))
require.True(t, handler.Execute("1", nil))
require.True(t, handler.Execute("0", nil))
require.False(t, handler.Execute("maybe", nil))
}
func Test_ConstraintExecute_FloatConstraint(t *testing.T) {
t.Parallel()
handler := floatConstraintType{}
require.True(t, handler.Execute("3.14", nil))
require.False(t, handler.Execute("abc", nil))
}
func Test_ConstraintExecute_AlphaConstraint(t *testing.T) {
t.Parallel()
handler := alphaConstraintType{}
require.True(t, handler.Execute("hello", nil))
require.True(t, handler.Execute("", nil))
require.False(t, handler.Execute("hello123", nil))
}
func Test_ConstraintExecute_GuidConstraint(t *testing.T) {
t.Parallel()
handler := guidConstraintType{}
require.True(t, handler.Execute("12345678-1234-1234-1234-123456789abc", nil))
require.False(t, handler.Execute("not-a-guid", nil))
}
func Test_ConstraintExecute_DatetimeConstraint_NilData(t *testing.T) {
t.Parallel()
handler := datetimeConstraintType{}
require.False(t, handler.Execute("2024-01-15", nil))
}
func Test_ConstraintExecute_DatetimeConstraint_EmptyLayout(t *testing.T) {
t.Parallel()
handler := datetimeConstraintType{}
require.False(t, handler.Execute("2024-01-15", []any{""}))
}
func Test_ConstraintExecute_MinLenConstraint_NilData(t *testing.T) {
t.Parallel()
handler := minLenConstraintType{}
require.False(t, handler.Execute("hello", nil))
}
func Test_ConstraintExecute_MaxLenConstraint_NilData(t *testing.T) {
t.Parallel()
handler := maxLenConstraintType{}
require.False(t, handler.Execute("hello", nil))
}
func Test_ConstraintExecute_LenConstraint_NilData(t *testing.T) {
t.Parallel()
handler := lenConstraintType{}
require.False(t, handler.Execute("hello", nil))
}
func Test_ConstraintExecute_BetweenLenConstraint_NilData(t *testing.T) {
t.Parallel()
handler := betweenLenConstraintType{}
require.False(t, handler.Execute("hello", nil))
}
func Test_ConstraintExecute_MinConstraint_NilData(t *testing.T) {
t.Parallel()
handler := minConstraintType{}
require.False(t, handler.Execute("10", nil))
}
func Test_ConstraintExecute_MaxConstraint_NilData(t *testing.T) {
t.Parallel()
handler := maxConstraintType{}
require.False(t, handler.Execute("10", nil))
}
func Test_ConstraintExecute_RangeConstraint_NilData(t *testing.T) {
t.Parallel()
handler := rangeConstraintType{}
require.False(t, handler.Execute("5", nil))
}
func Test_ConstraintExecute_RegexConstraint_NilData(t *testing.T) {
t.Parallel()
handler := regexConstraintType{}
require.False(t, handler.Execute("hello", nil))
}
func Test_ConstraintExecute_RegexConstraint_Compiled(t *testing.T) {
t.Parallel()
re := regexp.MustCompile(`^\d+$`)
handler := regexConstraintType{}
require.True(t, handler.Execute("123", []any{re}))
require.False(t, handler.Execute("abc", []any{re}))
}
func Test_ConstraintMatchConstraint_WithTypedData(t *testing.T) {
t.Parallel()
handler := minLenConstraintType{}
c := newConstraint(handler, ConstraintMinLen, []string{"3"})
require.True(t, c.matchConstraint("hello"))
require.False(t, c.matchConstraint("hi"))
}
func Test_ConstraintMatchConstraint_NilHandlerWithAnalyzerError(t *testing.T) {
t.Parallel()
c := &Constraint{
Name: ConstraintMinLen,
Data: []string{"notanumber"},
}
require.False(t, c.matchConstraint("hello"))
}
func Test_FindConstraintHandler_CustomPriority(t *testing.T) {
t.Parallel()
custom := &testCustomConstraintForCoverage{allowed: "x"}
handler := findConstraintHandler("customTest", nil, []CustomConstraint{custom})
require.NotNil(t, handler)
}
func Test_FindConstraintHandler_Builtin(t *testing.T) {
t.Parallel()
handler := findConstraintHandler("int", nil, nil)
require.NotNil(t, handler)
require.Equal(t, "int", handler.Name())
}
func Test_FindConstraintHandler_Unknown(t *testing.T) {
t.Parallel()
handler := findConstraintHandler("nonexistent", nil, nil)
require.Nil(t, handler)
}
type testCustomConstraintForCoverage struct {
allowed string
}
func (*testCustomConstraintForCoverage) Name() string { return "customTest" }
func (t *testCustomConstraintForCoverage) Execute(param string, _ ...string) bool {
return param == t.allowed
}
type testCustomConstraintWithAnalyzer struct {
layout string
}
func (*testCustomConstraintWithAnalyzer) Name() string { return "customDatetime" }
func (t *testCustomConstraintWithAnalyzer) Execute(param string, _ ...string) bool {
return param == t.layout
}
func (t *testCustomConstraintWithAnalyzer) Analyze(args []string) ([]any, error) {
if len(args) > 0 {
t.layout = args[0]
}
return stringArgsToAny(args), nil
}
func Test_CustomConstraintWrapper_DelegatesAnalyze(t *testing.T) {
t.Parallel()
custom := &testCustomConstraintWithAnalyzer{}
handler := findConstraintHandler("customDatetime", nil, []CustomConstraint{custom})
require.NotNil(t, handler)
analyzer, ok := handler.(ConstraintAnalyzer)
require.True(t, ok)
typed, err := analyzer.Analyze([]string{"2006-01-02"})
require.NoError(t, err)
require.Equal(t, []any{[]string{"2006-01-02"}}, typed)
require.Equal(t, "2006-01-02", custom.layout)
}
type testCustomConstraintWithTypedAnalyzer struct{}
func (*testCustomConstraintWithTypedAnalyzer) Name() string { return "customRole" }
func (*testCustomConstraintWithTypedAnalyzer) Execute(param string, args ...string) bool {
return len(args) == 1 && args[0] == "admin" && param == "admin"
}
func (*testCustomConstraintWithTypedAnalyzer) Analyze(args []string) ([]any, error) {
return stringArgsToAny(args), nil
}
func Test_CustomConstraintWrapper_ExecuteKeepsLegacyArgsWithAnalyzer(t *testing.T) {
t.Parallel()
custom := &testCustomConstraintWithTypedAnalyzer{}
handler := findConstraintHandler("customRole", nil, []CustomConstraint{custom})
require.NotNil(t, handler)
c := newConstraint(handler, "customRole", []string{"admin"})
require.True(t, c.matchConstraint("admin"))
require.False(t, c.matchConstraint("guest"))
}