mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
* Add new session prop for oauth * Make it isOAuthUser to differentiate better * Fix up caps * Fix tests * Add tests for IsOAuthUser
96 lines
2.5 KiB
Go
96 lines
2.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestSessionDeepCopy(t *testing.T) {
|
|
sessionId := NewId()
|
|
userId := NewId()
|
|
mapKey := "key"
|
|
mapValue := "val"
|
|
|
|
session := &Session{Id: sessionId, Props: map[string]string{mapKey: mapValue}, TeamMembers: []*TeamMember{{UserId: userId, TeamId: "someteamId"}}}
|
|
|
|
copySession := session.DeepCopy()
|
|
copySession.Id = "changed"
|
|
copySession.Props[mapKey] = "changed"
|
|
copySession.TeamMembers[0].UserId = "changed"
|
|
|
|
assert.Equal(t, sessionId, session.Id)
|
|
assert.Equal(t, mapValue, session.Props[mapKey])
|
|
assert.Equal(t, userId, session.TeamMembers[0].UserId)
|
|
|
|
session = &Session{Id: sessionId}
|
|
copySession = session.DeepCopy()
|
|
|
|
assert.Equal(t, sessionId, copySession.Id)
|
|
|
|
session = &Session{TeamMembers: []*TeamMember{}}
|
|
copySession = session.DeepCopy()
|
|
|
|
assert.Equal(t, 0, len(copySession.TeamMembers))
|
|
}
|
|
|
|
func TestSessionJson(t *testing.T) {
|
|
session := Session{}
|
|
session.PreSave()
|
|
json := session.ToJson()
|
|
rsession := SessionFromJson(strings.NewReader(json))
|
|
|
|
require.Equal(t, rsession.Id, session.Id, "Ids do not match")
|
|
|
|
session.Sanitize()
|
|
|
|
require.False(t, session.IsExpired(), "Shouldn't expire")
|
|
|
|
session.ExpiresAt = GetMillis()
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
require.True(t, session.IsExpired(), "Should expire")
|
|
|
|
session.SetExpireInDays(10)
|
|
}
|
|
|
|
func TestSessionCSRF(t *testing.T) {
|
|
s := Session{}
|
|
token := s.GetCSRF()
|
|
assert.Empty(t, token)
|
|
|
|
token = s.GenerateCSRF()
|
|
assert.NotEmpty(t, token)
|
|
|
|
token2 := s.GetCSRF()
|
|
assert.NotEmpty(t, token2)
|
|
assert.Equal(t, token, token2)
|
|
}
|
|
|
|
func TestSessionIsOAuthUser(t *testing.T) {
|
|
testCases := []struct {
|
|
Description string
|
|
Session Session
|
|
isOAuthUser bool
|
|
}{
|
|
{"False on empty props", Session{}, false},
|
|
{"True when key is set to true", Session{Props: StringMap{USER_AUTH_SERVICE_IS_OAUTH: strconv.FormatBool(true)}}, true},
|
|
{"False when key is set to false", Session{Props: StringMap{USER_AUTH_SERVICE_IS_OAUTH: strconv.FormatBool(false)}}, false},
|
|
{"Not affected by Session.IsOauth being true", Session{IsOAuth: true}, false},
|
|
{"Not affected by Session.IsOauth being false", Session{IsOAuth: false, Props: StringMap{USER_AUTH_SERVICE_IS_OAUTH: strconv.FormatBool(true)}}, true},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.Description, func(t *testing.T) {
|
|
require.Equal(t, tc.isOAuthUser, tc.Session.IsOAuthUser())
|
|
})
|
|
}
|
|
}
|