mirror of
https://github.com/coder/coder.git
synced 2026-11-10 19:24:37 +08:00
Upgrades to slog v3 which includes a small, but backward incompatible API change to the acceptible call arguments when logging. This change allows us to verify via compile time type checking that arguments are correct and won't cause a panic, as was possible in slog v1, which this replaces (v2 was tagged but never used in coder/coder). It also updates dependencies that also use slog and were updated. I've left the `aibridge` dependency as a commit SHA, under the assumption that the team there (cc @pawbana @dannykopping ) will tag and update the dependency soon and on their own schedule. Other dependencies, I pushed new tags.
98 lines
2.3 KiB
Go
98 lines
2.3 KiB
Go
package enidpsync_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"cdr.dev/slog/v3/sloggers/slogtest"
|
|
|
|
"github.com/coder/coder/v2/coderd/entitlements"
|
|
"github.com/coder/coder/v2/coderd/idpsync"
|
|
"github.com/coder/coder/v2/coderd/runtimeconfig"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/enterprise/coderd/enidpsync"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestEnterpriseParseGroupClaims(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
entitled := entitlements.New()
|
|
entitled.Modify(func(entitlements *codersdk.Entitlements) {
|
|
entitlements.Features[codersdk.FeatureTemplateRBAC] = codersdk.Feature{
|
|
Entitlement: codersdk.EntitlementEntitled,
|
|
Enabled: true,
|
|
}
|
|
})
|
|
|
|
t.Run("NoEntitlements", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
|
|
runtimeconfig.NewManager(),
|
|
entitlements.New(),
|
|
idpsync.DeploymentSyncSettings{})
|
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
|
|
|
params, err := s.ParseGroupClaims(ctx, jwt.MapClaims{})
|
|
require.Nil(t, err)
|
|
|
|
require.False(t, params.SyncEntitled)
|
|
})
|
|
|
|
t.Run("NotInAllowList", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
|
|
runtimeconfig.NewManager(),
|
|
entitled,
|
|
idpsync.DeploymentSyncSettings{
|
|
GroupField: "groups",
|
|
GroupAllowList: map[string]struct{}{
|
|
"foo": {},
|
|
},
|
|
})
|
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
|
|
|
// Try with incorrect group
|
|
_, err := s.ParseGroupClaims(ctx, jwt.MapClaims{
|
|
"groups": []string{"bar"},
|
|
})
|
|
require.NotNil(t, err)
|
|
require.Equal(t, 403, err.Code)
|
|
|
|
// Try with no groups
|
|
_, err = s.ParseGroupClaims(ctx, jwt.MapClaims{})
|
|
require.NotNil(t, err)
|
|
require.Equal(t, 403, err.Code)
|
|
})
|
|
|
|
t.Run("InAllowList", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
|
|
runtimeconfig.NewManager(),
|
|
entitled,
|
|
idpsync.DeploymentSyncSettings{
|
|
GroupField: "groups",
|
|
GroupAllowList: map[string]struct{}{
|
|
"foo": {},
|
|
},
|
|
})
|
|
|
|
ctx := testutil.Context(t, testutil.WaitMedium)
|
|
|
|
claims := jwt.MapClaims{
|
|
"groups": []string{"foo", "bar"},
|
|
}
|
|
params, err := s.ParseGroupClaims(ctx, claims)
|
|
require.Nil(t, err)
|
|
require.True(t, params.SyncEntitled)
|
|
require.Equal(t, claims, params.MergedClaims)
|
|
})
|
|
}
|