@@ -3,9 +3,11 @@ package codersdk_test
3
3
import (
4
4
"strings"
5
5
"testing"
6
+ "time"
6
7
7
8
"github.com/stretchr/testify/assert"
8
9
"github.com/stretchr/testify/require"
10
+ "golang.org/x/exp/rand"
9
11
10
12
"github.com/coder/coder/v2/codersdk"
11
13
"github.com/coder/coder/v2/testutil"
@@ -254,3 +256,40 @@ func TestUserRealNameValid(t *testing.T) {
254
256
})
255
257
}
256
258
}
259
+
260
+ func TestGroupNameValid (t * testing.T ) {
261
+ t .Parallel ()
262
+
263
+ testCases := []struct {
264
+ Name string
265
+ Valid bool
266
+ }{
267
+ {"" , false },
268
+ {"my-group" , true },
269
+ {"create" , false },
270
+ {"new" , false },
271
+ {"Lord Voldemort Team" , false },
272
+ {randomString (255 ), true },
273
+ {randomString (256 ), false },
274
+ }
275
+ for _ , testCase := range testCases {
276
+ testCase := testCase
277
+ t .Run (testCase .Name , func (t * testing.T ) {
278
+ t .Parallel ()
279
+ err := codersdk .GroupNameValid (testCase .Name )
280
+ assert .Equal (t , testCase .Valid , err == nil )
281
+ })
282
+ }
283
+ }
284
+
285
+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
286
+
287
+ // RandomString generates a random string of a given length.
288
+ func randomString (length int ) string {
289
+ seededRand := rand .New (rand .NewSource (uint64 (time .Now ().UnixNano ())))
290
+ result := make ([]byte , length )
291
+ for i := range result {
292
+ result [i ] = charset [seededRand .Intn (len (charset ))]
293
+ }
294
+ return string (result )
295
+ }
0 commit comments