weldr: deleting an unknown source should return an error

This adds a check to see if the source is valid and returns a 400 error
if it doesn't exist.
This commit is contained in:
Brian C. Lane 2021-09-17 13:53:13 -07:00 committed by Ondřej Budai
parent 50b85abd1c
commit e92424b5e3
2 changed files with 12 additions and 0 deletions

View file

@ -948,6 +948,17 @@ func (api *API) sourceDeleteHandler(writer http.ResponseWriter, request *http.Re
}
}
// Return an error for unknown sources
s := api.store.GetSource(name[0][1:])
if s == nil {
errors := responseError{
ID: "UnknownSource",
Msg: name[0][1:] + " is not a valid source.",
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}
// Only delete the first name, which will have a / at the start because of the /*source route
if isRequestVersionAtLeast(params, 1) {
api.store.DeleteSourceByID(name[0][1:])

View file

@ -1334,6 +1334,7 @@ func TestSourcesDelete(t *testing.T) {
}{
{"DELETE", "/api/v0/projects/source/delete/", ``, http.StatusNotFound, `{"status":false,"errors":[{"code":404,"id":"HTTPError","msg":"Not Found"}]}`},
{"DELETE", "/api/v0/projects/source/delete/fish", ``, http.StatusOK, `{"status":true}`},
{"DELETE", "/api/v0/projects/source/delete/unknown", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"UnknownSource","msg":"unknown is not a valid source."}]}`},
}
tempdir, err := ioutil.TempDir("", "weldr-tests-")