fleet/server/service/middleware/authzcheck/authzcheck_test.go
Victor Lyuboslavsky c88cc953fb
Refactor endpoint_utils for modularization (#36484)
Resolves #37192

Separating generic endpoint_utils middleware logic from domain-specific
business logic. New bounded contexts would share the generic logic and
implement their own domain-specific logic. The two approaches used in
this PR are:
- Use common `platform` types
- Use interfaces

In the next PR we will move `endpointer_utils`, `authzcheck` and
`ratelimit` into `platform` directory.

# Checklist for submitter

- [x] Added changes file

## Testing

- [x] Added/updated tests
- [x] QA'd all new/changed functionality manually



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Restructured internal error handling and context management to support
bounded context architecture.
* Improved error context collection and telemetry observability through
a provider-based mechanism.
* Decoupled licensing and authentication concerns into interfaces for
better modularity.

* **Chores**
* Updated internal package dependencies to align with new architectural
boundaries.

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-12-31 09:12:00 -06:00

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)
}