-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathchatlabels_test.go
More file actions
191 lines (169 loc) · 4.4 KB
/
chatlabels_test.go
File metadata and controls
191 lines (169 loc) · 4.4 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
package httpapi_test
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/httpapi"
)
func TestValidateChatLabels(t *testing.T) {
t.Parallel()
t.Run("NilMap", func(t *testing.T) {
t.Parallel()
errs := httpapi.ValidateChatLabels(nil)
require.Empty(t, errs)
})
t.Run("EmptyMap", func(t *testing.T) {
t.Parallel()
errs := httpapi.ValidateChatLabels(map[string]string{})
require.Empty(t, errs)
})
t.Run("ValidLabels", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"env": "production",
"github.repo": "coder/coder",
"automation/pr": "12345",
"team-backend": "core",
"version_number": "v1.2.3",
"A1.b2/c3-d4_e5": "mixed",
}
errs := httpapi.ValidateChatLabels(labels)
require.Empty(t, errs)
})
t.Run("TooManyLabels", func(t *testing.T) {
t.Parallel()
labels := make(map[string]string, 51)
for i := range 51 {
labels[strings.Repeat("k", i+1)] = "v"
}
errs := httpapi.ValidateChatLabels(labels)
require.NotEmpty(t, errs)
found := false
for _, e := range errs {
if strings.Contains(e.Detail, "too many labels") {
found = true
break
}
}
assert.True(t, found, "expected a 'too many labels' error")
})
t.Run("KeyTooLong", func(t *testing.T) {
t.Parallel()
longKey := strings.Repeat("a", 65)
labels := map[string]string{
longKey: "value",
}
errs := httpapi.ValidateChatLabels(labels)
require.NotEmpty(t, errs)
found := false
for _, e := range errs {
if strings.Contains(e.Detail, "exceeds maximum length of 64 bytes") {
found = true
break
}
}
assert.True(t, found, "expected a key-too-long error")
})
t.Run("ValueTooLong", func(t *testing.T) {
t.Parallel()
longValue := strings.Repeat("v", 257)
labels := map[string]string{
"key": longValue,
}
errs := httpapi.ValidateChatLabels(labels)
require.NotEmpty(t, errs)
found := false
for _, e := range errs {
if strings.Contains(e.Detail, "exceeds maximum length of 256 bytes") {
found = true
break
}
}
assert.True(t, found, "expected a value-too-long error")
})
t.Run("InvalidKeyWithSpaces", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"invalid key": "value",
}
errs := httpapi.ValidateChatLabels(labels)
require.NotEmpty(t, errs)
found := false
for _, e := range errs {
if strings.Contains(e.Detail, "contains invalid characters") {
found = true
break
}
}
assert.True(t, found, "expected an invalid-characters error for spaces")
})
t.Run("InvalidKeyWithSpecialChars", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"key@value": "value",
}
errs := httpapi.ValidateChatLabels(labels)
require.NotEmpty(t, errs)
found := false
for _, e := range errs {
if strings.Contains(e.Detail, "contains invalid characters") {
found = true
break
}
}
assert.True(t, found, "expected an invalid-characters error for special chars")
})
t.Run("KeyStartsWithNonAlphanumeric", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
".dotfirst": "value",
"-dashfirst": "value",
"_underfirst": "value",
"/slashfirst": "value",
}
errs := httpapi.ValidateChatLabels(labels)
// Each of the four keys should produce an error.
require.Len(t, errs, 4)
for _, e := range errs {
assert.Contains(t, e.Detail, "contains invalid characters")
}
})
t.Run("EmptyKey", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"": "value",
}
errs := httpapi.ValidateChatLabels(labels)
require.Len(t, errs, 1)
assert.Contains(t, errs[0].Detail, "must not be empty")
})
t.Run("EmptyValue", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"key": "",
}
errs := httpapi.ValidateChatLabels(labels)
require.Len(t, errs, 1)
assert.Contains(t, errs[0].Detail, "must not be empty")
})
t.Run("AllFieldsAreLabels", func(t *testing.T) {
t.Parallel()
labels := map[string]string{
"bad key": "",
}
errs := httpapi.ValidateChatLabels(labels)
for _, e := range errs {
assert.Equal(t, "labels", e.Field)
}
})
t.Run("ExactlyAtLimits", func(t *testing.T) {
t.Parallel()
// Keys and values exactly at their limits should be valid.
labels := map[string]string{
strings.Repeat("a", 64): strings.Repeat("v", 256),
}
errs := httpapi.ValidateChatLabels(labels)
require.Empty(t, errs)
})
}