MM-13893: refactor config (#10230)
* refactor utils/config* to config/ * pull validateLdapFilter into app * clean up Config/GetConfig/GetSanitizedConfig usage Eliminate app.GetConfig() in favour of just using app.Config() directly, but expose app.GetSanitizedConfig() for when the old behaviour was required. * web: isolate config setup * TestInvitePeopleProvider: make config explicit * regenerateClientConfig: avoid racey map access * integrate watch flag into app.ConfigFile option * make app.Option return an error * release.mk: only cp static files from config/ * release.mk: fix cp static files from config/ * api4: TestPlugin cleanup * s/c/cfg/ for clarity * fix merge conflict * testlib: allow customization of testlib driver name
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
aca8914e35
Коммит
3a71709103
@@ -8,10 +8,8 @@ import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/app"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func handlerForHTTPErrors(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -19,14 +17,10 @@ func handlerForHTTPErrors(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func TestHandlerServeHTTPErrors(t *testing.T) {
|
||||
s, err := app.NewServer(app.StoreOverride(mainHelper.Store), app.DisableConfigWatch)
|
||||
require.Nil(t, err)
|
||||
defer s.Shutdown()
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
web := New(s, s.AppOptions, s.Router)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
web := New(th.Server, th.Server.AppOptions, th.Server.Router)
|
||||
handler := web.NewHandler(handlerForHTTPErrors)
|
||||
|
||||
var flagtests = []struct {
|
||||
@@ -63,21 +57,15 @@ func handlerForHTTPSecureTransport(c *Context, w http.ResponseWriter, r *http.Re
|
||||
}
|
||||
|
||||
func TestHandlerServeHTTPSecureTransport(t *testing.T) {
|
||||
s, err := app.NewServer(app.StoreOverride(mainHelper.Store), app.DisableConfigWatch)
|
||||
require.Nil(t, err)
|
||||
defer s.Shutdown()
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
a := s.FakeApp()
|
||||
|
||||
a.UpdateConfig(func(config *model.Config) {
|
||||
th.App.UpdateConfig(func(config *model.Config) {
|
||||
*config.ServiceSettings.TLSStrictTransport = true
|
||||
*config.ServiceSettings.TLSStrictTransportMaxAge = 6000
|
||||
})
|
||||
|
||||
web := New(s, s.AppOptions, s.Router)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
web := New(th.Server, th.Server.AppOptions, th.Server.Router)
|
||||
handler := web.NewHandler(handlerForHTTPSecureTransport)
|
||||
|
||||
request := httptest.NewRequest("GET", "/api/v4/test", nil)
|
||||
@@ -94,7 +82,7 @@ func TestHandlerServeHTTPSecureTransport(t *testing.T) {
|
||||
t.Errorf("Expected max-age=6000, got %s", header)
|
||||
}
|
||||
|
||||
a.UpdateConfig(func(config *model.Config) {
|
||||
th.App.UpdateConfig(func(config *model.Config) {
|
||||
*config.ServiceSettings.TLSStrictTransport = false
|
||||
})
|
||||
|
||||
@@ -109,7 +97,6 @@ func TestHandlerServeHTTPSecureTransport(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func handlerForCSRFToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
@@ -117,11 +104,11 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
session :=&model.Session{
|
||||
UserId: th.BasicUser.Id,
|
||||
session := &model.Session{
|
||||
UserId: th.BasicUser.Id,
|
||||
CreateAt: model.GetMillis(),
|
||||
Roles: model.SYSTEM_USER_ROLE_ID,
|
||||
IsOAuth: false,
|
||||
Roles: model.SYSTEM_USER_ROLE_ID,
|
||||
IsOAuth: false,
|
||||
}
|
||||
session.GenerateCSRF()
|
||||
session.SetExpireInDays(1)
|
||||
@@ -142,15 +129,15 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
}
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: model.SESSION_COOKIE_USER,
|
||||
Name: model.SESSION_COOKIE_USER,
|
||||
Value: th.BasicUser.Username,
|
||||
}
|
||||
cookie2 := &http.Cookie{
|
||||
Name: model.SESSION_COOKIE_TOKEN,
|
||||
Name: model.SESSION_COOKIE_TOKEN,
|
||||
Value: session.Token,
|
||||
}
|
||||
cookie3 := &http.Cookie{
|
||||
Name: model.SESSION_COOKIE_CSRF,
|
||||
Name: model.SESSION_COOKIE_CSRF,
|
||||
Value: session.GetCSRF(),
|
||||
}
|
||||
|
||||
@@ -183,7 +170,7 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
|
||||
// Fallback Behavior Used - Success expected
|
||||
// ToDo (DSchalla) 2019/01/04: Remove once legacy CSRF Handling is removed
|
||||
th.App.UpdateConfig(func(config *model.Config){
|
||||
th.App.UpdateConfig(func(config *model.Config) {
|
||||
*config.ServiceSettings.ExperimentalStrictCSRFEnforcement = false
|
||||
})
|
||||
request = httptest.NewRequest("POST", "/api/v4/test", nil)
|
||||
@@ -200,7 +187,7 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
|
||||
// Fallback Behavior Used with Strict Enforcement - Failure Expected
|
||||
// ToDo (DSchalla) 2019/01/04: Remove once legacy CSRF Handling is removed
|
||||
th.App.UpdateConfig(func(config *model.Config){
|
||||
th.App.UpdateConfig(func(config *model.Config) {
|
||||
*config.ServiceSettings.ExperimentalStrictCSRFEnforcement = true
|
||||
})
|
||||
response = httptest.NewRecorder()
|
||||
|
||||
@@ -5,10 +5,14 @@ package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/app"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/utils/fileutils"
|
||||
)
|
||||
|
||||
var ApiClient *model.Client4
|
||||
@@ -28,7 +32,25 @@ type TestHelper struct {
|
||||
func Setup() *TestHelper {
|
||||
mainHelper.Store.DropAllTables()
|
||||
|
||||
s, err := app.NewServer(app.StoreOverride(mainHelper.Store), app.DisableConfigWatch)
|
||||
permConfig, err := os.Open(fileutils.FindConfigFile("config.json"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer permConfig.Close()
|
||||
tempConfig, err := ioutil.TempFile("", "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = io.Copy(tempConfig, permConfig)
|
||||
tempConfig.Close()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
options := []app.Option{app.ConfigFile(tempConfig.Name(), false)}
|
||||
options = append(options, app.StoreOverride(mainHelper.Store))
|
||||
|
||||
s, err := app.NewServer(options...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -235,13 +235,13 @@ func TestCommandWebhooks(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
cmd, err := th.App.CreateCommand(&model.Command{
|
||||
cmd, appErr := th.App.CreateCommand(&model.Command{
|
||||
CreatorId: th.BasicUser.Id,
|
||||
TeamId: th.BasicTeam.Id,
|
||||
URL: "http://nowhere.com",
|
||||
Method: model.COMMAND_METHOD_POST,
|
||||
Trigger: "delayed"})
|
||||
require.Nil(t, err)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
args := &model.CommandArgs{
|
||||
TeamId: th.BasicTeam.Id,
|
||||
@@ -249,22 +249,22 @@ func TestCommandWebhooks(t *testing.T) {
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
}
|
||||
|
||||
hook, err := th.App.CreateCommandWebhook(cmd.Id, args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
hook, appErr := th.App.CreateCommandWebhook(cmd.Id, args)
|
||||
if appErr != nil {
|
||||
t.Fatal(appErr)
|
||||
}
|
||||
|
||||
if resp, _ := http.Post(ApiClient.Url+"/hooks/commands/123123123123", "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); resp.StatusCode != http.StatusNotFound {
|
||||
t.Fatal("expected not-found for non-existent hook")
|
||||
}
|
||||
resp, err := http.Post(ApiClient.Url+"/hooks/commands/123123123123", "application/json", bytes.NewBufferString(`{"text":"this is a test"}`))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode, "expected not-found for non-existent hook")
|
||||
|
||||
if resp, err := http.Post(ApiClient.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"invalid`)); err != nil || resp.StatusCode != http.StatusBadRequest {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err = http.Post(ApiClient.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"invalid`))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
if resp, err := http.Post(ApiClient.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); err != nil || resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(err)
|
||||
if resp, appErr := http.Post(ApiClient.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); err != nil || resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(appErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user