github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/app/web_conn_test.go (about)

     1  // Copyright (c) 2015-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/v5/model"
    13  	"github.com/mattermost/mattermost-server/v5/utils"
    14  )
    15  
    16  func TestWebConnShouldSendEvent(t *testing.T) {
    17  	th := Setup(t).InitBasic()
    18  	defer th.TearDown()
    19  	session, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser.Id, Roles: th.BasicUser.GetRawRoles(), TeamMembers: []*model.TeamMember{
    20  		{
    21  			UserId: th.BasicUser.Id,
    22  			TeamId: th.BasicTeam.Id,
    23  			Roles:  model.TEAM_USER_ROLE_ID,
    24  		},
    25  	}})
    26  	require.Nil(t, err)
    27  
    28  	basicUserWc := &WebConn{
    29  		App:    th.App,
    30  		UserId: th.BasicUser.Id,
    31  		T:      utils.T,
    32  	}
    33  
    34  	basicUserWc.SetSession(session)
    35  	basicUserWc.SetSessionToken(session.Token)
    36  	basicUserWc.SetSessionExpiresAt(session.ExpiresAt)
    37  
    38  	session2, err := th.App.CreateSession(&model.Session{UserId: th.BasicUser2.Id, Roles: th.BasicUser2.GetRawRoles(), TeamMembers: []*model.TeamMember{
    39  		{
    40  			UserId: th.BasicUser2.Id,
    41  			TeamId: th.BasicTeam.Id,
    42  			Roles:  model.TEAM_ADMIN_ROLE_ID,
    43  		},
    44  	}})
    45  	require.Nil(t, err)
    46  
    47  	basicUser2Wc := &WebConn{
    48  		App:    th.App,
    49  		UserId: th.BasicUser2.Id,
    50  		T:      utils.T,
    51  	}
    52  
    53  	basicUser2Wc.SetSession(session2)
    54  	basicUser2Wc.SetSessionToken(session2.Token)
    55  	basicUser2Wc.SetSessionExpiresAt(session2.ExpiresAt)
    56  
    57  	session3, err := th.App.CreateSession(&model.Session{UserId: th.SystemAdminUser.Id, Roles: th.SystemAdminUser.GetRawRoles()})
    58  	require.Nil(t, err)
    59  
    60  	adminUserWc := &WebConn{
    61  		App:    th.App,
    62  		UserId: th.SystemAdminUser.Id,
    63  		T:      utils.T,
    64  	}
    65  
    66  	adminUserWc.SetSession(session3)
    67  	adminUserWc.SetSessionToken(session3.Token)
    68  	adminUserWc.SetSessionExpiresAt(session3.ExpiresAt)
    69  
    70  	cases := []struct {
    71  		Description   string
    72  		Broadcast     *model.WebsocketBroadcast
    73  		User1Expected bool
    74  		User2Expected bool
    75  		AdminExpected bool
    76  	}{
    77  		{"should send to all", &model.WebsocketBroadcast{}, true, true, true},
    78  		{"should only send to basic user", &model.WebsocketBroadcast{UserId: th.BasicUser.Id}, true, false, false},
    79  		{"should omit basic user 2", &model.WebsocketBroadcast{OmitUsers: map[string]bool{th.BasicUser2.Id: true}}, true, false, true},
    80  		{"should only send to admin", &model.WebsocketBroadcast{ContainsSensitiveData: true}, false, false, true},
    81  		{"should only send to non-admins", &model.WebsocketBroadcast{ContainsSanitizedData: true}, true, true, false},
    82  		{"should send to nobody", &model.WebsocketBroadcast{ContainsSensitiveData: true, ContainsSanitizedData: true}, false, false, false},
    83  		// needs more cases to get full coverage
    84  	}
    85  
    86  	event := model.NewWebSocketEvent("some_event", "", "", "", nil)
    87  	for _, c := range cases {
    88  		event = event.SetBroadcast(c.Broadcast)
    89  		assert.Equal(t, c.User1Expected, basicUserWc.shouldSendEvent(event), c.Description)
    90  		assert.Equal(t, c.User2Expected, basicUser2Wc.shouldSendEvent(event), c.Description)
    91  		assert.Equal(t, c.AdminExpected, adminUserWc.shouldSendEvent(event), c.Description)
    92  	}
    93  
    94  	event2 := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_UPDATE_TEAM, th.BasicTeam.Id, "", "", nil)
    95  	assert.True(t, basicUserWc.shouldSendEvent(event2))
    96  	assert.True(t, basicUser2Wc.shouldSendEvent(event2))
    97  
    98  	event3 := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_UPDATE_TEAM, "wrongId", "", "", nil)
    99  	assert.False(t, basicUserWc.shouldSendEvent(event3))
   100  }