fleet/server/service/device_client_test.go
Lucas Manuel Rodriguez 7dadec3ecf
Add mTLS support to fleetd (#11319)
#7970

- [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.
- ~[ ] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)~
- ~[ ] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [x] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [x] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- [x] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).
2023-04-27 08:44:39 -03:00

60 lines
1.4 KiB
Go

package service
import (
"bytes"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type mockHttpClient struct {
resBody string
statusCode int
err error
}
func (m *mockHttpClient) Do(req *http.Request) (*http.Response, error) {
if m.err != nil {
return nil, m.err
}
res := &http.Response{
StatusCode: m.statusCode,
Body: io.NopCloser(bytes.NewBufferString(m.resBody)),
}
return res, nil
}
func TestDeviceClientGetDesktopPayload(t *testing.T) {
client, err := NewDeviceClient("https://test.com", true, "", nil, "")
token := "test_token"
require.NoError(t, err)
mockRequestDoer := &mockHttpClient{}
client.http = mockRequestDoer
t.Run("with wrong license", func(t *testing.T) {
mockRequestDoer.statusCode = http.StatusPaymentRequired
_, err = client.NumberOfFailingPolicies(token)
require.ErrorIs(t, err, ErrMissingLicense)
})
t.Run("with no failing policies", func(t *testing.T) {
mockRequestDoer.statusCode = http.StatusOK
mockRequestDoer.resBody = `{}`
result, err := client.NumberOfFailingPolicies(token)
require.NoError(t, err)
require.Equal(t, uint(0), result)
})
t.Run("with failing policies", func(t *testing.T) {
mockRequestDoer.statusCode = http.StatusOK
mockRequestDoer.resBody = `{"failing_policies_count": 1}`
result, err := client.NumberOfFailingPolicies(token)
require.NoError(t, err)
require.Equal(t, uint(1), result)
})
}