github.com/kaisawind/go-swagger@v0.19.0/cmd/swagger/commands/validate_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"io/ioutil"
     5  	"log"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  // Commands requires at least one arg
    14  func TestCmd_Validate_MissingArgs(t *testing.T) {
    15  	log.SetOutput(ioutil.Discard)
    16  	defer log.SetOutput(os.Stdout)
    17  	v := ValidateSpec{}
    18  	result := v.Execute([]string{})
    19  	assert.Error(t, result)
    20  
    21  	result = v.Execute([]string{"nowhere.json"})
    22  	assert.Error(t, result)
    23  }
    24  
    25  // Test proper validation: items in object error
    26  func TestCmd_Validate_Issue1238(t *testing.T) {
    27  	log.SetOutput(ioutil.Discard)
    28  	defer log.SetOutput(os.Stdout)
    29  	v := ValidateSpec{}
    30  	base := filepath.FromSlash("../../../")
    31  	specDoc := filepath.Join(base, "fixtures", "bugs", "1238", "swagger.yaml")
    32  	result := v.Execute([]string{specDoc})
    33  	if assert.Error(t, result) {
    34  		/*
    35  			The swagger spec at "../../../fixtures/bugs/1238/swagger.yaml" is invalid against swagger specification 2.0. see errors :
    36  				- definitions.RRSets in body must be of type array
    37  		*/
    38  		assert.Contains(t, result.Error(), "is invalid against swagger specification 2.0")
    39  		assert.Contains(t, result.Error(), "definitions.RRSets in body must be of type array")
    40  	}
    41  }
    42  
    43  // Test proper validation: missing items in array error
    44  func TestCmd_Validate_Issue1171(t *testing.T) {
    45  	log.SetOutput(ioutil.Discard)
    46  	defer log.SetOutput(os.Stdout)
    47  	v := ValidateSpec{}
    48  	base := filepath.FromSlash("../../../")
    49  	specDoc := filepath.Join(base, "fixtures", "bugs", "1171", "swagger.yaml")
    50  	result := v.Execute([]string{specDoc})
    51  	assert.Error(t, result)
    52  }
    53  
    54  // Test proper validation: reference to inner property in schema
    55  func TestCmd_Validate_Issue342_ForbiddenProperty(t *testing.T) {
    56  	log.SetOutput(ioutil.Discard)
    57  	defer log.SetOutput(os.Stdout)
    58  	v := ValidateSpec{}
    59  	base := filepath.FromSlash("../../../")
    60  	specDoc := filepath.Join(base, "fixtures", "bugs", "342", "fixture-342.yaml")
    61  	result := v.Execute([]string{specDoc})
    62  	assert.Error(t, result)
    63  }
    64  
    65  // fixture 342-2 (a variant of invalid specification) (cannot unmarshal)
    66  // Test proper validation: reference to shared top level parameter, but with incorrect
    67  // yaml syntax: use map key instead of array item.
    68  //
    69  // NOTE: this error message is not clear enough. The role of this test
    70  // is to determine that the validation does not panic and correctly states the spec is invalid.
    71  // Open a dedicated issue on message relevance. This test shall be updated with the finalized message.
    72  func TestCmd_Validate_Issue342_CannotUnmarshal(t *testing.T) {
    73  	v := ValidateSpec{}
    74  	base := filepath.FromSlash("../../../")
    75  	specDoc := filepath.Join(base, "fixtures", "bugs", "342", "fixture-342-2.yaml")
    76  	if !assert.NotPanics(t, func() {
    77  		_ = v.Execute([]string{specDoc})
    78  	}) {
    79  		t.FailNow()
    80  		return
    81  	}
    82  	result := v.Execute([]string{specDoc})
    83  	if assert.Error(t, result, "This spec should not pass validation") {
    84  		// assert.Contains(t, result.Error(), "is invalid against swagger specification 2.0")
    85  		assert.Contains(t, result.Error(), "json: cannot unmarshal object into Go struct field OperationProps.parameters of type []spec.Parameter")
    86  	}
    87  }
    88  
    89  // This one is a correct version of issue#342 and it validates
    90  func TestCmd_Validate_Issue342_Correct(t *testing.T) {
    91  	log.SetOutput(ioutil.Discard)
    92  	defer log.SetOutput(os.Stdout)
    93  	log.SetOutput(ioutil.Discard)
    94  	defer func() {
    95  		log.SetOutput(os.Stdout)
    96  	}()
    97  
    98  	v := ValidateSpec{}
    99  	base := filepath.FromSlash("../../../")
   100  	specDoc := filepath.Join(base, "fixtures", "bugs", "342", "fixture-342-3.yaml")
   101  	result := v.Execute([]string{specDoc})
   102  	assert.NoError(t, result)
   103  }