Skip to content

Commit

Permalink
Add unit test for fips metadata attribute in enroll/checkin (#4514)
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-laterman authored Feb 26, 2025
1 parent d3cdf9b commit ba68a24
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
21 changes: 19 additions & 2 deletions internal/pkg/api/handleCheckin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,23 @@ func TestValidateCheckinRequest(t *testing.T) {
},
expValid: validatedCheckin{},
},
{
name: "local metadata has fips attribute",
req: &http.Request{
Body: io.NopCloser(strings.NewReader(`{"status": "online", "message": "test message", "local_metadata": {"elastic": {"agent": {"id": "testid", "fips": true}}}}`)),
},
expErr: nil,
cfg: &config.Server{
Limits: config.ServerLimits{
CheckinLimit: config.Limit{
MaxBody: 0,
},
},
},
expValid: validatedCheckin{
rawMeta: []byte(`{"elastic": {"agent": {"id": "testid", "fips": true}}}`),
},
},
}

for _, tc := range tests {
Expand All @@ -981,17 +998,17 @@ func TestValidateCheckinRequest(t *testing.T) {
assert.NoError(t, err)
wr := httptest.NewRecorder()
logger := testlog.SetLogger(t)
valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, nil)
valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, &model.Agent{LocalMetadata: json.RawMessage(`{}`)})
if tc.expErr == nil {
assert.NoError(t, err)
assert.Equal(t, tc.expValid.rawMeta, valid.rawMeta)
} else {
// Asserting error messages prior to ErrorAs becuase ErrorAs modifies
// the target error. If we assert error messages after calling ErrorAs
// we will end up with false positives.
assert.Equal(t, tc.expErr.Error(), err.Error())
assert.ErrorAs(t, err, &tc.expErr)
}
assert.Equal(t, tc.expValid, valid)
})
}
}
21 changes: 15 additions & 6 deletions internal/pkg/api/handleEnroll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"strings"
"testing"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/elastic/fleet-server/v7/internal/pkg/apikey"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
Expand All @@ -24,9 +28,6 @@ import (
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/fleet-server/v7/internal/pkg/rollback"
ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestRemoveDuplicateStr(t *testing.T) {
Expand Down Expand Up @@ -569,7 +570,15 @@ func TestEnrollerT_retrieveStaticTokenEnrollmentToken(t *testing.T) {
}

func TestValidateEnrollRequest(t *testing.T) {
req, err := validateRequest(context.Background(), strings.NewReader("not a json"))
assert.Equal(t, "Bad request: unable to decode enroll request", err.Error())
assert.Nil(t, req)
t.Run("invalid json", func(t *testing.T) {
req, err := validateRequest(context.Background(), strings.NewReader("not a json"))
assert.Equal(t, "Bad request: unable to decode enroll request", err.Error())
assert.Nil(t, req)
})
t.Run("fips attribute in local metadata", func(t *testing.T) {
req, err := validateRequest(context.Background(), strings.NewReader(`{"type": "PERMANENT", "metadata": {"local": {"elastic": {"agent": {"fips": true, "snapshot": false}}}}}`))
assert.NoError(t, err)
assert.Equal(t, PERMANENT, req.Type)
assert.Equal(t, json.RawMessage(`{"elastic": {"agent": {"fips": true, "snapshot": false}}}`), req.Metadata.Local)
})
}
11 changes: 11 additions & 0 deletions internal/pkg/checkin/bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ func TestBulkSimple(t *testing.T) {
"",
nil,
},
{
"has meta with fips attribute",
"metaCaseID",
"online",
"message",
[]byte(`{"fips":true,"snapshot":false}`),
nil,
nil,
"",
nil,
},
{
"Singled field case",
"singleFieldId",
Expand Down

0 comments on commit ba68a24

Please sign in to comment.