Skip to content

Commit

Permalink
Add enrollment set storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Feb 2, 2023
1 parent 1a89eb1 commit 6e5d1e9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 48 deletions.
45 changes: 45 additions & 0 deletions storage/internal/test/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package test

import (
"context"
"testing"

"github.com/jessepeterson/kmfddm/ddm"
"github.com/jessepeterson/kmfddm/http/api"
)

const testDecl = `{
"Type": "com.apple.configuration.management.test",
"Payload": {
"Echo": "Foo"
},
"Identifier": "test_golang_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"
}`

type allTestStorage interface {
setAndDeclStorage
api.EnrollmentAPIStorage
}

func TestBasic(t *testing.T, storage allTestStorage, ctx context.Context) {
decl, err := ddm.ParseDeclaration([]byte(testDecl))
if err != nil {
t.Fatal(err)
}

t.Run("StoreDeclaration", func(t *testing.T) {
testStoreDeclaration(t, storage, ctx, decl)
})

t.Run("TestSet", func(t *testing.T) {
testSet(t, storage, ctx, decl, "test_golang_set1")
})

t.Run("TestEnrollmentSets", func(t *testing.T) {
testEnrollments(t, storage, ctx, "455399EA-4C94-4FA1-A87A-85A6CFEC4932", "test_golang_set1")
})

t.Run("DeleteDeclaration", func(t *testing.T) {
testDeleteDeclaration(t, storage, ctx, decl.Identifier)
})
}
24 changes: 0 additions & 24 deletions storage/internal/test/declarations.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/jessepeterson/kmfddm/http/api"
)

const testDecl = `{
"Type": "com.apple.configuration.management.test",
"Payload": {
"Echo": "Foo"
},
"Identifier": "test_golang_9e6a3aa7-5e4b-4d38-aacf-0f8058b2a899"
}`

func testStoreDeclaration(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context, decl *ddm.Declaration) {
_, err := storage.StoreDeclaration(ctx, decl)
if err != nil {
Expand Down Expand Up @@ -68,19 +60,3 @@ func testDeleteDeclaration(t *testing.T, storage api.DeclarationAPIStorage, ctx
t.Error("found declaration id in list (should have been deleted)")
}
}

// TestDeclarations performs a simple store, retrieve and delete test for declarations
func TestDeclarations(t *testing.T, storage api.DeclarationAPIStorage, ctx context.Context) {
decl, err := ddm.ParseDeclaration([]byte(testDecl))
if err != nil {
t.Fatal(err)
}

t.Run("StoreDeclaration", func(t *testing.T) {
testStoreDeclaration(t, storage, ctx, decl)
})

t.Run("DeleteDeclaration", func(t *testing.T) {
testDeleteDeclaration(t, storage, ctx, decl.Identifier)
})
}
54 changes: 54 additions & 0 deletions storage/internal/test/enrollments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package test

import (
"context"
"testing"

"github.com/jessepeterson/kmfddm/http/api"
)

func testEnrollments(t *testing.T, storage api.EnrollmentAPIStorage, ctx context.Context, enrollmentID, setName string) {
// associate
_, err := storage.StoreEnrollmentSet(ctx, enrollmentID, setName)
if err != nil {
t.Fatal(err)
}

// find ref
setNames, err := storage.RetrieveEnrollmentSets(ctx, enrollmentID)
if err != nil {
t.Fatal(err)
}
found := false
for _, v := range setNames {
if v == setName {
found = true
break
}
}
if !found {
t.Error("set name not found in enrollment sets (should exist)")
}

// dissociate
_, err = storage.RemoveEnrollmentSet(ctx, enrollmentID, setName)
if err != nil {
t.Fatal(err)
}

// verify no ref
setNames, err = storage.RetrieveEnrollmentSets(ctx, enrollmentID)
if err != nil {
t.Fatal(err)
}
found = false
for _, v := range setNames {
if v == setName {
found = true
break
}
}
if found {
t.Error("set name found in enrollment sets (should not exist)")
}
}
19 changes: 0 additions & 19 deletions storage/internal/test/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,3 @@ func testSet(t *testing.T, storage setAndDeclStorage, ctx context.Context, decl
t.Fatal(err)
}
}

func TestSets(t *testing.T, storage setAndDeclStorage, ctx context.Context) {
decl, err := ddm.ParseDeclaration([]byte(testDecl))
if err != nil {
t.Fatal(err)
}

t.Run("StoreDeclaration", func(t *testing.T) {
testStoreDeclaration(t, storage, ctx, decl)
})

t.Run("TestSet", func(t *testing.T) {
testSet(t, storage, ctx, decl, "test_golang_set1")
})

t.Run("DeleteDeclaration", func(t *testing.T) {
testDeleteDeclaration(t, storage, ctx, decl.Identifier)
})
}
6 changes: 1 addition & 5 deletions storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,5 @@ func TestMySQL(t *testing.T) {
t.Fatal(err)
}

ctx := context.Background()

test.TestDeclarations(t, storage, ctx)

test.TestSets(t, storage, ctx)
test.TestBasic(t, storage, context.Background())
}

0 comments on commit 6e5d1e9

Please sign in to comment.