Skip to content

Commit f223710

Browse files
committed
add unit tests
Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
1 parent 54b468b commit f223710

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

api/datareading_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ func TestDataReading_UnmarshalJSON(t *testing.T) {
7575
}`,
7676
wantDataType: &DynamicData{},
7777
},
78+
{
79+
name: "OIDCDiscoveryData type",
80+
input: `{
81+
"cluster_id": "11111111-2222-3333-4444-555555555555",
82+
"data-gatherer": "oidc",
83+
"timestamp": "2024-06-01T12:00:00Z",
84+
"data": {
85+
"openid_configuration": {"issuer": "https://example.com"},
86+
"jwks": {"keys": []}
87+
},
88+
"schema_version": "v1"
89+
}`,
90+
wantDataType: &OIDCDiscoveryData{},
91+
},
7892
{
7993
name: "Invalid JSON",
8094
input: `not a json`,

pkg/client/client_cyberark_convertdatareadings_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,70 @@ func TestExtractServerVersionFromReading(t *testing.T) {
126126
}
127127
}
128128

129+
// TestExtractOIDCFromReading tests the extractOIDCFromReading function.
130+
func TestExtractOIDCFromReading(t *testing.T) {
131+
type testCase struct {
132+
name string
133+
reading *api.DataReading
134+
expectedSnapshot dataupload.Snapshot
135+
expectError string
136+
}
137+
tests := []testCase{
138+
{
139+
name: "nil reading",
140+
expectError: `programmer mistake: the DataReading must not be nil`,
141+
},
142+
{
143+
name: "nil data",
144+
reading: &api.DataReading{
145+
DataGatherer: "ark/oidc",
146+
Data: nil,
147+
},
148+
expectError: `programmer mistake: the DataReading must have data type *api.OIDCDiscoveryData. This DataReading (ark/oidc) has data type <nil>`,
149+
},
150+
{
151+
name: "wrong data type",
152+
reading: &api.DataReading{
153+
DataGatherer: "ark/oidc",
154+
Data: &api.DiscoveryData{},
155+
},
156+
expectError: `programmer mistake: the DataReading must have data type *api.OIDCDiscoveryData. This DataReading (ark/oidc) has data type *api.DiscoveryData`,
157+
},
158+
{
159+
name: "happy path",
160+
reading: &api.DataReading{
161+
DataGatherer: "ark/oidc",
162+
Data: &api.OIDCDiscoveryData{
163+
OIDCConfig: map[string]any{"issuer": "https://example.com"},
164+
OIDCConfigError: "oidc-err",
165+
JWKS: map[string]any{"keys": []any{}},
166+
JWKSError: "jwks-err",
167+
},
168+
},
169+
expectedSnapshot: dataupload.Snapshot{
170+
OIDCConfig: map[string]any{"issuer": "https://example.com"},
171+
OIDCConfigError: "oidc-err",
172+
JWKS: map[string]any{"keys": []any{}},
173+
JWKSError: "jwks-err",
174+
},
175+
},
176+
}
177+
178+
for _, test := range tests {
179+
t.Run(test.name, func(t *testing.T) {
180+
var snapshot dataupload.Snapshot
181+
err := extractOIDCFromReading(test.reading, &snapshot)
182+
if test.expectError != "" {
183+
assert.EqualError(t, err, test.expectError)
184+
assert.Equal(t, dataupload.Snapshot{}, snapshot)
185+
return
186+
}
187+
require.NoError(t, err)
188+
assert.Equal(t, test.expectedSnapshot, snapshot)
189+
})
190+
}
191+
}
192+
129193
// TestExtractResourceListFromReading tests the extractResourceListFromReading function.
130194
func TestExtractResourceListFromReading(t *testing.T) {
131195
type testCase struct {

0 commit comments

Comments
 (0)