fleet/server/service/targets_test.go

81 lines
2.5 KiB
Go
Raw Permalink Normal View History

2016-11-02 14:59:53 +00:00
package service
import (
"context"
2016-11-02 14:59:53 +00:00
"testing"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
"github.com/fleetdm/fleet/v4/server/ptr"
2016-11-02 14:59:53 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSearchTargets(t *testing.T) {
ds := new(mock.Store)
svc, ctx := newTestService(t, ds, nil, nil)
2016-11-02 14:59:53 +00:00
user := &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}
ctx = viewer.NewContext(ctx, viewer.Viewer{User: user})
2016-11-02 14:59:53 +00:00
hosts := []*fleet.Host{
{Hostname: "foo.local"},
2016-11-02 14:59:53 +00:00
}
labels := []*fleet.Label{
{
Name: "label foo",
Query: "query foo",
},
2016-11-02 14:59:53 +00:00
}
teams := []*fleet.Team{
{Name: "team1"},
}
2016-11-02 14:59:53 +00:00
ds.SearchHostsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Host, error) {
assert.Equal(t, user, filter.User)
return hosts, nil
}
ds.SearchLabelsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Label, error) {
assert.Equal(t, user, filter.User)
return labels, nil
2016-11-02 14:59:53 +00:00
}
ds.SearchTeamsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Team, error) {
assert.Equal(t, user, filter.User)
return teams, nil
}
2016-11-02 14:59:53 +00:00
results, err := svc.SearchTargets(ctx, "foo", nil, fleet.HostTargets{})
require.NoError(t, err)
assert.Equal(t, hosts[0], results.Hosts[0])
assert.Equal(t, labels[0], results.Labels[0])
assert.Equal(t, teams[0], results.Teams[0])
2016-11-02 14:59:53 +00:00
}
func TestSearchWithOmit(t *testing.T) {
ds := new(mock.Store)
svc, ctx := newTestService(t, ds, nil, nil)
2016-11-02 14:59:53 +00:00
user := &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}
ctx = viewer.NewContext(ctx, viewer.Viewer{User: user})
2016-11-02 14:59:53 +00:00
ds.SearchHostsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Host, error) {
assert.Equal(t, user, filter.User)
assert.Equal(t, []uint{1, 2}, omit)
return nil, nil
}
ds.SearchLabelsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Label, error) {
assert.Equal(t, user, filter.User)
assert.Equal(t, []uint{3, 4}, omit)
return nil, nil
2016-11-02 14:59:53 +00:00
}
ds.SearchTeamsFunc = func(ctx context.Context, filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Team, error) {
assert.Equal(t, user, filter.User)
assert.Equal(t, []uint{5, 6}, omit)
return nil, nil
}
_, err := svc.SearchTargets(ctx, "foo", nil, fleet.HostTargets{HostIDs: []uint{1, 2}, LabelIDs: []uint{3, 4}, TeamIDs: []uint{5, 6}})
2022-06-10 18:29:45 +00:00
require.NoError(t, err)
2016-11-02 14:59:53 +00:00
}