mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: add cli command to fetch group sync settings as json (#14694)
* chore: add cli command to fetch group sync settings as json
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"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/rbac"
|
||||
"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 TestUpdateGroupSync(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := coderdtest.DeploymentValues(t)
|
||||
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
|
||||
|
||||
owner, first := coderdenttest.New(t, &coderdenttest.Options{
|
||||
Options: &coderdtest.Options{
|
||||
DeploymentValues: dv,
|
||||
},
|
||||
LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureMultipleOrganizations: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
inv, root := clitest.New(t, "organization", "settings", "set", "groupsync")
|
||||
//nolint:gocritic // Using the owner, testing the cli not perms
|
||||
clitest.SetupConfig(t, owner, root)
|
||||
|
||||
expectedSettings := codersdk.GroupSyncSettings{
|
||||
Field: "groups",
|
||||
Mapping: map[string][]uuid.UUID{
|
||||
"test": {first.OrganizationID},
|
||||
},
|
||||
RegexFilter: regexp.MustCompile("^foo"),
|
||||
AutoCreateMissing: true,
|
||||
LegacyNameMapping: nil,
|
||||
}
|
||||
expectedData, err := json.Marshal(expectedSettings)
|
||||
require.NoError(t, err)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
inv.Stdout = buf
|
||||
inv.Stdin = bytes.NewBuffer(expectedData)
|
||||
err = inv.WithContext(ctx).Run()
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(expectedData), buf.String())
|
||||
|
||||
// Now read it back
|
||||
inv, root = clitest.New(t, "organization", "settings", "show", "groupsync")
|
||||
//nolint:gocritic // Using the owner, testing the cli not perms
|
||||
clitest.SetupConfig(t, owner, root)
|
||||
|
||||
buf = new(bytes.Buffer)
|
||||
inv.Stdout = buf
|
||||
err = inv.WithContext(ctx).Run()
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(expectedData), buf.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateRoleSync(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dv := coderdtest.DeploymentValues(t)
|
||||
dv.Experiments = []string{string(codersdk.ExperimentMultiOrganization)}
|
||||
|
||||
owner, _ := coderdenttest.New(t, &coderdenttest.Options{
|
||||
Options: &coderdtest.Options{
|
||||
DeploymentValues: dv,
|
||||
},
|
||||
LicenseOptions: &coderdenttest.LicenseOptions{
|
||||
Features: license.Features{
|
||||
codersdk.FeatureMultipleOrganizations: 1,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
inv, root := clitest.New(t, "organization", "settings", "set", "rolesync")
|
||||
//nolint:gocritic // Using the owner, testing the cli not perms
|
||||
clitest.SetupConfig(t, owner, root)
|
||||
|
||||
expectedSettings := codersdk.RoleSyncSettings{
|
||||
Field: "roles",
|
||||
Mapping: map[string][]string{
|
||||
"test": {rbac.RoleOrgAdmin()},
|
||||
},
|
||||
}
|
||||
expectedData, err := json.Marshal(expectedSettings)
|
||||
require.NoError(t, err)
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
inv.Stdout = buf
|
||||
inv.Stdin = bytes.NewBuffer(expectedData)
|
||||
err = inv.WithContext(ctx).Run()
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(expectedData), buf.String())
|
||||
|
||||
// Now read it back
|
||||
inv, root = clitest.New(t, "organization", "settings", "show", "rolesync")
|
||||
//nolint:gocritic // Using the owner, testing the cli not perms
|
||||
clitest.SetupConfig(t, owner, root)
|
||||
|
||||
buf = new(bytes.Buffer)
|
||||
inv.Stdout = buf
|
||||
err = inv.WithContext(ctx).Run()
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(expectedData), buf.String())
|
||||
})
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/idpsync"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
)
|
||||
|
||||
// @Summary Get group IdP Sync settings by organization
|
||||
@@ -61,6 +62,20 @@ func (api *API) patchGroupIDPSyncSettings(rw http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
if len(req.LegacyNameMapping) > 0 {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
Message: "Unexpected field 'legacy_group_name_mapping'. Field not allowed, set to null or remove it.",
|
||||
Detail: "legacy_group_name_mapping is deprecated, use mapping instead",
|
||||
Validations: []codersdk.ValidationError{
|
||||
{
|
||||
Field: "legacy_group_name_mapping",
|
||||
Detail: "field is not allowed",
|
||||
},
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
//nolint:gocritic // Requires system context to update runtime config
|
||||
sysCtx := dbauthz.AsSystemRestricted(ctx)
|
||||
err := api.IDPSync.UpdateGroupSettings(sysCtx, org.ID, api.Database, req)
|
||||
|
||||
Reference in New Issue
Block a user