Skip to content

Commit 67306d4

Browse files
feat: [DGP-434] add accessors for all of the API spec's test results (#366)
1 parent 1449d9e commit 67306d4

File tree

4 files changed

+484
-184
lines changed

4 files changed

+484
-184
lines changed

pkg/apiclients/mocks/testapi.go

Lines changed: 85 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apiclients/testapi/mock_server_test.go

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,18 @@ type JobPollResponseConfig struct {
4141

4242
// Defines the response for GET /tests/{testID}.
4343
type FinalTestResultConfig struct {
44-
Outcome testapi.PassFail
45-
OutcomeReason *testapi.TestOutcomeReason
46-
ApiErrors *[]testapi.IoSnykApiCommonError
47-
ApiWarnings *[]testapi.IoSnykApiCommonError
48-
CustomHandler http.HandlerFunc // If set, this custom handler is used instead of standard result mock
44+
Outcome testapi.PassFail
45+
OutcomeReason *testapi.TestOutcomeReason
46+
ApiErrors *[]testapi.IoSnykApiCommonError
47+
ApiWarnings *[]testapi.IoSnykApiCommonError
48+
TestConfiguration *testapi.TestConfiguration
49+
CreatedAt *time.Time
50+
TestSubject testapi.TestSubject
51+
SubjectLocators *[]testapi.TestSubjectLocator
52+
BreachedPolicies *testapi.PolicyRefSet
53+
EffectiveSummary *testapi.FindingSummary
54+
RawSummary *testapi.FindingSummary
55+
CustomHandler http.HandlerFunc // If set, this custom handler is used instead of standard result mock
4956
}
5057

5158
// Configures mocking for GET /tests/{testID}/findings.
@@ -153,11 +160,19 @@ func handleTestResultRequest(t *testing.T, w http.ResponseWriter, r *http.Reques
153160
config.FinalTestResult.CustomHandler(w, r)
154161
return
155162
}
156-
resultResp := mockTestResultResponse(t, config.TestID,
163+
resultResp := mockTestResultResponse(t,
164+
config.TestID,
157165
config.FinalTestResult.Outcome,
158166
config.FinalTestResult.OutcomeReason,
159167
config.FinalTestResult.ApiErrors,
160168
config.FinalTestResult.ApiWarnings,
169+
config.FinalTestResult.TestConfiguration,
170+
config.FinalTestResult.CreatedAt,
171+
config.FinalTestResult.TestSubject,
172+
config.FinalTestResult.SubjectLocators,
173+
config.FinalTestResult.BreachedPolicies,
174+
config.FinalTestResult.EffectiveSummary,
175+
config.FinalTestResult.RawSummary,
161176
)
162177
w.WriteHeader(http.StatusOK)
163178
_, err := w.Write(resultResp)
@@ -439,19 +454,32 @@ func mockTestResultResponse(
439454
outcomeReason *testapi.TestOutcomeReason,
440455
apiErrors *[]testapi.IoSnykApiCommonError,
441456
apiWarnings *[]testapi.IoSnykApiCommonError,
457+
testConfig *testapi.TestConfiguration,
458+
createdAt *time.Time,
459+
testSubject testapi.TestSubject,
460+
subjectLocators *[]testapi.TestSubjectLocator,
461+
breachedPolicies *testapi.PolicyRefSet,
462+
effectiveSummary *testapi.FindingSummary,
463+
rawSummary *testapi.FindingSummary,
442464
) []byte {
443465
t.Helper()
444466
attributes := testapi.TestAttributes{
467+
Config: testConfig,
468+
CreatedAt: createdAt,
469+
Subject: testSubject,
470+
SubjectLocators: subjectLocators,
471+
EffectiveSummary: effectiveSummary,
472+
RawSummary: rawSummary,
445473
Outcome: &testapi.TestOutcome{
446-
Result: outcomeResult,
447-
Reason: outcomeReason,
474+
Result: outcomeResult,
475+
Reason: outcomeReason,
476+
BreachedPolicies: breachedPolicies,
448477
},
449478
State: &testapi.TestState{
450479
Execution: testapi.Finished,
451480
Errors: apiErrors,
452481
Warnings: apiWarnings,
453482
},
454-
EffectiveSummary: nil,
455483
}
456484

457485
responseData := testapi.TestData{

pkg/apiclients/testapi/testapi.go

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,23 @@ type client struct {
9393

9494
// TestResult defines the contract for accessing test result information.
9595
type TestResult interface {
96-
GetState() string
96+
GetTestID() *uuid.UUID
97+
GetTestConfiguration() *TestConfiguration
98+
GetCreatedAt() *time.Time
99+
GetTestSubject() TestSubject
100+
GetSubjectLocators() *[]TestSubjectLocator
101+
102+
GetExecutionState() TestExecutionStates
97103
GetErrors() *[]IoSnykApiCommonError
98104
GetWarnings() *[]IoSnykApiCommonError
99-
GetTestID() *uuid.UUID
100-
GetOutcome() *PassFail
105+
106+
GetPassFail() *PassFail
101107
GetOutcomeReason() *TestOutcomeReason
108+
GetBreachedPolicies() *PolicyRefSet
109+
102110
GetEffectiveSummary() *FindingSummary
103111
GetRawSummary() *FindingSummary
112+
104113
Findings(ctx context.Context) (resultFindings []FindingData, complete bool, err error)
105114
}
106115

@@ -156,14 +165,22 @@ type StartTestParams struct {
156165
// testResult is the concrete implementation of the TestResult interface for
157166
// accessing summary and findings data of a completed test.
158167
type testResult struct {
159-
State string // e.g., "finished", "errored"
160-
Errors *[]IoSnykApiCommonError
161-
Warnings *[]IoSnykApiCommonError
162-
TestID *uuid.UUID // The final Test ID (different from Job ID)
163-
Outcome *PassFail // Pass or Fail
168+
TestID *uuid.UUID // The final Test ID (different from Job ID)
169+
TestConfiguration *TestConfiguration
170+
CreatedAt *time.Time
171+
TestSubject TestSubject
172+
SubjectLocators *[]TestSubjectLocator
173+
174+
ExecutionState TestExecutionStates // e.g., "finished", "errored"
175+
Errors *[]IoSnykApiCommonError
176+
Warnings *[]IoSnykApiCommonError
177+
178+
PassFail *PassFail // Pass or Fail
164179
OutcomeReason *TestOutcomeReason // Reason for the outcome (e.g., policy_breach)
165-
EffectiveSummary *FindingSummary // Summary excluding suppressed findings
166-
RawSummary *FindingSummary // Summary including suppressed findings
180+
BreachedPolicies *PolicyRefSet // A set of local or managed policies
181+
182+
EffectiveSummary *FindingSummary // Summary excluding suppressed findings
183+
RawSummary *FindingSummary // Summary including suppressed findings
167184

168185
findings []FindingData // Stores the actual findings
169186
findingsComplete bool // True if all findings pages were fetched
@@ -174,8 +191,8 @@ type testResult struct {
174191
handle *testHandle // Populated when testResult is created
175192
}
176193

177-
// GetState returns the execution state of the test.
178-
func (r *testResult) GetState() string { return r.State }
194+
// GetExecutionState returns the execution state of the test.
195+
func (r *testResult) GetExecutionState() TestExecutionStates { return r.ExecutionState }
179196

180197
// GetErrors returns any API errors encountered during the test execution.
181198
func (r *testResult) GetErrors() *[]IoSnykApiCommonError { return r.Errors }
@@ -186,12 +203,27 @@ func (r *testResult) GetWarnings() *[]IoSnykApiCommonError { return r.Warnings }
186203
// GetTestID returns the final Test ID.
187204
func (r *testResult) GetTestID() *uuid.UUID { return r.TestID }
188205

189-
// GetOutcome returns the pass/fail outcome of the test.
190-
func (r *testResult) GetOutcome() *PassFail { return r.Outcome }
206+
// GetPassFail returns the pass/fail outcome of the test.
207+
func (r *testResult) GetPassFail() *PassFail { return r.PassFail }
191208

192209
// GetOutcomeReason returns the reason for the test outcome.
193210
func (r *testResult) GetOutcomeReason() *TestOutcomeReason { return r.OutcomeReason }
194211

212+
// GetBreachedPolicies returns the policies that were breached.
213+
func (r *testResult) GetBreachedPolicies() *PolicyRefSet { return r.BreachedPolicies }
214+
215+
// GetTestConfiguration returns the test configuration.
216+
func (r *testResult) GetTestConfiguration() *TestConfiguration { return r.TestConfiguration }
217+
218+
// GetCreatedAt returns the creation timestamp of the test.
219+
func (r *testResult) GetCreatedAt() *time.Time { return r.CreatedAt }
220+
221+
// GetTestSubject returns the test subject.
222+
func (r *testResult) GetTestSubject() TestSubject { return r.TestSubject }
223+
224+
// GetSubjectLocators returns the subject locators.
225+
func (r *testResult) GetSubjectLocators() *[]TestSubjectLocator { return r.SubjectLocators }
226+
195227
// GetEffectiveSummary returns the summary excluding suppressed findings.
196228
func (r *testResult) GetEffectiveSummary() *FindingSummary { return r.EffectiveSummary }
197229

@@ -471,31 +503,36 @@ func (h *testHandle) fetchResultStatus(ctx context.Context, testID uuid.UUID) (T
471503

472504
testData := resp.ApplicationvndApiJSON200.Data
473505
attrs := testData.Attributes
474-
status := &testResult{
475-
TestID: testData.Id,
476-
EffectiveSummary: attrs.EffectiveSummary,
477-
RawSummary: attrs.RawSummary,
478-
handle: h,
506+
result := &testResult{
507+
TestID: testData.Id,
508+
TestConfiguration: attrs.Config,
509+
CreatedAt: attrs.CreatedAt,
510+
TestSubject: attrs.Subject,
511+
SubjectLocators: attrs.SubjectLocators,
512+
EffectiveSummary: attrs.EffectiveSummary,
513+
RawSummary: attrs.RawSummary,
514+
handle: h,
479515
}
480516

481517
if attrs.State != nil {
482-
status.State = string(attrs.State.Execution)
518+
result.ExecutionState = attrs.State.Execution
483519
if attrs.State.Errors != nil && len(*attrs.State.Errors) > 0 {
484-
status.Errors = attrs.State.Errors
520+
result.Errors = attrs.State.Errors
485521
}
486522
if attrs.State.Warnings != nil && len(*attrs.State.Warnings) > 0 {
487-
status.Warnings = attrs.State.Warnings
523+
result.Warnings = attrs.State.Warnings
488524
}
489525
} else {
490-
status.State = "unknown" // API spec defines this as always set
526+
result.ExecutionState = TestExecutionStates("unknown") // API spec defines this as always set
491527
}
492528

493529
if attrs.Outcome != nil {
494-
status.Outcome = &attrs.Outcome.Result
495-
status.OutcomeReason = attrs.Outcome.Reason
530+
result.PassFail = &attrs.Outcome.Result
531+
result.OutcomeReason = attrs.Outcome.Reason
532+
result.BreachedPolicies = attrs.Outcome.BreachedPolicies
496533
}
497534

498-
return status, nil
535+
return result, nil
499536
}
500537

501538
// Returns a channel signaling completion of Wait()

0 commit comments

Comments
 (0)