@@ -32,11 +32,14 @@ import (
3232 "github.com/stretchr/testify/mock"
3333 "github.com/stretchr/testify/require"
3434
35+ openapi_types "github.com/oapi-codegen/runtime/types"
3536 mocks2 "github.com/snyk/code-client-go/bundle/mocks"
3637 confMocks "github.com/snyk/code-client-go/config/mocks"
3738 httpmocks "github.com/snyk/code-client-go/http/mocks"
3839 "github.com/snyk/code-client-go/internal/analysis"
39- v20241221 "github.com/snyk/code-client-go/internal/api/test/2024-12-21"
40+ v20250407 "github.com/snyk/code-client-go/internal/api/test/2025-04-07"
41+ externalRef0 "github.com/snyk/code-client-go/internal/api/test/2025-04-07/common"
42+ v20250407Models "github.com/snyk/code-client-go/internal/api/test/2025-04-07/models"
4043 "github.com/snyk/code-client-go/observability/mocks"
4144 "github.com/snyk/code-client-go/sarif"
4245 "github.com/snyk/code-client-go/scan"
@@ -51,11 +54,55 @@ func mockDeriveErrorFromStatusCode(statusCode int) error {
5154 return fmt .Errorf ("Statuscode: %d" , statusCode )
5255}
5356
54- func mockGetDocumentResponse (t * testing.T , sarifResponse sarif.SarifDocument , expectedDocumentPath string , mockHTTPClient * httpmocks.MockHTTPClient , responseCode int ) {
57+ func mockTestStatusResponse (t * testing.T , mockHTTPClient * httpmocks.MockHTTPClient , orgId string , testId uuid.UUID , responseCode int ) {
58+ t .Helper ()
59+
60+ response := v20250407Models.TestResult {
61+ Data : struct {
62+ Attributes v20250407Models.TestState `json:"attributes"`
63+ Id openapi_types.UUID `json:"id"`
64+ Type v20250407Models.TestResultDataType `json:"type"`
65+ }{
66+ Id : testId ,
67+ Type : v20250407Models .TestResultDataTypeTest ,
68+ },
69+ Jsonapi : externalRef0.JsonApi {Version : "1.0" },
70+ Links : externalRef0.SelfLink {Self : & externalRef0.LinkProperty {}},
71+ }
72+
73+ completedStateJSON := map [string ]interface {}{
74+ "created_at" : time .Now ().Format (time .RFC3339 ),
75+ "status" : "completed" ,
76+ "result" : "passed" ,
77+ }
78+
79+ stateBytes , err := json .Marshal (completedStateJSON )
80+ assert .NoError (t , err )
81+ response .Data .Attributes = v20250407Models.TestState {}
82+ err = response .Data .Attributes .UnmarshalJSON (stateBytes )
83+ assert .NoError (t , err )
84+
85+ responseBodyBytes , err := json .Marshal (response )
86+ assert .NoError (t , err )
87+
88+ expectedTestStatusUrl := fmt .Sprintf ("http://localhost/hidden/orgs/%s/tests/%s?version=%s" , orgId , testId , v20250407 .ApiVersion )
89+ mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
90+ req := i .(* http.Request )
91+ return req .URL .String () == expectedTestStatusUrl && req .Method == http .MethodGet
92+ })).Times (1 ).Return (& http.Response {
93+ StatusCode : responseCode ,
94+ Header : http.Header {
95+ "Content-Type" : []string {"application/json" },
96+ },
97+ Body : io .NopCloser (bytes .NewReader (responseBodyBytes )),
98+ }, mockDeriveErrorFromStatusCode (responseCode ))
99+ }
100+
101+ func mockGetComponentResponse (t * testing.T , sarifResponse sarif.SarifDocument , expectedDocumentPath string , mockHTTPClient * httpmocks.MockHTTPClient , responseCode int ) {
55102 t .Helper ()
56103 responseBodyBytes , err := json .Marshal (sarifResponse )
57104 assert .NoError (t , err )
58- expectedDocumentUrl := fmt .Sprintf ("http://localhost/hidden%s?version=%s" , expectedDocumentPath , v20241221 .DocumentApiVersion )
105+ expectedDocumentUrl := fmt .Sprintf ("http://localhost/hidden%s?version=%s" , expectedDocumentPath , v20250407 .DocumentApiVersion )
59106 mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
60107 req := i .(* http.Request )
61108 return req .URL .String () == expectedDocumentUrl
@@ -70,18 +117,20 @@ func mockGetDocumentResponse(t *testing.T, sarifResponse sarif.SarifDocument, ex
70117
71118func mockResultCompletedResponse (t * testing.T , mockHTTPClient * httpmocks.MockHTTPClient , expectedWebuilink string , projectId uuid.UUID , snapshotId uuid.UUID , orgId string , testId uuid.UUID , documentPath string , responseCode int ) {
72119 t .Helper ()
73- response := v20241221 .NewTestResponse ()
74- state := v20241221 .NewTestCompleteState ()
75- state .Documents .EnrichedSarif = documentPath
76- state .Results .Webui .Link = & expectedWebuilink
77- state .Results .Webui .ProjectId = & projectId
78- state .Results .Webui .SnapshotId = & snapshotId
79- stateBytes , err := json .Marshal (state )
80- assert .NoError (t , err )
81- response .Data .Attributes .UnmarshalJSON (stateBytes )
82- responseBodyBytes , err := json .Marshal (response )
120+ state := v20250407 .NewGetComponentsState ()
121+ state .Data [0 ].Attributes .Type = "sast"
122+ state .Data [0 ].Attributes .FindingsDocumentPath = & documentPath
123+ findingsDocumentType := v20250407Models .Sarif
124+ state .Data [0 ].Attributes .FindingsDocumentType = & findingsDocumentType
125+ state .Data [0 ].Attributes .Success = true
126+ state .Data [0 ].Attributes .Webui = & v20250407Models.WebUI {
127+ Link : & expectedWebuilink ,
128+ ProjectId : & projectId ,
129+ SnapshotId : & snapshotId ,
130+ }
131+ responseBodyBytes , err := json .Marshal (state )
83132 assert .NoError (t , err )
84- expectedRetrieveTestUrl := fmt .Sprintf ("http://localhost/hidden/orgs/%s/tests/%s?version=%s" , orgId , testId , v20241221 .ApiVersion )
133+ expectedRetrieveTestUrl := fmt .Sprintf ("http://localhost/hidden/orgs/%s/tests/%s/components ?version=%s" , orgId , testId , v20250407 .ApiVersion )
85134 mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
86135 req := i .(* http.Request )
87136 return req .URL .String () == expectedRetrieveTestUrl
@@ -96,13 +145,15 @@ func mockResultCompletedResponse(t *testing.T, mockHTTPClient *httpmocks.MockHTT
96145
97146func mockTestCreatedResponse (t * testing.T , mockHTTPClient * httpmocks.MockHTTPClient , testId uuid.UUID , orgId string , responseCode int ) {
98147 t .Helper ()
99- response := v20241221 .NewTestResponse ()
148+ response := v20250407 .NewTestResponse ()
100149 response .Data .Id = testId
101150 responseBodyBytes , err := json .Marshal (response )
102151 assert .NoError (t , err )
103- expectedTestCreatedUrl := fmt .Sprintf ("http://localhost/hidden/orgs/%s/tests?version=%s" , orgId , v20241221 .ApiVersion )
152+ expectedTestCreatedUrl := fmt .Sprintf ("http://localhost/hidden/orgs/%s/tests?version=%s" , orgId , v20250407 .ApiVersion )
104153 mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
105154 req := i .(* http.Request )
155+ validateTestRequestBody (t , req .Body )
156+
106157 return req .URL .String () == expectedTestCreatedUrl &&
107158 req .Method == http .MethodPost
108159 })).Times (1 ).Return (& http.Response {
@@ -114,6 +165,23 @@ func mockTestCreatedResponse(t *testing.T, mockHTTPClient *httpmocks.MockHTTPCli
114165 }, mockDeriveErrorFromStatusCode (responseCode ))
115166}
116167
168+ func validateTestRequestBody (t * testing.T , request io.Reader ) {
169+ t .Helper ()
170+ body , _ := io .ReadAll (request )
171+ var testRequestBody v20250407Models.CreateTestRequestBody
172+ err := json .Unmarshal (body , & testRequestBody )
173+ assert .NoError (t , err )
174+ bundle , err := testRequestBody .Data .Attributes .Input .AsTestInputSourceBundle ()
175+ assert .NoError (t , err )
176+
177+ if bundle .Metadata .CommitId != nil {
178+ assert .Regexp (t , "^[0-9a-f]{40}$" , * bundle .Metadata .CommitId )
179+ }
180+ if bundle .Metadata .RepoUrl != nil {
181+ assert .
Regexp (
t ,
"^[email protected] :[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+.git$" ,
* bundle .
Metadata .
RepoUrl )
182+ }
183+ }
184+
117185func setup (t * testing.T , timeout * time.Duration ) (* confMocks.MockConfig , * httpmocks.MockHTTPClient , * mocks.MockInstrumentor , * mocks.MockErrorReporter , * trackerMocks.MockTracker , * trackerMocks.MockTrackerFactory , zerolog.Logger ) {
118186 t .Helper ()
119187 ctrl := gomock .NewController (t )
@@ -164,6 +232,9 @@ func TestAnalysis_RunTest(t *testing.T) {
164232 // Test Created Response
165233 mockTestCreatedResponse (t , mockHTTPClient , testId , orgId , http .StatusCreated )
166234
235+ // Test Status Response
236+ mockTestStatusResponse (t , mockHTTPClient , orgId , testId , http .StatusOK )
237+
167238 // Get Test Result Response
168239 expectedWebuilink := ""
169240 expectedDocumentPath := "/1234"
@@ -173,7 +244,8 @@ func TestAnalysis_RunTest(t *testing.T) {
173244 sarifResponse := sarif.SarifDocument {
174245 Version : "42.0" ,
175246 }
176- mockGetDocumentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusOK )
247+
248+ mockGetComponentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusOK )
177249
178250 analysisOrchestrator := analysis .NewAnalysisOrchestrator (
179251 mockConfig ,
@@ -219,6 +291,9 @@ func TestAnalysis_RunTestRemote(t *testing.T) {
219291 // Test Created Response
220292 mockTestCreatedResponse (t , mockHTTPClient , testId , orgId , http .StatusCreated )
221293
294+ // Test Status Response
295+ mockTestStatusResponse (t , mockHTTPClient , orgId , testId , http .StatusOK )
296+
222297 // Get Test Result Response
223298 expectedWebuilink := ""
224299 expectedDocumentPath := "/1234"
@@ -228,7 +303,7 @@ func TestAnalysis_RunTestRemote(t *testing.T) {
228303 sarifResponse := sarif.SarifDocument {
229304 Version : "42.0" ,
230305 }
231- mockGetDocumentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusOK )
306+ mockGetComponentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusOK )
232307
233308 analysisOrchestrator := analysis .NewAnalysisOrchestrator (
234309 mockConfig ,
@@ -312,6 +387,9 @@ func TestAnalysis_RunTestRemote_PollingFailed(t *testing.T) {
312387 // Test Created Response
313388 mockTestCreatedResponse (t , mockHTTPClient , testId , orgId , http .StatusCreated )
314389
390+ // Test Status Response
391+ mockTestStatusResponse (t , mockHTTPClient , orgId , testId , http .StatusOK )
392+
315393 // Get Test Result Response
316394 expectedWebuilink := ""
317395 expectedDocumentPath := "/1234"
@@ -356,6 +434,9 @@ func TestAnalysis_RunTestRemote_GetDocumentFailed(t *testing.T) {
356434 // Test Created Response
357435 mockTestCreatedResponse (t , mockHTTPClient , testId , orgId , http .StatusCreated )
358436
437+ // Test Status Response
438+ mockTestStatusResponse (t , mockHTTPClient , orgId , testId , http .StatusOK )
439+
359440 // Get Test Result Response
360441 expectedWebuilink := ""
361442 expectedDocumentPath := "/1234"
@@ -365,7 +446,7 @@ func TestAnalysis_RunTestRemote_GetDocumentFailed(t *testing.T) {
365446 sarifResponse := sarif.SarifDocument {
366447 Version : "42.0" ,
367448 }
368- mockGetDocumentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusInternalServerError )
449+ mockGetComponentResponse (t , sarifResponse , expectedDocumentPath , mockHTTPClient , http .StatusInternalServerError )
369450
370451 analysisOrchestrator := analysis .NewAnalysisOrchestrator (
371452 mockConfig ,
0 commit comments