mirror of
https://github.com/coder/coder.git
synced 2025-07-08 11:39:50 +00:00
fix: do not skip properties on creating templates (#5060)
* fix: do not skip properties while creating templates * test: empty edit
This commit is contained in:
@ -17,7 +17,7 @@ import (
|
|||||||
func TestTemplateEdit(t *testing.T) {
|
func TestTemplateEdit(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
t.Run("Modified", func(t *testing.T) {
|
t.Run("FirstEmptyThenModified", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||||
user := coderdtest.CreateFirstUser(t, client)
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
@ -58,7 +58,7 @@ func TestTemplateEdit(t *testing.T) {
|
|||||||
assert.Equal(t, icon, updated.Icon)
|
assert.Equal(t, icon, updated.Icon)
|
||||||
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
|
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
|
||||||
})
|
})
|
||||||
t.Run("NotModified", func(t *testing.T) {
|
t.Run("FirstEmptyThenNotModified", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||||
user := coderdtest.CreateFirstUser(t, client)
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
@ -124,4 +124,102 @@ func TestTemplateEdit(t *testing.T) {
|
|||||||
assert.Equal(t, template.Name, updated.Name)
|
assert.Equal(t, template.Name, updated.Name)
|
||||||
assert.Equal(t, "", template.DisplayName)
|
assert.Equal(t, "", template.DisplayName)
|
||||||
})
|
})
|
||||||
|
t.Run("WithPropertiesThenModified", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||||
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
|
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
|
||||||
|
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
||||||
|
|
||||||
|
initialDisplayName := "This is a template"
|
||||||
|
initialDescription := "This is description"
|
||||||
|
initialIcon := "/img/icon.png"
|
||||||
|
|
||||||
|
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
|
||||||
|
ctr.DisplayName = initialDisplayName
|
||||||
|
ctr.Description = initialDescription
|
||||||
|
ctr.Icon = initialIcon
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test created template
|
||||||
|
created, err := client.Template(context.Background(), template.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, initialDisplayName, created.DisplayName)
|
||||||
|
assert.Equal(t, initialDescription, created.Description)
|
||||||
|
assert.Equal(t, initialIcon, created.Icon)
|
||||||
|
|
||||||
|
// Test the cli command.
|
||||||
|
displayName := "New Display Name 789"
|
||||||
|
icon := "/icons/new-icon.png"
|
||||||
|
cmdArgs := []string{
|
||||||
|
"templates",
|
||||||
|
"edit",
|
||||||
|
template.Name,
|
||||||
|
"--display-name", displayName,
|
||||||
|
"--icon", icon,
|
||||||
|
}
|
||||||
|
cmd, root := clitest.New(t, cmdArgs...)
|
||||||
|
clitest.SetupConfig(t, client, root)
|
||||||
|
|
||||||
|
ctx, _ := testutil.Context(t)
|
||||||
|
err = cmd.ExecuteContext(ctx)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Assert that the template metadata changed.
|
||||||
|
updated, err := client.Template(context.Background(), template.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, template.Name, updated.Name) // doesn't change
|
||||||
|
assert.Equal(t, initialDescription, updated.Description) // doesn't change
|
||||||
|
assert.Equal(t, displayName, updated.DisplayName)
|
||||||
|
assert.Equal(t, icon, updated.Icon)
|
||||||
|
})
|
||||||
|
t.Run("WithPropertiesThenEmptyEdit", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
|
||||||
|
user := coderdtest.CreateFirstUser(t, client)
|
||||||
|
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
|
||||||
|
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
|
||||||
|
|
||||||
|
initialDisplayName := "This is a template"
|
||||||
|
initialDescription := "This is description"
|
||||||
|
initialIcon := "/img/icon.png"
|
||||||
|
|
||||||
|
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
|
||||||
|
ctr.DisplayName = initialDisplayName
|
||||||
|
ctr.Description = initialDescription
|
||||||
|
ctr.Icon = initialIcon
|
||||||
|
})
|
||||||
|
|
||||||
|
// Test created template
|
||||||
|
created, err := client.Template(context.Background(), template.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, initialDisplayName, created.DisplayName)
|
||||||
|
assert.Equal(t, initialDescription, created.Description)
|
||||||
|
assert.Equal(t, initialIcon, created.Icon)
|
||||||
|
|
||||||
|
// Test the cli command.
|
||||||
|
cmdArgs := []string{
|
||||||
|
"templates",
|
||||||
|
"edit",
|
||||||
|
template.Name,
|
||||||
|
}
|
||||||
|
cmd, root := clitest.New(t, cmdArgs...)
|
||||||
|
clitest.SetupConfig(t, client, root)
|
||||||
|
|
||||||
|
ctx, _ := testutil.Context(t)
|
||||||
|
err = cmd.ExecuteContext(ctx)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Assert that the template metadata changed.
|
||||||
|
updated, err := client.Template(context.Background(), template.ID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
// Properties don't change
|
||||||
|
assert.Equal(t, template.Name, updated.Name)
|
||||||
|
assert.Equal(t, template.Description, updated.Description)
|
||||||
|
assert.Equal(t, template.DisplayName, updated.DisplayName)
|
||||||
|
// Icon is removed, as the API considers it as "delete" request
|
||||||
|
assert.Equal(t, "", updated.Icon)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
@ -2091,6 +2091,8 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
|
|||||||
CreatedBy: arg.CreatedBy,
|
CreatedBy: arg.CreatedBy,
|
||||||
UserACL: arg.UserACL,
|
UserACL: arg.UserACL,
|
||||||
GroupACL: arg.GroupACL,
|
GroupACL: arg.GroupACL,
|
||||||
|
DisplayName: arg.DisplayName,
|
||||||
|
Icon: arg.Icon,
|
||||||
}
|
}
|
||||||
q.templates = append(q.templates, template)
|
q.templates = append(q.templates, template)
|
||||||
return template, nil
|
return template, nil
|
||||||
|
@ -239,6 +239,8 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
|
|||||||
GroupACL: database.TemplateACL{
|
GroupACL: database.TemplateACL{
|
||||||
organization.ID.String(): []rbac.Action{rbac.ActionRead},
|
organization.ID.String(): []rbac.Action{rbac.ActionRead},
|
||||||
},
|
},
|
||||||
|
DisplayName: createTemplate.DisplayName,
|
||||||
|
Icon: createTemplate.Icon,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return xerrors.Errorf("insert template: %s", err)
|
return xerrors.Errorf("insert template: %s", err)
|
||||||
|
Reference in New Issue
Block a user