You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.4 KiB
73 lines
1.4 KiB
package models_test
|
|
|
|
import (
|
|
"context"
|
|
"git.aiterp.net/lucifer/lucifer/models"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSession_GenerateID_Works(t *testing.T) {
|
|
session := &models.Session{}
|
|
|
|
err := session.GenerateID()
|
|
|
|
assert.Nil(t, err)
|
|
}
|
|
|
|
func TestSession_Cookie_IsCorrect(t *testing.T) {
|
|
sessID := "testId"
|
|
expiry := time.Now().AddDate(0, 0, 2)
|
|
|
|
session := &models.Session{
|
|
ID: sessID,
|
|
UserID: 0,
|
|
Expires: expiry,
|
|
}
|
|
|
|
cookie := session.Cookie()
|
|
|
|
assert.Equal(t, sessID, cookie.Value)
|
|
assert.Equal(t, expiry, cookie.Expires)
|
|
}
|
|
|
|
func TestSession_Expired_BeforeExpiry(t *testing.T) {
|
|
session := &models.Session{
|
|
ID: "12345",
|
|
UserID: 0,
|
|
Expires: time.Now().AddDate(0, 0, 1),
|
|
}
|
|
|
|
assert.False(t, session.Expired())
|
|
}
|
|
|
|
func TestSession_Expired_AfterExpiry(t *testing.T) {
|
|
session := &models.Session{
|
|
ID: "12345",
|
|
UserID: 0,
|
|
Expires: time.Now().AddDate(0, 0, -1),
|
|
}
|
|
|
|
assert.True(t, session.Expired())
|
|
}
|
|
|
|
func TestSessionFromContext_InContext(t *testing.T) {
|
|
session := &models.Session{
|
|
ID: "12345",
|
|
UserID: 0,
|
|
Expires: time.Now().AddDate(0, 0, 1),
|
|
}
|
|
|
|
ctx := session.InContext(context.Background())
|
|
|
|
session2 := models.SessionFromContext(ctx)
|
|
|
|
assert.Equal(t, session2, session)
|
|
}
|
|
|
|
func TestSessionFromContext_Empty(t *testing.T) {
|
|
session := models.SessionFromContext(context.Background())
|
|
|
|
assert.Nil(t, session)
|
|
}
|