2016-11-02 14:59:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-11-02 14:59:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-05-25 04:34:08 +00:00
|
|
|
"github.com/fleetdm/fleet/server/contexts/viewer"
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-05-25 04:34:08 +00:00
|
|
|
"github.com/fleetdm/fleet/server/mock"
|
2021-05-25 22:46:46 +00:00
|
|
|
"github.com/fleetdm/fleet/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) {
|
2021-05-25 04:34:08 +00:00
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
user := &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}
|
2021-05-25 04:34:08 +00:00
|
|
|
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: user})
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
hosts := []*fleet.Host{
|
2021-06-24 00:32:19 +00:00
|
|
|
{Hostname: "foo.local"},
|
2016-11-02 14:59:53 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
labels := []*fleet.Label{
|
2021-05-25 04:34:08 +00:00
|
|
|
{
|
|
|
|
Name: "label foo",
|
|
|
|
Query: "query foo",
|
|
|
|
},
|
2016-11-02 14:59:53 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
teams := []*fleet.Team{
|
2021-05-27 20:18:00 +00:00
|
|
|
{Name: "team1"},
|
|
|
|
}
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchHostsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Host, error) {
|
2021-05-25 04:34:08 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
return hosts, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchLabelsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Label, error) {
|
2021-05-25 04:34:08 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
return labels, nil
|
2016-11-02 14:59:53 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchTeamsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Team, error) {
|
2021-05-27 20:18:00 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
return teams, nil
|
|
|
|
}
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
results, err := svc.SearchTargets(ctx, "foo", nil, fleet.HostTargets{})
|
2021-05-25 04:34:08 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-26 01:53:22 +00:00
|
|
|
assert.Equal(t, hosts[0], results.Hosts[0])
|
2021-05-25 04:34:08 +00:00
|
|
|
assert.Equal(t, labels[0], results.Labels[0])
|
2021-05-27 20:18:00 +00:00
|
|
|
assert.Equal(t, teams[0], results.Teams[0])
|
2016-11-02 14:59:53 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 04:34:08 +00:00
|
|
|
func TestSearchWithOmit(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2021-06-03 23:24:15 +00:00
|
|
|
svc := newTestService(ds, nil, nil)
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
user := &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)}
|
2021-05-25 04:34:08 +00:00
|
|
|
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: user})
|
2016-11-02 14:59:53 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchHostsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Host, error) {
|
2021-05-25 04:34:08 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
assert.Equal(t, []uint{1, 2}, omit)
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchLabelsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Label, error) {
|
2021-05-25 04:34:08 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
assert.Equal(t, []uint{3, 4}, omit)
|
|
|
|
return nil, nil
|
2016-11-02 14:59:53 +00:00
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
ds.SearchTeamsFunc = func(filter fleet.TeamFilter, query string, omit ...uint) ([]*fleet.Team, error) {
|
2021-05-27 20:18:00 +00:00
|
|
|
assert.Equal(t, user, filter.User)
|
|
|
|
assert.Equal(t, []uint{5, 6}, omit)
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-05-25 04:34:08 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
_, err := svc.SearchTargets(ctx, "foo", nil, fleet.HostTargets{HostIDs: []uint{1, 2}, LabelIDs: []uint{3, 4}, TeamIDs: []uint{5, 6}})
|
2016-11-02 14:59:53 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
}
|