Skip to content

Commit ba68a24

Browse files
Add unit test for fips metadata attribute in enroll/checkin (#4514)
1 parent d3cdf9b commit ba68a24

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

internal/pkg/api/handleCheckin_test.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,23 @@ func TestValidateCheckinRequest(t *testing.T) {
973973
},
974974
expValid: validatedCheckin{},
975975
},
976+
{
977+
name: "local metadata has fips attribute",
978+
req: &http.Request{
979+
Body: io.NopCloser(strings.NewReader(`{"status": "online", "message": "test message", "local_metadata": {"elastic": {"agent": {"id": "testid", "fips": true}}}}`)),
980+
},
981+
expErr: nil,
982+
cfg: &config.Server{
983+
Limits: config.ServerLimits{
984+
CheckinLimit: config.Limit{
985+
MaxBody: 0,
986+
},
987+
},
988+
},
989+
expValid: validatedCheckin{
990+
rawMeta: []byte(`{"elastic": {"agent": {"id": "testid", "fips": true}}}`),
991+
},
992+
},
976993
}
977994

978995
for _, tc := range tests {
@@ -981,17 +998,17 @@ func TestValidateCheckinRequest(t *testing.T) {
981998
assert.NoError(t, err)
982999
wr := httptest.NewRecorder()
9831000
logger := testlog.SetLogger(t)
984-
valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, nil)
1001+
valid, err := checkin.validateRequest(logger, wr, tc.req, time.Time{}, &model.Agent{LocalMetadata: json.RawMessage(`{}`)})
9851002
if tc.expErr == nil {
9861003
assert.NoError(t, err)
1004+
assert.Equal(t, tc.expValid.rawMeta, valid.rawMeta)
9871005
} else {
9881006
// Asserting error messages prior to ErrorAs becuase ErrorAs modifies
9891007
// the target error. If we assert error messages after calling ErrorAs
9901008
// we will end up with false positives.
9911009
assert.Equal(t, tc.expErr.Error(), err.Error())
9921010
assert.ErrorAs(t, err, &tc.expErr)
9931011
}
994-
assert.Equal(t, tc.expValid, valid)
9951012
})
9961013
}
9971014
}

internal/pkg/api/handleEnroll_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
"strings"
1616
"testing"
1717

18+
"github.com/rs/zerolog"
19+
"github.com/stretchr/testify/assert"
20+
"github.com/stretchr/testify/mock"
21+
1822
"github.com/elastic/fleet-server/v7/internal/pkg/apikey"
1923
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
2024
"github.com/elastic/fleet-server/v7/internal/pkg/cache"
@@ -24,9 +28,6 @@ import (
2428
"github.com/elastic/fleet-server/v7/internal/pkg/model"
2529
"github.com/elastic/fleet-server/v7/internal/pkg/rollback"
2630
ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
27-
"github.com/rs/zerolog"
28-
"github.com/stretchr/testify/assert"
29-
"github.com/stretchr/testify/mock"
3031
)
3132

3233
func TestRemoveDuplicateStr(t *testing.T) {
@@ -569,7 +570,15 @@ func TestEnrollerT_retrieveStaticTokenEnrollmentToken(t *testing.T) {
569570
}
570571

571572
func TestValidateEnrollRequest(t *testing.T) {
572-
req, err := validateRequest(context.Background(), strings.NewReader("not a json"))
573-
assert.Equal(t, "Bad request: unable to decode enroll request", err.Error())
574-
assert.Nil(t, req)
573+
t.Run("invalid json", func(t *testing.T) {
574+
req, err := validateRequest(context.Background(), strings.NewReader("not a json"))
575+
assert.Equal(t, "Bad request: unable to decode enroll request", err.Error())
576+
assert.Nil(t, req)
577+
})
578+
t.Run("fips attribute in local metadata", func(t *testing.T) {
579+
req, err := validateRequest(context.Background(), strings.NewReader(`{"type": "PERMANENT", "metadata": {"local": {"elastic": {"agent": {"fips": true, "snapshot": false}}}}}`))
580+
assert.NoError(t, err)
581+
assert.Equal(t, PERMANENT, req.Type)
582+
assert.Equal(t, json.RawMessage(`{"elastic": {"agent": {"fips": true, "snapshot": false}}}`), req.Metadata.Local)
583+
})
575584
}

internal/pkg/checkin/bulk_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ func TestBulkSimple(t *testing.T) {
107107
"",
108108
nil,
109109
},
110+
{
111+
"has meta with fips attribute",
112+
"metaCaseID",
113+
"online",
114+
"message",
115+
[]byte(`{"fips":true,"snapshot":false}`),
116+
nil,
117+
nil,
118+
"",
119+
nil,
120+
},
110121
{
111122
"Singled field case",
112123
"singleFieldId",

0 commit comments

Comments
 (0)