-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also make it a little easier to update tests when rules are added.
- Loading branch information
1 parent
8eee752
commit 5e11313
Showing
8 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2022 Dave Shanley / Quobix | ||
// SPDX-License-Identifier: MIT | ||
|
||
package openapi | ||
|
||
import ( | ||
"fmt" | ||
"github.com/daveshanley/vacuum/model" | ||
"github.com/pb33f/libopenapi/utils" | ||
"gopkg.in/yaml.v3" | ||
"strings" | ||
) | ||
|
||
// VerbsInPaths Checks to make sure that no HTTP verbs have been used | ||
type VerbsInPaths struct { | ||
} | ||
|
||
// GetSchema returns a model.RuleFunctionSchema defining the schema of the VerbsInPath rule. | ||
func (vp VerbsInPaths) GetSchema() model.RuleFunctionSchema { | ||
return model.RuleFunctionSchema{Name: "noVerbsInPath"} | ||
} | ||
|
||
// RunRule will execute the VerbsInPath rule, based on supplied context and a supplied []*yaml.Node slice. | ||
func (vp VerbsInPaths) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult { | ||
|
||
if len(nodes) <= 0 { | ||
return nil | ||
} | ||
|
||
var results []model.RuleFunctionResult | ||
|
||
ops := context.Index.GetPathsNode() | ||
|
||
var opPath string | ||
|
||
if ops != nil { | ||
for i, op := range ops.Content { | ||
if i%2 == 0 { | ||
opPath = op.Value | ||
continue | ||
} | ||
path := fmt.Sprintf("$.paths.%s", opPath) | ||
containsVerb, verb := checkPath(opPath) | ||
if containsVerb { | ||
results = append(results, model.RuleFunctionResult{ | ||
Message: fmt.Sprintf("Path `%s` contains an HTTP Verb `%s`", opPath, verb), | ||
StartNode: op, | ||
EndNode: op, | ||
Path: path, | ||
Rule: context.Rule, | ||
}) | ||
} | ||
} | ||
} | ||
return results | ||
} | ||
|
||
func checkPath(path string) (bool, string) { | ||
segs := strings.Split(path, "/")[1:] | ||
for _, seg := range segs { | ||
if utils.IsHttpVerb(strings.ToLower(seg)) { | ||
return true, seg | ||
} | ||
} | ||
return false, "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package openapi | ||
|
||
import ( | ||
"github.com/daveshanley/vacuum/model" | ||
"github.com/pb33f/libopenapi/index" | ||
"github.com/pb33f/libopenapi/utils" | ||
"github.com/stretchr/testify/assert" | ||
"gopkg.in/yaml.v3" | ||
"testing" | ||
) | ||
|
||
func TestVerbsInPaths_GetSchema(t *testing.T) { | ||
def := VerbsInPaths{} | ||
assert.Equal(t, "noVerbsInPath", def.GetSchema().Name) | ||
} | ||
|
||
func TestVerbsInPaths_RunRule(t *testing.T) { | ||
def := VerbsInPaths{} | ||
res := def.RunRule(nil, model.RuleFunctionContext{}) | ||
assert.Len(t, res, 0) | ||
} | ||
|
||
func TestVerbsInPaths_Success(t *testing.T) { | ||
|
||
yml := `openapi: 3.0.0 | ||
paths: | ||
'/oh/no/post/man': | ||
get: | ||
summary: bad path | ||
'/this/one/is/OK': | ||
get: | ||
summary: good path | ||
'/i/am/going/to/get/failed': | ||
get: | ||
summary: not a good one. | ||
'/will/you/patch/my/code': | ||
get: | ||
summary: this is also doomed | ||
'/put/my/cake/away': | ||
get: | ||
summary: another bad one. | ||
'/this/is/fine': | ||
get: | ||
summary: all done squire` | ||
path := "$" | ||
|
||
var rootNode yaml.Node | ||
yaml.Unmarshal([]byte(yml), &rootNode) | ||
|
||
nodes, _ := utils.FindNodes([]byte(yml), path) | ||
|
||
rule := buildOpenApiTestRuleAction(path, "verbsInPath", "", nil) | ||
ctx := buildOpenApiTestContext(model.CastToRuleAction(rule.Then), nil) | ||
ctx.Rule = &rule | ||
ctx.Index = index.NewSpecIndex(&rootNode) | ||
|
||
def := VerbsInPaths{} | ||
res := def.RunRule(nodes, ctx) | ||
|
||
assert.Len(t, res, 4) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters