fleet/server/webhooks/host_status_test.go
Victor Lyuboslavsky 592a7450e3
Enabling setting host status webhook at the team level via REST API and fleetctl apply/gitops. (#17186)
Enabling setting host status webhook at the team level via REST API and
fleetctl apply/gitops.
#14916

Example payload:
```json
{
    "data": {
        "days_unseen": 3,
        "host_ids": [
            10724,
            10726,
            10738,
            10739,
            10740,
            10741,
            10742,
            10744,
            10745,
            10746,
            10747,
            10748,
            10749
        ],
        "team_id": 3,
        "total_hosts": 15,
        "unseen_hosts": 13
    },
    "text": "More than 86.67% of your hosts have not checked into Fleet for more than 3 days. You've been sent this message because the Host status webhook is enabled in your Fleet instance."
}
```

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2024-03-04 12:35:27 -06:00

152 lines
4.3 KiB
Go

package webhooks
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
kitlog "github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTriggerHostStatusWebhook(t *testing.T) {
ds := new(mock.Store)
requestBody := ""
count := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestBodyBytes, err := io.ReadAll(r.Body)
require.NoError(t, err)
requestBody = string(requestBodyBytes)
count++
}))
defer ts.Close()
ac := &fleet.AppConfig{
WebhookSettings: fleet.WebhookSettings{
HostStatusWebhook: fleet.HostStatusWebhookSettings{
Enable: true,
DestinationURL: ts.URL,
HostPercentage: 43,
DaysCount: 2,
},
},
}
ds.AppConfigFunc = func(context.Context) (*fleet.AppConfig, error) {
return ac, nil
}
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, teamID *uint, daysCount int) (int, []uint, error) {
assert.Equal(t, 2, daysCount)
return 10, []uint{1, 2, 3, 4, 5, 6}, nil
}
ds.TeamsSummaryFunc = func(ctx context.Context) ([]*fleet.TeamSummary, error) {
return nil, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger()))
assert.Equal(
t,
`{"data":{"days_unseen":2,"host_ids":[1,2,3,4,5,6],"total_hosts":10,"unseen_hosts":6},"text":"More than 60.00% of your hosts have not checked into Fleet for more than 2 days. You've been sent this message because the Host status webhook is enabled in your Fleet instance."}`,
requestBody,
)
assert.Equal(t, 1, count)
requestBody = ""
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, teamID *uint, daysCount int) (int, []uint, error) {
assert.Equal(t, 2, daysCount)
return 10, []uint{1}, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger()))
assert.Equal(t, "", requestBody)
assert.Equal(t, 1, count)
}
func TestTriggerHostStatusWebhookTeam(t *testing.T) {
ds := new(mock.Store)
requestBody := ""
count := 0
ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
requestBodyBytes, err := io.ReadAll(r.Body)
require.NoError(t, err)
requestBody = string(requestBodyBytes)
count++
},
),
)
defer ts.Close()
ac := &fleet.AppConfig{
WebhookSettings: fleet.WebhookSettings{
HostStatusWebhook: fleet.HostStatusWebhookSettings{
Enable: false,
DestinationURL: ts.URL,
HostPercentage: 43,
DaysCount: 3,
},
},
}
teamSettings := fleet.HostStatusWebhookSettings{
Enable: true,
DestinationURL: ts.URL,
HostPercentage: 43,
DaysCount: 2,
}
ds.AppConfigFunc = func(context.Context) (*fleet.AppConfig, error) {
return ac, nil
}
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, teamID *uint, daysCount int) (int, []uint, error) {
assert.Equal(t, 2, daysCount)
assert.Equal(t, uint(1), *teamID)
return 10, []uint{1, 2, 3, 4, 5, 6}, nil
}
ds.TeamsSummaryFunc = func(ctx context.Context) ([]*fleet.TeamSummary, error) {
return []*fleet.TeamSummary{{ID: 1}}, nil
}
ds.TeamFunc = func(ctx context.Context, id uint) (*fleet.Team, error) {
assert.Equal(t, uint(1), id)
return &fleet.Team{
ID: 1,
Config: fleet.TeamConfig{
WebhookSettings: fleet.TeamWebhookSettings{
HostStatusWebhook: teamSettings,
},
},
}, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger()))
assert.Equal(
t,
`{"data":{"days_unseen":2,"host_ids":[1,2,3,4,5,6],"team_id":1,"total_hosts":10,"unseen_hosts":6},"text":"More than 60.00% of your hosts have not checked into Fleet for more than 2 days. You've been sent this message because the Host status webhook is enabled in your Fleet instance."}`,
requestBody,
)
assert.Equal(t, 1, count)
requestBody = ""
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, teamID *uint, daysCount int) (int, []uint, error) {
assert.Equal(t, 2, daysCount)
assert.Equal(t, uint(1), *teamID)
return 10, []uint{1}, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger()))
assert.Equal(t, "", requestBody)
assert.Equal(t, 1, count)
}