fleet/server/webhooks/host_status_test.go
Victor Lyuboslavsky b174a476a2
Fixing unreleased spec bug in team host status webhook feature. (#17502)
Fixing unreleased spec bug in team host status webhook feature #17094.
Bug #17498
# Checklist for submitter

<!-- Note that API documentation changes are now addressed by the
product design team. -->

- [ ] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
  - Not needed. Part of new feature.
- [x] Added/updated tests
- [x] Manual QA for all new/changed functionality
2024-03-08 15:09:33 -06:00

153 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)
}