2023-04-24 13:13:15 -05:00
|
|
|
//go:build userprefs
|
|
|
|
// +build userprefs
|
|
|
|
|
|
|
|
package extensions_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2023-05-10 12:09:53 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2023-04-24 13:13:15 -05:00
|
|
|
|
|
|
|
zerr "zotregistry.io/zot/errors"
|
2023-05-10 12:09:53 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2023-04-24 13:13:15 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions"
|
2023-05-10 12:09:53 -05:00
|
|
|
extconf "zotregistry.io/zot/pkg/extensions/config"
|
2023-04-24 13:13:15 -05:00
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-07-18 12:27:26 -05:00
|
|
|
mTypes "zotregistry.io/zot/pkg/meta/types"
|
2023-05-10 12:09:53 -05:00
|
|
|
"zotregistry.io/zot/pkg/test"
|
2023-04-24 13:13:15 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrTestError = errors.New("TestError")
|
|
|
|
|
2023-05-25 13:44:54 -05:00
|
|
|
func TestAllowedMethodsHeaderUserPrefs(t *testing.T) {
|
2023-05-10 12:09:53 -05:00
|
|
|
defaultVal := true
|
|
|
|
|
|
|
|
Convey("Test http options response", t, func() {
|
|
|
|
conf := config.New()
|
|
|
|
port := test.GetFreePort()
|
|
|
|
conf.HTTP.Port = port
|
2023-08-02 13:58:34 -05:00
|
|
|
conf.Extensions = &extconf.ExtensionConfig{}
|
|
|
|
conf.Extensions.Search = &extconf.SearchConfig{}
|
|
|
|
conf.Extensions.Search.Enable = &defaultVal
|
|
|
|
conf.Extensions.Search.CVE = nil
|
|
|
|
conf.Extensions.UI = &extconf.UIConfig{}
|
|
|
|
conf.Extensions.UI.Enable = &defaultVal
|
|
|
|
|
2023-05-10 12:09:53 -05:00
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
ctlr.Config.Storage.RootDirectory = t.TempDir()
|
|
|
|
|
|
|
|
ctrlManager := test.NewControllerManager(ctlr)
|
|
|
|
|
|
|
|
ctrlManager.StartAndWait(port)
|
|
|
|
defer ctrlManager.StopServer()
|
|
|
|
|
2023-08-02 13:58:34 -05:00
|
|
|
resp, _ := resty.R().Options(baseURL + constants.FullUserPrefs)
|
2023-05-10 12:09:53 -05:00
|
|
|
So(resp, ShouldNotBeNil)
|
2023-05-25 13:44:54 -05:00
|
|
|
So(resp.Header().Get("Access-Control-Allow-Methods"), ShouldResemble, "PUT,OPTIONS")
|
2023-05-10 12:09:53 -05:00
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusNoContent)
|
|
|
|
})
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
|
|
|
func TestHandlers(t *testing.T) {
|
2023-05-10 12:09:53 -05:00
|
|
|
const UserprefsBaseURL = "http://127.0.0.1:8080/v2/_zot/ext/userprefs"
|
|
|
|
|
2023-04-24 13:13:15 -05:00
|
|
|
log := log.NewLogger("debug", "")
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB := mocks.MetaDBMock{}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
|
|
|
Convey("No repo in request", t, func() {
|
2023-04-27 02:09:46 -05:00
|
|
|
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"", strings.NewReader("My string"))
|
2023-04-24 13:13:15 -05:00
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutStar(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res := response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusBadRequest)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutBookmark(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res = response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusBadRequest)
|
|
|
|
defer res.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Empty repo in request", t, func() {
|
2023-04-27 02:09:46 -05:00
|
|
|
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=", strings.NewReader("My string"))
|
2023-04-24 13:13:15 -05:00
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutStar(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res := response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutBookmark(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res = response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
|
|
|
|
defer res.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("ToggleStarRepo different errors", t, func() {
|
2023-04-27 02:09:46 -05:00
|
|
|
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=test",
|
2023-04-24 13:13:15 -05:00
|
|
|
strings.NewReader("My string"))
|
|
|
|
|
|
|
|
Convey("ErrRepoMetaNotFound", func() {
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrRepoMetaNotFound
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrRepoMetaNotFound
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
response := httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutBookmark(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res := response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
response = httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutStar(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res = response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
|
|
|
|
defer res.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("ErrUserDataNotAllowed", func() {
|
|
|
|
request = mux.SetURLVars(request, map[string]string{
|
|
|
|
"name": "repo",
|
|
|
|
})
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
response := httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutBookmark(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res := response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusForbidden)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
response = httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutStar(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res = response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusForbidden)
|
|
|
|
defer res.Body.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("ErrUnexpectedError", func() {
|
|
|
|
request = mux.SetURLVars(request, map[string]string{
|
|
|
|
"name": "repo",
|
|
|
|
})
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, ErrTestError
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
|
|
|
|
return mTypes.NotChanged, ErrTestError
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
response := httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutBookmark(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res := response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusInternalServerError)
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
response = httptest.NewRecorder()
|
2023-07-18 12:27:26 -05:00
|
|
|
extensions.PutStar(response, request, mockmetaDB, log)
|
2023-04-24 13:13:15 -05:00
|
|
|
res = response.Result()
|
|
|
|
So(res.StatusCode, ShouldEqual, http.StatusInternalServerError)
|
|
|
|
defer res.Body.Close()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|