github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/app/web_conn_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/utils"
    14  )
    15  
    16  func TestWebConnShouldSendEvent(t *testing.T) {
    17  	th := Setup().InitBasic().InitSystemAdmin()
    18  	defer th.TearDown()
    19  
    20  	session, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser.Id, Roles: th.BasicUser.GetRawRoles()})
    21  	require.Nil(t, err)
    22  
    23  	basicUserWc := &WebConn{
    24  		App:    th.App,
    25  		UserId: th.BasicUser.Id,
    26  		T:      utils.T,
    27  	}
    28  
    29  	basicUserWc.SetSession(session)
    30  	basicUserWc.SetSessionToken(session.Token)
    31  	basicUserWc.SetSessionExpiresAt(session.ExpiresAt)
    32  
    33  	session2, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser2.Id, Roles: th.BasicUser2.GetRawRoles()})
    34  	require.Nil(t, err)
    35  
    36  	basicUser2Wc := &WebConn{
    37  		App:    th.App,
    38  		UserId: th.BasicUser2.Id,
    39  		T:      utils.T,
    40  	}
    41  
    42  	basicUser2Wc.SetSession(session2)
    43  	basicUser2Wc.SetSessionToken(session2.Token)
    44  	basicUser2Wc.SetSessionExpiresAt(session2.ExpiresAt)
    45  
    46  	session3, err := th.App.CreateSession(&model.Session{UserId: th.SystemAdminUser.Id, Roles: th.SystemAdminUser.GetRawRoles()})
    47  	require.Nil(t, err)
    48  
    49  	adminUserWc := &WebConn{
    50  		App:    th.App,
    51  		UserId: th.SystemAdminUser.Id,
    52  		T:      utils.T,
    53  	}
    54  
    55  	adminUserWc.SetSession(session3)
    56  	adminUserWc.SetSessionToken(session3.Token)
    57  	adminUserWc.SetSessionExpiresAt(session3.ExpiresAt)
    58  
    59  	cases := []struct {
    60  		Description   string
    61  		Broadcast     *model.WebsocketBroadcast
    62  		User1Expected bool
    63  		User2Expected bool
    64  		AdminExpected bool
    65  	}{
    66  		{"should send to all", &model.WebsocketBroadcast{}, true, true, true},
    67  		{"should only send to basic user", &model.WebsocketBroadcast{UserId: th.BasicUser.Id}, true, false, false},
    68  		{"should omit basic user 2", &model.WebsocketBroadcast{OmitUsers: map[string]bool{th.BasicUser2.Id: true}}, true, false, true},
    69  		{"should only send to admin", &model.WebsocketBroadcast{ContainsSensitiveData: true}, false, false, true},
    70  		{"should only send to non-admins", &model.WebsocketBroadcast{ContainsSanitizedData: true}, true, true, false},
    71  		{"should send to nobody", &model.WebsocketBroadcast{ContainsSensitiveData: true, ContainsSanitizedData: true}, false, false, false},
    72  		// needs more cases to get full coverage
    73  	}
    74  
    75  	event := &model.WebSocketEvent{Event: "some_event"}
    76  	for _, c := range cases {
    77  		event.Broadcast = c.Broadcast
    78  		assert.Equal(t, c.User1Expected, basicUserWc.ShouldSendEvent(event), c.Description)
    79  		assert.Equal(t, c.User2Expected, basicUser2Wc.ShouldSendEvent(event), c.Description)
    80  		assert.Equal(t, c.AdminExpected, adminUserWc.ShouldSendEvent(event), c.Description)
    81  	}
    82  }