mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Added support for PostActions in ephemeral posts The general approach is that we take all the metadata that DoPostAction needs to process client DoPostActionRequests, and store it in a serialized, encrypted Cookie field, in the PostAction struct. The client then must send it back, and it is then used to process PostActions as a fallback top the metadata in the database. This PR adds a new config setting, `ServiceSettings.ActionCookieSecret`. In a cluster environment it must be the same for all instances. - Added type PostActionCookie, and a Cookie string to PostAction. - Added App.AddActionCookiesToPost. - Use App.AddActionCookiesToPost in api4.createEphemeralPost, App.SendEphemeralPost, App.UpdateEphemeralPost. - Added App.DoPostActionWithCookie to process incoming requests with cookies. For backward compatibility, it prefers the metadata in the database; falls back to cookie. - Added plugin.API.UpdateEphemeralPost and plugin.API.DeleteEphemeralPost. - Added App.encryptActionCookie/App.decryptActionCookie. * Style * Fixed an unfortunate typo, tested with matterpoll * minor PR feedback * Fixed uninitialized Context * Fixed another test failure * Fixed permission check * Added api test for DoPostActionWithCookie * Replaced config.ActionCookieSecret with Server.PostActionCookieSecret Modeled after AsymetricSigningKey * style * Set DeleteAt in DeleteEphemeralPost * PR feedback * Removed deadwood comment * Added EXPERIMENTAL comment to the 2 APIs in question
153 lines
4.8 KiB
Go
153 lines
4.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See License.txt for license information.
|
|
|
|
package app
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
"github.com/mattermost/mattermost-server/store/sqlstore"
|
|
"github.com/mattermost/mattermost-server/utils"
|
|
)
|
|
|
|
func TestConfigListener(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
originalSiteName := th.App.Config().TeamSettings.SiteName
|
|
|
|
listenerCalled := false
|
|
listener := func(oldConfig *model.Config, newConfig *model.Config) {
|
|
assert.False(t, listenerCalled, "listener called twice")
|
|
|
|
assert.Equal(t, *originalSiteName, *oldConfig.TeamSettings.SiteName, "old config contains incorrect site name")
|
|
assert.Equal(t, "test123", *newConfig.TeamSettings.SiteName, "new config contains incorrect site name")
|
|
|
|
listenerCalled = true
|
|
}
|
|
listenerId := th.App.AddConfigListener(listener)
|
|
defer th.App.RemoveConfigListener(listenerId)
|
|
|
|
listener2Called := false
|
|
listener2 := func(oldConfig *model.Config, newConfig *model.Config) {
|
|
assert.False(t, listener2Called, "listener2 called twice")
|
|
|
|
listener2Called = true
|
|
}
|
|
listener2Id := th.App.AddConfigListener(listener2)
|
|
defer th.App.RemoveConfigListener(listener2Id)
|
|
|
|
th.App.UpdateConfig(func(cfg *model.Config) {
|
|
*cfg.TeamSettings.SiteName = "test123"
|
|
})
|
|
|
|
assert.True(t, listenerCalled, "listener should've been called")
|
|
assert.True(t, listener2Called, "listener 2 should've been called")
|
|
}
|
|
|
|
func TestAsymmetricSigningKey(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
assert.NotNil(t, th.App.AsymmetricSigningKey())
|
|
assert.NotEmpty(t, th.App.ClientConfig()["AsymmetricSigningPublicKey"])
|
|
}
|
|
|
|
func TestPostActionCookieSecret(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
assert.Equal(t, 32, len(th.App.PostActionCookieSecret()))
|
|
}
|
|
|
|
func TestClientConfigWithComputed(t *testing.T) {
|
|
th := Setup(t).InitBasic()
|
|
defer th.TearDown()
|
|
|
|
config := th.App.ClientConfigWithComputed()
|
|
if _, ok := config["NoAccounts"]; !ok {
|
|
t.Fatal("expected NoAccounts in returned config")
|
|
}
|
|
if _, ok := config["MaxPostSize"]; !ok {
|
|
t.Fatal("expected MaxPostSize in returned config")
|
|
}
|
|
}
|
|
|
|
func TestEnsureInstallationDate(t *testing.T) {
|
|
th := Setup(t)
|
|
defer th.TearDown()
|
|
|
|
tt := []struct {
|
|
Name string
|
|
PrevInstallationDate *int64
|
|
UsersCreationDates []int64
|
|
ExpectedInstallationDate *int64
|
|
}{
|
|
{
|
|
Name: "New installation: no users, no installation date",
|
|
PrevInstallationDate: nil,
|
|
UsersCreationDates: nil,
|
|
ExpectedInstallationDate: model.NewInt64(utils.MillisFromTime(time.Now())),
|
|
},
|
|
{
|
|
Name: "Old installation: users, no installation date",
|
|
PrevInstallationDate: nil,
|
|
UsersCreationDates: []int64{10000000000, 30000000000, 20000000000},
|
|
ExpectedInstallationDate: model.NewInt64(10000000000),
|
|
},
|
|
{
|
|
Name: "New installation, second run: no users, installation date",
|
|
PrevInstallationDate: model.NewInt64(80000000000),
|
|
UsersCreationDates: []int64{10000000000, 30000000000, 20000000000},
|
|
ExpectedInstallationDate: model.NewInt64(80000000000),
|
|
},
|
|
{
|
|
Name: "Old installation already updated: users, installation date",
|
|
PrevInstallationDate: model.NewInt64(90000000000),
|
|
UsersCreationDates: []int64{10000000000, 30000000000, 20000000000},
|
|
ExpectedInstallationDate: model.NewInt64(90000000000),
|
|
},
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
sqlStore := th.App.Srv.Store.User().(*sqlstore.SqlUserStore)
|
|
sqlStore.GetMaster().Exec("DELETE FROM Users")
|
|
|
|
for _, createAt := range tc.UsersCreationDates {
|
|
user := th.CreateUser()
|
|
user.CreateAt = createAt
|
|
sqlStore.GetMaster().Exec("UPDATE Users SET CreateAt = :CreateAt WHERE Id = :UserId", map[string]interface{}{"CreateAt": createAt, "UserId": user.Id})
|
|
}
|
|
|
|
if tc.PrevInstallationDate == nil {
|
|
<-th.App.Srv.Store.System().PermanentDeleteByName(model.SYSTEM_INSTALLATION_DATE_KEY)
|
|
} else {
|
|
<-th.App.Srv.Store.System().SaveOrUpdate(&model.System{
|
|
Name: model.SYSTEM_INSTALLATION_DATE_KEY,
|
|
Value: strconv.FormatInt(*tc.PrevInstallationDate, 10),
|
|
})
|
|
}
|
|
|
|
err := th.App.ensureInstallationDate()
|
|
|
|
if tc.ExpectedInstallationDate == nil {
|
|
assert.Error(t, err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
|
|
result := <-th.App.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY)
|
|
assert.Nil(t, result.Err)
|
|
data, _ := result.Data.(*model.System)
|
|
value, _ := strconv.ParseInt(data.Value, 10, 64)
|
|
assert.True(t, *tc.ExpectedInstallationDate <= value && *tc.ExpectedInstallationDate+1000 >= value)
|
|
}
|
|
|
|
sqlStore.GetMaster().Exec("DELETE FROM Users")
|
|
})
|
|
}
|
|
}
|