Skip to content

Commit cf5d48b

Browse files
authored
fix: do not skip properties on creating templates (#5060)
* fix: do not skip properties while creating templates * test: empty edit
1 parent 4b3d211 commit cf5d48b

File tree

3 files changed

+104
-2
lines changed

3 files changed

+104
-2
lines changed

cli/templateedit_test.go

+100-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestTemplateEdit(t *testing.T) {
1818
t.Parallel()
1919

20-
t.Run("Modified", func(t *testing.T) {
20+
t.Run("FirstEmptyThenModified", func(t *testing.T) {
2121
t.Parallel()
2222
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
2323
user := coderdtest.CreateFirstUser(t, client)
@@ -58,7 +58,7 @@ func TestTemplateEdit(t *testing.T) {
5858
assert.Equal(t, icon, updated.Icon)
5959
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
6060
})
61-
t.Run("NotModified", func(t *testing.T) {
61+
t.Run("FirstEmptyThenNotModified", func(t *testing.T) {
6262
t.Parallel()
6363
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
6464
user := coderdtest.CreateFirstUser(t, client)
@@ -124,4 +124,102 @@ func TestTemplateEdit(t *testing.T) {
124124
assert.Equal(t, template.Name, updated.Name)
125125
assert.Equal(t, "", template.DisplayName)
126126
})
127+
t.Run("WithPropertiesThenModified", func(t *testing.T) {
128+
t.Parallel()
129+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
130+
user := coderdtest.CreateFirstUser(t, client)
131+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
132+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
133+
134+
initialDisplayName := "This is a template"
135+
initialDescription := "This is description"
136+
initialIcon := "/img/icon.png"
137+
138+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
139+
ctr.DisplayName = initialDisplayName
140+
ctr.Description = initialDescription
141+
ctr.Icon = initialIcon
142+
})
143+
144+
// Test created template
145+
created, err := client.Template(context.Background(), template.ID)
146+
require.NoError(t, err)
147+
assert.Equal(t, initialDisplayName, created.DisplayName)
148+
assert.Equal(t, initialDescription, created.Description)
149+
assert.Equal(t, initialIcon, created.Icon)
150+
151+
// Test the cli command.
152+
displayName := "New Display Name 789"
153+
icon := "/icons/new-icon.png"
154+
cmdArgs := []string{
155+
"templates",
156+
"edit",
157+
template.Name,
158+
"--display-name", displayName,
159+
"--icon", icon,
160+
}
161+
cmd, root := clitest.New(t, cmdArgs...)
162+
clitest.SetupConfig(t, client, root)
163+
164+
ctx, _ := testutil.Context(t)
165+
err = cmd.ExecuteContext(ctx)
166+
167+
require.NoError(t, err)
168+
169+
// Assert that the template metadata changed.
170+
updated, err := client.Template(context.Background(), template.ID)
171+
require.NoError(t, err)
172+
assert.Equal(t, template.Name, updated.Name) // doesn't change
173+
assert.Equal(t, initialDescription, updated.Description) // doesn't change
174+
assert.Equal(t, displayName, updated.DisplayName)
175+
assert.Equal(t, icon, updated.Icon)
176+
})
177+
t.Run("WithPropertiesThenEmptyEdit", func(t *testing.T) {
178+
t.Parallel()
179+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
180+
user := coderdtest.CreateFirstUser(t, client)
181+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
182+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
183+
184+
initialDisplayName := "This is a template"
185+
initialDescription := "This is description"
186+
initialIcon := "/img/icon.png"
187+
188+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
189+
ctr.DisplayName = initialDisplayName
190+
ctr.Description = initialDescription
191+
ctr.Icon = initialIcon
192+
})
193+
194+
// Test created template
195+
created, err := client.Template(context.Background(), template.ID)
196+
require.NoError(t, err)
197+
assert.Equal(t, initialDisplayName, created.DisplayName)
198+
assert.Equal(t, initialDescription, created.Description)
199+
assert.Equal(t, initialIcon, created.Icon)
200+
201+
// Test the cli command.
202+
cmdArgs := []string{
203+
"templates",
204+
"edit",
205+
template.Name,
206+
}
207+
cmd, root := clitest.New(t, cmdArgs...)
208+
clitest.SetupConfig(t, client, root)
209+
210+
ctx, _ := testutil.Context(t)
211+
err = cmd.ExecuteContext(ctx)
212+
213+
require.NoError(t, err)
214+
215+
// Assert that the template metadata changed.
216+
updated, err := client.Template(context.Background(), template.ID)
217+
require.NoError(t, err)
218+
// Properties don't change
219+
assert.Equal(t, template.Name, updated.Name)
220+
assert.Equal(t, template.Description, updated.Description)
221+
assert.Equal(t, template.DisplayName, updated.DisplayName)
222+
// Icon is removed, as the API considers it as "delete" request
223+
assert.Equal(t, "", updated.Icon)
224+
})
127225
}

coderd/database/databasefake/databasefake.go

+2
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,8 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
20912091
CreatedBy: arg.CreatedBy,
20922092
UserACL: arg.UserACL,
20932093
GroupACL: arg.GroupACL,
2094+
DisplayName: arg.DisplayName,
2095+
Icon: arg.Icon,
20942096
}
20952097
q.templates = append(q.templates, template)
20962098
return template, nil

coderd/templates.go

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
239239
GroupACL: database.TemplateACL{
240240
organization.ID.String(): []rbac.Action{rbac.ActionRead},
241241
},
242+
DisplayName: createTemplate.DisplayName,
243+
Icon: createTemplate.Icon,
242244
})
243245
if err != nil {
244246
return xerrors.Errorf("insert template: %s", err)

0 commit comments

Comments
 (0)