fleet/client/orbit_client_eua_test.go
Konstantin Sykulev 2245359ad1
Orbit passes EUA token during enrollment (#43369)
**Related issue:** Resolves #41379

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [x] Added/updated automated tests
- [ ] Where appropriate, [automated tests simulate multiple hosts and
test for host
isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing)
(updates to one hosts's records do not affect another)
- [ ] 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
- [ ] Verified that fleetd runs on macOS, Linux and Windows
- [ ] 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**
  * Added EUA token support to Orbit enrollment workflow
  * Introduced `--eua-token` CLI flag for Windows MDM enrollment
  * Windows MSI packages now support EUA_TOKEN property (Orbit v1.55.0+)

* **Tests**
* Added tests for EUA token handling in enrollment and Windows packaging

* **Documentation**
* Added changelog entry documenting EUA token inclusion in enrollment
requests

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-13 16:19:47 -05:00

80 lines
2.3 KiB
Go

package client
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnrollSendsEUAToken(t *testing.T) {
// nolint:gosec // not a real credential, test-only JWT fragment
euaTokenValue := "eyJhbGciOiJSUzI1NiJ9.test-eua-token"
const testNodeKey = "test-node-key-abc"
testCases := []struct {
name string
token string
assert func(t *testing.T, receivedBody fleet.EnrollOrbitRequest, rawBody []byte)
}{
{
name: "eua_token included in enroll request when set",
token: euaTokenValue,
assert: func(t *testing.T, receivedBody fleet.EnrollOrbitRequest, rawBody []byte) {
require.Equal(t, euaTokenValue, receivedBody.EUAToken)
},
},
{
name: "eua_token omitted from enroll request when empty",
token: "",
assert: func(t *testing.T, receivedBody fleet.EnrollOrbitRequest, rawBody []byte) {
// Verify the eua_token key is not present in the JSON body (omitempty).
require.Falsef(t, bytes.Contains(rawBody, []byte(`"eua_token"`)),
"eua_token should not appear in JSON when empty, got: %s", string(rawBody))
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var receivedBody fleet.EnrollOrbitRequest
var rawBody []byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error
rawBody, err = io.ReadAll(r.Body)
assert.NoError(t, err)
assert.NoError(t, json.Unmarshal(rawBody, &receivedBody))
resp := fleet.EnrollOrbitResponse{OrbitNodeKey: testNodeKey}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(resp)
assert.NoError(t, err)
}))
defer srv.Close()
oc := &OrbitClient{
enrollSecret: "secret",
hostInfo: fleet.OrbitHostInfo{HardwareUUID: "uuid-1", Platform: "windows"},
}
oc.SetEUAToken(tc.token)
bc, err := NewBaseClient(srv.URL, true, "", "", nil, fleet.CapabilityMap{}, nil)
require.NoError(t, err)
oc.BaseClient = bc
nodeKey, err := oc.enroll()
require.NoError(t, err)
require.Equal(t, testNodeKey, nodeKey)
require.Equal(t, "secret", receivedBody.EnrollSecret)
require.Equal(t, "uuid-1", receivedBody.HardwareUUID)
tc.assert(t, receivedBody, rawBody)
})
}
}