github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/model/session_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "strings" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestSessionDeepCopy(t *testing.T) { 15 sessionId := NewId() 16 userId := NewId() 17 mapKey := "key" 18 mapValue := "val" 19 20 session := &Session{Id: sessionId, Props: map[string]string{mapKey: mapValue}, TeamMembers: []*TeamMember{&TeamMember{UserId: userId, TeamId: "someteamId"}}} 21 22 copySession := session.DeepCopy() 23 copySession.Id = "changed" 24 copySession.Props[mapKey] = "changed" 25 copySession.TeamMembers[0].UserId = "changed" 26 27 assert.Equal(t, sessionId, session.Id) 28 assert.Equal(t, mapValue, session.Props[mapKey]) 29 assert.Equal(t, userId, session.TeamMembers[0].UserId) 30 31 session = &Session{Id: sessionId} 32 copySession = session.DeepCopy() 33 34 assert.Equal(t, sessionId, session.Id) 35 36 session = &Session{TeamMembers: []*TeamMember{}} 37 copySession = session.DeepCopy() 38 39 assert.Equal(t, 0, len(copySession.TeamMembers)) 40 } 41 42 func TestSessionJson(t *testing.T) { 43 session := Session{} 44 session.PreSave() 45 json := session.ToJson() 46 rsession := SessionFromJson(strings.NewReader(json)) 47 48 if rsession.Id != session.Id { 49 t.Fatal("Ids do not match") 50 } 51 52 session.Sanitize() 53 54 if session.IsExpired() { 55 t.Fatal("Shouldn't expire") 56 } 57 58 session.ExpiresAt = GetMillis() 59 time.Sleep(10 * time.Millisecond) 60 if !session.IsExpired() { 61 t.Fatal("Should expire") 62 } 63 64 session.SetExpireInDays(10) 65 }