@@ -3,13 +3,12 @@ package codersdk_test
3
3
import (
4
4
"strings"
5
5
"testing"
6
- "time"
7
6
8
7
"github.com/stretchr/testify/assert"
9
8
"github.com/stretchr/testify/require"
10
- "golang.org/x/exp/rand"
11
9
12
10
"github.com/coder/coder/v2/codersdk"
11
+ "github.com/coder/coder/v2/cryptorand"
13
12
"github.com/coder/coder/v2/testutil"
14
13
)
15
14
@@ -260,6 +259,11 @@ func TestUserRealNameValid(t *testing.T) {
260
259
func TestGroupNameValid (t * testing.T ) {
261
260
t .Parallel ()
262
261
262
+ random255String , err := cryptorand .String (255 )
263
+ require .NoError (t , err , "failed to generate 255 random string" )
264
+ random256String , err := cryptorand .String (256 )
265
+ require .NoError (t , err , "failed to generate 256 random string" )
266
+
263
267
testCases := []struct {
264
268
Name string
265
269
Valid bool
@@ -269,27 +273,23 @@ func TestGroupNameValid(t *testing.T) {
269
273
{"create" , false },
270
274
{"new" , false },
271
275
{"Lord Voldemort Team" , false },
272
- {randomString ( 255 ) , true },
273
- {randomString ( 256 ) , false },
276
+ {random255String , true },
277
+ {random256String , false },
274
278
}
275
279
for _ , testCase := range testCases {
276
280
testCase := testCase
277
281
t .Run (testCase .Name , func (t * testing.T ) {
278
282
t .Parallel ()
279
283
err := codersdk .GroupNameValid (testCase .Name )
280
- assert .Equal (t , testCase .Valid , err == nil )
284
+ assert .Equal (
285
+ t ,
286
+ testCase .Valid ,
287
+ err == nil ,
288
+ "Test case %s failed: expected valid=%t but got error: %v" ,
289
+ testCase .Name ,
290
+ testCase .Valid ,
291
+ err ,
292
+ )
281
293
})
282
294
}
283
295
}
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