|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package api |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/elastic/fleet-server/v7/internal/pkg/config" |
| 17 | + "github.com/elastic/fleet-server/v7/internal/pkg/dl" |
| 18 | + "github.com/elastic/fleet-server/v7/internal/pkg/model" |
| 19 | + ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing" |
| 20 | + testlog "github.com/elastic/fleet-server/v7/internal/pkg/testing/log" |
| 21 | + "github.com/stretchr/testify/mock" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func Test_Audit_validateUnenrollRequst(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + req *http.Request |
| 29 | + cfg *config.Server |
| 30 | + valid *AuditUnenrollRequest |
| 31 | + err error |
| 32 | + }{{ |
| 33 | + name: "ok", |
| 34 | + req: &http.Request{ |
| 35 | + Body: io.NopCloser(strings.NewReader(`{"reason":"uninstall", "timestamp": "2024-01-01T12:00:00.000Z"}`)), |
| 36 | + }, |
| 37 | + cfg: &config.Server{}, |
| 38 | + valid: &AuditUnenrollRequest{ |
| 39 | + Reason: Uninstall, |
| 40 | + Timestamp: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC), |
| 41 | + }, |
| 42 | + err: nil, |
| 43 | + }, { |
| 44 | + name: "not json object", |
| 45 | + req: &http.Request{ |
| 46 | + Body: io.NopCloser(strings.NewReader(`{"invalidJson":}`)), |
| 47 | + }, |
| 48 | + cfg: &config.Server{}, |
| 49 | + valid: nil, |
| 50 | + err: &BadRequestErr{msg: "unable to decode audit/unenroll request"}, |
| 51 | + }, { |
| 52 | + name: "bad reason", |
| 53 | + req: &http.Request{ |
| 54 | + Body: io.NopCloser(strings.NewReader(`{"reason":"bad reason", "timestamp": "2024-01-01T12:00:00.000Z"}`)), |
| 55 | + }, |
| 56 | + cfg: &config.Server{}, |
| 57 | + valid: nil, |
| 58 | + err: &BadRequestErr{msg: "audit/unenroll request invalid reason"}, |
| 59 | + }, { |
| 60 | + name: "too large", |
| 61 | + req: &http.Request{ |
| 62 | + Body: io.NopCloser(strings.NewReader(`{"reason":"uninstalled", "timestamp": "2024-01-01T12:00:00.000Z"}`)), |
| 63 | + }, |
| 64 | + cfg: &config.Server{ |
| 65 | + Limits: config.ServerLimits{ |
| 66 | + AuditUnenrollLimit: config.Limit{ |
| 67 | + MaxBody: 10, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + valid: nil, |
| 72 | + err: &BadRequestErr{msg: "unable to decode audit/unenroll request"}, |
| 73 | + }} |
| 74 | + |
| 75 | + for _, tc := range tests { |
| 76 | + t.Run(tc.name, func(t *testing.T) { |
| 77 | + audit := AuditT{cfg: tc.cfg} |
| 78 | + w := httptest.NewRecorder() |
| 79 | + |
| 80 | + r, err := audit.validateUnenrollRequest(testlog.SetLogger(t), w, tc.req) |
| 81 | + if tc.err != nil { |
| 82 | + require.EqualError(t, err, tc.err.Error()) |
| 83 | + } else { |
| 84 | + require.NoError(t, err) |
| 85 | + } |
| 86 | + require.Equal(t, tc.valid, r) |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func Test_Audit_markUnenroll(t *testing.T) { |
| 92 | + agent := &model.Agent{ |
| 93 | + ESDocument: model.ESDocument{ |
| 94 | + Id: "test-id", |
| 95 | + }, |
| 96 | + } |
| 97 | + bulker := ftesting.NewMockBulk() |
| 98 | + bulker.On("Update", mock.Anything, dl.FleetAgents, agent.Id, mock.Anything, mock.Anything, mock.Anything).Return(nil) |
| 99 | + audit := AuditT{bulk: bulker} |
| 100 | + logger := testlog.SetLogger(t) |
| 101 | + err := audit.markUnenroll(context.Background(), logger, &AuditUnenrollRequest{Reason: Uninstall, Timestamp: time.Now().UTC()}, agent) |
| 102 | + require.NoError(t, err) |
| 103 | + bulker.AssertExpectations(t) |
| 104 | +} |
| 105 | + |
| 106 | +func Test_Audit_unenroll(t *testing.T) { |
| 107 | + t.Run("agent has audit_unenroll_reason", func(t *testing.T) { |
| 108 | + agent := &model.Agent{ |
| 109 | + AuditUnenrolledReason: string(Uninstall), |
| 110 | + } |
| 111 | + audit := &AuditT{} |
| 112 | + err := audit.unenroll(testlog.SetLogger(t), nil, nil, agent) |
| 113 | + require.EqualError(t, err, ErrAuditUnenrollReason.Error()) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("ok", func(t *testing.T) { |
| 117 | + agent := &model.Agent{ |
| 118 | + ESDocument: model.ESDocument{ |
| 119 | + Id: "test-id", |
| 120 | + }, |
| 121 | + } |
| 122 | + bulker := ftesting.NewMockBulk() |
| 123 | + bulker.On("Update", mock.Anything, dl.FleetAgents, agent.Id, mock.Anything, mock.Anything, mock.Anything).Return(nil) |
| 124 | + |
| 125 | + audit := &AuditT{ |
| 126 | + bulk: bulker, |
| 127 | + cfg: &config.Server{}, |
| 128 | + } |
| 129 | + req := &http.Request{ |
| 130 | + Body: io.NopCloser(strings.NewReader(`{"reason": "uninstall", "timestamp": "2024-01-01T12:00:00.000Z"}`)), |
| 131 | + } |
| 132 | + err := audit.unenroll(testlog.SetLogger(t), httptest.NewRecorder(), req, agent) |
| 133 | + require.NoError(t, err) |
| 134 | + bulker.AssertExpectations(t) |
| 135 | + }) |
| 136 | +} |
0 commit comments