Files
zot/pkg/extensions/extension_userprefs_test.go
T
Luca Muscariello 2402296e9a fix: migrate to Go module v2 for proper semantic versioning (#3462)
* fix: migrate to Go module v2 for proper semantic versioning

This change updates the module path from 'zotregistry.dev/zot' to
'zotregistry.dev/zot/v2' to comply with Go's semantic versioning rules.

According to Go's module versioning requirements, major version v2+
must include the major version in the module path. The current
module path 'zotregistry.dev/zot' only supports v0.x.x and v1.x.x
versions, making existing v2.x.x tags (like v2.1.8) unusable.

Changes:
- Updated go.mod module path to zotregistry.dev/zot/v2
- Updated all internal import paths across 280+ Go source files
- Updated configuration files (golangcilint.yaml, gqlgen.yml)
- Updated README.md Go reference badge

This fix enables proper use of existing v2.x.x Git tags and allows
external packages to import zot v2+ versions without compatibility
errors.

Resolves: Go module import compatibility for v2+ versions
Fixes: #3071
Signed-off-by: Luca Muscariello <muscariello@ieee.org>

* fix: regenerate GraphQL files with updated v2 import paths

The gqlgen tool needs to regenerate the GraphQL schema files after
the module path change to use the new v2 imports.

Signed-off-by: Luca Muscariello <muscariello@ieee.org>

---------

Signed-off-by: Luca Muscariello <muscariello@ieee.org>
2025-10-16 22:43:47 -07:00

193 lines
5.5 KiB
Go

//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"
"gopkg.in/resty.v1"
zerr "zotregistry.dev/zot/v2/errors"
"zotregistry.dev/zot/v2/pkg/api"
"zotregistry.dev/zot/v2/pkg/api/config"
"zotregistry.dev/zot/v2/pkg/api/constants"
"zotregistry.dev/zot/v2/pkg/extensions"
extconf "zotregistry.dev/zot/v2/pkg/extensions/config"
"zotregistry.dev/zot/v2/pkg/log"
mTypes "zotregistry.dev/zot/v2/pkg/meta/types"
test "zotregistry.dev/zot/v2/pkg/test/common"
"zotregistry.dev/zot/v2/pkg/test/mocks"
)
var ErrTestError = errors.New("TestError")
func TestAllowedMethodsHeaderUserPrefs(t *testing.T) {
defaultVal := true
Convey("Test http options response", t, func() {
conf := config.New()
port := test.GetFreePort()
conf.HTTP.Port = port
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
baseURL := test.GetBaseURL(port)
ctlr := api.NewController(conf)
ctlr.Config.Storage.RootDirectory = t.TempDir()
ctrlManager := test.NewControllerManager(ctlr)
ctrlManager.StartAndWait(port)
defer ctrlManager.StopServer()
resp, _ := resty.R().Options(baseURL + constants.FullUserPrefs)
So(resp, ShouldNotBeNil)
So(resp.Header().Get("Access-Control-Allow-Methods"), ShouldResemble, "PUT,OPTIONS")
So(resp.StatusCode(), ShouldEqual, http.StatusNoContent)
})
}
func TestHandlers(t *testing.T) {
const UserprefsBaseURL = "http://127.0.0.1:8080/v2/_zot/ext/userprefs"
log := log.NewTestLogger()
mockmetaDB := mocks.MetaDBMock{}
Convey("No repo in request", t, func() {
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"", strings.NewReader("My string"))
response := httptest.NewRecorder()
extensions.PutStar(response, request, mockmetaDB, log)
res := response.Result()
So(res.StatusCode, ShouldEqual, http.StatusBadRequest)
defer res.Body.Close()
extensions.PutBookmark(response, request, mockmetaDB, log)
res = response.Result()
So(res.StatusCode, ShouldEqual, http.StatusBadRequest)
defer res.Body.Close()
})
Convey("Empty repo in request", t, func() {
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=", strings.NewReader("My string"))
response := httptest.NewRecorder()
extensions.PutStar(response, request, mockmetaDB, log)
res := response.Result()
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
defer res.Body.Close()
extensions.PutBookmark(response, request, mockmetaDB, log)
res = response.Result()
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
defer res.Body.Close()
})
Convey("ToggleStarRepo different errors", t, func() {
request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=test",
strings.NewReader("My string"))
Convey("ErrRepoMetaNotFound", func() {
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, zerr.ErrRepoMetaNotFound
}
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, zerr.ErrRepoMetaNotFound
}
response := httptest.NewRecorder()
extensions.PutBookmark(response, request, mockmetaDB, log)
res := response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
response = httptest.NewRecorder()
extensions.PutStar(response, request, mockmetaDB, log)
res = response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusNotFound)
})
Convey("ErrUserDataNotAllowed", func() {
request = mux.SetURLVars(request, map[string]string{
"name": "repo",
})
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
}
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
}
response := httptest.NewRecorder()
extensions.PutBookmark(response, request, mockmetaDB, log)
res := response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusForbidden)
response = httptest.NewRecorder()
extensions.PutStar(response, request, mockmetaDB, log)
res = response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusForbidden)
})
Convey("ErrUnexpectedError", func() {
request = mux.SetURLVars(request, map[string]string{
"name": "repo",
})
mockmetaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, ErrTestError
}
mockmetaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (mTypes.ToggleState, error) {
return mTypes.NotChanged, ErrTestError
}
response := httptest.NewRecorder()
extensions.PutBookmark(response, request, mockmetaDB, log)
res := response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusInternalServerError)
response = httptest.NewRecorder()
extensions.PutStar(response, request, mockmetaDB, log)
res = response.Result()
defer res.Body.Close()
So(res.StatusCode, ShouldEqual, http.StatusInternalServerError)
})
})
}