...

Source file src/github.com/go-openapi/validate/default_validator_test.go

Documentation: github.com/go-openapi/validate

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validate
    16  
    17  import (
    18  	"path/filepath"
    19  	"testing"
    20  
    21  	"github.com/go-openapi/analysis"
    22  	"github.com/go-openapi/loads"
    23  	"github.com/go-openapi/spec"
    24  	"github.com/go-openapi/strfmt"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  const (
    30  	jsonExt       = ".json"
    31  	hasErrorMsg   = " should have errors"
    32  	noErrorMsg    = " should not have errors"
    33  	hasWarningMsg = " should have warnings"
    34  )
    35  
    36  func TestDefault_ValidatePetStore(t *testing.T) {
    37  	doc, _ := loads.Analyzed(PetStoreJSONMessage, "")
    38  	validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
    39  	validator.spec = doc
    40  	validator.analyzer = analysis.New(doc.Spec())
    41  	myDefaultValidator := &defaultValidator{SpecValidator: validator}
    42  	res := myDefaultValidator.Validate()
    43  	assert.Empty(t, res.Errors)
    44  }
    45  
    46  func makeSpecValidator(t *testing.T, fp string) *SpecValidator {
    47  	doc, err := loads.Spec(fp)
    48  	require.NoError(t, err)
    49  
    50  	validator := NewSpecValidator(spec.MustLoadSwagger20Schema(), strfmt.Default)
    51  	validator.spec = doc
    52  	validator.analyzer = analysis.New(doc.Spec())
    53  	return validator
    54  }
    55  
    56  func TestDefault_ValidateDefaults(t *testing.T) {
    57  	tests := []string{
    58  		"parameter",
    59  		"parameter-required",
    60  		"parameter-ref",
    61  		"parameter-items",
    62  		"header",
    63  		"header-items",
    64  		"schema",
    65  		"schema-ref",
    66  		"schema-additionalProperties",
    67  		"schema-patternProperties",
    68  		"schema-items",
    69  		"schema-allOf",
    70  		"parameter-schema",
    71  		"default-response",
    72  		"header-response",
    73  		"header-items-default-response",
    74  		"header-items-response",
    75  		"header-pattern",
    76  		"header-badpattern",
    77  		"schema-items-allOf",
    78  		"response-ref",
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		path := filepath.Join("fixtures", "validation", "default", "valid-default-value-"+tt+jsonExt)
    83  		if DebugTest {
    84  			t.Logf("Testing valid default values for: %s", path)
    85  		}
    86  		validator := makeSpecValidator(t, path)
    87  		myDefaultValidator := &defaultValidator{SpecValidator: validator}
    88  		res := myDefaultValidator.Validate()
    89  		assert.Empty(t, res.Errors, tt+noErrorMsg)
    90  
    91  		// Special case: warning only
    92  		if tt == "parameter-required" {
    93  			warns := verifiedTestWarnings(res)
    94  			assert.Contains(t, warns, "limit in query has a default value and is required as parameter")
    95  		}
    96  
    97  		path = filepath.Join("fixtures", "validation", "default", "invalid-default-value-"+tt+jsonExt)
    98  		if DebugTest {
    99  			t.Logf("Testing invalid default values for: %s", path)
   100  		}
   101  
   102  		validator = makeSpecValidator(t, path)
   103  		myDefaultValidator = &defaultValidator{SpecValidator: validator}
   104  		res = myDefaultValidator.Validate()
   105  		assert.NotEmpty(t, res.Errors, tt+hasErrorMsg)
   106  
   107  		// Update: now we have an additional message to explain it's all about a default value
   108  		// Example:
   109  		// - default value for limit in query does not validate its Schema
   110  		// - limit in query must be of type integer: "string"]
   111  		assert.NotEmptyf(t, res.Errors, tt+" should have at least 1 error")
   112  	}
   113  }
   114  
   115  func TestDefault_EdgeCase(t *testing.T) {
   116  	// Testing guards
   117  	var myDefaultvalidator *defaultValidator
   118  	res := myDefaultvalidator.Validate()
   119  	assert.True(t, res.IsValid())
   120  
   121  	myDefaultvalidator = &defaultValidator{}
   122  	res = myDefaultvalidator.Validate()
   123  	assert.True(t, res.IsValid())
   124  }
   125  

View as plain text