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