* add request context

* move initialialization to server

* use app interface instead of global app functions

* remove app context from webconn

* cleanup

* remove duplicated services

* move context to separate package

* remove finalize init method and move content to NewServer function

* restart workers and schedulers after adding license for tests

* reflect review comments

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2021-05-11 13:00:44 +03:00
коммит произвёл GitHub
родитель c09369f14a
Коммит 5ea06e51d0
235 изменённых файлов: 4048 добавлений и 3819 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package app
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
@@ -19,6 +20,7 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/v5/app/request"
"github.com/mattermost/mattermost-server/v5/einterfaces/mocks"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
@@ -87,7 +89,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
plugin.ClientMain(&MyPlugin{})
}
`,
}, th.App, th.App.NewPluginAPI)
}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -96,7 +98,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
Message: "message_",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(post, th.BasicChannel, false, true)
_, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
if assert.NotNil(t, err) {
assert.Equal(t, "Post rejected by plugin. rejected", err.Message)
}
@@ -128,7 +130,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
plugin.ClientMain(&MyPlugin{})
}
`,
}, th.App, th.App.NewPluginAPI)
}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -137,7 +139,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
Message: "message_",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(post, th.BasicChannel, false, true)
_, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
if assert.NotNil(t, err) {
assert.Equal(t, "Post rejected by plugin. rejected", err.Message)
}
@@ -168,7 +170,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
plugin.ClientMain(&MyPlugin{})
}
`,
}, th.App, th.App.NewPluginAPI)
}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -177,7 +179,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
post, err := th.App.CreatePost(post, th.BasicChannel, false, true)
post, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
assert.Equal(t, "message", post.Message)
@@ -212,7 +214,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
plugin.ClientMain(&MyPlugin{})
}
`,
}, th.App, th.App.NewPluginAPI)
}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -221,7 +223,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
post, err := th.App.CreatePost(post, th.BasicChannel, false, true)
post, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
assert.Equal(t, "message_fromplugin", post.Message)
@@ -278,7 +280,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
plugin.ClientMain(&MyPlugin{})
}
`,
}, th.App, th.App.NewPluginAPI)
}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -287,7 +289,7 @@ func TestHookMessageWillBePosted(t *testing.T) {
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
post, err := th.App.CreatePost(post, th.BasicChannel, false, true)
post, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
assert.Equal(t, "prefix_message_suffix", post.Message)
})
@@ -331,7 +333,7 @@ func TestHookMessageHasBeenPosted(t *testing.T) {
Message: "message",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(post, th.BasicChannel, false, true)
_, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
}
@@ -361,7 +363,7 @@ func TestHookMessageWillBeUpdated(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
post := &model.Post{
@@ -370,11 +372,11 @@ func TestHookMessageWillBeUpdated(t *testing.T) {
Message: "message_",
CreateAt: model.GetMillis() - 10000,
}
post, err := th.App.CreatePost(post, th.BasicChannel, false, true)
post, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
assert.Equal(t, "message_", post.Message)
post.Message = post.Message + "edited_"
post, err = th.App.UpdatePost(post, true)
post, err = th.App.UpdatePost(th.Context, post, true)
require.Nil(t, err)
assert.Equal(t, "message_edited_fromplugin", post.Message)
}
@@ -418,11 +420,11 @@ func TestHookMessageHasBeenUpdated(t *testing.T) {
Message: "message_",
CreateAt: model.GetMillis() - 10000,
}
post, err := th.App.CreatePost(post, th.BasicChannel, false, true)
post, err := th.App.CreatePost(th.Context, post, th.BasicChannel, false, true)
require.Nil(t, err)
assert.Equal(t, "message_", post.Message)
post.Message = post.Message + "edited"
_, err = th.App.UpdatePost(post, true)
_, err = th.App.UpdatePost(th.Context, post, true)
require.Nil(t, err)
}
@@ -460,7 +462,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}, th.App, func(*model.Manifest) plugin.API { return &mockAPI })
defer tearDown()
_, err := th.App.UploadFiles(
_, err := th.App.UploadFiles(th.Context,
"noteam",
th.BasicChannel.Id,
th.BasicUser.Id,
@@ -513,7 +515,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}, th.App, func(*model.Manifest) plugin.API { return &mockAPI })
defer tearDown()
_, err := th.App.UploadFiles(
_, err := th.App.UploadFiles(th.Context,
"noteam",
th.BasicChannel.Id,
th.BasicUser.Id,
@@ -560,7 +562,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}, th.App, func(*model.Manifest) plugin.API { return &mockAPI })
defer tearDown()
response, err := th.App.UploadFiles(
response, err := th.App.UploadFiles(th.Context,
"noteam",
th.BasicChannel.Id,
th.BasicUser.Id,
@@ -636,7 +638,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}, th.App, func(*model.Manifest) plugin.API { return &mockAPI })
defer tearDown()
response, err := th.App.UploadFiles(
response, err := th.App.UploadFiles(th.Context,
"noteam",
th.BasicChannel.Id,
th.BasicUser.Id,
@@ -690,12 +692,12 @@ func TestUserWillLogIn_Blocked(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
r := &http.Request{}
w := httptest.NewRecorder()
err = th.App.DoLogin(w, r, th.BasicUser, "", false, false, false)
err = th.App.DoLogin(th.Context, w, r, th.BasicUser, "", false, false, false)
assert.Contains(t, err.Id, "Login rejected by plugin", "Expected Login rejected by plugin, got %s", err.Id)
}
@@ -729,15 +731,15 @@ func TestUserWillLogInIn_Passed(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
r := &http.Request{}
w := httptest.NewRecorder()
err = th.App.DoLogin(w, r, th.BasicUser, "", false, false, false)
err = th.App.DoLogin(th.Context, w, r, th.BasicUser, "", false, false, false)
assert.Nil(t, err, "Expected nil, got %s", err)
assert.Equal(t, th.App.Session().UserId, th.BasicUser.Id)
assert.Equal(t, th.Context.Session().UserId, th.BasicUser.Id)
}
func TestUserHasLoggedIn(t *testing.T) {
@@ -770,12 +772,12 @@ func TestUserHasLoggedIn(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
r := &http.Request{}
w := httptest.NewRecorder()
err = th.App.DoLogin(w, r, th.BasicUser, "", false, false, false)
err = th.App.DoLogin(th.Context, w, r, th.BasicUser, "", false, false, false)
assert.Nil(t, err, "Expected nil, got %s", err)
@@ -812,7 +814,7 @@ func TestUserHasBeenCreated(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
user := &model.User{
@@ -822,7 +824,7 @@ func TestUserHasBeenCreated(t *testing.T) {
Password: "passwd1",
AuthService: "",
}
_, err := th.App.CreateUser(user)
_, err := th.App.CreateUser(th.Context, user)
require.Nil(t, err)
time.Sleep(1 * time.Second)
@@ -859,7 +861,7 @@ func TestErrorString(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
require.Len(t, activationErrors, 1)
@@ -889,7 +891,7 @@ func TestErrorString(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
require.Len(t, activationErrors, 1)
@@ -909,19 +911,16 @@ func TestHookContext(t *testing.T) {
defer th.TearDown()
// We don't actually have a session, we are faking it so just set something arbitrarily
th.App.Session().Id = model.NewId()
th.App.requestId = model.NewId()
th.App.ipAddress = model.NewId()
th.App.acceptLanguage = model.NewId()
th.App.userAgent = model.NewId()
ctx := request.NewContext(context.Background(), model.NewId(), model.NewId(), model.NewId(), model.NewId(), model.NewId(), model.Session{}, nil)
ctx.Session().Id = model.NewId()
var mockAPI plugintest.API
mockAPI.On("LoadPluginConfiguration", mock.Anything).Return(nil)
mockAPI.On("LogDebug", th.App.Session().Id).Return(nil)
mockAPI.On("LogInfo", th.App.RequestId()).Return(nil)
mockAPI.On("LogError", th.App.IpAddress()).Return(nil)
mockAPI.On("LogWarn", th.App.AcceptLanguage()).Return(nil)
mockAPI.On("DeleteTeam", th.App.UserAgent()).Return(nil)
mockAPI.On("LogDebug", ctx.Session().Id).Return(nil)
mockAPI.On("LogInfo", ctx.RequestId()).Return(nil)
mockAPI.On("LogError", ctx.IpAddress()).Return(nil)
mockAPI.On("LogWarn", ctx.AcceptLanguage()).Return(nil)
mockAPI.On("DeleteTeam", ctx.UserAgent()).Return(nil)
tearDown, _, _ := SetAppEnvironmentWithPlugins(t,
[]string{
@@ -957,7 +956,7 @@ func TestHookContext(t *testing.T) {
Message: "not this",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.CreatePost(post, th.BasicChannel, false, true)
_, err := th.App.CreatePost(ctx, post, th.BasicChannel, false, true)
require.Nil(t, err)
}
@@ -996,7 +995,7 @@ func TestActiveHooks(t *testing.T) {
func main() {
plugin.ClientMain(&MyPlugin{})
}
`}, th.App, th.App.NewPluginAPI)
`}, th.App, th.NewPluginAPI)
defer tearDown()
require.Len(t, pluginIDs, 1)
@@ -1010,7 +1009,7 @@ func TestActiveHooks(t *testing.T) {
Password: "passwd1",
AuthService: "",
}
_, appErr := th.App.CreateUser(user1)
_, appErr := th.App.CreateUser(th.Context, user1)
require.Nil(t, appErr)
time.Sleep(1 * time.Second)
user1, appErr = th.App.GetUser(user1.Id)
@@ -1046,7 +1045,7 @@ func TestHookMetrics(t *testing.T) {
defer os.RemoveAll(pluginDir)
defer os.RemoveAll(webappPluginDir)
env, err := plugin.NewEnvironment(th.App.NewPluginAPI, pluginDir, webappPluginDir, th.App.Log(), metricsMock)
env, err := plugin.NewEnvironment(th.NewPluginAPI, pluginDir, webappPluginDir, th.App.Log(), metricsMock)
require.NoError(t, err)
th.App.SetPluginsEnvironment(env)
@@ -1116,7 +1115,7 @@ func TestHookMetrics(t *testing.T) {
Password: "passwd1",
AuthService: "",
}
_, appErr := th.App.CreateUser(user1)
_, appErr := th.App.CreateUser(th.Context, user1)
require.Nil(t, appErr)
time.Sleep(1 * time.Second)
user1, appErr = th.App.GetUser(user1.Id)
@@ -1169,7 +1168,7 @@ func TestHookReactionHasBeenAdded(t *testing.T) {
EmojiName: "smile",
CreateAt: model.GetMillis() - 10000,
}
_, err := th.App.SaveReactionForPost(reaction)
_, err := th.App.SaveReactionForPost(th.Context, reaction)
require.Nil(t, err)
}
@@ -1212,7 +1211,7 @@ func TestHookReactionHasBeenRemoved(t *testing.T) {
CreateAt: model.GetMillis() - 10000,
}
err := th.App.DeleteReactionForPost(reaction)
err := th.App.DeleteReactionForPost(th.Context, reaction)
require.Nil(t, err)
}