fleet/server/service/service_targets_test.go

70 lines
2.0 KiB
Go
Raw Normal View History

2016-11-02 14:59:53 +00:00
package service
import (
"context"
2016-11-02 14:59:53 +00:00
"testing"
"github.com/fleetdm/fleet/server/contexts/viewer"
"github.com/fleetdm/fleet/server/kolide"
"github.com/fleetdm/fleet/server/mock"
"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) {
ds := new(mock.Store)
svc, err := newTestService(ds, nil, nil)
2016-11-02 14:59:53 +00:00
require.Nil(t, err)
user := &kolide.User{GlobalRole: ptr.String(kolide.RoleAdmin)}
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: user})
2016-11-02 14:59:53 +00:00
hosts := []*kolide.Host{
{HostName: "foo.local"},
2016-11-02 14:59:53 +00:00
}
labels := []*kolide.Label{
{
Name: "label foo",
Query: "query foo",
},
2016-11-02 14:59:53 +00:00
}
ds.SearchHostsFunc = func(filter kolide.TeamFilter, query string, omit ...uint) ([]*kolide.Host, error) {
assert.Equal(t, user, filter.User)
return hosts, nil
}
ds.SearchLabelsFunc = func(filter kolide.TeamFilter, query string, omit ...uint) ([]*kolide.Label, error) {
assert.Equal(t, user, filter.User)
return labels, nil
2016-11-02 14:59:53 +00:00
}
results, err := svc.SearchTargets(ctx, "foo", nil, nil, nil)
require.NoError(t, err)
assert.Equal(t, hosts[0], results.Hosts[0])
assert.Equal(t, labels[0], results.Labels[0])
2016-11-02 14:59:53 +00:00
}
func TestSearchWithOmit(t *testing.T) {
ds := new(mock.Store)
svc, err := newTestService(ds, nil, nil)
2016-11-02 14:59:53 +00:00
require.Nil(t, err)
user := &kolide.User{GlobalRole: ptr.String(kolide.RoleAdmin)}
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: user})
2016-11-02 14:59:53 +00:00
ds.SearchHostsFunc = func(filter kolide.TeamFilter, query string, omit ...uint) ([]*kolide.Host, error) {
assert.Equal(t, user, filter.User)
assert.Equal(t, []uint{1, 2}, omit)
return nil, nil
}
ds.SearchLabelsFunc = func(filter kolide.TeamFilter, query string, omit ...uint) ([]*kolide.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
}
_, err = svc.SearchTargets(ctx, "foo", nil, []uint{1, 2}, []uint{3, 4})
2016-11-02 14:59:53 +00:00
require.Nil(t, err)
}