github.com/adacta-ru/mattermost-server/v6@v6.0.0/app/slashcommands/command_mute_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package slashcommands
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/mattermost/go-i18n/i18n"
    11  	"github.com/adacta-ru/mattermost-server/v6/model"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestMuteCommandNoChannel(t *testing.T) {
    16  	th := setup(t).initBasic()
    17  	defer th.tearDown()
    18  
    19  	if testing.Short() {
    20  		t.SkipNow()
    21  	}
    22  
    23  	channel1 := th.BasicChannel
    24  	channel1M, channel1MError := th.App.GetChannelMember(channel1.Id, th.BasicUser.Id)
    25  
    26  	assert.Nil(t, channel1MError, "User is not a member of channel 1")
    27  	assert.NotEqual(
    28  		t,
    29  		channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP],
    30  		model.CHANNEL_NOTIFY_MENTION,
    31  		"Channel shouldn't be muted on initial setup",
    32  	)
    33  
    34  	cmd := &MuteProvider{}
    35  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    36  		T:      i18n.IdentityTfunc(),
    37  		UserId: th.BasicUser.Id,
    38  	}, "")
    39  	assert.Equal(t, "api.command_mute.no_channel.error", resp.Text)
    40  }
    41  
    42  func TestMuteCommandNoArgs(t *testing.T) {
    43  	th := setup(t).initBasic()
    44  	defer th.tearDown()
    45  
    46  	channel1 := th.BasicChannel
    47  	channel1M, _ := th.App.GetChannelMember(channel1.Id, th.BasicUser.Id)
    48  
    49  	assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel1M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
    50  
    51  	cmd := &MuteProvider{}
    52  
    53  	// First mute the channel
    54  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    55  		T:         i18n.IdentityTfunc(),
    56  		ChannelId: channel1.Id,
    57  		UserId:    th.BasicUser.Id,
    58  	}, "")
    59  	assert.Equal(t, "api.command_mute.success_mute", resp.Text)
    60  
    61  	// Now unmute the channel
    62  	time.Sleep(time.Millisecond)
    63  	resp = cmd.DoCommand(th.App, &model.CommandArgs{
    64  		T:         i18n.IdentityTfunc(),
    65  		ChannelId: channel1.Id,
    66  		UserId:    th.BasicUser.Id,
    67  	}, "")
    68  
    69  	assert.Equal(t, "api.command_mute.success_unmute", resp.Text)
    70  }
    71  
    72  func TestMuteCommandSpecificChannel(t *testing.T) {
    73  	th := setup(t).initBasic()
    74  	defer th.tearDown()
    75  
    76  	if testing.Short() {
    77  		t.SkipNow()
    78  	}
    79  
    80  	channel1 := th.BasicChannel
    81  	channel2, _ := th.App.CreateChannel(&model.Channel{
    82  		DisplayName: "AA",
    83  		Name:        "aa" + model.NewId() + "a",
    84  		Type:        model.CHANNEL_OPEN,
    85  		TeamId:      th.BasicTeam.Id,
    86  		CreatorId:   th.BasicUser.Id,
    87  	}, true)
    88  
    89  	channel2M, _ := th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
    90  
    91  	assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
    92  
    93  	cmd := &MuteProvider{}
    94  
    95  	// First mute the channel
    96  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
    97  		T:         i18n.IdentityTfunc(),
    98  		ChannelId: channel1.Id,
    99  		UserId:    th.BasicUser.Id,
   100  	}, channel2.Name)
   101  	assert.Equal(t, "api.command_mute.success_mute", resp.Text)
   102  	channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
   103  	assert.Equal(t, model.CHANNEL_NOTIFY_MENTION, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
   104  
   105  	// Now unmute the channel
   106  	resp = cmd.DoCommand(th.App, &model.CommandArgs{
   107  		T:         i18n.IdentityTfunc(),
   108  		ChannelId: channel1.Id,
   109  		UserId:    th.BasicUser.Id,
   110  	}, "~"+channel2.Name)
   111  
   112  	assert.Equal(t, "api.command_mute.success_unmute", resp.Text)
   113  	channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
   114  	assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
   115  }
   116  
   117  func TestMuteCommandNotMember(t *testing.T) {
   118  	th := setup(t).initBasic()
   119  	defer th.tearDown()
   120  
   121  	if testing.Short() {
   122  		t.SkipNow()
   123  	}
   124  
   125  	channel1 := th.BasicChannel
   126  	channel2, _ := th.App.CreateChannel(&model.Channel{
   127  		DisplayName: "AA",
   128  		Name:        "aa" + model.NewId() + "a",
   129  		Type:        model.CHANNEL_OPEN,
   130  		TeamId:      th.BasicTeam.Id,
   131  		CreatorId:   th.BasicUser.Id,
   132  	}, false)
   133  
   134  	cmd := &MuteProvider{}
   135  
   136  	// First mute the channel
   137  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
   138  		T:         i18n.IdentityTfunc(),
   139  		ChannelId: channel1.Id,
   140  		UserId:    th.BasicUser.Id,
   141  	}, channel2.Name)
   142  	assert.Equal(t, "api.command_mute.not_member.error", resp.Text)
   143  }
   144  
   145  func TestMuteCommandNotChannel(t *testing.T) {
   146  	th := setup(t).initBasic()
   147  	defer th.tearDown()
   148  
   149  	if testing.Short() {
   150  		t.SkipNow()
   151  	}
   152  
   153  	channel1 := th.BasicChannel
   154  
   155  	cmd := &MuteProvider{}
   156  
   157  	// First mute the channel
   158  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
   159  		T:         i18n.IdentityTfunc(),
   160  		ChannelId: channel1.Id,
   161  		UserId:    th.BasicUser.Id,
   162  	}, "~noexists")
   163  	assert.Equal(t, "api.command_mute.error", resp.Text)
   164  }
   165  
   166  func TestMuteCommandDMChannel(t *testing.T) {
   167  	th := setup(t).initBasic()
   168  	defer th.tearDown()
   169  
   170  	if testing.Short() {
   171  		t.SkipNow()
   172  	}
   173  
   174  	channel2, _ := th.App.GetOrCreateDirectChannel(th.BasicUser.Id, th.BasicUser2.Id)
   175  	channel2M, _ := th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
   176  
   177  	assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
   178  
   179  	cmd := &MuteProvider{}
   180  
   181  	// First mute the channel
   182  	resp := cmd.DoCommand(th.App, &model.CommandArgs{
   183  		T:         i18n.IdentityTfunc(),
   184  		ChannelId: channel2.Id,
   185  		UserId:    th.BasicUser.Id,
   186  	}, "")
   187  	assert.Equal(t, "api.command_mute.success_mute_direct_msg", resp.Text)
   188  	time.Sleep(time.Millisecond)
   189  	channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
   190  	assert.Equal(t, model.CHANNEL_NOTIFY_MENTION, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
   191  
   192  	// Now unmute the channel
   193  	resp = cmd.DoCommand(th.App, &model.CommandArgs{
   194  		T:         i18n.IdentityTfunc(),
   195  		ChannelId: channel2.Id,
   196  		UserId:    th.BasicUser.Id,
   197  	}, "")
   198  
   199  	assert.Equal(t, "api.command_mute.success_unmute_direct_msg", resp.Text)
   200  	time.Sleep(time.Millisecond)
   201  	channel2M, _ = th.App.GetChannelMember(channel2.Id, th.BasicUser.Id)
   202  	assert.Equal(t, model.CHANNEL_NOTIFY_ALL, channel2M.NotifyProps[model.MARK_UNREAD_NOTIFY_PROP])
   203  }