fix(cli): address chat share add review feedback

This commit is contained in:
Danielle Maywood
2026-05-29 14:38:33 +00:00
parent 13e7082517
commit 730c769ab3
3 changed files with 117 additions and 7 deletions
+6 -7
View File
@@ -29,11 +29,10 @@ func (r *RootCmd) chatShareCommand() *serpent.Command {
const chatShareDefaultGroupDisplay = "-" const chatShareDefaultGroupDisplay = "-"
type chatRoleLookupParams struct { type chatRoleLookupParams struct {
Client *codersdk.Client Client *codersdk.Client
OrgID uuid.UUID OrgID uuid.UUID
OrgName string Users [][2]string
Users [][2]string Groups [][2]string
Groups [][2]string
} }
func parseChatShareID(raw string) (uuid.UUID, error) { func parseChatShareID(raw string) (uuid.UUID, error) {
@@ -86,7 +85,7 @@ func fetchChatUsersAndGroups(ctx context.Context, params chatRoleLookupParams) (
} }
} }
if userID == "" { if userID == "" {
return nil, nil, xerrors.Errorf("could not find user %s in the organization %s", username, params.OrgName) return nil, nil, xerrors.Errorf("could not find user %s in the organization %s", username, params.OrgID.String())
} }
chatRole, err := stringToChatRole(role) chatRole, err := stringToChatRole(role)
@@ -118,7 +117,7 @@ func fetchChatUsersAndGroups(ctx context.Context, params chatRoleLookupParams) (
} }
} }
if orgGroup == nil { if orgGroup == nil {
return nil, nil, xerrors.Errorf("could not find group named %s belonging to the organization %s", groupName, params.OrgName) return nil, nil, xerrors.Errorf("could not find group named %s belonging to the organization %s", groupName, params.OrgID.String())
} }
chatRole, err := stringToChatRole(role) chatRole, err := stringToChatRole(role)
+23
View File
@@ -152,6 +152,29 @@ func TestExpChatShareAdd(t *testing.T) {
require.Contains(t, err.Error(), "invalid role \"write\"") require.Contains(t, err.Error(), "invalid role \"write\"")
}) })
t.Run("RejectsMissingUserWithOrganizationID", func(t *testing.T) {
t.Parallel()
client, db := coderdtest.NewWithDatabase(t, nil)
firstUser := coderdtest.CreateFirstUser(t, client)
modelConfig := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{})
chat := dbgen.Chat(t, db, database.Chat{
OrganizationID: firstUser.OrganizationID,
OwnerID: firstUser.UserID,
LastModelConfigID: modelConfig.ID,
Title: "share add missing user",
})
ctx := testutil.Context(t, testutil.WaitMedium)
inv, root := clitest.New(t, "exp", "chat", "share", "add", chat.ID.String(), "--user", "missing-user:read")
clitest.SetupConfig(t, client, root) //nolint:gocritic // Chat ACL operations require the chat owner in this fixture.
err := inv.WithContext(ctx).Run()
require.Error(t, err)
require.Contains(t, err.Error(), "could not find user missing-user in the organization")
require.Contains(t, err.Error(), firstUser.OrganizationID.String())
})
t.Run("RequiresActor", func(t *testing.T) { t.Run("RequiresActor", func(t *testing.T) {
t.Parallel() t.Parallel()
+88
View File
@@ -0,0 +1,88 @@
package cli_test
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cli/clitest"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/testutil"
)
func TestEnterpriseExpChatShareAdd(t *testing.T) {
t.Parallel()
t.Run("ShareWithGroupExplicitReadRole", func(t *testing.T) {
t.Parallel()
client, db, firstUser := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
},
})
group := coderdtest.CreateGroup(t, client, firstUser.OrganizationID, "chat-share-group")
modelConfig := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{})
chat := dbgen.Chat(t, db, database.Chat{
OrganizationID: firstUser.OrganizationID,
OwnerID: firstUser.UserID,
LastModelConfigID: modelConfig.ID,
Title: "share add group",
})
ctx := testutil.Context(t, testutil.WaitMedium)
inv, root := newCLI(t, "exp", "chat", "share", "add", chat.ID.String(), "--group", group.Name+":read")
clitest.SetupConfig(t, client, root)
out := new(bytes.Buffer)
inv.Stdout = out
err := inv.WithContext(ctx).Run()
require.NoError(t, err)
acl, err := codersdk.NewExperimentalClient(client).GetChatACL(ctx, chat.ID)
require.NoError(t, err)
require.Len(t, acl.Groups, 1)
assert.Equal(t, group.ID, acl.Groups[0].ID)
assert.Equal(t, group.Name, acl.Groups[0].Name)
assert.Equal(t, codersdk.ChatRoleRead, acl.Groups[0].Role)
assert.Contains(t, out.String(), group.Name)
assert.Contains(t, out.String(), string(codersdk.ChatRoleRead))
})
t.Run("RejectsMissingGroupWithOrganizationID", func(t *testing.T) {
t.Parallel()
client, db, firstUser := coderdenttest.NewWithDatabase(t, &coderdenttest.Options{
LicenseOptions: &coderdenttest.LicenseOptions{
Features: license.Features{
codersdk.FeatureTemplateRBAC: 1,
},
},
})
modelConfig := dbgen.ChatModelConfig(t, db, database.ChatModelConfig{})
chat := dbgen.Chat(t, db, database.Chat{
OrganizationID: firstUser.OrganizationID,
OwnerID: firstUser.UserID,
LastModelConfigID: modelConfig.ID,
Title: "share add missing group",
})
ctx := testutil.Context(t, testutil.WaitMedium)
inv, root := newCLI(t, "exp", "chat", "share", "add", chat.ID.String(), "--group", "missing-group:read")
clitest.SetupConfig(t, client, root)
err := inv.WithContext(ctx).Run()
require.Error(t, err)
require.Contains(t, err.Error(), "could not find group named missing-group belonging to the organization")
require.Contains(t, err.Error(), firstUser.OrganizationID.String())
})
}