From c5e9dbc360b3243bb1b59cb6f7078d6de7acf653 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Sat, 18 Feb 2023 17:10:52 -0500 Subject: [PATCH] Bumping coverage on 0.6.0 --- index/find_component.go | 9 +-- index/find_component_test.go | 101 ++++++++++++++++++++++++++++++++++ index/search_index_test.go | 6 +- index/spec_index_test.go | 15 ----- index/utility_methods_test.go | 6 ++ 5 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 index/find_component_test.go diff --git a/index/find_component.go b/index/find_component.go index e904abe..2f22846 100644 --- a/index/find_component.go +++ b/index/find_component.go @@ -296,11 +296,9 @@ func (index *SpecIndex) performExternalLookup(uri []string, componentId string, } else { j, _ = url.Parse(uri[0]) } - path := GenerateCleanSpecConfigBaseURL(j, uri[0], false) - - newUrl, e := url.Parse(path) - if e == nil { + newUrl, _ := url.Parse(path) + if newUrl != nil { newConfig := &SpecIndexConfig{ BaseURL: newUrl, AllowRemoteLookup: index.config.AllowRemoteLookup, @@ -318,9 +316,6 @@ func (index *SpecIndex) performExternalLookup(uri []string, componentId string, index.AddChild(newIndex) index.refLock.Unlock() externalSpecIndex = newIndex - - } else { - return nil } } diff --git a/index/find_component_test.go b/index/find_component_test.go new file mode 100644 index 0000000..cd6ddce --- /dev/null +++ b/index/find_component_test.go @@ -0,0 +1,101 @@ +// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley +// SPDX-License-Identifier: MIT + +package index + +import ( + "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" + "testing" +) + +func TestSpecIndex_performExternalLookup(t *testing.T) { + yml := `{ + "openapi": "3.1.0", + "paths": [ + {"/": { + "get": {} + }} + ] +}` + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + c := CreateOpenAPIIndexConfig() + index := NewSpecIndexWithConfig(&rootNode, c) + assert.Len(t, index.GetPathsNode().Content, 1) +} + +func TestSpecIndex_performExternalLookup_invalidURL(t *testing.T) { + yml := `openapi: 3.1.0 +components: + schemas: + thing: + properties: + thong: + $ref: 'httpssss://not-gonna-work.com'` + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + c := CreateOpenAPIIndexConfig() + index := NewSpecIndexWithConfig(&rootNode, c) + assert.Len(t, index.GetReferenceIndexErrors(), 2) +} + +func TestSpecIndex_FindComponentInRoot(t *testing.T) { + yml := `openapi: 3.1.0 +components: + schemas: + thing: + properties: + thong: hi!` + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + c := CreateOpenAPIIndexConfig() + index := NewSpecIndexWithConfig(&rootNode, c) + + thing := index.FindComponentInRoot("#/$splish/$.../slash#$///./") + assert.Nil(t, thing) + assert.Len(t, index.GetReferenceIndexErrors(), 0) +} + +func TestSpecIndex_FailLookupRemoteComponent_badPath(t *testing.T) { + yml := `openapi: 3.1.0 +components: + schemas: + thing: + properties: + thong: + $ref: 'https://pb33f.io/site.webmanifest#/....$.ok../oh#/$$_-'` + + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + c := CreateOpenAPIIndexConfig() + index := NewSpecIndexWithConfig(&rootNode, c) + + thing := index.FindComponentInRoot("#/$splish/$.../slash#$///./") + assert.Nil(t, thing) + assert.Len(t, index.GetReferenceIndexErrors(), 2) +} + +func TestSpecIndex_FailLookupRemoteComponent_Ok_butNotFound(t *testing.T) { + yml := `openapi: 3.1.0 +components: + schemas: + thing: + properties: + thong: + $ref: 'https://pb33f.io/site.webmanifest#/valid-but-missing'` + + var rootNode yaml.Node + _ = yaml.Unmarshal([]byte(yml), &rootNode) + + c := CreateOpenAPIIndexConfig() + index := NewSpecIndexWithConfig(&rootNode, c) + + thing := index.FindComponentInRoot("#/valid-but-missing") + assert.Nil(t, thing) + assert.Len(t, index.GetReferenceIndexErrors(), 1) +} diff --git a/index/search_index_test.go b/index/search_index_test.go index 77a95ba..3ce9864 100644 --- a/index/search_index_test.go +++ b/index/search_index_test.go @@ -23,7 +23,7 @@ func TestSpecIndex_SearchIndexForReference(t *testing.T) { assert.NotNil(t, ref) } -func TestSpecIndex_SearchIndexForReferene_ExternalSpecs(t *testing.T) { +func TestSpecIndex_SearchIndexForReference_ExternalSpecs(t *testing.T) { // load up an index with lots of references petstore, _ := ioutil.ReadFile("../test_specs/digitalocean.yaml") @@ -46,4 +46,8 @@ func TestSpecIndex_SearchIndexForReferene_ExternalSpecs(t *testing.T) { assert.NotNil(t, ref) assert.Equal(t, "description", ref[0].Node.Content[0].Value) + ref = idx.SearchIndexForReference("../models/options.yml") + assert.NotNil(t, ref) + assert.Equal(t, "kubernetes_options", ref[0].Node.Content[0].Value) + } diff --git a/index/spec_index_test.go b/index/spec_index_test.go index 0bb9cae..9585616 100644 --- a/index/spec_index_test.go +++ b/index/spec_index_test.go @@ -546,21 +546,6 @@ func TestSpecIndex_FindComponenth(t *testing.T) { assert.Nil(t, index.FindComponent("I-do-not-exist", nil)) } -func TestSpecIndex_performExternalLookup(t *testing.T) { - yml := `{ - "openapi": "3.1.0", - "paths": [ - {"/": { - "get": {} - }} - ] -}` - var rootNode yaml.Node - yaml.Unmarshal([]byte(yml), &rootNode) - - index := NewSpecIndex(&rootNode) - assert.Len(t, index.GetPathsNode().Content, 1) -} func TestSpecIndex_TestPathsNodeAsArray(t *testing.T) { yml := `components: diff --git a/index/utility_methods_test.go b/index/utility_methods_test.go index 49af743..ba0ae43 100644 --- a/index/utility_methods_test.go +++ b/index/utility_methods_test.go @@ -25,3 +25,9 @@ func TestGenerateCleanSpecConfigBaseURL_RelativeDeep(t *testing.T) { assert.Equal(t, "https://pb33f.io/things/stuff/foo/bar/baz/crap.yaml#thang", GenerateCleanSpecConfigBaseURL(u, path, true)) } + +func TestSpecIndex_extractDefinitionRequiredRefProperties(t *testing.T) { + c := CreateOpenAPIIndexConfig() + idx := NewSpecIndexWithConfig(nil, c) + assert.Nil(t, idx.extractDefinitionRequiredRefProperties(nil, nil)) +} \ No newline at end of file