Fix broken auth tests, optimize how locale is passed on
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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{})
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user