From 3e510ffc997c046b0aef5c53c8f7bec230f7ec81 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Wed, 27 Jan 2021 10:46:46 -0800 Subject: [PATCH] test: Add tests for trying to override a system source repo This add tests to client and weldr to make sure that overriding an existing system source repo returns an error. Related: rhbz#1915359 --- internal/client/source_test.go | 133 +++++++++++++++++++++++++++++++++ internal/weldr/api_test.go | 2 + 2 files changed, 135 insertions(+) diff --git a/internal/client/source_test.go b/internal/client/source_test.go index 8d26101ad..0040ae397 100644 --- a/internal/client/source_test.go +++ b/internal/client/source_test.go @@ -9,6 +9,7 @@ package client import ( + "fmt" "strings" "testing" @@ -88,6 +89,73 @@ func TestPOSTInvalidJSONSourceV1(t *testing.T) { require.False(t, resp.Status, "did not return an error") } +// POST a JSON system source using V0 API +func TestPOSTSystemJSONSourceV0(t *testing.T) { + sources_list, api, err := ListSourcesV0(testState.socket) + require.NoError(t, err, "GET source failed with a client error") + require.Nil(t, api, "ListSources failed: %#v", api) + + // Cannot override system sources + source := `{ + "name": "REPO-NAME", + "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"] + }` + + for _, repoName := range []string{"test-system-repo", "fedora", "baseos"} { + // skip repository names which are not present b/c this test can be + // executed both as a unit test and as an integration test + if !Include(sources_list, repoName) { + continue + } + useSource := strings.Replace(source, "REPO-NAME", repoName, 1) + + resp, err := PostJSONSourceV0(testState.socket, useSource) + require.NoError(t, err, "POST source failed with a client error") + require.False(t, resp.Status, "did not return an error") + msg := fmt.Sprintf("%s is a system source, it cannot be changed.", repoName) + require.Equal(t, APIErrorMsg{ID: "SystemSource", Msg: msg}, resp.Errors[0]) + } +} + +// POST a JSON system source using V1 API +func TestPOSTSystemJSONSourceV1(t *testing.T) { + sources_list, api, err := ListSourcesV1(testState.socket) + require.NoError(t, err, "GET source failed with a client error") + require.Nil(t, api, "ListSources failed: %#v", api) + + // Cannot override system sources + source := `{ + "id": "REPO-NAME", + "name": "json package system repo", + "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"] + }` + + for _, repoName := range []string{"test-system-repo", "fedora", "baseos"} { + // skip repository names which are not present b/c this test can be + // executed both as a unit test and as an integration test + if !Include(sources_list, repoName) { + continue + } + useSource := strings.Replace(source, "REPO-NAME", repoName, 1) + + resp, err := PostJSONSourceV1(testState.socket, useSource) + require.NoError(t, err, "POST source failed with a client error") + require.False(t, resp.Status, "did not return an error") + msg := fmt.Sprintf("%s is a system source, it cannot be changed.", repoName) + require.Equal(t, APIErrorMsg{ID: "SystemSource", Msg: msg}, resp.Errors[0]) + } +} + // POST a new TOML source using V0 API func TestPOSTTOMLSourceV0(t *testing.T) { source := ` @@ -223,6 +291,71 @@ func TestPOSTWrongTOMLSourceV1(t *testing.T) { require.False(t, resp.Status, "did not return an error") } +// POST a TOML system source using V0 API +func TestPOSTTOMLSystemSourceV0(t *testing.T) { + sources_list, api, err := ListSourcesV0(testState.socket) + require.NoError(t, err, "GET source failed with a client error") + require.Nil(t, api, "ListSources failed: %#v", api) + + source := ` + name = "REPO-NAME" + 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"] + ` + source = strings.Replace(source, "REPO-PATH", testState.repoDir, 1) + for _, repoName := range []string{"test-system-repo", "fedora", "baseos"} { + // skip repository names which are not present b/c this test can be + // executed both as a unit test and as an integration test + if !Include(sources_list, repoName) { + continue + } + useSource := strings.Replace(source, "REPO-NAME", repoName, 1) + + resp, err := PostTOMLSourceV0(testState.socket, useSource) + require.NoError(t, err, "POST source failed with a client error") + require.False(t, resp.Status, "did not return an error") + msg := fmt.Sprintf("%s is a system source, it cannot be changed.", repoName) + require.Equal(t, APIErrorMsg{ID: "SystemSource", Msg: msg}, resp.Errors[0]) + } +} + +// POST a new TOML system source using V1 API +func TestPOSTTOMLSystemSourceV1(t *testing.T) { + sources_list, api, err := ListSourcesV1(testState.socket) + require.NoError(t, err, "GET source failed with a client error") + require.Nil(t, api, "ListSources failed: %#v", api) + + source := ` + id = "REPO-NAME" + name = "toml package repo" + 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"] + ` + source = strings.Replace(source, "REPO-PATH", testState.repoDir, 1) + for _, repoName := range []string{"test-system-repo", "fedora", "baseos"} { + // skip repository names which are not present b/c this test can be + // executed both as a unit test and as an integration test + if !Include(sources_list, repoName) { + continue + } + useSource := strings.Replace(source, "REPO-NAME", repoName, 1) + + resp, err := PostTOMLSourceV1(testState.socket, useSource) + require.NoError(t, err, "POST source failed with a client error") + require.False(t, resp.Status, "did not return an error") + msg := fmt.Sprintf("%s is a system source, it cannot be changed.", repoName) + require.Equal(t, APIErrorMsg{ID: "SystemSource", Msg: msg}, resp.Errors[0]) + } +} + // list sources using the v0 API func TestListSourcesV0(t *testing.T) { sources := []string{`{ diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go index 9f32dec3e..8af9c1fc8 100644 --- a/internal/weldr/api_test.go +++ b/internal/weldr/api_test.go @@ -925,6 +925,8 @@ func TestSourcesNew(t *testing.T) { {"POST", "/api/v0/projects/source/new", `{"url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "ProjectsError","msg": "Problem parsing POST body: 'name' field is missing from request"}],"status":false}`}, {"POST", "/api/v0/projects/source/new", `{"name": "fish", "type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "ProjectsError","msg": "Problem parsing POST body: 'url' field is missing from request"}],"status":false}`}, {"POST", "/api/v0/projects/source/new", `{"name": "fish", "url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "ProjectsError","msg": "Problem parsing POST body: 'type' field is missing from request"}],"status":false}`}, + {"POST", "/api/v0/projects/source/new", `{"name": "test-id", "url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "SystemSource","msg": "test-id is a system source, it cannot be changed."}],"status":false}`}, + {"POST", "/api/v1/projects/source/new", `{"id": "test-id", "name": "test system repo", "url": "https://download.opensuse.org/repositories/shells:/fish:/release:/3/Fedora_29/","type": "yum-baseurl","check_ssl": false,"check_gpg": false}`, http.StatusBadRequest, `{"errors": [{"id": "SystemSource","msg": "test-id is a system source, it cannot be changed."}],"status":false}`}, } tempdir, err := ioutil.TempDir("", "weldr-tests-")