Fix racy test issues (#24971)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
366d1613b7
Коммит
486e836b83
@@ -20,7 +20,7 @@ import (
|
||||
|
||||
type Context struct {
|
||||
App app.AppIface
|
||||
AppContext *request.Context
|
||||
AppContext request.CTX
|
||||
Logger *mlog.Logger
|
||||
Params *Params
|
||||
Err *model.AppError
|
||||
|
||||
@@ -71,7 +71,7 @@ func TestMfaRequired(t *testing.T) {
|
||||
|
||||
th.App.Srv().SetLicense(model.NewTestLicense("mfa"))
|
||||
|
||||
th.Context.SetSession(&model.Session{Id: "abc", UserId: "userid"})
|
||||
th.Context = th.Context.WithSession(&model.Session{Id: "abc", UserId: "userid"})
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.AnnouncementSettings.UserNoticesEnabled = false
|
||||
|
||||
@@ -205,7 +205,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
span.Finish()
|
||||
}()
|
||||
c.AppContext.SetContext(ctx)
|
||||
c.AppContext = c.AppContext.WithContext(ctx)
|
||||
|
||||
tmpSrv := *c.App.Srv()
|
||||
tmpSrv.SetStore(opentracinglayer.New(c.App.Srv().Store(), ctx))
|
||||
@@ -285,7 +285,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
} else if !session.IsOAuth && tokenLocation == app.TokenLocationQueryString {
|
||||
c.Err = model.NewAppError("ServeHTTP", "api.context.token_provided.app_error", nil, "token="+token, http.StatusUnauthorized)
|
||||
} else {
|
||||
c.AppContext.SetSession(session)
|
||||
c.AppContext = c.AppContext.WithSession(session)
|
||||
}
|
||||
|
||||
// Rate limit by UserID
|
||||
@@ -301,7 +301,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
c.Logger.Warn("Invalid CWS token", mlog.Err(err))
|
||||
c.Err = err
|
||||
} else {
|
||||
c.AppContext.SetSession(session)
|
||||
c.AppContext = c.AppContext.WithSession(session)
|
||||
}
|
||||
} else if token != "" && c.App.Channels().License() != nil && c.App.Channels().License().HasRemoteClusterService() && tokenLocation == app.TokenLocationRemoteClusterHeader {
|
||||
// Get the remote cluster
|
||||
@@ -315,7 +315,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
c.Logger.Warn("Invalid remote cluster token", mlog.Err(err))
|
||||
c.Err = err
|
||||
} else {
|
||||
c.AppContext.SetSession(session)
|
||||
c.AppContext = c.AppContext.WithSession(session)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,7 +327,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
mlog.String("user_id", c.AppContext.Session().UserId),
|
||||
mlog.String("method", r.Method),
|
||||
)
|
||||
c.AppContext.SetLogger(c.Logger)
|
||||
c.AppContext = c.AppContext.WithLogger(c.Logger)
|
||||
|
||||
if c.Err == nil && h.RequireSession {
|
||||
c.SessionRequired()
|
||||
@@ -354,7 +354,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// shape IP:PORT (it will be "@" in Linux, for example)
|
||||
isLocalOrigin := !strings.Contains(r.RemoteAddr, ":")
|
||||
if *c.App.Config().ServiceSettings.EnableLocalMode && isLocalOrigin {
|
||||
c.AppContext.SetSession(&model.Session{Local: true})
|
||||
c.AppContext = c.AppContext.WithSession(&model.Session{Local: true})
|
||||
} else if !isLocalOrigin {
|
||||
c.Err = model.NewAppError("", "api.context.local_origin_required.app_error", nil, "LocalOriginRequired", http.StatusUnauthorized)
|
||||
}
|
||||
@@ -501,7 +501,7 @@ func (h *Handler) checkCSRFToken(c *Context, r *http.Request, token string, toke
|
||||
}
|
||||
|
||||
if !csrfCheckPassed {
|
||||
c.AppContext.SetSession(&model.Session{})
|
||||
c.AppContext = c.AppContext.WithSession(&model.Session{})
|
||||
c.Err = model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token+" Appears to be a CSRF attempt", http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,13 +321,14 @@ func completeOAuth(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
} else if action == model.OAuthActionSSOToEmail {
|
||||
redirectURL = app.GetProtocol(r) + "://" + r.Host + "/claim?email=" + url.QueryEscape(props["email"])
|
||||
} else {
|
||||
err = c.App.DoLogin(c.AppContext, w, r, user, "", isMobile, false, false)
|
||||
session, err := c.App.DoLogin(c.AppContext, w, r, user, "", isMobile, false, false)
|
||||
if err != nil {
|
||||
err.Translate(c.AppContext.T)
|
||||
mlog.Error(err.Error())
|
||||
renderError(err)
|
||||
return
|
||||
}
|
||||
c.AppContext = c.AppContext.WithSession(session)
|
||||
|
||||
// Old mobile version
|
||||
if isMobile && !hasRedirectURL {
|
||||
|
||||
@@ -18,8 +18,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils"
|
||||
"github.com/mattermost/mattermost/server/v8/einterfaces"
|
||||
@@ -397,20 +395,18 @@ func TestMobileLoginWithOAuth(t *testing.T) {
|
||||
c := &Context{
|
||||
App: th.App,
|
||||
AppContext: th.Context,
|
||||
Logger: th.TestLogger,
|
||||
Params: &Params{
|
||||
Service: "gitlab",
|
||||
},
|
||||
}
|
||||
|
||||
var siteURL = "http://localhost:8065"
|
||||
siteURL := "http://localhost:8065"
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.SiteURL = siteURL
|
||||
*cfg.GitLabSettings.Enable = true
|
||||
})
|
||||
|
||||
translationFunc := i18n.GetUserTranslations("en")
|
||||
c.AppContext.SetT(translationFunc)
|
||||
c.Logger = th.TestLogger
|
||||
provider := &MattermostTestProvider{}
|
||||
einterfaces.RegisterOAuthProvider(model.ServiceGitlab, provider)
|
||||
|
||||
@@ -617,14 +613,12 @@ func TestOAuthComplete_ErrorMessages(t *testing.T) {
|
||||
c := &Context{
|
||||
App: th.App,
|
||||
AppContext: th.Context,
|
||||
Logger: th.TestLogger,
|
||||
Params: &Params{
|
||||
Service: "gitlab",
|
||||
},
|
||||
}
|
||||
|
||||
translationFunc := i18n.GetUserTranslations("en")
|
||||
c.AppContext.SetT(translationFunc)
|
||||
c.Logger = mlog.CreateConsoleTestLogger(t)
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.GitLabSettings.Enable = true })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOAuthServiceProvider = true })
|
||||
provider := &MattermostTestProvider{}
|
||||
|
||||
@@ -178,11 +178,12 @@ func completeSaml(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
auditRec.AddMeta("obtained_user_id", user.Id)
|
||||
c.LogAuditWithUserId(user.Id, "obtained user")
|
||||
|
||||
err = c.App.DoLogin(c.AppContext, w, r, user, "", isMobile, false, true)
|
||||
session, err := c.App.DoLogin(c.AppContext, w, r, user, "", isMobile, false, true)
|
||||
if err != nil {
|
||||
handleError(err)
|
||||
return
|
||||
}
|
||||
c.AppContext = c.AppContext.WithSession(session)
|
||||
|
||||
auditRec.Success()
|
||||
c.LogAuditWithUserId(user.Id, "success")
|
||||
|
||||
@@ -33,7 +33,7 @@ var URL string
|
||||
|
||||
type TestHelper struct {
|
||||
App app.AppIface
|
||||
Context *request.Context
|
||||
Context request.CTX
|
||||
Server *app.Server
|
||||
Web *Web
|
||||
|
||||
@@ -141,7 +141,6 @@ func setupTestHelper(tb testing.TB, includeCacheLayer bool, options []app.Option
|
||||
IncludeCacheLayer: includeCacheLayer,
|
||||
TestLogger: testLogger,
|
||||
}
|
||||
th.Context.SetLogger(testLogger)
|
||||
|
||||
return th
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user