Skip to content

Commit

Permalink
openapi3filter: simplify validation test (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Jun 19, 2023
1 parent 41d2575 commit 5eeca32
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions openapi3filter/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,26 +497,26 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test
tc := []struct {
name string
schemes *[]ExampleSecurityScheme
expectedSchemes *[]ExampleSecurityScheme
expectedSchemes []ExampleSecurityScheme
}{
{
name: "/inherited-security",
schemes: nil,
expectedSchemes: &[]ExampleSecurityScheme{
expectedSchemes: []ExampleSecurityScheme{
securitySchemes[1],
},
},
{
name: "/overwrite-without-security",
schemes: &[]ExampleSecurityScheme{},
expectedSchemes: &[]ExampleSecurityScheme{},
expectedSchemes: []ExampleSecurityScheme{},
},
{
name: "/overwrite-with-security",
schemes: &[]ExampleSecurityScheme{
securitySchemes[0],
},
expectedSchemes: &[]ExampleSecurityScheme{
expectedSchemes: []ExampleSecurityScheme{
securitySchemes[0],
},
},
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test

// Add the paths from the test cases to the spec's paths
for _, tc := range tc {
var securityRequirements *openapi3.SecurityRequirements = nil
var securityRequirements *openapi3.SecurityRequirements
if tc.schemes != nil {
tempS := openapi3.NewSecurityRequirements()
for _, scheme := range *tc.schemes {
Expand All @@ -572,13 +572,9 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test
// Test each case
for _, path := range tc {
// Make a map of the schemes and whether they're
var schemesValidated *map[*openapi3.SecurityScheme]bool = nil
if path.expectedSchemes != nil {
temp := make(map[*openapi3.SecurityScheme]bool)
schemesValidated = &temp
for _, scheme := range *path.expectedSchemes {
(*schemesValidated)[scheme.Scheme] = false
}
schemesValidated := make(map[*openapi3.SecurityScheme]bool)
for _, scheme := range path.expectedSchemes {
schemesValidated[scheme.Scheme] = false
}

// Create the request
Expand All @@ -591,15 +587,13 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test
Route: route,
Options: &Options{
AuthenticationFunc: func(ctx context.Context, input *AuthenticationInput) error {
if schemesValidated != nil {
if validated, ok := (*schemesValidated)[input.SecurityScheme]; ok {
if validated {
t.Fatalf("The path %q had the schemes %v named %q validated more than once",
path.name, input.SecurityScheme, input.SecuritySchemeName)
}
(*schemesValidated)[input.SecurityScheme] = true
return nil
if validated, ok := schemesValidated[input.SecurityScheme]; ok {
if validated {
t.Fatalf("The path %q had the schemes %v named %q validated more than once",
path.name, input.SecurityScheme, input.SecuritySchemeName)
}
schemesValidated[input.SecurityScheme] = true
return nil
}

t.Fatalf("The path %q had the schemes %v named %q",
Expand All @@ -614,7 +608,7 @@ func TestRootSecurityRequirementsAreUsedIfNotProvidedAtTheOperationLevel(t *test
err = ValidateRequest(context.Background(), &req)
require.NoError(t, err)

for securityRequirement, validated := range *schemesValidated {
for securityRequirement, validated := range schemesValidated {
if !validated {
t.Fatalf("The security requirement %v was unexpected to be validated but wasn't",
securityRequirement)
Expand Down

0 comments on commit 5eeca32

Please sign in to comment.