Skip to content

Commit 4b3fc65

Browse files
authored
fix: app display name validation (#344)
1 parent aef6220 commit 4b3fc65

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

provider/app.go

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
appSlugRegex = regexp.MustCompile(`^[a-z0-9](-?[a-z0-9])*$`)
2424
)
2525

26+
const appDisplayNameMaxLength = 64 // database column limit
27+
2628
func appResource() *schema.Resource {
2729
return &schema.Resource{
2830
SchemaVersion: 1,
@@ -124,6 +126,17 @@ func appResource() *schema.Resource {
124126
Description: "A display name to identify the app. Defaults to the slug.",
125127
ForceNew: true,
126128
Optional: true,
129+
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
130+
valStr, ok := val.(string)
131+
if !ok {
132+
return diag.Errorf("expected string, got %T", val)
133+
}
134+
135+
if len(valStr) > appDisplayNameMaxLength {
136+
return diag.Errorf("display name is too long (max %d characters)", appDisplayNameMaxLength)
137+
}
138+
return nil
139+
},
127140
},
128141
"subdomain": {
129142
Type: schema.TypeBool,

provider/app_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,66 @@ func TestApp(t *testing.T) {
415415
}
416416
})
417417

418+
t.Run("DisplayName", func(t *testing.T) {
419+
t.Parallel()
420+
421+
cases := []struct {
422+
name string
423+
displayName string
424+
expectValue string
425+
expectError *regexp.Regexp
426+
}{
427+
{
428+
name: "Empty",
429+
displayName: "",
430+
},
431+
{
432+
name: "Regular",
433+
displayName: "Regular Application",
434+
},
435+
{
436+
name: "DisplayNameStillOK",
437+
displayName: "0123456789012345678901234567890123456789012345678901234567890123",
438+
},
439+
{
440+
name: "DisplayNameTooLong",
441+
displayName: "01234567890123456789012345678901234567890123456789012345678901234",
442+
expectError: regexp.MustCompile("display name is too long"),
443+
},
444+
}
445+
446+
for _, c := range cases {
447+
c := c
448+
449+
t.Run(c.name, func(t *testing.T) {
450+
t.Parallel()
451+
452+
config := fmt.Sprintf(`
453+
provider "coder" {
454+
}
455+
resource "coder_agent" "dev" {
456+
os = "linux"
457+
arch = "amd64"
458+
}
459+
resource "coder_app" "code-server" {
460+
agent_id = coder_agent.dev.id
461+
slug = "code-server"
462+
display_name = "%s"
463+
url = "http://localhost:13337"
464+
open_in = "slim-window"
465+
}
466+
`, c.displayName)
467+
468+
resource.Test(t, resource.TestCase{
469+
ProviderFactories: coderFactory(),
470+
IsUnitTest: true,
471+
Steps: []resource.TestStep{{
472+
Config: config,
473+
ExpectError: c.expectError,
474+
}},
475+
})
476+
})
477+
}
478+
})
479+
418480
}

0 commit comments

Comments
 (0)