github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/session_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/model"
    13  	"github.com/mattermost/mattermost-server/v5/store"
    14  )
    15  
    16  const (
    17  	TenMinutes = 600000
    18  )
    19  
    20  func TestSessionStore(t *testing.T, ss store.Store) {
    21  	// Run serially to prevent interfering with other tests
    22  	testSessionCleanup(t, ss)
    23  
    24  	t.Run("Save", func(t *testing.T) { testSessionStoreSave(t, ss) })
    25  	t.Run("SessionGet", func(t *testing.T) { testSessionGet(t, ss) })
    26  	t.Run("SessionGetWithDeviceId", func(t *testing.T) { testSessionGetWithDeviceId(t, ss) })
    27  	t.Run("SessionRemove", func(t *testing.T) { testSessionRemove(t, ss) })
    28  	t.Run("SessionRemoveAll", func(t *testing.T) { testSessionRemoveAll(t, ss) })
    29  	t.Run("SessionRemoveByUser", func(t *testing.T) { testSessionRemoveByUser(t, ss) })
    30  	t.Run("SessionRemoveToken", func(t *testing.T) { testSessionRemoveToken(t, ss) })
    31  	t.Run("SessionUpdateDeviceId", func(t *testing.T) { testSessionUpdateDeviceId(t, ss) })
    32  	t.Run("SessionUpdateDeviceId2", func(t *testing.T) { testSessionUpdateDeviceId2(t, ss) })
    33  	t.Run("UpdateExpiresAt", func(t *testing.T) { testSessionStoreUpdateExpiresAt(t, ss) })
    34  	t.Run("UpdateLastActivityAt", func(t *testing.T) { testSessionStoreUpdateLastActivityAt(t, ss) })
    35  	t.Run("SessionCount", func(t *testing.T) { testSessionCount(t, ss) })
    36  	t.Run("GetSessionsExpired", func(t *testing.T) { testGetSessionsExpired(t, ss) })
    37  	t.Run("UpdateExpiredNotify", func(t *testing.T) { testUpdateExpiredNotify(t, ss) })
    38  }
    39  
    40  func testSessionStoreSave(t *testing.T, ss store.Store) {
    41  	s1 := &model.Session{}
    42  	s1.UserId = model.NewId()
    43  
    44  	_, err := ss.Session().Save(s1)
    45  	require.NoError(t, err)
    46  }
    47  
    48  func testSessionGet(t *testing.T, ss store.Store) {
    49  	s1 := &model.Session{}
    50  	s1.UserId = model.NewId()
    51  
    52  	s1, err := ss.Session().Save(s1)
    53  	require.NoError(t, err)
    54  
    55  	s2 := &model.Session{}
    56  	s2.UserId = s1.UserId
    57  
    58  	s2, err = ss.Session().Save(s2)
    59  	require.NoError(t, err)
    60  
    61  	s3 := &model.Session{}
    62  	s3.UserId = s1.UserId
    63  	s3.ExpiresAt = 1
    64  
    65  	s3, err = ss.Session().Save(s3)
    66  	require.NoError(t, err)
    67  
    68  	session, err := ss.Session().Get(s1.Id)
    69  	require.NoError(t, err)
    70  	require.Equal(t, session.Id, s1.Id, "should match")
    71  
    72  	data, err := ss.Session().GetSessions(s1.UserId)
    73  	require.NoError(t, err)
    74  	require.Len(t, data, 3, "should match len")
    75  }
    76  
    77  func testSessionGetWithDeviceId(t *testing.T, ss store.Store) {
    78  	s1 := &model.Session{}
    79  	s1.UserId = model.NewId()
    80  	s1.ExpiresAt = model.GetMillis() + 10000
    81  
    82  	s1, err := ss.Session().Save(s1)
    83  	require.NoError(t, err)
    84  
    85  	s2 := &model.Session{}
    86  	s2.UserId = s1.UserId
    87  	s2.DeviceId = model.NewId()
    88  	s2.ExpiresAt = model.GetMillis() + 10000
    89  
    90  	s2, err = ss.Session().Save(s2)
    91  	require.NoError(t, err)
    92  
    93  	s3 := &model.Session{}
    94  	s3.UserId = s1.UserId
    95  	s3.ExpiresAt = 1
    96  	s3.DeviceId = model.NewId()
    97  
    98  	s3, err = ss.Session().Save(s3)
    99  	require.NoError(t, err)
   100  
   101  	data, err := ss.Session().GetSessionsWithActiveDeviceIds(s1.UserId)
   102  	require.NoError(t, err)
   103  	require.Len(t, data, 1, "should match len")
   104  }
   105  
   106  func testSessionRemove(t *testing.T, ss store.Store) {
   107  	s1 := &model.Session{}
   108  	s1.UserId = model.NewId()
   109  
   110  	s1, err := ss.Session().Save(s1)
   111  	require.NoError(t, err)
   112  
   113  	session, err := ss.Session().Get(s1.Id)
   114  	require.NoError(t, err)
   115  	require.Equal(t, session.Id, s1.Id, "should match")
   116  
   117  	removeErr := ss.Session().Remove(s1.Id)
   118  	require.NoError(t, removeErr)
   119  
   120  	_, err = ss.Session().Get(s1.Id)
   121  	require.Error(t, err, "should have been removed")
   122  }
   123  
   124  func testSessionRemoveAll(t *testing.T, ss store.Store) {
   125  	s1 := &model.Session{}
   126  	s1.UserId = model.NewId()
   127  
   128  	s1, err := ss.Session().Save(s1)
   129  	require.NoError(t, err)
   130  
   131  	session, err := ss.Session().Get(s1.Id)
   132  	require.NoError(t, err)
   133  	require.Equal(t, session.Id, s1.Id, "should match")
   134  
   135  	removeErr := ss.Session().RemoveAllSessions()
   136  	require.NoError(t, removeErr)
   137  
   138  	_, err = ss.Session().Get(s1.Id)
   139  	require.Error(t, err, "should have been removed")
   140  }
   141  
   142  func testSessionRemoveByUser(t *testing.T, ss store.Store) {
   143  	s1 := &model.Session{}
   144  	s1.UserId = model.NewId()
   145  
   146  	s1, err := ss.Session().Save(s1)
   147  	require.NoError(t, err)
   148  
   149  	session, err := ss.Session().Get(s1.Id)
   150  	require.NoError(t, err)
   151  	require.Equal(t, session.Id, s1.Id, "should match")
   152  
   153  	deleteErr := ss.Session().PermanentDeleteSessionsByUser(s1.UserId)
   154  	require.NoError(t, deleteErr)
   155  
   156  	_, err = ss.Session().Get(s1.Id)
   157  	require.Error(t, err, "should have been removed")
   158  }
   159  
   160  func testSessionRemoveToken(t *testing.T, ss store.Store) {
   161  	s1 := &model.Session{}
   162  	s1.UserId = model.NewId()
   163  
   164  	s1, err := ss.Session().Save(s1)
   165  	require.NoError(t, err)
   166  
   167  	session, err := ss.Session().Get(s1.Id)
   168  	require.NoError(t, err)
   169  	require.Equal(t, session.Id, s1.Id, "should match")
   170  
   171  	removeErr := ss.Session().Remove(s1.Token)
   172  	require.NoError(t, removeErr)
   173  
   174  	_, err = ss.Session().Get(s1.Id)
   175  	require.Error(t, err, "should have been removed")
   176  
   177  	data, err := ss.Session().GetSessions(s1.UserId)
   178  	require.NoError(t, err)
   179  	require.Empty(t, data, "should match len")
   180  }
   181  
   182  func testSessionUpdateDeviceId(t *testing.T, ss store.Store) {
   183  	s1 := &model.Session{}
   184  	s1.UserId = model.NewId()
   185  
   186  	s1, err := ss.Session().Save(s1)
   187  	require.NoError(t, err)
   188  
   189  	_, err = ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)
   190  	require.NoError(t, err)
   191  
   192  	s2 := &model.Session{}
   193  	s2.UserId = model.NewId()
   194  
   195  	s2, err = ss.Session().Save(s2)
   196  	require.NoError(t, err)
   197  
   198  	_, err = ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)
   199  	require.NoError(t, err)
   200  }
   201  
   202  func testSessionUpdateDeviceId2(t *testing.T, ss store.Store) {
   203  	s1 := &model.Session{}
   204  	s1.UserId = model.NewId()
   205  
   206  	s1, err := ss.Session().Save(s1)
   207  	require.NoError(t, err)
   208  
   209  	_, err = ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)
   210  	require.NoError(t, err)
   211  
   212  	s2 := &model.Session{}
   213  	s2.UserId = model.NewId()
   214  
   215  	s2, err = ss.Session().Save(s2)
   216  	require.NoError(t, err)
   217  
   218  	_, err = ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)
   219  	require.NoError(t, err)
   220  }
   221  
   222  func testSessionStoreUpdateExpiresAt(t *testing.T, ss store.Store) {
   223  	s1 := &model.Session{}
   224  	s1.UserId = model.NewId()
   225  
   226  	s1, err := ss.Session().Save(s1)
   227  	require.NoError(t, err)
   228  
   229  	err = ss.Session().UpdateExpiresAt(s1.Id, 1234567890)
   230  	require.NoError(t, err)
   231  
   232  	session, err := ss.Session().Get(s1.Id)
   233  	require.NoError(t, err)
   234  	require.EqualValues(t, session.ExpiresAt, 1234567890, "ExpiresAt not updated correctly")
   235  }
   236  
   237  func testSessionStoreUpdateLastActivityAt(t *testing.T, ss store.Store) {
   238  	s1 := &model.Session{}
   239  	s1.UserId = model.NewId()
   240  
   241  	s1, err := ss.Session().Save(s1)
   242  	require.NoError(t, err)
   243  
   244  	err = ss.Session().UpdateLastActivityAt(s1.Id, 1234567890)
   245  	require.NoError(t, err)
   246  
   247  	session, err := ss.Session().Get(s1.Id)
   248  	require.NoError(t, err)
   249  	require.EqualValues(t, session.LastActivityAt, 1234567890, "LastActivityAt not updated correctly")
   250  }
   251  
   252  func testSessionCount(t *testing.T, ss store.Store) {
   253  	s1 := &model.Session{}
   254  	s1.UserId = model.NewId()
   255  	s1.ExpiresAt = model.GetMillis() + 100000
   256  
   257  	s1, err := ss.Session().Save(s1)
   258  	require.NoError(t, err)
   259  
   260  	count, err := ss.Session().AnalyticsSessionCount()
   261  	require.NoError(t, err)
   262  	require.NotZero(t, count, "should have at least 1 session")
   263  }
   264  
   265  func testSessionCleanup(t *testing.T, ss store.Store) {
   266  	now := model.GetMillis()
   267  
   268  	s1 := &model.Session{}
   269  	s1.UserId = model.NewId()
   270  	s1.ExpiresAt = 0 // never expires
   271  
   272  	s1, err := ss.Session().Save(s1)
   273  	require.NoError(t, err)
   274  
   275  	s2 := &model.Session{}
   276  	s2.UserId = s1.UserId
   277  	s2.ExpiresAt = now + 1000000 // expires in the future
   278  
   279  	s2, err = ss.Session().Save(s2)
   280  	require.NoError(t, err)
   281  
   282  	s3 := &model.Session{}
   283  	s3.UserId = model.NewId()
   284  	s3.ExpiresAt = 1 // expired
   285  
   286  	s3, err = ss.Session().Save(s3)
   287  	require.NoError(t, err)
   288  
   289  	s4 := &model.Session{}
   290  	s4.UserId = model.NewId()
   291  	s4.ExpiresAt = 2 // expired
   292  
   293  	s4, err = ss.Session().Save(s4)
   294  	require.NoError(t, err)
   295  
   296  	ss.Session().Cleanup(now, 1)
   297  
   298  	_, err = ss.Session().Get(s1.Id)
   299  	assert.NoError(t, err)
   300  
   301  	_, err = ss.Session().Get(s2.Id)
   302  	assert.NoError(t, err)
   303  
   304  	_, err = ss.Session().Get(s3.Id)
   305  	assert.Error(t, err)
   306  
   307  	_, err = ss.Session().Get(s4.Id)
   308  	assert.Error(t, err)
   309  
   310  	removeErr := ss.Session().Remove(s1.Id)
   311  	require.NoError(t, removeErr)
   312  
   313  	removeErr = ss.Session().Remove(s2.Id)
   314  	require.NoError(t, removeErr)
   315  }
   316  
   317  func testGetSessionsExpired(t *testing.T, ss store.Store) {
   318  	now := model.GetMillis()
   319  
   320  	// Clear existing sessions.
   321  	err := ss.Session().RemoveAllSessions()
   322  	require.NoError(t, err)
   323  
   324  	s1 := &model.Session{}
   325  	s1.UserId = model.NewId()
   326  	s1.DeviceId = model.NewId()
   327  	s1.ExpiresAt = 0 // never expires
   328  	s1, err = ss.Session().Save(s1)
   329  	require.NoError(t, err)
   330  
   331  	s2 := &model.Session{}
   332  	s2.UserId = model.NewId()
   333  	s2.DeviceId = model.NewId()
   334  	s2.ExpiresAt = now - TenMinutes // expired within threshold
   335  	s2, err = ss.Session().Save(s2)
   336  	require.NoError(t, err)
   337  
   338  	s3 := &model.Session{}
   339  	s3.UserId = model.NewId()
   340  	s3.DeviceId = model.NewId()
   341  	s3.ExpiresAt = now - (TenMinutes * 100) // expired outside threshold
   342  	s3, err = ss.Session().Save(s3)
   343  	require.NoError(t, err)
   344  
   345  	s4 := &model.Session{}
   346  	s4.UserId = model.NewId()
   347  	s4.ExpiresAt = now - TenMinutes // expired within threshold, but not mobile
   348  	s4, err = ss.Session().Save(s4)
   349  	require.NoError(t, err)
   350  
   351  	s5 := &model.Session{}
   352  	s5.UserId = model.NewId()
   353  	s5.DeviceId = model.NewId()
   354  	s5.ExpiresAt = now + (TenMinutes * 100000) // not expired
   355  	s5, err = ss.Session().Save(s5)
   356  	require.NoError(t, err)
   357  
   358  	sessions, err := ss.Session().GetSessionsExpired(TenMinutes*2, true, true) // mobile only
   359  	require.NoError(t, err)
   360  	require.Len(t, sessions, 1)
   361  	require.Equal(t, s2.Id, sessions[0].Id)
   362  
   363  	sessions, err = ss.Session().GetSessionsExpired(TenMinutes*2, false, true) // all client types
   364  	require.NoError(t, err)
   365  	require.Len(t, sessions, 2)
   366  	expected := []string{s2.Id, s4.Id}
   367  	for _, sess := range sessions {
   368  		require.Contains(t, expected, sess.Id)
   369  	}
   370  }
   371  
   372  func testUpdateExpiredNotify(t *testing.T, ss store.Store) {
   373  	s1 := &model.Session{}
   374  	s1.UserId = model.NewId()
   375  	s1.DeviceId = model.NewId()
   376  	s1.ExpiresAt = model.GetMillis() + TenMinutes
   377  	s1, err := ss.Session().Save(s1)
   378  	require.NoError(t, err)
   379  
   380  	session, err := ss.Session().Get(s1.Id)
   381  	require.NoError(t, err)
   382  	require.False(t, session.ExpiredNotify)
   383  
   384  	err = ss.Session().UpdateExpiredNotify(session.Id, true)
   385  	require.NoError(t, err)
   386  	session, err = ss.Session().Get(s1.Id)
   387  	require.NoError(t, err)
   388  	require.True(t, session.ExpiredNotify)
   389  
   390  	err = ss.Session().UpdateExpiredNotify(session.Id, false)
   391  	require.NoError(t, err)
   392  	session, err = ss.Session().Get(s1.Id)
   393  	require.NoError(t, err)
   394  	require.False(t, session.ExpiredNotify)
   395  }