|
| 1 | +// Copyright Mondoo, Inc. 2024, 2026 |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.mondoo.com/mql/v13/llx" |
| 12 | + "go.mondoo.com/mql/v13/providers-sdk/v1/plugin" |
| 13 | +) |
| 14 | + |
| 15 | +func TestParseCLI_WithFlags(t *testing.T) { |
| 16 | + s := Init() |
| 17 | + |
| 18 | + res, err := s.ParseCLI(&plugin.ParseCLIReq{ |
| 19 | + Connector: "jamf", |
| 20 | + Flags: map[string]*llx.Primitive{ |
| 21 | + "client-id": {Value: []byte("my-client-id")}, |
| 22 | + "client-secret": {Value: []byte("my-client-secret")}, |
| 23 | + "instance-domain": {Value: []byte("https://example.jamfcloud.com")}, |
| 24 | + }, |
| 25 | + }) |
| 26 | + require.NoError(t, err) |
| 27 | + require.NotNil(t, res.Asset) |
| 28 | + |
| 29 | + conf := res.Asset.Connections[0] |
| 30 | + assert.Equal(t, "jamf", conf.Type) |
| 31 | + assert.Equal(t, "https://example.jamfcloud.com", conf.Options["instance_domain"]) |
| 32 | + |
| 33 | + // Credentials should be set |
| 34 | + require.Len(t, conf.Credentials, 1) |
| 35 | + assert.Equal(t, "my-client-id", conf.Credentials[0].User) |
| 36 | + assert.Equal(t, "my-client-secret", string(conf.Credentials[0].Secret)) |
| 37 | + |
| 38 | + // Secrets should NOT be in Options |
| 39 | + _, hasClientId := conf.Options["client_id"] |
| 40 | + _, hasClientSecret := conf.Options["client_secret"] |
| 41 | + assert.False(t, hasClientId, "client_id should not be in Options") |
| 42 | + assert.False(t, hasClientSecret, "client_secret should not be in Options") |
| 43 | +} |
| 44 | + |
| 45 | +func TestParseCLI_EmptyFlags(t *testing.T) { |
| 46 | + s := Init() |
| 47 | + |
| 48 | + res, err := s.ParseCLI(&plugin.ParseCLIReq{ |
| 49 | + Connector: "jamf", |
| 50 | + Flags: nil, |
| 51 | + }) |
| 52 | + require.NoError(t, err) |
| 53 | + require.NotNil(t, res.Asset) |
| 54 | + |
| 55 | + conf := res.Asset.Connections[0] |
| 56 | + assert.Equal(t, "jamf", conf.Type) |
| 57 | + assert.Empty(t, conf.Credentials) |
| 58 | +} |
| 59 | + |
| 60 | +func TestParseCLI_PartialFlags(t *testing.T) { |
| 61 | + s := Init() |
| 62 | + |
| 63 | + res, err := s.ParseCLI(&plugin.ParseCLIReq{ |
| 64 | + Connector: "jamf", |
| 65 | + Flags: map[string]*llx.Primitive{ |
| 66 | + "instance-domain": {Value: []byte("https://example.jamfcloud.com")}, |
| 67 | + }, |
| 68 | + }) |
| 69 | + require.NoError(t, err) |
| 70 | + require.NotNil(t, res.Asset) |
| 71 | + |
| 72 | + conf := res.Asset.Connections[0] |
| 73 | + assert.Equal(t, "https://example.jamfcloud.com", conf.Options["instance_domain"]) |
| 74 | + assert.Empty(t, conf.Credentials, "no credentials when only domain is provided") |
| 75 | +} |
| 76 | + |
| 77 | +func TestParseCLI_EnvVarFallback(t *testing.T) { |
| 78 | + t.Setenv("CLIENT_ID", "env-client-id") |
| 79 | + t.Setenv("CLIENT_SECRET", "env-client-secret") |
| 80 | + t.Setenv("INSTANCE_DOMAIN", "https://env.jamfcloud.com") |
| 81 | + |
| 82 | + s := Init() |
| 83 | + |
| 84 | + res, err := s.ParseCLI(&plugin.ParseCLIReq{ |
| 85 | + Connector: "jamf", |
| 86 | + Flags: map[string]*llx.Primitive{}, |
| 87 | + }) |
| 88 | + require.NoError(t, err) |
| 89 | + require.NotNil(t, res.Asset) |
| 90 | + |
| 91 | + conf := res.Asset.Connections[0] |
| 92 | + assert.Equal(t, "https://env.jamfcloud.com", conf.Options["instance_domain"]) |
| 93 | + require.Len(t, conf.Credentials, 1) |
| 94 | + assert.Equal(t, "env-client-id", conf.Credentials[0].User) |
| 95 | + assert.Equal(t, "env-client-secret", string(conf.Credentials[0].Secret)) |
| 96 | +} |
| 97 | + |
| 98 | +func TestParseCLI_FlagsTakePrecedenceOverEnv(t *testing.T) { |
| 99 | + t.Setenv("CLIENT_ID", "env-client-id") |
| 100 | + t.Setenv("INSTANCE_DOMAIN", "https://env.jamfcloud.com") |
| 101 | + |
| 102 | + s := Init() |
| 103 | + |
| 104 | + res, err := s.ParseCLI(&plugin.ParseCLIReq{ |
| 105 | + Connector: "jamf", |
| 106 | + Flags: map[string]*llx.Primitive{ |
| 107 | + "client-id": {Value: []byte("flag-client-id")}, |
| 108 | + "instance-domain": {Value: []byte("https://flag.jamfcloud.com")}, |
| 109 | + }, |
| 110 | + }) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + conf := res.Asset.Connections[0] |
| 114 | + assert.Equal(t, "https://flag.jamfcloud.com", conf.Options["instance_domain"]) |
| 115 | + require.Len(t, conf.Credentials, 1) |
| 116 | + assert.Equal(t, "flag-client-id", conf.Credentials[0].User) |
| 117 | +} |
0 commit comments