Files
mattermost/app/web_conn_test.go
Jesse Hallam a78913178c Ensure unittest isolation (#9819)
* api4: fix TestGetUsersNotInTeam assertions

This test was relying on data from a previous test run. With the data cleared before each test, the assertions much match reality.

* *testlib: always InitSystemAdmin

Some tests implicitly relied on the basic user having system
administrator privileges because it was the first user created as such.
Eliminate `InitSystemAdmin` and explicitly create the system admin user
instead to avoid this ambiguity going forward.

* *testlib: drop all tables before each test

* api4: split up TestChannelDelete to avoid duplicate InitBasic

* api4: teardown in TestResetPassword, for when this test comes back

* invalidate cache on DropAllTables

This is necessary since the test store persists across tests.

* disable parallel tests

While tests within a package must be explicitly parallelized using `t.Parallel()`, tests across packages are run in parallel by default.  This causes problems given that the tests all currently share the same database instance.

Unfortunately, this also means that running the tests is much slower, but we can return to this later.
2018-11-20 20:16:25 -05:00

83 lines
2.7 KiB
Go

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
func TestWebConnShouldSendEvent(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
session, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser.Id, Roles: th.BasicUser.GetRawRoles()})
require.Nil(t, err)
basicUserWc := &WebConn{
App: th.App,
UserId: th.BasicUser.Id,
T: utils.T,
}
basicUserWc.SetSession(session)
basicUserWc.SetSessionToken(session.Token)
basicUserWc.SetSessionExpiresAt(session.ExpiresAt)
session2, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser2.Id, Roles: th.BasicUser2.GetRawRoles()})
require.Nil(t, err)
basicUser2Wc := &WebConn{
App: th.App,
UserId: th.BasicUser2.Id,
T: utils.T,
}
basicUser2Wc.SetSession(session2)
basicUser2Wc.SetSessionToken(session2.Token)
basicUser2Wc.SetSessionExpiresAt(session2.ExpiresAt)
session3, err := th.App.CreateSession(&model.Session{UserId: th.SystemAdminUser.Id, Roles: th.SystemAdminUser.GetRawRoles()})
require.Nil(t, err)
adminUserWc := &WebConn{
App: th.App,
UserId: th.SystemAdminUser.Id,
T: utils.T,
}
adminUserWc.SetSession(session3)
adminUserWc.SetSessionToken(session3.Token)
adminUserWc.SetSessionExpiresAt(session3.ExpiresAt)
cases := []struct {
Description string
Broadcast *model.WebsocketBroadcast
User1Expected bool
User2Expected bool
AdminExpected bool
}{
{"should send to all", &model.WebsocketBroadcast{}, true, true, true},
{"should only send to basic user", &model.WebsocketBroadcast{UserId: th.BasicUser.Id}, true, false, false},
{"should omit basic user 2", &model.WebsocketBroadcast{OmitUsers: map[string]bool{th.BasicUser2.Id: true}}, true, false, true},
{"should only send to admin", &model.WebsocketBroadcast{ContainsSensitiveData: true}, false, false, true},
{"should only send to non-admins", &model.WebsocketBroadcast{ContainsSanitizedData: true}, true, true, false},
{"should send to nobody", &model.WebsocketBroadcast{ContainsSensitiveData: true, ContainsSanitizedData: true}, false, false, false},
// needs more cases to get full coverage
}
event := &model.WebSocketEvent{Event: "some_event"}
for _, c := range cases {
event.Broadcast = c.Broadcast
assert.Equal(t, c.User1Expected, basicUserWc.ShouldSendEvent(event), c.Description)
assert.Equal(t, c.User2Expected, basicUser2Wc.ShouldSendEvent(event), c.Description)
assert.Equal(t, c.AdminExpected, adminUserWc.ShouldSendEvent(event), c.Description)
}
}