github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+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, copySession.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 } 66 67 func TestSessionCSRF(t *testing.T) { 68 s := Session{} 69 token := s.GetCSRF() 70 assert.Empty(t, token) 71 72 token = s.GenerateCSRF() 73 assert.NotEmpty(t, token) 74 75 token2 := s.GetCSRF() 76 assert.NotEmpty(t, token2) 77 assert.Equal(t, token, token2) 78 }