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