github.com/djarvur/go-swagger@v0.18.0/scan/operations_test.go (about)

     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 scan
    16  
    17  import (
    18  	goparser "go/parser"
    19  	"log"
    20  	"testing"
    21  
    22  	"github.com/go-openapi/spec"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestOperationsExpression(t *testing.T) {
    27  	assert.Regexp(t, rxOperation, "swagger:operation DELETE /orders/{id} deleteOrder")
    28  	assert.Regexp(t, rxOperation, "swagger:operation GET /v1.2/something deleteOrder")
    29  }
    30  
    31  func TestOperationsParser(t *testing.T) {
    32  	docFile := "../fixtures/goparsing/classification/operations_annotation/operations.go"
    33  	fileTree, err := goparser.ParseFile(classificationProg.Fset, docFile, nil, goparser.ParseComments)
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	op := newOperationsParser(classificationProg)
    39  	var ops spec.Paths
    40  	err = op.Parse(fileTree, &ops, nil, nil)
    41  	assert.NoError(t, err)
    42  
    43  	assert.Len(t, ops.Paths, 3)
    44  
    45  	po, ok := ops.Paths["/pets"]
    46  	assert.True(t, ok)
    47  	assert.NotNil(t, po.Get)
    48  	assertAnnotationOperation(t,
    49  		po.Get,
    50  		"getPet",
    51  		"",
    52  		"List all pets",
    53  		[]string{"pets"},
    54  	)
    55  	if po.Get != nil {
    56  		rsp, k := po.Get.Responses.StatusCodeResponses[200]
    57  		if assert.True(t, k) {
    58  			assert.Equal(t, "An paged array of pets", rsp.Description)
    59  		}
    60  		if assert.NotNil(t, po.Get.Responses.Default) {
    61  			assert.Equal(t, "unexpected error", po.Get.Responses.Default.Description)
    62  		}
    63  	}
    64  
    65  	po, ok = ops.Paths["/pets/{id}"]
    66  	assert.True(t, ok)
    67  	assert.NotNil(t, po.Put)
    68  	assertAnnotationOperation(t,
    69  		po.Put,
    70  		"updatePet",
    71  		"Updates the details for a pet.",
    72  		"Some long explanation,\nspanning over multipele lines,\nAKA the description.",
    73  		[]string{"pets"},
    74  	)
    75  	if po.Put != nil {
    76  		rsp, k := po.Put.Responses.StatusCodeResponses[400]
    77  		if assert.True(t, k) {
    78  			assert.Equal(t, "Invalid ID supplied", rsp.Description)
    79  		}
    80  		rsp, k = po.Put.Responses.StatusCodeResponses[404]
    81  		if assert.True(t, k) {
    82  			assert.Equal(t, "Pet not found", rsp.Description)
    83  		}
    84  		rsp, k = po.Put.Responses.StatusCodeResponses[405]
    85  		if assert.True(t, k) {
    86  			assert.Equal(t, "Validation exception", rsp.Description)
    87  		}
    88  	}
    89  
    90  	po, ok = ops.Paths["/v1/events"]
    91  	assert.True(t, ok)
    92  	assert.NotNil(t, po.Get)
    93  	assertAnnotationOperation(t,
    94  		po.Get,
    95  		"getEvents",
    96  		"Events",
    97  		"Mitigation Events",
    98  		[]string{"Events"},
    99  	)
   100  	if po.Get != nil {
   101  		rsp, k := po.Get.Responses.StatusCodeResponses[200]
   102  		if assert.True(t, k) {
   103  			assert.Equal(t, "#/definitions/ListResponse", rsp.Schema.Ref.String())
   104  			assert.Equal(t, "200", rsp.Description)
   105  		}
   106  		rsp, k = po.Get.Responses.StatusCodeResponses[400]
   107  		if assert.True(t, k) {
   108  			assert.Equal(t, "#/definitions/ErrorResponse", rsp.Schema.Ref.String())
   109  			assert.Equal(t, "400", rsp.Description)
   110  		}
   111  	}
   112  }
   113  
   114  func assertAnnotationOperation(t *testing.T, op *spec.Operation, id, summary, description string, tags []string) {
   115  	assert.NotNil(t, op)
   116  	assert.Equal(t, summary, op.Summary)
   117  	assert.Equal(t, description, op.Description)
   118  	assert.Equal(t, id, op.ID)
   119  	assert.EqualValues(t, tags, op.Tags)
   120  	assert.Contains(t, op.Consumes, "application/json")
   121  	assert.Contains(t, op.Consumes, "application/xml")
   122  	assert.Contains(t, op.Produces, "application/json")
   123  	assert.Contains(t, op.Produces, "application/xml")
   124  	assert.Len(t, op.Security, 1)
   125  	if len(op.Security) > 0 {
   126  		akv, ok := op.Security[0]["petstore_auth"]
   127  		assert.True(t, ok)
   128  		// akv must be defined & not empty
   129  		assert.NotNil(t, akv)
   130  		assert.NotEmpty(t, akv)
   131  	}
   132  }