fleet/server/activity/internal/tests/mocks_test.go
Victor Lyuboslavsky 6019fa6d5a
Activity bounded context: /api/latest/fleet/activities (1 of 2) (#38115)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #37806 

This PR creates an activity bounded context and moves the following HTTP
endpoint (including the full vertical slice) there:
`/api/latest/fleet/activities`

NONE of the other activity functionality is moved! This is an
incremental approach starting with just 1 API/service endpoint.

A significant part of this PR is tests. This feature is now receiving
significantly more unit/integration test coverage than before.

Also, this PR does not remove the `ListActivities` datastore method in
the legacy code. That will be done in the follow up PR (part 2 of 2).

This refactoring effort also uncovered an activity/user authorization
issue: https://fleetdm.slack.com/archives/C02A8BRABB5/p1768582236611479

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.

## 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

* **New Features**
* Activity listing API now available with query filtering, date-range
filtering, and type-based filtering
* Pagination support for activity results with cursor-based and
offset-based options
* Configurable sorting by creation date or activity ID in ascending or
descending order
* Automatic enrichment of activity records with actor user details
(name, email, avatar)
* Role-based access controls applied to activity visibility based on
user permissions

<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-01-19 09:07:14 -05:00

56 lines
1.5 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"
)
// 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
}