2022-09-28 09:01:17 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewSession(t *testing.T) {
|
|
|
|
t.Run("NoSessionData", func(t *testing.T) {
|
2022-10-03 22:59:29 +02:00
|
|
|
m := NewSession(UnixDay, UnixHour*6)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
assert.True(t, rnd.IsSessionID(m.ID))
|
|
|
|
assert.False(t, m.CreatedAt.IsZero())
|
|
|
|
assert.False(t, m.UpdatedAt.IsZero())
|
2022-10-02 11:38:30 +02:00
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
2022-09-28 09:01:17 +02:00
|
|
|
assert.NotEmpty(t, m.ID)
|
|
|
|
assert.NotNil(t, m.Data())
|
|
|
|
assert.Equal(t, 0, len(m.Data().Tokens))
|
|
|
|
})
|
|
|
|
t.Run("EmptySessionData", func(t *testing.T) {
|
2022-10-03 22:59:29 +02:00
|
|
|
m := NewSession(UnixDay, UnixHour*6)
|
2022-09-28 09:01:17 +02:00
|
|
|
m.SetData(NewSessionData())
|
|
|
|
|
|
|
|
assert.True(t, rnd.IsSessionID(m.ID))
|
|
|
|
assert.False(t, m.CreatedAt.IsZero())
|
|
|
|
assert.False(t, m.UpdatedAt.IsZero())
|
2022-10-02 11:38:30 +02:00
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
2022-09-28 09:01:17 +02:00
|
|
|
assert.NotEmpty(t, m.ID)
|
|
|
|
assert.NotNil(t, m.Data())
|
|
|
|
assert.Equal(t, 0, len(m.Data().Tokens))
|
|
|
|
})
|
|
|
|
t.Run("WithSessionData", func(t *testing.T) {
|
|
|
|
data := NewSessionData()
|
|
|
|
data.Tokens = []string{"foo", "bar"}
|
2022-10-03 22:59:29 +02:00
|
|
|
m := NewSession(UnixDay, UnixHour*6)
|
2022-09-28 09:01:17 +02:00
|
|
|
m.SetData(data)
|
|
|
|
|
|
|
|
assert.True(t, rnd.IsSessionID(m.ID))
|
|
|
|
assert.False(t, m.CreatedAt.IsZero())
|
|
|
|
assert.False(t, m.UpdatedAt.IsZero())
|
2022-10-02 11:38:30 +02:00
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
2022-09-28 09:01:17 +02:00
|
|
|
assert.NotEmpty(t, m.ID)
|
|
|
|
assert.NotNil(t, m.Data())
|
|
|
|
assert.Len(t, m.Data().Tokens, 2)
|
|
|
|
assert.Equal(t, "foo", m.Data().Tokens[0])
|
|
|
|
assert.Equal(t, "bar", m.Data().Tokens[1])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_SetData(t *testing.T) {
|
|
|
|
t.Run("Nil", func(t *testing.T) {
|
2022-10-03 22:59:29 +02:00
|
|
|
m := NewSession(UnixDay, UnixHour*6)
|
2022-09-28 09:01:17 +02:00
|
|
|
|
|
|
|
assert.NotNil(t, m)
|
|
|
|
|
|
|
|
sess := m.SetData(nil)
|
|
|
|
|
|
|
|
assert.NotNil(t, sess)
|
|
|
|
assert.NotEmpty(t, sess.ID)
|
|
|
|
assert.Equal(t, sess.ID, m.ID)
|
|
|
|
})
|
|
|
|
}
|
2022-10-03 22:59:29 +02:00
|
|
|
|
|
|
|
func TestSession_TimedOut(t *testing.T) {
|
|
|
|
t.Run("NewSession", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.Equal(t, m.ExpiresAt(), m.TimeoutAt())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
t.Run("NoExpiration", func(t *testing.T) {
|
|
|
|
m := NewSession(0, UnixHour)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
assert.True(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.Equal(t, m.ExpiresAt(), m.TimeoutAt())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
assert.True(t, m.ExpiresAt().IsZero())
|
|
|
|
})
|
|
|
|
t.Run("NoTimeout", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, 0)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.Equal(t, m.ExpiresAt(), m.TimeoutAt())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
|
|
|
})
|
|
|
|
t.Run("TimedOut", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
utc := UnixTime()
|
|
|
|
|
|
|
|
m.LastActive = utc - (UnixHour + 1)
|
|
|
|
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.True(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
t.Run("NotTimedOut", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
utc := UnixTime()
|
|
|
|
|
|
|
|
m.LastActive = utc - (UnixHour - 10)
|
|
|
|
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSession_Expired(t *testing.T) {
|
|
|
|
t.Run("NewSession", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
|
|
|
assert.False(t, m.Expired())
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
t.Run("NoExpiration", func(t *testing.T) {
|
|
|
|
m := NewSession(0, 0)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
assert.True(t, m.ExpiresAt().IsZero())
|
|
|
|
assert.False(t, m.Expired())
|
|
|
|
assert.True(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
t.Run("NoExpiration", func(t *testing.T) {
|
|
|
|
m := NewSession(0, 0)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
assert.True(t, m.ExpiresAt().IsZero())
|
|
|
|
assert.False(t, m.Expired())
|
|
|
|
assert.True(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
})
|
|
|
|
t.Run("Expired", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
t.Logf("Timeout: %s, Expiration: %s", m.TimeoutAt().String(), m.ExpiresAt())
|
|
|
|
utc := UnixTime()
|
|
|
|
|
|
|
|
m.SessExpires = utc - 10
|
|
|
|
|
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
|
|
|
assert.True(t, m.Expired())
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.True(t, m.TimedOut())
|
|
|
|
assert.Equal(t, m.ExpiresAt(), m.TimeoutAt())
|
|
|
|
})
|
|
|
|
t.Run("NotExpired", func(t *testing.T) {
|
|
|
|
m := NewSession(UnixDay, UnixHour)
|
|
|
|
utc := UnixTime()
|
|
|
|
|
|
|
|
m.SessExpires = utc + 10
|
|
|
|
|
|
|
|
assert.False(t, m.ExpiresAt().IsZero())
|
|
|
|
assert.False(t, m.Expired())
|
|
|
|
assert.False(t, m.TimeoutAt().IsZero())
|
|
|
|
assert.False(t, m.TimedOut())
|
|
|
|
assert.Equal(t, m.ExpiresAt(), m.TimeoutAt())
|
|
|
|
})
|
|
|
|
}
|