@@ -26,6 +26,7 @@ import (
26
26
"testing"
27
27
"time"
28
28
29
+ "github.com/google/uuid"
29
30
"github.com/snyk/code-client-go/sarif"
30
31
31
32
"github.com/golang/mock/gomock"
@@ -641,18 +642,6 @@ func TestAnalysis_RunAnalysis_GetFindingsError(t *testing.T) {
641
642
req := i .(* http.Request )
642
643
return req .URL .String () == "http://localhost/rest/orgs/b6fc8954-5918-45ce-bc89-54591815ce1b/scans/a6fb2742-b67f-4dc3-bb27-42b67f1dc344?version=2024-02-16~experimental" &&
643
644
req .Method == http .MethodGet
644
- })).Times (1 ).Return (& http.Response {
645
- StatusCode : http .StatusOK ,
646
- Header : http.Header {
647
- "Content-Type" : []string {"application/vnd.api+json" },
648
- },
649
- Body : io .NopCloser (bytes .NewReader ([]byte (`{"data":{"attributes": {"status": "done", "components":[{"findings_url": "http://findings_url"}]}, "id": "a6fb2742-b67f-4dc3-bb27-42b67f1dc344"}}` ))),
650
- }, nil )
651
-
652
- mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
653
- req := i .(* http.Request )
654
- return req .URL .String () == "http://findings_url" &&
655
- req .Method == http .MethodGet
656
645
})).Times (1 ).Return (nil , errors .New ("error" ))
657
646
658
647
analysisOrchestrator := analysis .NewAnalysisOrchestrator (
@@ -665,8 +654,9 @@ func TestAnalysis_RunAnalysis_GetFindingsError(t *testing.T) {
665
654
)
666
655
667
656
_ , err := analysisOrchestrator .RunAnalysis (context .Background (), "b6fc8954-5918-45ce-bc89-54591815ce1b" , "rootPath" , "c172d1db-b465-4764-99e1-ecedad03b06a" )
668
- require .ErrorContains (t , err , "error" )
657
+ assert .ErrorContains (t , err , "error" )
669
658
}
659
+
670
660
func TestAnalysis_RunAnalysis_GetFindingsNotSuccessful (t * testing.T ) {
671
661
mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
672
662
@@ -718,3 +708,138 @@ func TestAnalysis_RunAnalysis_GetFindingsNotSuccessful(t *testing.T) {
718
708
_ , err := analysisOrchestrator .RunAnalysis (context .Background (), "b6fc8954-5918-45ce-bc89-54591815ce1b" , "rootPath" , "c172d1db-b465-4764-99e1-ecedad03b06a" )
719
709
require .ErrorContains (t , err , "failed to retrieve findings from findings URL" )
720
710
}
711
+
712
+ func TestAnalysis_RunTestRemote (t * testing.T ) {
713
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
714
+
715
+ mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for remote project" ), gomock .Eq ("Retrieving results..." )).Return ()
716
+ mockTracker .EXPECT ().End (gomock .Eq ("Analysis complete." )).Return ()
717
+
718
+ projectId := uuid .New ()
719
+ commitId := "abc123"
720
+ report := true
721
+
722
+ // Mock the initial test creation request
723
+ mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
724
+ req := i .(* http.Request )
725
+ return req .URL .String () == "http://localhost/hidden/orgs/4a72d1db-b465-4764-99e1-ecedad03b06a/tests?version=2024-12-21" &&
726
+ req .Method == http .MethodPost
727
+ })).Times (1 ).Return (& http.Response {
728
+ StatusCode : http .StatusCreated ,
729
+ Header : http.Header {
730
+ "Content-Type" : []string {"application/json" },
731
+ },
732
+ Body : io .NopCloser (bytes .NewReader ([]byte (`{
733
+ "data": {
734
+ "id": "a6fb2742-b67f-4dc3-bb27-42b67f1dc344",
735
+ "type": "test-result",
736
+ "attributes": {
737
+ "status": "completed",
738
+ "documents": {
739
+ "enriched_sarif": "/tests/123/sarif"
740
+ }
741
+ }
742
+ },
743
+ "jsonapi": {
744
+ "version": "1.0"
745
+ },
746
+ "links": {
747
+ "self": "http://localhost/hidden/orgs/4a72d1db-b465-4764-99e1-ecedad03b06a/tests/a6fb2742-b67f-4dc3-bb27-42b67f1dc344"
748
+ }
749
+ }` ))),
750
+ }, nil )
751
+ // Mock the findings retrieval request
752
+ mockHTTPClient .EXPECT ().Do (mock .MatchedBy (func (i interface {}) bool {
753
+ req := i .(* http.Request )
754
+ return req .URL .String () == "http://localhost/tests/123/sarif" &&
755
+ req .Method == http .MethodGet
756
+ })).Times (1 ).Return (& http.Response {
757
+ StatusCode : http .StatusOK ,
758
+ Body : io .NopCloser (bytes .NewReader (fakeResponse )),
759
+ }, nil )
760
+
761
+ analysisOrchestrator := analysis .NewAnalysisOrchestrator (
762
+ mockConfig ,
763
+ mockHTTPClient ,
764
+ analysis .WithLogger (& logger ),
765
+ analysis .WithInstrumentor (mockInstrumentor ),
766
+ analysis .WithTrackerFactory (mockTrackerFactory ),
767
+ analysis .WithErrorReporter (mockErrorReporter ),
768
+ )
769
+
770
+ result , err := analysisOrchestrator .RunTestRemote (
771
+ context .Background (),
772
+ "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
773
+ "b372d1db-b465-4764-99e1-ecedad03b06a" ,
774
+ analysis.ReportingConfig {
775
+ ProjectId : & projectId ,
776
+ CommitId : & commitId ,
777
+ Report : & report ,
778
+ },
779
+ )
780
+
781
+ require .NoError (t , err )
782
+ assert .NotNil (t , result )
783
+ }
784
+
785
+ func TestAnalysis_RunTestRemote_MissingRequiredParams (t * testing.T ) {
786
+ mockConfig , mockHTTPClient , mockInstrumentor , mockErrorReporter , mockTracker , mockTrackerFactory , logger := setup (t , nil )
787
+ mockTrackerFactory .EXPECT ().GenerateTracker ().Return (mockTracker ).AnyTimes ()
788
+
789
+ mockTracker .EXPECT ().Begin (gomock .Eq ("Snyk Code analysis for remote project" ), gomock .Eq ("Retrieving results..." )).Return ().AnyTimes ()
790
+ mockHTTPClient .EXPECT ().Do (gomock .Any ()).Times (0 )
791
+
792
+ analysisOrchestrator := analysis .NewAnalysisOrchestrator (
793
+ mockConfig ,
794
+ mockHTTPClient ,
795
+ analysis .WithLogger (& logger ),
796
+ analysis .WithInstrumentor (mockInstrumentor ),
797
+ analysis .WithTrackerFactory (mockTrackerFactory ),
798
+ analysis .WithErrorReporter (mockErrorReporter ),
799
+ )
800
+
801
+ t .Run ("missing both projectId and commitId" , func (t * testing.T ) {
802
+ result , err := analysisOrchestrator .RunTestRemote (
803
+ context .Background (),
804
+ "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
805
+ "b372d1db-b465-4764-99e1-ecedad03b06a" ,
806
+ analysis.ReportingConfig {},
807
+ )
808
+
809
+ assert .Error (t , err )
810
+ assert .Contains (t , err .Error (), "projectId and commitId are required" )
811
+ assert .Nil (t , result )
812
+ })
813
+
814
+ t .Run ("missing projectId" , func (t * testing.T ) {
815
+ commitId := "abc123"
816
+ result , err := analysisOrchestrator .RunTestRemote (
817
+ context .Background (),
818
+ "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
819
+ "b372d1db-b465-4764-99e1-ecedad03b06a" ,
820
+ analysis.ReportingConfig {
821
+ CommitId : & commitId ,
822
+ },
823
+ )
824
+
825
+ assert .Error (t , err )
826
+ assert .Contains (t , err .Error (), "projectId and commitId are required" )
827
+ assert .Nil (t , result )
828
+ })
829
+
830
+ t .Run ("missing commitId" , func (t * testing.T ) {
831
+ projectId := uuid .New ()
832
+ result , err := analysisOrchestrator .RunTestRemote (
833
+ context .Background (),
834
+ "4a72d1db-b465-4764-99e1-ecedad03b06a" ,
835
+ "b372d1db-b465-4764-99e1-ecedad03b06a" ,
836
+ analysis.ReportingConfig {
837
+ ProjectId : & projectId ,
838
+ },
839
+ )
840
+
841
+ assert .Error (t , err )
842
+ assert .Contains (t , err .Error (), "projectId and commitId are required" )
843
+ assert .Nil (t , result )
844
+ })
845
+ }
0 commit comments