Skip to content

Commit 60cb055

Browse files
committed
Apply review suggestions
1 parent 950a903 commit 60cb055

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

codersdk/name_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package codersdk_test
33
import (
44
"strings"
55
"testing"
6-
"time"
76

87
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
10-
"golang.org/x/exp/rand"
119

1210
"github.com/coder/coder/v2/codersdk"
11+
"github.com/coder/coder/v2/cryptorand"
1312
"github.com/coder/coder/v2/testutil"
1413
)
1514

@@ -260,6 +259,11 @@ func TestUserRealNameValid(t *testing.T) {
260259
func TestGroupNameValid(t *testing.T) {
261260
t.Parallel()
262261

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+
263267
testCases := []struct {
264268
Name string
265269
Valid bool
@@ -269,27 +273,23 @@ func TestGroupNameValid(t *testing.T) {
269273
{"create", false},
270274
{"new", false},
271275
{"Lord Voldemort Team", false},
272-
{randomString(255), true},
273-
{randomString(256), false},
276+
{random255String, true},
277+
{random256String, false},
274278
}
275279
for _, testCase := range testCases {
276280
testCase := testCase
277281
t.Run(testCase.Name, func(t *testing.T) {
278282
t.Parallel()
279283
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+
)
281293
})
282294
}
283295
}
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

Comments
 (0)