github.com/Axway/agent-sdk@v1.1.101/pkg/apic/specsecuritybuilder_test.go (about)

     1  package apic
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/getkin/kin-openapi/openapi2"
     7  	"github.com/getkin/kin-openapi/openapi3"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestAPIKeySecuritySchemeBuilder(t *testing.T) {
    12  	// OAS2
    13  	b := newSpecSecurityBuilder(2)
    14  
    15  	// api key builder
    16  	oas2Schemes := b.APIKey().InCookie().InHeader().InQueryParam().SetArgumentName("api-key").Build()
    17  	assert.Len(t, oas2Schemes, 2)
    18  	assert.NotContains(t, oas2Schemes, "apiKeyCookie")
    19  	oas2Header, ok := oas2Schemes["apiKeyHeader"].(*openapi2.SecurityScheme)
    20  	if !ok {
    21  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
    22  	}
    23  	assert.Equal(t, "apiKey", oas2Header.Type)
    24  	assert.Equal(t, "header", oas2Header.In)
    25  	assert.Equal(t, "api-key", oas2Header.Name)
    26  	oas2Query, ok := oas2Schemes["apiKeyQuery"].(*openapi2.SecurityScheme)
    27  	if !ok {
    28  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
    29  	}
    30  	assert.Equal(t, "apiKey", oas2Query.Type)
    31  	assert.Equal(t, "query", oas2Query.In)
    32  	assert.Equal(t, "api-key", oas2Query.Name)
    33  
    34  	// OAS3
    35  	b = newSpecSecurityBuilder(3)
    36  
    37  	// api key builder
    38  	oas3Schemes := b.APIKey().InCookie().InHeader().InQueryParam().SetArgumentName("api-key").Build()
    39  	assert.Len(t, oas3Schemes, 3)
    40  	assert.Contains(t, oas3Schemes, "apiKeyCookie")
    41  	oas3Header, ok := oas3Schemes["apiKeyHeader"].(*openapi3.SecurityScheme)
    42  	if !ok {
    43  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
    44  	}
    45  	assert.Equal(t, "apiKey", oas3Header.Type)
    46  	assert.Equal(t, "header", oas3Header.In)
    47  	assert.Equal(t, "api-key", oas3Header.Name)
    48  	oas3Query, ok := oas3Schemes["apiKeyQuery"].(*openapi3.SecurityScheme)
    49  	if !ok {
    50  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
    51  	}
    52  	assert.Equal(t, "apiKey", oas3Query.Type)
    53  	assert.Equal(t, "query", oas3Query.In)
    54  	assert.Equal(t, "api-key", oas3Query.Name)
    55  	oas3Cookie, ok := oas3Schemes["apiKeyCookie"].(*openapi3.SecurityScheme)
    56  	if !ok {
    57  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
    58  	}
    59  	assert.Equal(t, "apiKey", oas3Cookie.Type)
    60  	assert.Equal(t, "cookie", oas3Cookie.In)
    61  	assert.Equal(t, "api-key", oas3Cookie.Name)
    62  }
    63  
    64  func TestHTTPBasicSecuritySchemeBuilder(t *testing.T) {
    65  	// OAS2
    66  	b := newSpecSecurityBuilder(2)
    67  
    68  	oas2Schemes := b.HTTPBasic().Build()
    69  	assert.Len(t, oas2Schemes, 1)
    70  	oas2Basic, ok := oas2Schemes["basicAuth"].(*openapi2.SecurityScheme)
    71  	if !ok {
    72  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
    73  	}
    74  	assert.Equal(t, "basic", oas2Basic.Type)
    75  
    76  	// OAS3
    77  	b = newSpecSecurityBuilder(3)
    78  
    79  	oas3Schemes := b.HTTPBasic().Build()
    80  	assert.Len(t, oas3Schemes, 1)
    81  	oas3Basic, ok := oas3Schemes["basicAuth"].(*openapi3.SecurityScheme)
    82  	if !ok {
    83  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
    84  	}
    85  	assert.Equal(t, "http", oas3Basic.Type)
    86  	assert.Equal(t, "basic", oas3Basic.Scheme)
    87  }
    88  
    89  func TestBearerSecuritySchemeBuilder(t *testing.T) {
    90  	// OAS2
    91  	b := newSpecSecurityBuilder(2)
    92  
    93  	oas2Schemes := b.Bearer().SetFormat("jwt").Build()
    94  	assert.Len(t, oas2Schemes, 0)
    95  
    96  	// OAS3
    97  	b = newSpecSecurityBuilder(3)
    98  
    99  	oas3Schemes := b.Bearer().SetFormat("jwt").Build()
   100  	assert.Len(t, oas3Schemes, 1)
   101  	oas3Bearer, ok := oas3Schemes["bearerAuth"].(*openapi3.SecurityScheme)
   102  	if !ok {
   103  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
   104  	}
   105  	assert.Equal(t, "http", oas3Bearer.Type)
   106  	assert.Equal(t, "bearer", oas3Bearer.Scheme)
   107  	assert.Equal(t, "jwt", oas3Bearer.BearerFormat)
   108  }
   109  
   110  func TestOpenIDSecuritySchemeBuilder(t *testing.T) {
   111  	// OAS2
   112  	b := newSpecSecurityBuilder(2)
   113  
   114  	oas2Schemes := b.OpenID().SetURL("http://test.com").Build()
   115  	assert.Len(t, oas2Schemes, 0)
   116  
   117  	// OAS3
   118  	b = newSpecSecurityBuilder(3)
   119  
   120  	oas3Schemes := b.OpenID().SetURL("http://test.com").Build()
   121  	assert.Len(t, oas3Schemes, 1)
   122  	oas3Bearer, ok := oas3Schemes["openId"].(*openapi3.SecurityScheme)
   123  	if !ok {
   124  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
   125  	}
   126  	assert.Equal(t, "openIdConnect", oas3Bearer.Type)
   127  	assert.Equal(t, "http://test.com", oas3Bearer.OpenIdConnectUrl)
   128  }
   129  
   130  func TestOAuthSecuritySchemeBuilder(t *testing.T) {
   131  	// OAS2
   132  	b := newSpecSecurityBuilder(2)
   133  
   134  	oas2Schemes := b.OAuth().
   135  		AddFlow(NewOAuthFlowBuilder().
   136  			SetScopes(map[string]string{"scope1": ""}).
   137  			AddScope("scope2", "").
   138  			SetAuthorizationURL("http://authurl.com").
   139  			Implicit()).
   140  		AddFlow(NewOAuthFlowBuilder().
   141  			SetScopes(map[string]string{"scope1": ""}).
   142  			AddScope("scope2", "").
   143  			SetTokenURL("http://tokenurl.com").
   144  			ClientCredentials()).
   145  		AddFlow(NewOAuthFlowBuilder().
   146  			SetScopes(map[string]string{"scope1": ""}).
   147  			AddScope("scope2", "").
   148  			SetAuthorizationURL("http://authurl.com").
   149  			SetTokenURL("http://tokenurl.com").
   150  			AuthorizationCode()).
   151  		AddFlow(NewOAuthFlowBuilder().
   152  			SetScopes(map[string]string{"scope1": ""}).
   153  			AddScope("scope2", "").
   154  			SetTokenURL("http://tokenurl.com").
   155  			Password()).
   156  		Build()
   157  	assert.Len(t, oas2Schemes, 4)
   158  
   159  	oas2Implicit, ok := oas2Schemes["oauth2Implicit"].(*openapi2.SecurityScheme)
   160  	if !ok {
   161  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
   162  	}
   163  	assert.Equal(t, "oauth2", oas2Implicit.Type)
   164  	assert.Equal(t, "implicit", oas2Implicit.Flow)
   165  	assert.Equal(t, "http://authurl.com", oas2Implicit.AuthorizationURL)
   166  	assert.Len(t, oas2Implicit.Scopes, 2)
   167  
   168  	oas2Application, ok := oas2Schemes["oauth2Application"].(*openapi2.SecurityScheme)
   169  	if !ok {
   170  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
   171  	}
   172  	assert.Equal(t, "oauth2", oas2Application.Type)
   173  	assert.Equal(t, "application", oas2Application.Flow)
   174  	assert.Equal(t, "http://tokenurl.com", oas2Application.TokenURL)
   175  	assert.Len(t, oas2Application.Scopes, 2)
   176  
   177  	oas2AccessCode, ok := oas2Schemes["oauth2Accesscode"].(*openapi2.SecurityScheme)
   178  	if !ok {
   179  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
   180  	}
   181  	assert.Equal(t, "oauth2", oas2AccessCode.Type)
   182  	assert.Equal(t, "accessCode", oas2AccessCode.Flow)
   183  	assert.Equal(t, "http://authurl.com", oas2AccessCode.AuthorizationURL)
   184  	assert.Equal(t, "http://tokenurl.com", oas2AccessCode.TokenURL)
   185  	assert.Len(t, oas2AccessCode.Scopes, 2)
   186  
   187  	oas2Password, ok := oas2Schemes["oauth2Password"].(*openapi2.SecurityScheme)
   188  	if !ok {
   189  		assert.FailNow(t, "interface was not an *openapi2.SecurityScheme type")
   190  	}
   191  	assert.Equal(t, "oauth2", oas2Password.Type)
   192  	assert.Equal(t, "password", oas2Password.Flow)
   193  	assert.Equal(t, "http://tokenurl.com", oas2Password.TokenURL)
   194  	assert.Len(t, oas2Password.Scopes, 2)
   195  
   196  	// OAS3
   197  	b = newSpecSecurityBuilder(3)
   198  
   199  	oas3Schemes := b.OAuth().
   200  		AddFlow(NewOAuthFlowBuilder().
   201  			SetScopes(map[string]string{"scope1": ""}).
   202  			AddScope("scope2", "").
   203  			SetAuthorizationURL("http://authurl.com").
   204  			SetRefreshURL("http://refreshurl.com").
   205  			Implicit()).
   206  		AddFlow(NewOAuthFlowBuilder().
   207  			SetScopes(map[string]string{"scope1": ""}).
   208  			AddScope("scope2", "").
   209  			SetRefreshURL("http://refreshurl.com").
   210  			SetTokenURL("http://tokenurl.com").
   211  			ClientCredentials()).
   212  		AddFlow(NewOAuthFlowBuilder().
   213  			SetScopes(map[string]string{"scope1": ""}).
   214  			AddScope("scope2", "").
   215  			SetAuthorizationURL("http://authurl.com").
   216  			SetRefreshURL("http://refreshurl.com").
   217  			SetTokenURL("http://tokenurl.com").
   218  			AuthorizationCode()).
   219  		AddFlow(NewOAuthFlowBuilder().
   220  			SetScopes(map[string]string{"scope1": ""}).
   221  			AddScope("scope2", "").
   222  			SetRefreshURL("http://refreshurl.com").
   223  			SetTokenURL("http://tokenurl.com").
   224  			Password()).
   225  		Build()
   226  	assert.Len(t, oas3Schemes, 1)
   227  
   228  	oas3Auth, ok := oas3Schemes["oauth2"].(*openapi3.SecurityScheme)
   229  	if !ok {
   230  		assert.FailNow(t, "interface was not an *openapi3.SecurityScheme type")
   231  	}
   232  
   233  	assert.Equal(t, "oauth2", oas3Auth.Type)
   234  	assert.Equal(t, "http://authurl.com", oas3Auth.Flows.Implicit.AuthorizationURL)
   235  	assert.Equal(t, "http://refreshurl.com", oas3Auth.Flows.Implicit.RefreshURL)
   236  	assert.Len(t, oas3Auth.Flows.Implicit.Scopes, 2)
   237  
   238  	assert.Equal(t, "http://authurl.com", oas3Auth.Flows.AuthorizationCode.AuthorizationURL)
   239  	assert.Equal(t, "http://tokenurl.com", oas3Auth.Flows.AuthorizationCode.TokenURL)
   240  	assert.Equal(t, "http://refreshurl.com", oas3Auth.Flows.AuthorizationCode.RefreshURL)
   241  	assert.Len(t, oas3Auth.Flows.AuthorizationCode.Scopes, 2)
   242  
   243  	assert.Equal(t, "http://tokenurl.com", oas3Auth.Flows.Password.TokenURL)
   244  	assert.Equal(t, "http://refreshurl.com", oas3Auth.Flows.Password.RefreshURL)
   245  	assert.Len(t, oas3Auth.Flows.Password.Scopes, 2)
   246  
   247  	assert.Equal(t, "http://tokenurl.com", oas3Auth.Flows.ClientCredentials.TokenURL)
   248  	assert.Equal(t, "http://refreshurl.com", oas3Auth.Flows.ClientCredentials.RefreshURL)
   249  	assert.Len(t, oas3Auth.Flows.ClientCredentials.Scopes, 2)
   250  }