fleet/server/service/maintained_apps_test.go
Scott Gress 30632040b1
Fix addFleetMaintainedAppEndpoint to accept fleet_id param (#41805)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #41771

# Details

Solves two issues in 4.82:

1. The `fleet_id` param in `POST /software/fleet_maintained_apps` wasn't
being read, causing all FMAs using that param to be added to fleet ID 0
(unassigned aka No Team)
2. We were logging deprecation warnings for body params even if the
topic was turned off, meaning Fleet would generate deprecation warnings
in certain cases that users wouldn't be able to fix.

# 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
Added unit tests for the decoder since it's got one-off logic in it
- [X] QA'd all new/changed functionality manually
  - [X] Added an FMA to a fleet successfully using `fleet_id`
- [X] Added an FMA to a fleet successfully using `team_id` and saw
deprecation warning
  - [X] Added an FMA to "Unassigned" successfully using `fleet_id=0`
  - [X] Added an FMA to "Unassigned" successfully using `team_id=0`
- [X] Added an FMA to "Unassigned" successfully with no `fleet_id` or
`team_id` param (this seems like a bug but it's existing behavior)

---------

Co-authored-by: Ian Littman <iansltx@gmail.com>
2026-03-17 09:59:03 -05:00

73 lines
1.7 KiB
Go

package service
import (
"bytes"
"context"
"io"
"net/http"
"testing"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAddFleetMaintainedAppDecodeRequest(t *testing.T) {
t.Parallel()
tests := []struct {
name string
body string
wantTeamID *uint
wantErr string
}{
{
name: "fleet_id accepted",
body: `{"fleet_id": 42, "fleet_maintained_app_id": 1}`,
wantTeamID: ptr.Uint(42),
},
{
name: "team_id still accepted",
body: `{"team_id": 7, "fleet_maintained_app_id": 1}`,
wantTeamID: ptr.Uint(7),
},
{
name: "neither provided",
body: `{"fleet_maintained_app_id": 1}`,
wantTeamID: nil,
},
{
name: "both provided is an error",
body: `{"team_id": 1, "fleet_id": 2, "fleet_maintained_app_id": 1}`,
wantErr: `Specify only one of "team_id" or "fleet_id"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "/", io.NopCloser(bytes.NewBufferString(tt.body)))
require.NoError(t, err)
result, err := addFleetMaintainedAppRequest{}.DecodeRequest(context.Background(), r)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
req := result.(*addFleetMaintainedAppRequest)
if tt.wantTeamID == nil {
assert.Nil(t, req.TeamID)
} else {
require.NotNil(t, req.TeamID)
assert.Equal(t, *tt.wantTeamID, *req.TeamID)
}
// FleetID should always be nil after normalization
assert.Nil(t, req.FleetID)
})
}
}