Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore add unit test for role sync
  • Loading branch information
Emyrk committed Sep 17, 2024
commit 6225ddf92beca8d2795ca28fce1c7a98580cc512
11 changes: 7 additions & 4 deletions cli/organizationsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ func (r *RootCmd) updateOrganizationSetting(orgContext *OrganizationContext) *se
}
setting, err = client.PatchGroupIDPSyncSettings(ctx, org.ID.String(), req)
case "rolesync", "role-sync":
// TODO: Implement role sync settings.
return fmt.Errorf("role sync settings are not implemented")
var req codersdk.RoleSyncSettings
err = json.Unmarshal(inputData, &req)
if err != nil {
return xerrors.Errorf("unmarshalling role sync settings: %w", err)
}
setting, err = client.PatchRoleIDPSyncSettings(ctx, org.ID.String(), req)
default:
_, _ = fmt.Fprintln(inv.Stderr, "Valid organization settings are: 'groupsync', 'rolesync'")
return fmt.Errorf("unknown organization setting %s", inv.Args[0])
Expand Down Expand Up @@ -126,8 +130,7 @@ func (r *RootCmd) printOrganizationSetting(orgContext *OrganizationContext) *ser
case "groupsync", "group-sync":
setting, err = client.GroupIDPSyncSettings(ctx, org.ID.String())
case "rolesync", "role-sync":
// TODO: Implement role sync settings.
return fmt.Errorf("role sync settings are not implemented")
setting, err = client.RoleIDPSyncSettings(ctx, org.ID.String())
default:
_, _ = fmt.Fprintln(inv.Stderr, "Valid organization settings are: 'groupsync', 'rolesync'")
return fmt.Errorf("unknown organization setting %s", inv.Args[0])
Expand Down
51 changes: 51 additions & 0 deletions enterprise/cli/organizationsettings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

"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) {

Check failure on line 21 in enterprise/cli/organizationsettings_test.go

View workflow job for this annotation

GitHub Actions / lint

TestUpdateGroupSync's subtests should call t.Parallel (tparallel)
t.Parallel()

t.Run("OK", func(t *testing.T) {
Expand All @@ -37,7 +38,7 @@

ctx := testutil.Context(t, testutil.WaitLong)
inv, root := clitest.New(t, "organization", "settings", "set", "groupsync")
clitest.SetupConfig(t, owner, root)

Check failure on line 41 in enterprise/cli/organizationsettings_test.go

View workflow job for this annotation

GitHub Actions / lint

ruleguard: The CLI will be operating as the owner user, which has unrestricted permissions. Consider creating a different user. (gocritic)

expectedSettings := codersdk.GroupSyncSettings{
Field: "groups",
Expand Down Expand Up @@ -69,3 +70,53 @@
require.JSONEq(t, string(expectedData), buf.String())
})
}

func TestUpdateRoleSync(t *testing.T) {

Check failure on line 74 in enterprise/cli/organizationsettings_test.go

View workflow job for this annotation

GitHub Actions / lint

TestUpdateRoleSync's subtests should call t.Parallel (tparallel)
t.Parallel()

t.Run("OK", func(t *testing.T) {
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")
clitest.SetupConfig(t, owner, root)

Check failure on line 94 in enterprise/cli/organizationsettings_test.go

View workflow job for this annotation

GitHub Actions / lint

ruleguard: The CLI will be operating as the owner user, which has unrestricted permissions. Consider creating a different user. (gocritic)

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")
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())
})
}
Loading