|
| 1 | +// Copyright Mondoo, Inc. 2024, 2026 |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package connection |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/deploymenttheory/go-api-sdk-jamfpro/sdk/jamfpro" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "go.mondoo.com/mql/v13/providers-sdk/v1/inventory" |
| 13 | + "go.mondoo.com/mql/v13/providers-sdk/v1/vault" |
| 14 | +) |
| 15 | + |
| 16 | +func TestNewJamfConnection_MissingCredentials(t *testing.T) { |
| 17 | + _, err := NewJamfConnection(0, &inventory.Asset{}, &inventory.Config{ |
| 18 | + Options: map[string]string{}, |
| 19 | + }) |
| 20 | + require.Error(t, err) |
| 21 | + assert.Contains(t, err.Error(), "missing required Jamf credentials") |
| 22 | +} |
| 23 | + |
| 24 | +func TestNewJamfConnection_MissingDomain(t *testing.T) { |
| 25 | + cred := &vault.Credential{ |
| 26 | + Type: vault.CredentialType_password, |
| 27 | + User: "client-id", |
| 28 | + Secret: []byte("client-secret"), |
| 29 | + } |
| 30 | + _, err := NewJamfConnection(0, &inventory.Asset{}, &inventory.Config{ |
| 31 | + Options: map[string]string{}, |
| 32 | + Credentials: []*vault.Credential{cred}, |
| 33 | + }) |
| 34 | + require.Error(t, err) |
| 35 | + assert.Contains(t, err.Error(), "missing required Jamf credentials") |
| 36 | +} |
| 37 | + |
| 38 | +func TestNewJamfConnection_MissingClientID(t *testing.T) { |
| 39 | + _, err := NewJamfConnection(0, &inventory.Asset{}, &inventory.Config{ |
| 40 | + Options: map[string]string{ |
| 41 | + "instance_domain": "https://example.jamfcloud.com", |
| 42 | + }, |
| 43 | + }) |
| 44 | + require.Error(t, err) |
| 45 | + assert.Contains(t, err.Error(), "missing required Jamf credentials") |
| 46 | +} |
| 47 | + |
| 48 | +func TestJamfConnection_PlatformInfo(t *testing.T) { |
| 49 | + conn := &JamfConnection{} |
| 50 | + platform, err := conn.PlatformInfo() |
| 51 | + require.NoError(t, err) |
| 52 | + assert.Equal(t, "jamf", platform.Name) |
| 53 | + assert.Equal(t, "Jamf Pro", platform.Title) |
| 54 | + assert.Equal(t, "api", platform.Kind) |
| 55 | + assert.Equal(t, "jamf", platform.Runtime) |
| 56 | + assert.Equal(t, []string{"jamf"}, platform.Family) |
| 57 | +} |
| 58 | + |
| 59 | +func TestJamfConnection_Identifier(t *testing.T) { |
| 60 | + conn := &JamfConnection{ |
| 61 | + Conf: &inventory.Config{ |
| 62 | + Options: map[string]string{ |
| 63 | + "instance_domain": "https://MyCompany.jamfcloud.com", |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + id := conn.Identifier() |
| 68 | + assert.Equal(t, "//platformid.api.mondoo.app/runtime/jamf/mycompany.jamfcloud.com", id) |
| 69 | +} |
| 70 | + |
| 71 | +func TestJamfConnection_IdentifierBareHostname(t *testing.T) { |
| 72 | + conn := &JamfConnection{ |
| 73 | + Conf: &inventory.Config{ |
| 74 | + Options: map[string]string{ |
| 75 | + "instance_domain": "MyCompany.jamfcloud.com", |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | + id := conn.Identifier() |
| 80 | + assert.Equal(t, "//platformid.api.mondoo.app/runtime/jamf/mycompany.jamfcloud.com", id) |
| 81 | +} |
| 82 | + |
| 83 | +func TestJamfConnection_IdentifierStripsPath(t *testing.T) { |
| 84 | + conn := &JamfConnection{ |
| 85 | + Conf: &inventory.Config{ |
| 86 | + Options: map[string]string{ |
| 87 | + "instance_domain": "https://mycompany.jamfcloud.com/extra/path", |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + id := conn.Identifier() |
| 92 | + assert.Equal(t, "//platformid.api.mondoo.app/runtime/jamf/mycompany.jamfcloud.com", id) |
| 93 | +} |
| 94 | + |
| 95 | +func TestJamfConnection_Name(t *testing.T) { |
| 96 | + conn := &JamfConnection{} |
| 97 | + assert.Equal(t, "jamf", conn.Name()) |
| 98 | +} |
| 99 | + |
| 100 | +func TestJamfConnection_LocalUserAccountsCache(t *testing.T) { |
| 101 | + conn := &JamfConnection{ |
| 102 | + localUserAccounts: make(map[string][]jamfpro.ComputerInventorySubsetLocalUserAccount), |
| 103 | + } |
| 104 | + |
| 105 | + // Cache should be empty initially |
| 106 | + _, ok := conn.GetCachedLocalUserAccounts("123") |
| 107 | + assert.False(t, ok) |
| 108 | + |
| 109 | + // After caching, should return data |
| 110 | + testAccounts := []jamfpro.ComputerInventorySubsetLocalUserAccount{ |
| 111 | + {UID: "501", Username: "admin", Admin: true}, |
| 112 | + {UID: "502", Username: "user1", Admin: false}, |
| 113 | + } |
| 114 | + conn.CacheLocalUserAccounts("123", testAccounts) |
| 115 | + |
| 116 | + cached, ok := conn.GetCachedLocalUserAccounts("123") |
| 117 | + assert.True(t, ok) |
| 118 | + require.Len(t, cached, 2) |
| 119 | + assert.Equal(t, "admin", cached[0].Username) |
| 120 | + assert.True(t, cached[0].Admin) |
| 121 | + assert.Equal(t, "user1", cached[1].Username) |
| 122 | + assert.False(t, cached[1].Admin) |
| 123 | + |
| 124 | + // Different ID should miss cache |
| 125 | + _, ok = conn.GetCachedLocalUserAccounts("456") |
| 126 | + assert.False(t, ok) |
| 127 | +} |
0 commit comments