2021-08-27 14:15:36 +00:00
package webhooks
import (
"context"
2023-10-27 18:28:54 +00:00
"io"
2021-08-27 14:15:36 +00:00
"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 := ""
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 18:35:27 +00:00
count := 0
2021-08-27 14:15:36 +00:00
ts := httptest . NewServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
2023-10-27 18:28:54 +00:00
requestBodyBytes , err := io . ReadAll ( r . Body )
2021-08-27 14:15:36 +00:00
require . NoError ( t , err )
requestBody = string ( requestBodyBytes )
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 18:35:27 +00:00
count ++
2021-08-27 14:15:36 +00:00
} ) )
defer ts . Close ( )
ac := & fleet . AppConfig {
WebhookSettings : fleet . WebhookSettings {
HostStatusWebhook : fleet . HostStatusWebhookSettings {
Enable : true ,
DestinationURL : ts . URL ,
HostPercentage : 43 ,
DaysCount : 2 ,
} ,
} ,
}
2022-09-20 19:26:36 +00:00
ds . AppConfigFunc = func ( context . Context ) ( * fleet . AppConfig , error ) {
return ac , nil
}
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 18:35:27 +00:00
ds . TotalAndUnseenHostsSinceFunc = func ( ctx context . Context , teamID * uint , daysCount int ) ( int , [ ] uint , error ) {
2021-08-27 14:15:36 +00:00
assert . Equal ( t , 2 , daysCount )
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 18:35:27 +00:00
return 10 , [ ] uint { 1 , 2 , 3 , 4 , 5 , 6 } , nil
}
ds . TeamsSummaryFunc = func ( ctx context . Context ) ( [ ] * fleet . TeamSummary , error ) {
return nil , nil
2021-08-27 14:15:36 +00:00
}
2022-09-20 19:26:36 +00:00
require . NoError ( t , TriggerHostStatusWebhook ( context . Background ( ) , ds , kitlog . NewNopLogger ( ) ) )
2021-08-27 14:15:36 +00:00
assert . Equal (
t ,
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 18:35:27 +00:00
` { "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."} ` ,
2021-08-27 14:15:36 +00:00
requestBody ,
)
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 18:35:27 +00:00
assert . Equal ( t , 1 , count )
2021-08-27 14:15:36 +00:00
requestBody = ""
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 18:35:27 +00:00
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 )
}
2021-08-27 14:15:36 +00:00
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 18:35:27 +00:00
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 {
2024-03-08 21:09:33 +00:00
HostStatusWebhook : & teamSettings ,
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 18:35:27 +00:00
} ,
} ,
} , 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 ) {
2021-08-27 14:15:36 +00:00
assert . Equal ( t , 2 , daysCount )
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 18:35:27 +00:00
assert . Equal ( t , uint ( 1 ) , * teamID )
return 10 , [ ] uint { 1 } , nil
2021-08-27 14:15:36 +00:00
}
2022-09-20 19:26:36 +00:00
require . NoError ( t , TriggerHostStatusWebhook ( context . Background ( ) , ds , kitlog . NewNopLogger ( ) ) )
2021-08-27 14:15:36 +00:00
assert . Equal ( t , "" , requestBody )
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 18:35:27 +00:00
assert . Equal ( t , 1 , count )
2021-08-27 14:15:36 +00:00
}