weldr: Adding sources with empty name should return an error

Currently, if a TOML source is added with no name, or the source is
incorrectly inside a [section] it will add an empty source, causing
depsolving to crash.

This adds tests for 'name' and 'type' fields as a minimum requirement,
and returns an API error if they are empty or missing.

This also includes unit and integration tests.

Closes PR#462
This commit is contained in:
Brian C. Lane 2020-04-03 10:30:45 -07:00 committed by Tom Gundersen
parent 58c16f1e6d
commit 0eb3bfe89a
3 changed files with 53 additions and 0 deletions

View file

@ -109,6 +109,25 @@ func TestPOSTInvalidTOMLSourceV0(t *testing.T) {
require.False(t, resp.Status, "did not return an error")
}
// POST a wrong TOML source
func TestPOSTWrongTOMLSourceV0(t *testing.T) {
// Should not have a [] section
source := `
[package-repo-toml-v0]
name = "package-repo-toml-v0"
url = "file://REPO-PATH
type = "yum-baseurl"
proxy = "https://proxy-url/"
check_ssl = true
check_gpg = true
gpgkey_urls = ["https://url/path/to/gpg-key"]
`
resp, err := PostTOMLSourceV0(testState.socket, source)
require.NoError(t, err, "POST source failed with a client error")
require.False(t, resp.Status, "did not return an error")
}
// list sources
func TestListSourcesV0(t *testing.T) {
sources := []string{`{

View file

@ -379,6 +379,15 @@ func (api *API) sourceNewHandler(writer http.ResponseWriter, request *http.Reque
err = errors_package.New("blueprint must be in json or toml format")
}
// Basic check of the source, should at least have a name and type
if err == nil {
if len(source.Name) == 0 {
err = errors_package.New("'name' field is missing from API v0 request")
} else if len(source.Type) == 0 {
err = errors_package.New("'type' field is missing from API v0 request")
}
}
if err != nil {
errors := responseError{
ID: "ProjectsError",

View file

@ -878,6 +878,31 @@ check_gpg = false
test.SendHTTP(api, true, "DELETE", "/api/v0/projects/source/delete/fish", ``)
}
// Empty TOML, and invalid TOML should return an error
func TestSourcesNewWrongToml(t *testing.T) {
sources := []string{``, `
[fish]
name = "fish"
url = "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/"
type = "yum-baseurl"
check_ssl = false
check_gpg = false
`}
for _, source := range sources {
req := httptest.NewRequest("POST", "/api/v0/projects/source/new", bytes.NewReader([]byte(source)))
req.Header.Set("Content-Type", "text/x-toml")
recorder := httptest.NewRecorder()
api, _ := createWeldrAPI(rpmmd_mock.BaseFixture)
api.ServeHTTP(recorder, req)
r := recorder.Result()
if r.StatusCode != http.StatusBadRequest {
t.Errorf("unexpected status %v", r.StatusCode)
}
}
}
func TestSourcesInfo(t *testing.T) {
sourceStr := `{"name":"fish","type":"yum-baseurl","url":"https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","check_gpg":false,"check_ssl":false,"system":false}`