@@ -32,6 +32,28 @@ import (
3232 "golang.org/x/oauth2/jws"
3333)
3434
35+ func createOAuthTokenWithAudience (t * testing.T , audience string ) string {
36+ t .Helper ()
37+ header := & jws.Header {}
38+ claims := & jws.ClaimSet {
39+ Aud : audience ,
40+ }
41+ pk , err := rsa .GenerateKey (rand .Reader , 2048 )
42+ assert .NoError (t , err )
43+
44+ accessToken , err := jws .Encode (header , claims , pk )
45+ assert .NoError (t , err )
46+
47+ token := oauth2.Token {
48+ AccessToken : accessToken ,
49+ }
50+
51+ tokenBytes , err := json .Marshal (token )
52+ assert .NoError (t , err )
53+
54+ return string (tokenBytes )
55+ }
56+
3557func Test_AddsDefaultFunctionForCustomConfigFiles (t * testing.T ) {
3658 t .Run ("should load default config files (without given command line)" , func (t * testing.T ) {
3759 localConfig := configuration .NewWithOpts ()
@@ -792,33 +814,12 @@ func Test_auth_oauth(t *testing.T) {
792814 })
793815
794816 t .Run ("oauth token change updates api url extraction" , func (t * testing.T ) {
795- createOAuthTokenWithAudience := func (audience string ) string {
796- header := & jws.Header {}
797- claims := & jws.ClaimSet {
798- Aud : audience ,
799- }
800- pk , err := rsa .GenerateKey (rand .Reader , 2048 )
801- assert .NoError (t , err )
802-
803- accessToken , err := jws .Encode (header , claims , pk )
804- assert .NoError (t , err )
805-
806- token := oauth2.Token {
807- AccessToken : accessToken ,
808- }
809-
810- tokenBytes , err := json .Marshal (token )
811- assert .NoError (t , err )
812-
813- return string (tokenBytes )
814- }
815-
816817 globalConfig := engine .GetConfiguration ()
817818 globalConfig .Set (configuration .API_URL , "https://api.snyk.io" )
818819 invocationConfig := globalConfig .Clone ()
819820
820821 firstAPIURL := "https://api.eu.snyk.io"
821- firstOAuthToken := createOAuthTokenWithAudience (firstAPIURL )
822+ firstOAuthToken := createOAuthTokenWithAudience (t , firstAPIURL )
822823
823824 // Set auth type to OAuth
824825 invocationConfig .Set (localworkflows .AuthTypeParameter , auth .AUTH_TYPE_OAUTH )
@@ -849,7 +850,7 @@ func Test_auth_oauth(t *testing.T) {
849850
850851 // Now simulate re-authentication with EU
851852 secondAPIURL := "https://api.us.snyk.io"
852- secondOAuthToken := createOAuthTokenWithAudience (secondAPIURL )
853+ secondOAuthToken := createOAuthTokenWithAudience (t , secondAPIURL )
853854
854855 // Mock second authentication
855856 mockAuthenticator .EXPECT ().Authenticate ().DoAndReturn (func () error {
@@ -870,3 +871,59 @@ func Test_auth_oauth(t *testing.T) {
870871 assert .Equal (t , secondAPIURL , actualApiUrl , "Second OAuth token should contain US API URL" )
871872 })
872873}
874+
875+ // this tests compares the behavior of the config when it has caching enabled and when it doesn't
876+ func Test_config_compareCachedAndUncachedConfig (t * testing.T ) {
877+ tests := []struct {
878+ name string
879+ config configuration.Configuration
880+ }{
881+ {
882+ name : "Cached config" ,
883+ config : configuration .NewWithOpts (configuration .WithCachingEnabled (time .Hour * 1 )),
884+ },
885+ {
886+ name : "Uncached config" ,
887+ config : configuration .NewWithOpts (),
888+ },
889+ }
890+
891+ for _ , tt := range tests {
892+ t .Run (tt .name , func (t * testing.T ) {
893+ engine := CreateAppEngineWithOptions (WithConfiguration (tt .config ))
894+ assert .NotNil (t , engine )
895+
896+ // Default API URL
897+ assert .Equal (t , constants .SNYK_DEFAULT_API_URL , tt .config .GetString (configuration .API_URL ))
898+
899+ // set API URL explicitly
900+ tt .config .Set (configuration .API_URL , "https://api.us.snyk.io" )
901+ assert .Equal (t , "https://api.us.snyk.io" , tt .config .GetString (configuration .API_URL ))
902+ assert .Equal (t , "https://app.us.snyk.io" , tt .config .GetString (configuration .WEB_APP_URL ))
903+
904+ // set PAT and derive API URL
905+ tt .config .Set (configuration .AUTHENTICATION_TOKEN , createMockPAT (t , `{"h":"api.au.snyk.io"}` ))
906+ assert .Equal (t , "https://api.au.snyk.io" , tt .config .GetString (configuration .API_URL ))
907+ assert .Equal (t , "https://app.au.snyk.io" , tt .config .GetString (configuration .WEB_APP_URL ))
908+ tt .config .Unset (configuration .AUTHENTICATION_TOKEN )
909+
910+ // set OAuth token and derive API URL
911+ tt .config .Set (auth .CONFIG_KEY_OAUTH_TOKEN , createOAuthTokenWithAudience (t , "https://api.snykgov.io" ))
912+ assert .Equal (t , "https://api.snykgov.io" , tt .config .GetString (configuration .API_URL ))
913+ assert .Equal (t , "https://app.snykgov.io" , tt .config .GetString (configuration .WEB_APP_URL ))
914+
915+ // set PAT and derive API URL
916+ tt .config .Set (configuration .AUTHENTICATION_TOKEN , createMockPAT (t , `{"h":"api.eu.snyk.io"}` ))
917+ assert .Equal (t , "https://api.eu.snyk.io" , tt .config .GetString (configuration .API_URL ))
918+ assert .Equal (t , "https://app.eu.snyk.io" , tt .config .GetString (configuration .WEB_APP_URL ))
919+
920+ // unset PAT and OAuth token
921+ tt .config .Unset (configuration .AUTHENTICATION_TOKEN )
922+ tt .config .Unset (auth .CONFIG_KEY_OAUTH_TOKEN )
923+
924+ // exlicitly set API URL is restored
925+ assert .Equal (t , "https://api.us.snyk.io" , tt .config .GetString (configuration .API_URL ))
926+ assert .Equal (t , "https://app.us.snyk.io" , tt .config .GetString (configuration .WEB_APP_URL ))
927+ })
928+ }
929+ }
0 commit comments