fleet/server/service/middleware/endpoint_utils/endpoint_utils_test.go
Lucas Manuel Rodriguez 330a708392
Changes to not rely on Fleet Desktop for Linux setup experience (#33018)
For #32788.

## Testing

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

## fleetd/orbit/Fleet Desktop

- [X] Verified compatibility with the latest released version of Fleet
(see [Must
rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md))
- [X] If the change applies to only one platform, confirmed that
`runtime.GOOS` is used as needed to isolate changes
- [x] Verified that fleetd runs on macOS, Linux and Windows
- [X] Verified auto-update works from the released version of component
to the new version (see [tools/tuf/test](../tools/tuf/test/README.md))


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

## Summary by CodeRabbit

- New Features
- Enhanced Linux setup experience: persists status on disk, resumes
automatically, and completes when software/scripts finish.
- Opens the “My Device” page only when desktop is enabled, using a
user-aware launcher on Linux.
- Linux setup status now focuses on software progress for faster,
clearer feedback.

- Bug Fixes
- Corrected auth/MDM checks: macOS requires Apple MDM; Linux no longer
blocked by MDM configuration on shared endpoints.
- Improved reliability and logging around software installation and
temporary directory cleanup.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-09-16 13:26:00 -03:00

122 lines
3 KiB
Go

package endpoint_utils
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
authz_ctx "github.com/fleetdm/fleet/v4/server/contexts/authz"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)
func TestCustomMiddlewareAfterAuth(t *testing.T) {
var (
i = 0
beforeIndex = 0
authIndex = 0
afterFirstIndex = 0
afterSecondIndex = 0
)
beforeAuthMiddleware := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
i++
beforeIndex = i
return next(ctx, req)
}
}
authFunc := func(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
i++
authIndex = i
if authctx, ok := authz_ctx.FromContext(ctx); ok {
authctx.SetChecked()
}
return next(ctx, req)
}
}
afterAuthMiddlewareFirst := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
i++
afterFirstIndex = i
return next(ctx, req)
}
}
afterAuthMiddlewareSecond := func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
i++
afterSecondIndex = i
return next(ctx, req)
}
}
r := mux.NewRouter()
ce := &CommonEndpointer[HandlerFunc]{
EP: nopEP{},
MakeDecoderFn: func(iface interface{}) kithttp.DecodeRequestFunc {
return func(ctx context.Context, r *http.Request) (request interface{}, err error) {
return nopRequest{}, nil
}
},
EncodeFn: func(ctx context.Context, w http.ResponseWriter, i interface{}) error {
w.WriteHeader(http.StatusOK)
return nil
},
AuthFunc: authFunc,
CustomMiddleware: []endpoint.Middleware{
beforeAuthMiddleware,
},
CustomMiddlewareAfterAuth: []endpoint.Middleware{
afterAuthMiddlewareFirst,
afterAuthMiddlewareSecond,
},
Router: r,
}
ce.handleEndpoint("/", func(ctx context.Context, request interface{}, svc fleet.Service) (fleet.Errorer, error) {
fmt.Printf("handler\n")
return nopResponse{}, nil
}, nil, "GET")
s := httptest.NewServer(r)
t.Cleanup(func() {
s.Close()
})
req, err := http.NewRequest("GET", s.URL+"/", nil)
require.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
t.Cleanup(func() {
resp.Body.Close()
})
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Equal(t, 1, beforeIndex)
require.Equal(t, 2, authIndex)
require.Equal(t, 3, afterFirstIndex)
require.Equal(t, 4, afterSecondIndex)
}
type nopRequest struct{}
type nopResponse struct{}
func (n nopResponse) Error() error {
return nil
}
type nopEP struct{}
func (n nopEP) CallHandlerFunc(f HandlerFunc, ctx context.Context, request interface{}, svc interface{}) (fleet.Errorer, error) {
return nopResponse{}, nil
}
func (n nopEP) Service() interface{} {
return nil
}