fleet/server/webhooks/host_status_test.go
Tomas Touceda d0777ccfd3
Change message for text so webhook works on slack (#2838)
* Change message for text so webhook works on slack

* Update preview text for the webhook in the FE
2021-11-08 15:13:02 -03:00

60 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package webhooks
import (
"context"
"io/ioutil"
"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 := ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestBodyBytes, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
requestBody = string(requestBodyBytes)
}))
defer ts.Close()
ac := &fleet.AppConfig{
WebhookSettings: fleet.WebhookSettings{
HostStatusWebhook: fleet.HostStatusWebhookSettings{
Enable: true,
DestinationURL: ts.URL,
HostPercentage: 43,
DaysCount: 2,
},
},
}
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, daysCount int) (int, int, error) {
assert.Equal(t, 2, daysCount)
return 10, 6, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger(), ac))
assert.Equal(
t,
`{"data":{"days_unseen":2,"total_hosts":10,"unseen_hosts":6},"text":"More than 60.00% of your hosts have not checked into Fleet for more than 2 days. Youve been sent this message because the Host status webhook is enabled in your Fleet instance."}`,
requestBody,
)
requestBody = ""
ds.TotalAndUnseenHostsSinceFunc = func(ctx context.Context, daysCount int) (int, int, error) {
assert.Equal(t, 2, daysCount)
return 10, 1, nil
}
require.NoError(t, TriggerHostStatusWebhook(context.Background(), ds, kitlog.NewNopLogger(), ac))
assert.Equal(t, "", requestBody)
}