github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/app/expirynotify_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  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  )
    15  
    16  func TestNotifySessionsExpired(t *testing.T) {
    17  	th := Setup(t).InitBasic()
    18  	defer th.TearDown()
    19  
    20  	handler := &testPushNotificationHandler{t: t}
    21  	pushServer := httptest.NewServer(
    22  		http.HandlerFunc(handler.handleReq),
    23  	)
    24  	defer pushServer.Close()
    25  
    26  	th.App.UpdateConfig(func(cfg *model.Config) {
    27  		*cfg.EmailSettings.PushNotificationServer = pushServer.URL
    28  	})
    29  
    30  	t.Run("push notifications disabled", func(t *testing.T) {
    31  		th.App.UpdateConfig(func(cfg *model.Config) {
    32  			*cfg.EmailSettings.SendPushNotifications = false
    33  		})
    34  
    35  		err := th.App.NotifySessionsExpired()
    36  		// no error, but also no requests sent
    37  		require.Nil(t, err)
    38  		require.Equal(t, 0, handler.numReqs())
    39  	})
    40  
    41  	t.Run("two sessions expired", func(t *testing.T) {
    42  		th.App.UpdateConfig(func(cfg *model.Config) {
    43  			*cfg.EmailSettings.SendPushNotifications = true
    44  		})
    45  
    46  		data := []struct {
    47  			deviceId  string
    48  			expiresAt int64
    49  			notified  bool
    50  		}{
    51  			{deviceId: "android:11111", expiresAt: model.GetMillis() + 100000, notified: false},
    52  			{deviceId: "android:22222", expiresAt: model.GetMillis() - 1000, notified: false},
    53  			{deviceId: "android:33333", expiresAt: model.GetMillis() - 2000, notified: false},
    54  			{deviceId: "android:44444", expiresAt: model.GetMillis() - 3000, notified: true},
    55  		}
    56  
    57  		for _, d := range data {
    58  			_, err := th.App.CreateSession(&model.Session{
    59  				UserId:        th.BasicUser.Id,
    60  				DeviceId:      d.deviceId,
    61  				ExpiresAt:     d.expiresAt,
    62  				ExpiredNotify: d.notified,
    63  			})
    64  			require.Nil(t, err)
    65  		}
    66  
    67  		err := th.App.NotifySessionsExpired()
    68  
    69  		require.Nil(t, err)
    70  		require.Equal(t, 2, handler.numReqs())
    71  
    72  		expected := []string{"22222", "33333"}
    73  		require.Equal(t, model.PUSH_TYPE_SESSION, handler.notifications()[0].Type)
    74  		require.Contains(t, expected, handler.notifications()[0].DeviceId)
    75  		require.Contains(t, handler.notifications()[0].Message, "Session Expired")
    76  
    77  		require.Equal(t, model.PUSH_TYPE_SESSION, handler.notifications()[1].Type)
    78  		require.Contains(t, expected, handler.notifications()[1].DeviceId)
    79  		require.Contains(t, handler.notifications()[1].Message, "Session Expired")
    80  	})
    81  }