fleet/server/activity/internal/tests/suite_test.go
Victor Lyuboslavsky 92bc1c650e
Move PostJSONWithTimeout to platform/http package and activity cleanup (#40561)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #38536

- Moved PostJSONWithTimeout to platform/http
- Created platform/errors package with only types needed by ctxerr. This
way, ctxerr did not need to import fleethttp.
- Made activity bounded context use PostJSONWithTimeout directly
- Removed some activity types from legacy code that were no longer
needed

# Checklist for submitter

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
- Changes file `38536-new-activity-bc` already present, and this is just
cleanup from that work.

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **Refactor**
* Reorganized error handling utilities for improved clarity and
decoupling.
* Consolidated HTTP utilities to centralize JSON posting functionality
with timeout support.
* Simplified activity service initialization by removing unused internal
parameters.
* Cleaned up test utilities and removed webhook-related test
scaffolding.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-26 17:39:10 -06:00

132 lines
3.8 KiB
Go

package tests
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"github.com/fleetdm/fleet/v4/server/activity"
api_http "github.com/fleetdm/fleet/v4/server/activity/api/http"
"github.com/fleetdm/fleet/v4/server/activity/internal/mysql"
"github.com/fleetdm/fleet/v4/server/activity/internal/service"
"github.com/fleetdm/fleet/v4/server/activity/internal/testutils"
"github.com/go-kit/kit/endpoint"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)
// integrationTestSuite holds all dependencies for integration tests.
type integrationTestSuite struct {
*testutils.TestDB
ds *mysql.Datastore
server *httptest.Server
providers *mockDataProviders
}
// setupIntegrationTest creates a new test suite with a real database and HTTP server.
func setupIntegrationTest(t *testing.T) *integrationTestSuite {
t.Helper()
tdb := testutils.SetupTestDB(t, "activity_integration")
ds := mysql.NewDatastore(tdb.Conns(), tdb.Logger)
// Create mocks
authorizer := &mockAuthorizer{}
providers := newMockDataProviders()
// Create service
svc := service.NewService(authorizer, ds, providers, tdb.Logger)
// Create router with routes
router := mux.NewRouter()
// Pass-through auth middleware (authzcheck middleware handles creating the authz context)
authMiddleware := func(e endpoint.Endpoint) endpoint.Endpoint { return e }
routesFn := service.GetRoutes(svc, authMiddleware)
routesFn(router, nil)
// Create test server
server := httptest.NewServer(router)
t.Cleanup(func() {
server.Close()
})
return &integrationTestSuite{
TestDB: tdb,
ds: ds,
server: server,
providers: providers,
}
}
// truncateTables clears all test data between tests.
func (s *integrationTestSuite) truncateTables(t *testing.T) {
t.Helper()
s.TruncateTables(t)
}
// insertUser creates a user in the database and mock user provider.
func (s *integrationTestSuite) insertUser(t *testing.T, name, email string) uint {
t.Helper()
userID := s.TestDB.InsertUser(t, name, email)
// Also add to mock user provider for enrichment
s.providers.mockUserProvider.AddUser(&activity.User{
ID: userID,
Name: name,
Email: email,
})
return userID
}
// getActivities makes an HTTP request to list activities and returns the parsed response.
func (s *integrationTestSuite) getActivities(t *testing.T, queryParams string) (*api_http.ListActivitiesResponse, int) {
t.Helper()
url := s.server.URL + "/api/v1/fleet/activities"
if queryParams != "" {
url += "?" + queryParams
}
resp, err := http.Get(url) //nolint:gosec // test server URL is safe
require.NoError(t, err)
var result api_http.ListActivitiesResponse
err = json.NewDecoder(resp.Body).Decode(&result)
resp.Body.Close()
require.NoError(t, err)
return &result, resp.StatusCode
}
// insertHost creates a host in the database and mock host provider.
func (s *integrationTestSuite) insertHost(t *testing.T, hostname string, teamID *uint) uint {
t.Helper()
hostID := s.TestDB.InsertHost(t, hostname, teamID)
// Also add to mock host provider for authorization checks
s.providers.mockHostProvider.AddHost(&activity.Host{
ID: hostID,
TeamID: teamID,
})
return hostID
}
// getHostPastActivities makes an HTTP request to list host past activities and returns the parsed response.
func (s *integrationTestSuite) getHostPastActivities(t *testing.T, hostID uint, queryParams string) (*api_http.ListHostPastActivitiesResponse, int) {
t.Helper()
url := s.server.URL + "/api/v1/fleet/hosts/" + strconv.FormatUint(uint64(hostID), 10) + "/activities"
if queryParams != "" {
url += "?" + queryParams
}
resp, err := http.Get(url) //nolint:gosec // test server URL is safe
require.NoError(t, err)
var result api_http.ListHostPastActivitiesResponse
err = json.NewDecoder(resp.Body).Decode(&result)
resp.Body.Close()
require.NoError(t, err)
return &result, resp.StatusCode
}