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