2016-11-04 20:44:38 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-11-04 20:44:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
|
|
|
"github.com/fleetdm/fleet/server/mock"
|
2016-11-04 20:44:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-03-22 19:40:01 +00:00
|
|
|
func TestCleanupURL(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
expected string
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{" http://foo.bar.com ", "http://foo.bar.com", "leading and trailing whitespace"},
|
|
|
|
{"\n http://foo.com \t", "http://foo.com", "whitespace"},
|
|
|
|
{"http://foo.com", "http://foo.com", "noop"},
|
|
|
|
{"http://foo.com/", "http://foo.com", "trailing slash"},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(tt *testing.T) {
|
|
|
|
actual := cleanupURL(test.in)
|
|
|
|
assert.Equal(tt, test.expected, actual)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
func TestCreateAppConfig(t *testing.T) {
|
2020-05-29 16:12:39 +00:00
|
|
|
ds := new(mock.Store)
|
2020-03-23 01:33:04 +00:00
|
|
|
svc, err := newTestService(ds, nil, nil)
|
2016-11-04 20:44:38 +00:00
|
|
|
require.Nil(t, err)
|
2020-05-29 16:12:39 +00:00
|
|
|
|
|
|
|
ds.AppConfigFunc = func() (*kolide.AppConfig, error) {
|
|
|
|
return &kolide.AppConfig{}, nil
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
var appConfigTests = []struct {
|
|
|
|
configPayload kolide.AppConfigPayload
|
2016-11-04 20:44:38 +00:00
|
|
|
}{
|
|
|
|
{
|
2016-11-09 17:19:07 +00:00
|
|
|
configPayload: kolide.AppConfigPayload{
|
2016-11-04 20:44:38 +00:00
|
|
|
OrgInfo: &kolide.OrgInfo{
|
|
|
|
OrgLogoURL: stringPtr("acme.co/images/logo.png"),
|
|
|
|
OrgName: stringPtr("Acme"),
|
|
|
|
},
|
|
|
|
ServerSettings: &kolide.ServerSettings{
|
2020-05-29 16:12:39 +00:00
|
|
|
KolideServerURL: stringPtr("https://acme.co:8080/"),
|
2020-01-14 00:53:04 +00:00
|
|
|
LiveQueryDisabled: boolPtr(true),
|
2016-11-04 20:44:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
for _, tt := range appConfigTests {
|
2020-05-29 16:12:39 +00:00
|
|
|
var result *kolide.AppConfig
|
|
|
|
ds.NewAppConfigFunc = func(config *kolide.AppConfig) (*kolide.AppConfig, error) {
|
|
|
|
result = config
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotSecretSpec *kolide.EnrollSecretSpec
|
|
|
|
ds.ApplyEnrollSecretSpecFunc = func(spec *kolide.EnrollSecretSpec) error {
|
|
|
|
gotSecretSpec = spec
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := svc.NewAppConfig(context.Background(), tt.configPayload)
|
2016-11-04 20:44:38 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
payload := tt.configPayload
|
2016-11-04 20:44:38 +00:00
|
|
|
assert.Equal(t, *payload.OrgInfo.OrgLogoURL, result.OrgLogoURL)
|
|
|
|
assert.Equal(t, *payload.OrgInfo.OrgName, result.OrgName)
|
2017-03-22 19:40:01 +00:00
|
|
|
assert.Equal(t, "https://acme.co:8080", result.KolideServerURL)
|
2020-01-14 00:53:04 +00:00
|
|
|
assert.Equal(t, *payload.ServerSettings.LiveQueryDisabled, result.LiveQueryDisabled)
|
2020-05-29 16:12:39 +00:00
|
|
|
|
|
|
|
// Ensure enroll secret was set
|
|
|
|
require.NotNil(t, gotSecretSpec)
|
|
|
|
require.Len(t, gotSecretSpec.Secrets, 1)
|
|
|
|
assert.Len(t, gotSecretSpec.Secrets[0].Secret, 32)
|
2016-11-04 20:44:38 +00:00
|
|
|
}
|
|
|
|
}
|