Files
mattermost/store/sqlstore/session_store_test.go

259 lines
5.6 KiB
Go
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
package sqlstore
2015-06-14 23:53:32 -08:00
import (
"testing"
2017-09-06 17:12:54 -05:00
2017-09-06 23:05:10 -07:00
"github.com/mattermost/mattermost-server/model"
2017-10-06 08:12:10 -07:00
"github.com/mattermost/mattermost-server/store"
2015-06-14 23:53:32 -08:00
)
func TestSessionStoreSave(t *testing.T) {
ss := Setup()
2015-06-14 23:53:32 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
if err := (<-ss.Session().Save(&s1)).Err; err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(err)
}
}
func TestSessionGet(t *testing.T) {
ss := Setup()
2015-06-14 23:53:32 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
2015-06-14 23:53:32 -08:00
s2 := model.Session{}
s2.UserId = s1.UserId
store.Must(ss.Session().Save(&s2))
2015-06-14 23:53:32 -08:00
s3 := model.Session{}
s3.UserId = s1.UserId
s3.ExpiresAt = 1
store.Must(ss.Session().Save(&s3))
2015-06-14 23:53:32 -08:00
if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
t.Fatal("should match")
}
}
if rs2 := (<-ss.Session().GetSessions(s1.UserId)); rs2.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(rs2.Err)
} else {
if len(rs2.Data.([]*model.Session)) != 2 {
t.Fatal("should match len")
}
}
}
func TestSessionGetWithDeviceId(t *testing.T) {
ss := Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
s1.ExpiresAt = model.GetMillis() + 10000
store.Must(ss.Session().Save(&s1))
s2 := model.Session{}
s2.UserId = s1.UserId
s2.DeviceId = model.NewId()
s2.ExpiresAt = model.GetMillis() + 10000
store.Must(ss.Session().Save(&s2))
2015-06-14 23:53:32 -08:00
s3 := model.Session{}
s3.UserId = s1.UserId
s3.ExpiresAt = 1
s3.DeviceId = model.NewId()
store.Must(ss.Session().Save(&s3))
if rs1 := (<-ss.Session().GetSessionsWithActiveDeviceIds(s1.UserId)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if len(rs1.Data.([]*model.Session)) != 1 {
t.Fatal("should match len")
}
}
2015-06-14 23:53:32 -08:00
}
func TestSessionRemove(t *testing.T) {
ss := Setup()
2015-06-14 23:53:32 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
2015-06-14 23:53:32 -08:00
if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
t.Fatal("should match")
}
}
store.Must(ss.Session().Remove(s1.Id))
2015-06-14 23:53:32 -08:00
if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
2015-06-14 23:53:32 -08:00
t.Fatal("should have been removed")
}
}
func TestSessionRemoveAll(t *testing.T) {
ss := Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
t.Fatal("should match")
}
}
store.Must(ss.Session().RemoveAllSessions())
if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
t.Fatal("should have been removed")
}
}
2015-11-15 18:18:02 -08:00
func TestSessionRemoveByUser(t *testing.T) {
ss := Setup()
2015-11-15 18:18:02 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
2015-11-15 18:18:02 -08:00
if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
2015-11-15 18:18:02 -08:00
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
t.Fatal("should match")
}
}
store.Must(ss.Session().PermanentDeleteSessionsByUser(s1.UserId))
2015-11-15 18:18:02 -08:00
if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
2015-11-15 18:18:02 -08:00
t.Fatal("should have been removed")
}
}
func TestSessionRemoveToken(t *testing.T) {
ss := Setup()
2015-06-14 23:53:32 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
2015-06-14 23:53:32 -08:00
if rs1 := (<-ss.Session().Get(s1.Id)); rs1.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(rs1.Err)
} else {
if rs1.Data.(*model.Session).Id != s1.Id {
t.Fatal("should match")
}
}
store.Must(ss.Session().Remove(s1.Token))
2015-06-14 23:53:32 -08:00
if rs2 := (<-ss.Session().Get(s1.Id)); rs2.Err == nil {
2015-06-14 23:53:32 -08:00
t.Fatal("should have been removed")
}
if rs3 := (<-ss.Session().GetSessions(s1.UserId)); rs3.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(rs3.Err)
} else {
if len(rs3.Data.([]*model.Session)) != 0 {
t.Fatal("should match len")
}
}
}
func TestSessionUpdateDeviceId(t *testing.T) {
ss := Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
if rs1 := (<-ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs1.Err != nil {
t.Fatal(rs1.Err)
}
s2 := model.Session{}
s2.UserId = model.NewId()
store.Must(ss.Session().Save(&s2))
if rs2 := (<-ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE+":1234567890", s1.ExpiresAt)); rs2.Err != nil {
t.Fatal(rs2.Err)
}
}
func TestSessionUpdateDeviceId2(t *testing.T) {
ss := Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
if rs1 := (<-ss.Session().UpdateDeviceId(s1.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)); rs1.Err != nil {
t.Fatal(rs1.Err)
}
s2 := model.Session{}
s2.UserId = model.NewId()
store.Must(ss.Session().Save(&s2))
if rs2 := (<-ss.Session().UpdateDeviceId(s2.Id, model.PUSH_NOTIFY_APPLE_REACT_NATIVE+":1234567890", s1.ExpiresAt)); rs2.Err != nil {
t.Fatal(rs2.Err)
}
}
2015-06-14 23:53:32 -08:00
func TestSessionStoreUpdateLastActivityAt(t *testing.T) {
ss := Setup()
2015-06-14 23:53:32 -08:00
s1 := model.Session{}
s1.UserId = model.NewId()
store.Must(ss.Session().Save(&s1))
2015-06-14 23:53:32 -08:00
if err := (<-ss.Session().UpdateLastActivityAt(s1.Id, 1234567890)).Err; err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(err)
}
if r1 := <-ss.Session().Get(s1.Id); r1.Err != nil {
2015-06-14 23:53:32 -08:00
t.Fatal(r1.Err)
} else {
if r1.Data.(*model.Session).LastActivityAt != 1234567890 {
t.Fatal("LastActivityAt not updated correctly")
}
}
}
func TestSessionCount(t *testing.T) {
ss := Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
2016-02-29 08:08:11 -05:00
s1.ExpiresAt = model.GetMillis() + 100000
store.Must(ss.Session().Save(&s1))
if r1 := <-ss.Session().AnalyticsSessionCount(); r1.Err != nil {
t.Fatal(r1.Err)
} else {
if r1.Data.(int64) == 0 {
t.Fatal("should have at least 1 session")
}
}
}