mirror of
https://github.com/micromdm/micromdm/
synced 2026-06-28 01:05:36 +08:00
Add more logic to the way code is organized. /pkg -- library code not directly connected to micromdm /mdm -- packages meant for the services devices interract with. The MDM protocol. /dep -- DEP API and related packages. /platform -- Core APIs the server provides. Commands API, Devices API, queue, pubsub etc. /workflow -- Packages/API that build on top of platform. Today that's the webhook package. Depending on what ends up here, the workflow folder might become its own repository.
104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/boltdb/bolt"
|
|
"github.com/micromdm/mdm"
|
|
)
|
|
|
|
func TestService_NewCommand(t *testing.T) {
|
|
svc := setupDB(t)
|
|
mock := &mockPublisher{}
|
|
svc.publisher = mock
|
|
passPublisher := func(string, []byte) error { return nil }
|
|
failPublisher := func(string, []byte) error {
|
|
return errors.New("failed")
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
publisher func(string, []byte) error
|
|
request *mdm.CommandRequest
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "happy path",
|
|
wantErr: false,
|
|
publisher: passPublisher,
|
|
request: &mdm.CommandRequest{
|
|
UDID: "foobarbaz",
|
|
Command: mdm.Command{
|
|
RequestType: "DeviceInformation",
|
|
DeviceInformation: mdm.DeviceInformation{
|
|
Queries: []string{"foo", "bar", "baz"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "publish fail",
|
|
wantErr: true,
|
|
publisher: failPublisher,
|
|
request: &mdm.CommandRequest{
|
|
Command: mdm.Command{
|
|
RequestType: "DeviceInformation",
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "empty request",
|
|
wantErr: true,
|
|
publisher: passPublisher,
|
|
},
|
|
{
|
|
name: "bad payload",
|
|
wantErr: true,
|
|
publisher: passPublisher,
|
|
request: &mdm.CommandRequest{
|
|
UDID: "foobarbaz",
|
|
Command: mdm.Command{
|
|
RequestType: "DevicePropaganda",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
mock.PublishFn = tt.publisher
|
|
_, err := svc.NewCommand(context.Background(), tt.request)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("%q. CommandService.NewCommand() error = %v, wantErr %v",
|
|
tt.name, err, tt.wantErr)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
type mockPublisher struct {
|
|
PublishFn func(string, []byte) error
|
|
}
|
|
|
|
func (m *mockPublisher) Publish(ctx context.Context, s string, b []byte) error {
|
|
return m.PublishFn(s, b)
|
|
}
|
|
|
|
func setupDB(t *testing.T) *Command {
|
|
f, _ := ioutil.TempFile("", "bolt-")
|
|
f.Close()
|
|
os.Remove(f.Name())
|
|
|
|
db, err := bolt.Open(f.Name(), 0777, nil)
|
|
if err != nil {
|
|
t.Fatalf("couldn't open bolt, err %s\n", err)
|
|
}
|
|
svc, err := New(db, nil)
|
|
if err != nil {
|
|
t.Fatalf("couldn't create service, err %s\n", err)
|
|
}
|
|
return svc
|
|
}
|