github.com/AngusLu/go-swagger@v0.28.0/scan/operations_test.go (about)

     1  // +build !go1.11
     2  
     3  // Copyright 2015 go-swagger maintainers
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //    http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package scan
    18  
    19  import (
    20  	goparser "go/parser"
    21  	"log"
    22  	"testing"
    23  
    24  	"github.com/go-openapi/spec"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestOperationsExpression(t *testing.T) {
    29  	assert.Regexp(t, rxOperation, "swagger:operation DELETE /orders/{id} deleteOrder")
    30  	assert.Regexp(t, rxOperation, "swagger:operation GET /v1.2/something deleteOrder")
    31  }
    32  
    33  func TestOperationsParser(t *testing.T) {
    34  	docFile := "../fixtures/goparsing/classification/operations_annotation/operations.go"
    35  	fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  
    40  	op := newOperationsParser(classificationProg)
    41  	var ops spec.Paths
    42  	err = op.Parse(fileTree, &ops, nil, nil)
    43  	assert.NoError(t, err)
    44  
    45  	assert.Len(t, ops.Paths, 3)
    46  
    47  	po, ok := ops.Paths["/pets"]
    48  	assert.True(t, ok)
    49  	assert.NotNil(t, po.Get)
    50  	assertAnnotationOperation(t,
    51  		po.Get,
    52  		"getPet",
    53  		"",
    54  		"List all pets",
    55  		[]string{"pets"},
    56  	)
    57  	if po.Get != nil {
    58  		rsp, k := po.Get.Responses.StatusCodeResponses[200]
    59  		if assert.True(t, k) {
    60  			assert.Equal(t, "An paged array of pets", rsp.Description)
    61  		}
    62  		if assert.NotNil(t, po.Get.Responses.Default) {
    63  			assert.Equal(t, "unexpected error", po.Get.Responses.Default.Description)
    64  		}
    65  	}
    66  
    67  	po, ok = ops.Paths["/pets/{id}"]
    68  	assert.True(t, ok)
    69  	assert.NotNil(t, po.Put)
    70  	assertAnnotationOperation(t,
    71  		po.Put,
    72  		"updatePet",
    73  		"Updates the details for a pet.",
    74  		"Some long explanation,\nspanning over multipele lines,\nAKA the description.",
    75  		[]string{"pets"},
    76  	)
    77  	if po.Put != nil {
    78  		rsp, k := po.Put.Responses.StatusCodeResponses[400]
    79  		if assert.True(t, k) {
    80  			assert.Equal(t, "Invalid ID supplied", rsp.Description)
    81  		}
    82  		rsp, k = po.Put.Responses.StatusCodeResponses[404]
    83  		if assert.True(t, k) {
    84  			assert.Equal(t, "Pet not found", rsp.Description)
    85  		}
    86  		rsp, k = po.Put.Responses.StatusCodeResponses[405]
    87  		if assert.True(t, k) {
    88  			assert.Equal(t, "Validation exception", rsp.Description)
    89  		}
    90  	}
    91  
    92  	po, ok = ops.Paths["/v1/events"]
    93  	assert.True(t, ok)
    94  	assert.NotNil(t, po.Get)
    95  	assertAnnotationOperation(t,
    96  		po.Get,
    97  		"getEvents",
    98  		"Events",
    99  		"Mitigation Events",
   100  		[]string{"Events"},
   101  	)
   102  	if po.Get != nil {
   103  		rsp, k := po.Get.Responses.StatusCodeResponses[200]
   104  		if assert.True(t, k) {
   105  			assert.Equal(t, "#/definitions/ListResponse", rsp.Schema.Ref.String())
   106  			assert.Equal(t, "200", rsp.Description)
   107  		}
   108  		rsp, k = po.Get.Responses.StatusCodeResponses[400]
   109  		if assert.True(t, k) {
   110  			assert.Equal(t, "#/definitions/ErrorResponse", rsp.Schema.Ref.String())
   111  			assert.Equal(t, "400", rsp.Description)
   112  		}
   113  	}
   114  }
   115  
   116  func assertAnnotationOperation(t *testing.T, op *spec.Operation, id, summary, description string, tags []string) {
   117  	assert.NotNil(t, op)
   118  	assert.Equal(t, summary, op.Summary)
   119  	assert.Equal(t, description, op.Description)
   120  	assert.Equal(t, id, op.ID)
   121  	assert.EqualValues(t, tags, op.Tags)
   122  	assert.Contains(t, op.Consumes, "application/json")
   123  	assert.Contains(t, op.Consumes, "application/xml")
   124  	assert.Contains(t, op.Produces, "application/json")
   125  	assert.Contains(t, op.Produces, "application/xml")
   126  	assert.Len(t, op.Security, 1)
   127  	if len(op.Security) > 0 {
   128  		akv, ok := op.Security[0]["petstore_auth"]
   129  		assert.True(t, ok)
   130  		// akv must be defined & not empty
   131  		assert.NotNil(t, akv)
   132  		assert.NotEmpty(t, akv)
   133  	}
   134  }