Compare commits

...

4 Commits

Author SHA1 Message Date
Adrien Marquès 4221f8cf2c test internal/config trick to have a 100% coverage (as conflict check is undeterministic, merge all conditions)
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details
2019-11-21 22:29:58 +01:00
Adrien Marquès 8ba58b4748 test internal/config empty parameter rename should not rename 2019-11-21 22:27:57 +01:00
Adrien Marquès b18ea98497 remove dead code 2019-11-21 22:20:12 +01:00
Adrien Marquès 6218327fd2 expand internal/config test coverage
- make parameter type "?" invalid as it marks it as optional only
- check optional vs. required parameters
- test subservice in method description error check
2019-11-21 22:18:00 +01:00
2 changed files with 118 additions and 22 deletions

View File

@ -146,6 +146,18 @@ func TestParseMissingMethodDescription(t *testing.T) {
}`, }`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET /")), ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET /")),
}, },
{ // missing description
`{
"/": {
"subservice": {
"GET": {
}
}
}
}`,
ErrFormat.Wrap(ErrMissingMethodDesc.WrapString("GET subservice")),
},
{ // empty description { // empty description
`{ `{
"GET": { "GET": {
@ -197,6 +209,75 @@ func TestParseMissingMethodDescription(t *testing.T) {
} }
func TestParamEmptyRenameNoRename(t *testing.T) {
reader := strings.NewReader(`{
"GET": {
"info": "info",
"in": {
"original": { "info": "valid-desc", "type": "valid-type", "name": "" }
}
}
}`)
srv, err := Parse(reader)
if err != nil {
t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
method := srv.Method(http.MethodGet)
if method == nil {
t.Errorf("expected GET method not to be nil")
t.FailNow()
}
for _, param := range method.Parameters {
if param.Rename != "original" {
t.Errorf("expected the parameter 'original' not to be renamed to '%s'", param.Rename)
t.FailNow()
}
}
}
func TestOptionalParam(t *testing.T) {
reader := strings.NewReader(`{
"GET": {
"info": "info",
"in": {
"optional": { "info": "valid-desc", "type": "?optional-type" },
"required": { "info": "valid-desc", "type": "required-type" },
"required2": { "info": "valid-desc", "type": "a" },
"optional2": { "info": "valid-desc", "type": "?a" }
}
}
}`)
srv, err := Parse(reader)
if err != nil {
t.Errorf("unexpected error: '%s'", err)
t.FailNow()
}
method := srv.Method(http.MethodGet)
if method == nil {
t.Errorf("expected GET method not to be nil")
t.FailNow()
}
for pName, param := range method.Parameters {
if pName == "optional" || pName == "optional2" {
if !param.Optional {
t.Errorf("expected parameter '%s' to be optional", pName)
t.Failed()
}
}
if pName == "required" || pName == "required2" {
if param.Optional {
t.Errorf("expected parameter '%s' to be required", pName)
t.Failed()
}
}
}
}
func TestParseParameters(t *testing.T) { func TestParseParameters(t *testing.T) {
tests := []struct { tests := []struct {
Raw string Raw string
@ -284,6 +365,22 @@ func TestParseParameters(t *testing.T) {
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")), ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
nil, nil,
}, },
{ // invalid type (optional mark only)
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?"
}
}
}
}`,
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
ErrFormat.Wrap(ErrMissingParamType.WrapString("GET / {param1}")),
},
{ // valid description + valid type { // valid description + valid type
`{ `{
"GET": { "GET": {
@ -291,7 +388,22 @@ func TestParseParameters(t *testing.T) {
"in": { "in": {
"param1": { "param1": {
"info": "valid", "info": "valid",
"type": "valid" "type": "a"
}
}
}
}`,
nil,
nil,
},
{ // valid description + valid OPTIONAL type
`{
"GET": {
"info": "info",
"in": {
"param1": {
"info": "valid",
"type": "?valid"
} }
} }
} }
@ -378,7 +490,9 @@ func TestParseParameters(t *testing.T) {
if err != nil && test.Error != nil { if err != nil && test.Error != nil {
if err.Error() != test.Error.Error() && err.Error() != test.ErrorAlternative.Error() { if err.Error() != test.Error.Error() && err.Error() != test.ErrorAlternative.Error() {
t.Errorf("expected the error '%s' (got '%s')", test.Error.Error(), err.Error()) t.Errorf("got the error: '%s'", err.Error())
t.Errorf("expected error (alternative 1): '%s'", test.Error.Error())
t.Errorf("expected error (alternative 2): '%s'", test.ErrorAlternative.Error())
t.FailNow() t.FailNow()
} }
} }

View File

@ -39,17 +39,9 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
} }
// 3.2.1. Same rename field // 3.2.1. Same rename field
if pData.Rename == param.Rename {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
}
// 3.2.2. Not-renamed field matches a renamed field // 3.2.2. Not-renamed field matches a renamed field
if pName == param.Rename {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
}
// 3.2.3. Renamed field matches name // 3.2.3. Renamed field matches name
if pData.Rename == paramName { if pData.Rename == param.Rename || pName == param.Rename || pData.Rename == paramName {
return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}") return ErrParamNameConflict.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
} }
@ -61,7 +53,7 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
} }
// 3.4. Manage invalid type // 3.4. Manage invalid type
if len(pData.Type) < 1 { if len(pData.Type) < 1 || pData.Type == "?" {
return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}") return ErrMissingParamType.WrapString(httpMethod + " " + servicePath + " {" + pName + "}")
} }
@ -75,13 +67,3 @@ func (methodDef *Method) checkAndFormat(servicePath string, httpMethod string) e
return nil return nil
} }
// scopeHasPermission returns whether the permission fulfills a given scope
func scopeHasPermission(permission string, scope []string) bool {
for _, s := range scope {
if permission == s {
return true
}
}
return false
}