github.com/mad-app/mattermost-server@v5.11.1+incompatible/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/mattermost/mattermost-server/model"
    10  	"github.com/mattermost/mattermost-server/store"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestSessionStore(t *testing.T, ss store.Store) {
    16  	// Run serially to prevent interfering with other tests
    17  	testSessionCleanup(t, ss)
    18  
    19  	t.Run("Save", func(t *testing.T) { testSessionStoreSave(t, ss) })
    20  	t.Run("SessionGet", func(t *testing.T) { testSessionGet(t, ss) })
    21  	t.Run("SessionGetWithDeviceId", func(t *testing.T) { testSessionGetWithDeviceId(t, ss) })
    22  	t.Run("SessionRemove", func(t *testing.T) { testSessionRemove(t, ss) })
    23  	t.Run("SessionRemoveAll", func(t *testing.T) { testSessionRemoveAll(t, ss) })
    24  	t.Run("SessionRemoveByUser", func(t *testing.T) { testSessionRemoveByUser(t, ss) })
    25  	t.Run("SessionRemoveToken", func(t *testing.T) { testSessionRemoveToken(t, ss) })
    26  	t.Run("SessionUpdateDeviceId", func(t *testing.T) { testSessionUpdateDeviceId(t, ss) })
    27  	t.Run("SessionUpdateDeviceId2", func(t *testing.T) { testSessionUpdateDeviceId2(t, ss) })
    28  	t.Run("UpdateLastActivityAt", func(t *testing.T) { testSessionStoreUpdateLastActivityAt(t, ss) })
    29  	t.Run("SessionCount", func(t *testing.T) { testSessionCount(t, ss) })
    30  }
    31  
    32  func testSessionStoreSave(t *testing.T, ss store.Store) {
    33  	s1 := model.Session{}
    34  	s1.UserId = model.NewId()
    35  
    36  	if err := (<-ss.Session().Save(&s1)).Err; err != nil {
    37  		t.Fatal(err)
    38  	}
    39  }
    40  
    41  func testSessionGet(t *testing.T, ss store.Store) {
    42  	s1 := model.Session{}
    43  	s1.UserId = model.NewId()
    44  	store.Must(ss.Session().Save(&s1))
    45  
    46  	s2 := model.Session{}
    47  	s2.UserId = s1.UserId
    48  	store.Must(ss.Session().Save(&s2))
    49  
    50  	s3 := model.Session{}
    51  	s3.UserId = s1.UserId
    52  	s3.ExpiresAt = 1
    53  	store.Must(ss.Session().Save(&s3))
    54  
    55  	if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
    56  		t.Fatal(rs1.Err)
    57  	} else {
    58  		if rs1.Data.(*model.Session).Id != s1.Id {
    59  			t.Fatal("should match")
    60  		}
    61  	}
    62  
    63  	if rs2 := (<-ss.Session().GetSessions(s1.UserId)); rs2.Err != nil {
    64  		t.Fatal(rs2.Err)
    65  	} else {
    66  		if len(rs2.Data.([]*model.Session)) != 3 {
    67  			t.Fatal("should match len")
    68  		}
    69  	}
    70  }
    71  
    72  func testSessionGetWithDeviceId(t *testing.T, ss store.Store) {
    73  	s1 := model.Session{}
    74  	s1.UserId = model.NewId()
    75  	s1.ExpiresAt = model.GetMillis() + 10000
    76  	store.Must(ss.Session().Save(&s1))
    77  
    78  	s2 := model.Session{}
    79  	s2.UserId = s1.UserId
    80  	s2.DeviceId = model.NewId()
    81  	s2.ExpiresAt = model.GetMillis() + 10000
    82  	store.Must(ss.Session().Save(&s2))
    83  
    84  	s3 := model.Session{}
    85  	s3.UserId = s1.UserId
    86  	s3.ExpiresAt = 1
    87  	s3.DeviceId = model.NewId()
    88  	store.Must(ss.Session().Save(&s3))
    89  
    90  	if rs1 := (<-ss.Session().GetSessionsWithActiveDeviceIds(s1.UserId)); rs1.Err != nil {
    91  		t.Fatal(rs1.Err)
    92  	} else {
    93  		if len(rs1.Data.([]*model.Session)) != 1 {
    94  			t.Fatal("should match len")
    95  		}
    96  	}
    97  }
    98  
    99  func testSessionRemove(t *testing.T, ss store.Store) {
   100  	s1 := model.Session{}
   101  	s1.UserId = model.NewId()
   102  	store.Must(ss.Session().Save(&s1))
   103  
   104  	if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
   105  		t.Fatal(rs1.Err)
   106  	} else {
   107  		if rs1.Data.(*model.Session).Id != s1.Id {
   108  			t.Fatal("should match")
   109  		}
   110  	}
   111  
   112  	store.Must(ss.Session().Remove(s1.Id))
   113  
   114  	if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
   115  		t.Fatal("should have been removed")
   116  	}
   117  }
   118  
   119  func testSessionRemoveAll(t *testing.T, ss store.Store) {
   120  	s1 := model.Session{}
   121  	s1.UserId = model.NewId()
   122  	store.Must(ss.Session().Save(&s1))
   123  
   124  	if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
   125  		t.Fatal(rs1.Err)
   126  	} else {
   127  		if rs1.Data.(*model.Session).Id != s1.Id {
   128  			t.Fatal("should match")
   129  		}
   130  	}
   131  
   132  	store.Must(ss.Session().RemoveAllSessions())
   133  
   134  	if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
   135  		t.Fatal("should have been removed")
   136  	}
   137  }
   138  
   139  func testSessionRemoveByUser(t *testing.T, ss store.Store) {
   140  	s1 := model.Session{}
   141  	s1.UserId = model.NewId()
   142  	store.Must(ss.Session().Save(&s1))
   143  
   144  	if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
   145  		t.Fatal(rs1.Err)
   146  	} else {
   147  		if rs1.Data.(*model.Session).Id != s1.Id {
   148  			t.Fatal("should match")
   149  		}
   150  	}
   151  
   152  	store.Must(ss.Session().PermanentDeleteSessionsByUser(s1.UserId))
   153  
   154  	if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
   155  		t.Fatal("should have been removed")
   156  	}
   157  }
   158  
   159  func testSessionRemoveToken(t *testing.T, ss store.Store) {
   160  	s1 := model.Session{}
   161  	s1.UserId = model.NewId()
   162  	store.Must(ss.Session().Save(&s1))
   163  
   164  	if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
   165  		t.Fatal(rs1.Err)
   166  	} else {
   167  		if rs1.Data.(*model.Session).Id != s1.Id {
   168  			t.Fatal("should match")
   169  		}
   170  	}
   171  
   172  	store.Must(ss.Session().Remove(s1.Token))
   173  
   174  	if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
   175  		t.Fatal("should have been removed")
   176  	}
   177  
   178  	if rs3 := (<-ss.Session().GetSessions(s1.UserId)); rs3.Err != nil {
   179  		t.Fatal(rs3.Err)
   180  	} else {
   181  		if len(rs3.Data.([]*model.Session)) != 0 {
   182  			t.Fatal("should match len")
   183  		}
   184  	}
   185  }
   186  
   187  func testSessionUpdateDeviceId(t *testing.T, ss store.Store) {
   188  	s1 := model.Session{}
   189  	s1.UserId = model.NewId()
   190  	store.Must(ss.Session().Save(&s1))
   191  
   192  	if rs1 := (<-ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs1.Err != nil {
   193  		t.Fatal(rs1.Err)
   194  	}
   195  
   196  	s2 := model.Session{}
   197  	s2.UserId = model.NewId()
   198  	store.Must(ss.Session().Save(&s2))
   199  
   200  	if rs2 := (<-ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs2.Err != nil {
   201  		t.Fatal(rs2.Err)
   202  	}
   203  }
   204  
   205  func testSessionUpdateDeviceId2(t *testing.T, ss store.Store) {
   206  	s1 := model.Session{}
   207  	s1.UserId = model.NewId()
   208  	store.Must(ss.Session().Save(&s1))
   209  
   210  	if rs1 := (<-ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)); rs1.Err != nil {
   211  		t.Fatal(rs1.Err)
   212  	}
   213  
   214  	s2 := model.Session{}
   215  	s2.UserId = model.NewId()
   216  	store.Must(ss.Session().Save(&s2))
   217  
   218  	if rs2 := (<-ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)); rs2.Err != nil {
   219  		t.Fatal(rs2.Err)
   220  	}
   221  }
   222  
   223  func testSessionStoreUpdateLastActivityAt(t *testing.T, ss store.Store) {
   224  	s1 := model.Session{}
   225  	s1.UserId = model.NewId()
   226  	store.Must(ss.Session().Save(&s1))
   227  
   228  	if err := (<-ss.Session().UpdateLastActivityAt(s1.Id, 1234567890)).Err; err != nil {
   229  		t.Fatal(err)
   230  	}
   231  
   232  	if r1 := <-ss.Session().Get(s1.Id); r1.Err != nil {
   233  		t.Fatal(r1.Err)
   234  	} else {
   235  		if r1.Data.(*model.Session).LastActivityAt != 1234567890 {
   236  			t.Fatal("LastActivityAt not updated correctly")
   237  		}
   238  	}
   239  
   240  }
   241  
   242  func testSessionCount(t *testing.T, ss store.Store) {
   243  	s1 := model.Session{}
   244  	s1.UserId = model.NewId()
   245  	s1.ExpiresAt = model.GetMillis() + 100000
   246  	store.Must(ss.Session().Save(&s1))
   247  
   248  	if r1 := <-ss.Session().AnalyticsSessionCount(); r1.Err != nil {
   249  		t.Fatal(r1.Err)
   250  	} else {
   251  		if r1.Data.(int64) == 0 {
   252  			t.Fatal("should have at least 1 session")
   253  		}
   254  	}
   255  }
   256  
   257  func testSessionCleanup(t *testing.T, ss store.Store) {
   258  	now := model.GetMillis()
   259  
   260  	s1 := model.Session{}
   261  	s1.UserId = model.NewId()
   262  	s1.ExpiresAt = 0 // never expires
   263  	store.Must(ss.Session().Save(&s1))
   264  
   265  	s2 := model.Session{}
   266  	s2.UserId = s1.UserId
   267  	s2.ExpiresAt = now + 1000000 // expires in the future
   268  	store.Must(ss.Session().Save(&s2))
   269  
   270  	s3 := model.Session{}
   271  	s3.UserId = model.NewId()
   272  	s3.ExpiresAt = 1 // expired
   273  	store.Must(ss.Session().Save(&s3))
   274  
   275  	s4 := model.Session{}
   276  	s4.UserId = model.NewId()
   277  	s4.ExpiresAt = 2 // expired
   278  	store.Must(ss.Session().Save(&s4))
   279  
   280  	ss.Session().Cleanup(now, 1)
   281  
   282  	err := (<-ss.Session().Get(s1.Id)).Err
   283  	assert.Nil(t, err)
   284  
   285  	err = (<-ss.Session().Get(s2.Id)).Err
   286  	assert.Nil(t, err)
   287  
   288  	err = (<-ss.Session().Get(s3.Id)).Err
   289  	assert.NotNil(t, err)
   290  
   291  	err = (<-ss.Session().Get(s4.Id)).Err
   292  	assert.NotNil(t, err)
   293  
   294  	store.Must(ss.Session().Remove(s1.Id))
   295  	store.Must(ss.Session().Remove(s2.Id))
   296  }