github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+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().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().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().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  	firstPost, _ := 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, firstPost.Id)
   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  		autoResponderIsComment := false
   111  		for _, post := range list.Posts {
   112  			if post.Type == model.POST_AUTO_RESPONDER {
   113  				autoResponderIsComment = post.RootId == firstPost.Id
   114  				autoResponderPostFound = true
   115  			}
   116  		}
   117  		assert.True(t, autoResponderPostFound)
   118  		assert.True(t, autoResponderIsComment)
   119  	}
   120  }
   121  
   122  func TestSendAutoResponseFailure(t *testing.T) {
   123  	th := Setup().InitBasic()
   124  	defer th.TearDown()
   125  
   126  	user := th.CreateUser()
   127  	defer th.App.PermanentDeleteUser(user)
   128  
   129  	patch := &model.UserPatch{}
   130  	patch.NotifyProps = make(map[string]string)
   131  	patch.NotifyProps["auto_responder_active"] = "false"
   132  	patch.NotifyProps["auto_responder_message"] = "Hello, I'm unavailable today."
   133  
   134  	userUpdated1, err := th.App.PatchUser(user.Id, patch, true)
   135  	require.Nil(t, err)
   136  
   137  	firstPost, _ := th.App.CreatePost(&model.Post{
   138  		ChannelId: th.BasicChannel.Id,
   139  		Message:   "zz" + model.NewId() + "a",
   140  		UserId:    th.BasicUser.Id},
   141  		th.BasicChannel,
   142  		false)
   143  
   144  	th.App.SendAutoResponse(th.BasicChannel, userUpdated1, firstPost.Id)
   145  
   146  	if list, err := th.App.GetPosts(th.BasicChannel.Id, 0, 1); err != nil {
   147  		require.Nil(t, err)
   148  	} else {
   149  		autoResponderPostFound := false
   150  		autoResponderIsComment := false
   151  		for _, post := range list.Posts {
   152  			if post.Type == model.POST_AUTO_RESPONDER {
   153  				autoResponderIsComment = post.RootId == firstPost.Id
   154  				autoResponderPostFound = true
   155  			}
   156  		}
   157  		assert.False(t, autoResponderPostFound)
   158  		assert.False(t, autoResponderIsComment)
   159  	}
   160  }