Skip to content

Commit 75c79d3

Browse files
committed
unit: TestUsernameValid
1 parent b2c122b commit 75c79d3

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

coderd/httpapi/name_test.go

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/coder/coder/coderd/httpapi"
99
)
1010

11-
func TestValid(t *testing.T) {
11+
func TestUsernameValid(t *testing.T) {
1212
t.Parallel()
1313
// Tests whether usernames are valid or not.
1414
testCases := []struct {
@@ -65,6 +65,63 @@ func TestValid(t *testing.T) {
6565
}
6666
}
6767

68+
func TestTemplateDisplayNameValid(t *testing.T) {
69+
t.Parallel()
70+
// Tests whether display names are valid.
71+
testCases := []struct {
72+
Username string
73+
Valid bool
74+
}{
75+
{"", true},
76+
{"1", true},
77+
{"12", true},
78+
{"1 2", true},
79+
{"1234 678901234567890", true},
80+
{"1234567890123 5678901", true},
81+
{"S", true},
82+
{"a1", true},
83+
{"a1K2", true},
84+
{"a1b2c3 4e5f6g7h8i9j0", true},
85+
{"a1b2c3d4e5f6g h8i9j0k", true},
86+
{"aa", true},
87+
{"aNc", true},
88+
{"abcdefghijklmnopqrst", true},
89+
{"abcdefghijklmnopqrstu", true},
90+
{"Wow Test", true},
91+
92+
{" ", false},
93+
{" a", false},
94+
{" a ", false},
95+
{" 1", false},
96+
{"1 ", false},
97+
{" aa", false},
98+
{"aa ", false},
99+
{" 12", false},
100+
{"12 ", false},
101+
{" a1", false},
102+
{"a1 ", false},
103+
{"-abcdefghijKLmnopqrstu", false},
104+
{"abcdefghijklmnopqrstu-", false},
105+
{"-123456789012345678901", false},
106+
{"-a1b2c3d4e5f6g7h8i9j0k", false},
107+
{"a1b2c3d4e5f6g7h8i9j0k-", false},
108+
{"BANANAS_wow", false},
109+
{"test--now", false},
110+
111+
{"123456789012345678901234567890123", false},
112+
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", false},
113+
{"123456789012345678901234567890123123456789012345678901234567890123", false},
114+
}
115+
for _, testCase := range testCases {
116+
testCase := testCase
117+
t.Run(testCase.Username, func(t *testing.T) {
118+
t.Parallel()
119+
valid := httpapi.TemplateDisplayNameValid(testCase.Username)
120+
require.Equal(t, testCase.Valid, valid == nil)
121+
})
122+
}
123+
}
124+
68125
func TestFrom(t *testing.T) {
69126
t.Parallel()
70127
testCases := []struct {

0 commit comments

Comments
 (0)