From 98eda7d474928d8df9c67dcb95e1b5af6e560144 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sat, 18 Sep 2021 05:57:42 +0200 Subject: [PATCH] Fix broken auth tests, optimize how locale is passed on --- auth/auth.go | 2 ++ auth/handlers/handle_profile.go | 23 ++++++++++++++--------- auth/handlers/handle_profile_test.go | 7 ++++--- auth/handlers/handler.go | 13 ++++++++++--- auth/handlers/mock_test.go | 6 +++--- auth/request/context.go | 11 +++++++++-- pkg/locale/service.go | 4 ++++ 7 files changed, 46 insertions(+), 20 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 9d5c46d0c..aebcb4a34 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -21,6 +21,7 @@ import ( "github.com/cortezaproject/corteza-server/auth/settings" "github.com/cortezaproject/corteza-server/pkg/actionlog" "github.com/cortezaproject/corteza-server/pkg/auth" + "github.com/cortezaproject/corteza-server/pkg/locale" "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/pkg/version" "github.com/cortezaproject/corteza-server/store" @@ -211,6 +212,7 @@ func New(ctx context.Context, log *zap.Logger, s store.Storer, opt options.AuthO } svc.handlers = &handlers.AuthHandlers{ + Locale: locale.Global(), Log: log, Templates: tpls, SessionManager: sesManager, diff --git a/auth/handlers/handle_profile.go b/auth/handlers/handle_profile.go index 351c51109..3b01cf895 100644 --- a/auth/handlers/handle_profile.go +++ b/auth/handlers/handle_profile.go @@ -2,7 +2,6 @@ package handlers import ( "github.com/cortezaproject/corteza-server/auth/request" - "github.com/cortezaproject/corteza-server/pkg/locale" "github.com/cortezaproject/corteza-server/system/service" "github.com/cortezaproject/corteza-server/system/types" "go.uber.org/zap" @@ -10,25 +9,31 @@ import ( func (h *AuthHandlers) profileForm(req *request.AuthReq) error { req.Template = TmplProfile - u := req.AuthUser.User - req.Data["languages"] = locale.Global().LocalizedList(req.Context()) + var ( + preferredLanguage string + + u = req.AuthUser.User + ) + + if langList := h.Locale.LocalizedList(req.Context()); len(langList) > 0 { + req.Data["languages"] = langList + + preferredLanguage = langList[0].Tag.String() + if u.Meta != nil && u.Meta.PreferredLanguage != "" { + preferredLanguage = u.Meta.PreferredLanguage + } + } if form := req.PopKV(); len(form) > 0 { req.Data["form"] = form } else { - var preferredLanguage = locale.Global().Default().Tag.String() - if u.Meta != nil && u.Meta.PreferredLanguage != "" { - preferredLanguage = u.Meta.PreferredLanguage - } - req.Data["form"] = map[string]string{ "email": u.Email, "handle": u.Handle, "name": u.Name, "preferredLanguage": preferredLanguage, } - } req.Data["emailConfirmationRequired"] = !u.EmailConfirmed && h.Settings.EmailConfirmationRequired diff --git a/auth/handlers/handle_profile_test.go b/auth/handlers/handle_profile_test.go index 548d3bd71..4fc777fe1 100644 --- a/auth/handlers/handle_profile_test.go +++ b/auth/handlers/handle_profile_test.go @@ -37,9 +37,10 @@ func Test_profileForm(t *testing.T) { authHandlers = prepareClientAuthHandlers(ctx, authService, authSettings) userForm := map[string]string{ - "email": user.Email, - "handle": user.Handle, - "name": user.Name, + "email": user.Email, + "handle": user.Handle, + "name": user.Name, + "preferredLanguage": user.Meta.PreferredLanguage, } authReq.SetKV(map[string]string{}) diff --git a/auth/handlers/handler.go b/auth/handlers/handler.go index e8c7a4543..e5e0a4908 100644 --- a/auth/handlers/handler.go +++ b/auth/handlers/handler.go @@ -88,9 +88,16 @@ type ( ValidationBearerToken(r *http.Request) (oauth2.TokenInfo, error) } + localeService interface { + NS(ctx context.Context, ns string) func(key string, rr ...string) string + T(ctx context.Context, ns, key string, rr ...string) string + LocalizedList(ctx context.Context) []*locale.Language + } + AuthHandlers struct { Log *zap.Logger + Locale localeService Templates templateExecutor OAuth2 oauth2Service SessionManager *request.SessionManager @@ -139,7 +146,6 @@ func init() { func (h *AuthHandlers) handle(fn handlerFn) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( - loc = locale.Global() req = &request.AuthReq{ Response: w, Request: r, @@ -147,6 +153,7 @@ func (h *AuthHandlers) handle(fn handlerFn) http.HandlerFunc { NewAlerts: make([]request.Alert, 0), PrevAlerts: make([]request.Alert, 0), Session: h.SessionManager.Get(r), + Locale: h.Locale, } ) @@ -239,7 +246,7 @@ func (h *AuthHandlers) handle(fn handlerFn) http.HandlerFunc { ss[i] = cast.ToString(pp[i]) } - return template.HTML(loc.T(req.Context(), "auth", key, ss...)) + return template.HTML(h.Locale.T(req.Context(), "auth", key, ss...)) }, }) } @@ -403,5 +410,5 @@ func anonyOnly(fn handlerFn) handlerFn { } func translator(req *request.AuthReq, ns string) func(key string, rr ...string) string { - return locale.Global().NS(req.Context(), ns) + return req.Locale.NS(req.Context(), ns) } diff --git a/auth/handlers/mock_test.go b/auth/handlers/mock_test.go index 8126222b5..f113280a7 100644 --- a/auth/handlers/mock_test.go +++ b/auth/handlers/mock_test.go @@ -8,6 +8,7 @@ import ( "github.com/cortezaproject/corteza-server/auth/request" "github.com/cortezaproject/corteza-server/auth/settings" + "github.com/cortezaproject/corteza-server/pkg/locale" "github.com/cortezaproject/corteza-server/pkg/options" "github.com/cortezaproject/corteza-server/store" "github.com/cortezaproject/corteza-server/system/service" @@ -19,9 +20,6 @@ import ( ) type ( - mockedAuthService struct { - authService - } mockAuthService struct { authService @@ -347,6 +345,7 @@ func prepareClientAuthReq(ctx context.Context, req *http.Request, user *types.Us authReq := &request.AuthReq{ Request: req, + Locale: h.Locale, Session: session, Response: httptest.NewRecorder(), Data: make(map[string]interface{}), @@ -367,6 +366,7 @@ func prepareClientAuthService(ctx context.Context, user *types.User) *mockAuthSe func prepareClientAuthHandlers(ctx context.Context, authService authService, s *settings.Settings) *AuthHandlers { return &AuthHandlers{ Log: zap.NewNop(), + Locale: locale.Static(), AuthService: authService, Settings: s, } diff --git a/auth/request/context.go b/auth/request/context.go index cf1627014..2c958ad71 100644 --- a/auth/request/context.go +++ b/auth/request/context.go @@ -2,13 +2,18 @@ package request import ( "context" - "github.com/cortezaproject/corteza-server/system/types" - "github.com/gorilla/sessions" "html/template" "net/http" + + "github.com/cortezaproject/corteza-server/system/types" + "github.com/gorilla/sessions" ) type ( + localeService interface { + NS(ctx context.Context, ns string) func(key string, rr ...string) string + } + // auth context simplifies auth request & response handling AuthReq struct { // HTTP request sent @@ -25,6 +30,8 @@ type ( // Current client (when in oauth2 flow) Client *types.AuthClient + Locale localeService + // Redirect to RedirectTo string diff --git a/pkg/locale/service.go b/pkg/locale/service.go index 4aadede7b..ee1a334e7 100644 --- a/pkg/locale/service.go +++ b/pkg/locale/service.go @@ -54,6 +54,10 @@ func SetGlobal(ll *service) { global = ll } +func Static(ll ...*Language) *service { + return &service{} +} + func Service(log *zap.Logger, opt options.LocaleOpt) (*service, error) { svc := &service{ opt: opt,