mirror of
https://github.com/fleetdm/fleet
synced 2026-05-22 00:18:27 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38534 moved `/api/_version_/fleet/hosts/{id:[0-9]+}/activities` endpoint and `MarkActivitiesAsStreamed` to activity bounded context # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. ## Testing - [x] Added/updated automated tests - [x] Where appropriate, [automated tests simulate multiple hosts and test for host isolation](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/reference/patterns-backend.md#unit-testing) (updates to one hosts's records do not affect another) - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added new endpoint to retrieve host-specific past activities with pagination metadata. * **Refactor** * Refactored activity service architecture and authorization layer to improve data provider integration and activity streaming capabilities. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/activity"
|
|
authz_ctx "github.com/fleetdm/fleet/v4/server/contexts/authz"
|
|
platform_authz "github.com/fleetdm/fleet/v4/server/platform/authz"
|
|
platform_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
|
|
)
|
|
|
|
// Mock implementations for dependencies outside the bounded context
|
|
|
|
type mockAuthorizer struct{}
|
|
|
|
func (m *mockAuthorizer) Authorize(ctx context.Context, subject platform_authz.AuthzTyper, action platform_authz.Action) error {
|
|
// Mark authorization as checked (like the real authorizer does)
|
|
if authzCtx, ok := authz_ctx.FromContext(ctx); ok {
|
|
authzCtx.SetChecked()
|
|
}
|
|
return nil // Allow all for integration tests
|
|
}
|
|
|
|
type mockUserProvider struct {
|
|
users map[uint]*activity.User
|
|
}
|
|
|
|
func newMockUserProvider() *mockUserProvider {
|
|
return &mockUserProvider{users: make(map[uint]*activity.User)}
|
|
}
|
|
|
|
func (m *mockUserProvider) AddUser(u *activity.User) {
|
|
m.users[u.ID] = u
|
|
}
|
|
|
|
func (m *mockUserProvider) UsersByIDs(ctx context.Context, ids []uint) ([]*activity.User, error) {
|
|
var result []*activity.User
|
|
for _, id := range ids {
|
|
if u, ok := m.users[id]; ok {
|
|
result = append(result, u)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (m *mockUserProvider) FindUserIDs(ctx context.Context, query string) ([]uint, error) {
|
|
query = strings.ToLower(query)
|
|
var ids []uint
|
|
for _, u := range m.users {
|
|
if strings.Contains(strings.ToLower(u.Name), query) ||
|
|
strings.Contains(strings.ToLower(u.Email), query) {
|
|
ids = append(ids, u.ID)
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
type mockHostProvider struct {
|
|
hosts map[uint]*activity.Host
|
|
}
|
|
|
|
func newMockHostProvider() *mockHostProvider {
|
|
return &mockHostProvider{hosts: make(map[uint]*activity.Host)}
|
|
}
|
|
|
|
func (m *mockHostProvider) AddHost(h *activity.Host) {
|
|
m.hosts[h.ID] = h
|
|
}
|
|
|
|
func (m *mockHostProvider) GetHostLite(ctx context.Context, hostID uint) (*activity.Host, error) {
|
|
if h, ok := m.hosts[hostID]; ok {
|
|
return h, nil
|
|
}
|
|
return nil, platform_mysql.NotFound("Host").WithID(hostID)
|
|
}
|
|
|
|
// mockDataProviders combines user and host providers for testing.
|
|
type mockDataProviders struct {
|
|
*mockUserProvider
|
|
*mockHostProvider
|
|
}
|
|
|
|
func newMockDataProviders() *mockDataProviders {
|
|
return &mockDataProviders{
|
|
mockUserProvider: newMockUserProvider(),
|
|
mockHostProvider: newMockHostProvider(),
|
|
}
|
|
}
|