mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #37192 - Move /server/service/middleware/endpoint_utils to /server/platform/endpointer - Move /server/service/middleware/authzcheck to /server/platform/middleware/authzcheck - Move /server/service/middleware/ratelimit to /server/platform/middleware/ratelimit # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Refactor** * Reorganized internal endpoint utilities to a centralized platform location for improved code organization and maintainability. No functional changes to existing features or APIs. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
70 lines
1.7 KiB
Go
70 lines
1.7 KiB
Go
package authzcheck
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/authz"
|
|
platform_http "github.com/fleetdm/fleet/v4/server/platform/http"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAuthzCheck(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
checker := NewMiddleware()
|
|
|
|
check := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
authCtx, ok := authz.FromContext(ctx)
|
|
require.True(t, ok)
|
|
authCtx.SetChecked()
|
|
return struct{}{}, nil
|
|
}
|
|
check = checker.AuthzCheck()(check)
|
|
|
|
_, err := check(context.Background(), struct{}{})
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestAuthzCheckAuthFailed(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
checker := NewMiddleware()
|
|
|
|
check := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
return nil, platform_http.NewAuthFailedError("failed")
|
|
}
|
|
check = checker.AuthzCheck()(check)
|
|
|
|
_, err := check(context.Background(), struct{}{})
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "failed")
|
|
}
|
|
|
|
func TestAuthzCheckAuthRequired(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
checker := NewMiddleware()
|
|
|
|
check := func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
return nil, platform_http.NewAuthRequiredError("required")
|
|
}
|
|
check = checker.AuthzCheck()(check)
|
|
|
|
_, err := check(context.Background(), struct{}{})
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "required")
|
|
}
|
|
|
|
func TestAuthzCheckMissing(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
checker := NewMiddleware()
|
|
|
|
nocheck := func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
|
|
nocheck = checker.AuthzCheck()(nocheck)
|
|
|
|
_, err := nocheck(context.Background(), struct{}{})
|
|
assert.Error(t, err)
|
|
}
|