github.com/adacta-ru/mattermost-server@v5.11.1+incompatible/app/auto_responder_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/mattermost/mattermost-server/model"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestSetAutoResponderStatus(t *testing.T) {
    15  	th := Setup(t).InitBasic()
    16  	defer th.TearDown()
    17  
    18  	user := th.CreateUser()
    19  	defer th.App.PermanentDeleteUser(user)
    20  
    21  	th.App.SetStatusOnline(user.Id, true)
    22  
    23  	patch := &model.UserPatch{}
    24  	patch.NotifyProps = make(map[string]string)
    25  	patch.NotifyProps["auto_responder_active"] = "true"
    26  	patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
    27  
    28  	userUpdated1, _ := th.App.PatchUser(user.Id, patch, true)
    29  
    30  	// autoResponder is enabled, status should be OOO
    31  	th.App.SetAutoResponderStatus(userUpdated1, user.NotifyProps)
    32  
    33  	status, err := th.App.GetStatus(userUpdated1.Id)
    34  	require.Nil(t, err)
    35  	assert.Equal(t, model.STATUS_OUT_OF_OFFICE, status.Status)
    36  
    37  	patch2 := &model.UserPatch{}
    38  	patch2.NotifyProps = make(map[string]string)
    39  	patch2.NotifyProps["auto_responder_active"] = "false"
    40  	patch2.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
    41  
    42  	userUpdated2, _ := th.App.PatchUser(user.Id, patch2, true)
    43  
    44  	// autoResponder is disabled, status should be ONLINE
    45  	th.App.SetAutoResponderStatus(userUpdated2, userUpdated1.NotifyProps)
    46  
    47  	status, err = th.App.GetStatus(userUpdated2.Id)
    48  	require.Nil(t, err)
    49  	assert.Equal(t, model.STATUS_ONLINE, status.Status)
    50  
    51  }
    52  
    53  func TestDisableAutoResponder(t *testing.T) {
    54  	th := Setup(t).InitBasic()
    55  	defer th.TearDown()
    56  
    57  	user := th.CreateUser()
    58  	defer th.App.PermanentDeleteUser(user)
    59  
    60  	th.App.SetStatusOnline(user.Id, true)
    61  
    62  	patch := &model.UserPatch{}
    63  	patch.NotifyProps = make(map[string]string)
    64  	patch.NotifyProps["auto_responder_active"] = "true"
    65  	patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
    66  
    67  	th.App.PatchUser(user.Id, patch, true)
    68  
    69  	th.App.DisableAutoResponder(user.Id, true)
    70  
    71  	userUpdated1, err := th.App.GetUser(user.Id)
    72  	require.Nil(t, err)
    73  	assert.Equal(t, userUpdated1.NotifyProps["auto_responder_active"], "false")
    74  
    75  	th.App.DisableAutoResponder(user.Id, true)
    76  
    77  	userUpdated2, err := th.App.GetUser(user.Id)
    78  	require.Nil(t, err)
    79  	assert.Equal(t, userUpdated2.NotifyProps["auto_responder_active"], "false")
    80  }
    81  
    82  func TestSendAutoResponseSuccess(t *testing.T) {
    83  	th := Setup(t).InitBasic()
    84  	defer th.TearDown()
    85  
    86  	user := th.CreateUser()
    87  	defer th.App.PermanentDeleteUser(user)
    88  
    89  	patch := &model.UserPatch{}
    90  	patch.NotifyProps = make(map[string]string)
    91  	patch.NotifyProps["auto_responder_active"] = "true"
    92  	patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
    93  
    94  	userUpdated1, err := th.App.PatchUser(user.Id, patch, true)
    95  	require.Nil(t, err)
    96  
    97  	th.App.CreatePost(&model.Post{
    98  		ChannelId: th.BasicChannel.Id,
    99  		Message:   "zz" + model.NewId() + "a",
   100  		UserId:    th.BasicUser.Id},
   101  		th.BasicChannel,
   102  		false)
   103  
   104  	th.App.SendAutoResponse(th.BasicChannel, userUpdated1)
   105  
   106  	if list, err := th.App.GetPosts(th.BasicChannel.Id, 0, 1); err != nil {
   107  		require.Nil(t, err)
   108  	} else {
   109  		autoResponderPostFound := false
   110  		for _, post := range list.Posts {
   111  			if post.Type == model.POST_AUTO_RESPONDER {
   112  				autoResponderPostFound = true
   113  			}
   114  		}
   115  		assert.True(t, autoResponderPostFound)
   116  	}
   117  }
   118  
   119  func TestSendAutoResponseFailure(t *testing.T) {
   120  	th := Setup(t).InitBasic()
   121  	defer th.TearDown()
   122  
   123  	user := th.CreateUser()
   124  	defer th.App.PermanentDeleteUser(user)
   125  
   126  	patch := &model.UserPatch{}
   127  	patch.NotifyProps = make(map[string]string)
   128  	patch.NotifyProps["auto_responder_active"] = "false"
   129  	patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
   130  
   131  	userUpdated1, err := th.App.PatchUser(user.Id, patch, true)
   132  	require.Nil(t, err)
   133  
   134  	th.App.CreatePost(&model.Post{
   135  		ChannelId: th.BasicChannel.Id,
   136  		Message:   "zz" + model.NewId() + "a",
   137  		UserId:    th.BasicUser.Id},
   138  		th.BasicChannel,
   139  		false)
   140  
   141  	th.App.SendAutoResponse(th.BasicChannel, userUpdated1)
   142  
   143  	if list, err := th.App.GetPosts(th.BasicChannel.Id, 0, 1); err != nil {
   144  		require.Nil(t, err)
   145  	} else {
   146  		autoResponderPostFound := false
   147  		for _, post := range list.Posts {
   148  			if post.Type == model.POST_AUTO_RESPONDER {
   149  				autoResponderPostFound = true
   150  			}
   151  		}
   152  		assert.False(t, autoResponderPostFound)
   153  	}
   154  }