Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openapi3filter: simplify validation test #806

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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