@@ -17,6 +17,7 @@ import (
1717 "github.com/snyk/error-catalog-golang-public/snyk_errors"
1818 "github.com/stretchr/testify/assert"
1919
20+ "github.com/snyk/go-application-framework/internal/api/contract"
2021 policyApi "github.com/snyk/go-application-framework/internal/api/policy/2024-10-15"
2122 "github.com/snyk/go-application-framework/pkg/configuration"
2223 localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
@@ -51,7 +52,7 @@ func setupMockIgnoreContext(t *testing.T, payload string, statusCode int) *mocks
5152 config := configuration .New ()
5253 config .Set (configuration .API_URL , "https://api.snyk.io" )
5354 config .Set (configuration .ORGANIZATION , uuid .New ().String ())
54- config .Set (configuration . FF_IAW_ENABLED , true )
55+ config .Set (ConfigIgnoreApprovalEnabled , true )
5556 // setup mocks
5657 ctrl := gomock .NewController (t )
5758 networkAccessMock := mocks .NewMockNetworkAccess (ctrl )
@@ -306,7 +307,7 @@ func Test_ignoreCreateWorkflowEntryPoint(t *testing.T) {
306307
307308 invocationContext := setupMockIgnoreContext (t , "{}" , http .StatusCreated )
308309 config := invocationContext .GetConfiguration ()
309- config .Set (configuration . FF_IAW_ENABLED , false )
310+ config .Set (ConfigIgnoreApprovalEnabled , false )
310311
311312 config .Set (InteractiveKey , false )
312313
@@ -330,7 +331,7 @@ func setupInteractiveMockContext(t *testing.T, mockApiResponse string, mockApiSt
330331 config := configuration .New ()
331332 config .Set (configuration .API_URL , "https://api.snyk.io" )
332333 config .Set (configuration .ORGANIZATION , uuid .New ().String ())
333- config .Set (configuration . FF_IAW_ENABLED , true )
334+ config .Set (ConfigIgnoreApprovalEnabled , true )
334335 config .Set (InteractiveKey , true ) // Always interactive
335336 config .Set (EnrichResponseKey , true )
336337 config .Set (configuration .ORGANIZATION_SLUG , "some-org" )
@@ -705,3 +706,114 @@ func Test_getExpireValue(t *testing.T) {
705706 assert .Nil (t , result )
706707 })
707708}
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+ t .Helper ()
791+ ctrl := gomock .NewController (t )
792+ defer ctrl .Finish ()
793+
794+ orgId := uuid .New ().String ()
795+ apiUrl := "https://api.snyk.io"
796+
797+ responseJSON , err := json .Marshal (response )
798+ assert .NoError (t , err )
799+
800+ mockEngine := mocks .NewMockEngine (ctrl )
801+ mockConfig := mocks .NewMockConfiguration (ctrl )
802+ mockNetworkAccess := mocks .NewMockNetworkAccess (ctrl )
803+
804+ httpClient := localworkflows .NewTestClient (func (req * http.Request ) * http.Response {
805+ return & http.Response {
806+ StatusCode : http .StatusOK ,
807+ Body : io .NopCloser (bytes .NewBuffer (responseJSON )),
808+ }
809+ })
810+
811+ mockEngine .EXPECT ().GetConfiguration ().Return (mockConfig )
812+ mockEngine .EXPECT ().GetNetworkAccess ().Return (mockNetworkAccess )
813+ mockConfig .EXPECT ().GetString (configuration .ORGANIZATION ).Return (orgId )
814+ mockConfig .EXPECT ().GetString (configuration .API_URL ).Return (apiUrl )
815+ mockNetworkAccess .EXPECT ().GetHttpClient ().Return (httpClient )
816+
817+ defaultValueFunc := getOrgIgnoreApprovalEnabled (mockEngine )
818+ return defaultValueFunc (nil , nil )
819+ }
0 commit comments