github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/cmd/swagger/commands/validate_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"io"
     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(io.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(io.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(io.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(io.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")
    86  		assert.Contains(t, result.Error(), "of type []spec.Parameter")
    87  	}
    88  }
    89  
    90  // This one is a correct version of issue#342 and it validates
    91  func TestCmd_Validate_Issue342_Correct(t *testing.T) {
    92  	log.SetOutput(io.Discard)
    93  	defer log.SetOutput(os.Stdout)
    94  	log.SetOutput(io.Discard)
    95  	defer func() {
    96  		log.SetOutput(os.Stdout)
    97  	}()
    98  
    99  	v := ValidateSpec{}
   100  	base := filepath.FromSlash("../../../")
   101  	specDoc := filepath.Join(base, "fixtures", "bugs", "342", "fixture-342-3.yaml")
   102  	result := v.Execute([]string{specDoc})
   103  	assert.NoError(t, result)
   104  }