@@ -17,6 +17,7 @@ import (
17
17
"github.com/snyk/error-catalog-golang-public/snyk_errors"
18
18
"github.com/stretchr/testify/assert"
19
19
20
+ "github.com/snyk/go-application-framework/internal/api/contract"
20
21
policyApi "github.com/snyk/go-application-framework/internal/api/policy/2024-10-15"
21
22
"github.com/snyk/go-application-framework/pkg/configuration"
22
23
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
@@ -705,3 +706,113 @@ func Test_getExpireValue(t *testing.T) {
705
706
assert .Nil (t , result )
706
707
})
707
708
}
709
+
710
+ func Test_getOrgIgnoreApprovalEnabled (t * testing.T ) {
711
+ t .Run ("returns existing value when not nil" , func (t * testing.T ) {
712
+ ctrl := gomock .NewController (t )
713
+ defer ctrl .Finish ()
714
+
715
+ mockEngine := mocks .NewMockEngine (ctrl )
716
+ defaultValueFunc := getOrgIgnoreApprovalEnabled (mockEngine )
717
+
718
+ result , err := defaultValueFunc (nil , true )
719
+ assert .NoError (t , err )
720
+ assert .Equal (t , true , result )
721
+
722
+ result , err = defaultValueFunc (nil , false )
723
+ assert .NoError (t , err )
724
+ assert .Equal (t , false , result )
725
+ })
726
+
727
+ t .Run ("approval workflow enabled" , func (t * testing.T ) {
728
+ result , err := setupMockEngineForOrgSettings (t , & contract.OrgSettingsResponse {
729
+ Ignores : & contract.OrgIgnoreSettings {ApprovalWorkflowEnabled : true },
730
+ })
731
+
732
+ assert .NoError (t , err )
733
+ assert .Equal (t , true , result )
734
+ })
735
+
736
+ t .Run ("approval workflow disabled" , func (t * testing.T ) {
737
+ result , err := setupMockEngineForOrgSettings (t , & contract.OrgSettingsResponse {
738
+ Ignores : & contract.OrgIgnoreSettings {ApprovalWorkflowEnabled : false },
739
+ })
740
+
741
+ assert .NoError (t , err )
742
+ assert .Equal (t , false , result )
743
+ })
744
+
745
+ t .Run ("ignores field is nil" , func (t * testing.T ) {
746
+ result , err := setupMockEngineForOrgSettings (t , & contract.OrgSettingsResponse {
747
+ Ignores : nil ,
748
+ })
749
+
750
+ assert .NoError (t , err )
751
+ assert .Equal (t , false , result )
752
+ })
753
+
754
+ t .Run ("API call fails" , func (t * testing.T ) {
755
+ ctrl := gomock .NewController (t )
756
+ defer ctrl .Finish ()
757
+
758
+ logger := zerolog.Logger {}
759
+ orgId := uuid .New ().String ()
760
+ apiUrl := "https://api.snyk.io"
761
+
762
+ mockEngine := mocks .NewMockEngine (ctrl )
763
+ mockConfig := mocks .NewMockConfiguration (ctrl )
764
+ mockNetworkAccess := mocks .NewMockNetworkAccess (ctrl )
765
+
766
+ httpClient := localworkflows .NewTestClient (func (req * http.Request ) * http.Response {
767
+ return & http.Response {
768
+ StatusCode : http .StatusInternalServerError ,
769
+ Body : io .NopCloser (bytes .NewBufferString ("Internal Server Error" )),
770
+ }
771
+ })
772
+
773
+ mockEngine .EXPECT ().GetConfiguration ().Return (mockConfig )
774
+ mockEngine .EXPECT ().GetNetworkAccess ().Return (mockNetworkAccess )
775
+ mockEngine .EXPECT ().GetLogger ().Return (& logger )
776
+ mockConfig .EXPECT ().GetString (configuration .ORGANIZATION ).Return (orgId )
777
+ mockConfig .EXPECT ().GetString (configuration .API_URL ).Return (apiUrl )
778
+ mockNetworkAccess .EXPECT ().GetHttpClient ().Return (httpClient )
779
+
780
+ defaultValueFunc := getOrgIgnoreApprovalEnabled (mockEngine )
781
+ result , err := defaultValueFunc (nil , nil )
782
+
783
+ assert .Error (t , err )
784
+ assert .Nil (t , result )
785
+ assert .Contains (t , err .Error (), "unable to retrieve org settings" )
786
+ })
787
+ }
788
+
789
+ func setupMockEngineForOrgSettings (t * testing.T , response * contract.OrgSettingsResponse ) (interface {}, error ) {
790
+ ctrl := gomock .NewController (t )
791
+ defer ctrl .Finish ()
792
+
793
+ orgId := uuid .New ().String ()
794
+ apiUrl := "https://api.snyk.io"
795
+
796
+ responseJSON , err := json .Marshal (response )
797
+ assert .NoError (t , err )
798
+
799
+ mockEngine := mocks .NewMockEngine (ctrl )
800
+ mockConfig := mocks .NewMockConfiguration (ctrl )
801
+ mockNetworkAccess := mocks .NewMockNetworkAccess (ctrl )
802
+
803
+ httpClient := localworkflows .NewTestClient (func (req * http.Request ) * http.Response {
804
+ return & http.Response {
805
+ StatusCode : http .StatusOK ,
806
+ Body : io .NopCloser (bytes .NewBuffer (responseJSON )),
807
+ }
808
+ })
809
+
810
+ mockEngine .EXPECT ().GetConfiguration ().Return (mockConfig )
811
+ mockEngine .EXPECT ().GetNetworkAccess ().Return (mockNetworkAccess )
812
+ mockConfig .EXPECT ().GetString (configuration .ORGANIZATION ).Return (orgId )
813
+ mockConfig .EXPECT ().GetString (configuration .API_URL ).Return (apiUrl )
814
+ mockNetworkAccess .EXPECT ().GetHttpClient ().Return (httpClient )
815
+
816
+ defaultValueFunc := getOrgIgnoreApprovalEnabled (mockEngine )
817
+ return defaultValueFunc (nil , nil )
818
+ }
0 commit comments