2019-12-28 09:48:36 +01:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-04-20 13:50:28 +02:00
|
|
|
"time"
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 01:20:58 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-28 09:48:36 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-04-20 13:50:28 +02:00
|
|
|
func TestSession_Create(t *testing.T) {
|
|
|
|
s := New(time.Hour, "testdata")
|
2020-06-25 01:20:58 +02:00
|
|
|
|
|
|
|
data := Data{
|
|
|
|
User: entity.Admin,
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
id := s.Create(data)
|
|
|
|
t.Logf("id: %s", id)
|
|
|
|
assert.Equal(t, 48, len(id))
|
2019-12-28 09:48:36 +01:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:40:09 +02:00
|
|
|
func TestSession_Update(t *testing.T) {
|
|
|
|
s := New(time.Hour, "testdata")
|
|
|
|
|
|
|
|
data := Data{
|
2020-06-25 01:20:58 +02:00
|
|
|
User: entity.Admin,
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
id := NewID()
|
|
|
|
assert.Equal(t, 48, len(id))
|
2020-06-24 17:40:09 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if result := s.Get(id); result.Valid() {
|
|
|
|
t.Fatalf("session %s should not exist", id)
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if err := s.Update(id, data); err == nil {
|
|
|
|
t.Fatalf("update should fail for unknown session id %s", id)
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
newId := s.Create(data)
|
|
|
|
assert.Equal(t, 48, len(newId))
|
2020-06-24 17:40:09 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
cachedData := s.Get(newId)
|
2020-06-24 17:40:09 +02:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if cachedData.Invalid() {
|
|
|
|
t.Fatalf("session %s should exist", newId)
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
assert.Equal(t, cachedData, data)
|
2020-06-24 17:40:09 +02:00
|
|
|
|
|
|
|
newData := Data{
|
2020-06-26 14:26:36 +02:00
|
|
|
User: entity.Guest,
|
2020-06-25 14:54:04 +02:00
|
|
|
Shares: UIDs{"a000000000000001"},
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if err := s.Update(newId, newData); err != nil {
|
2020-06-24 17:40:09 +02:00
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if cachedData := s.Get(newId); cachedData.Invalid() {
|
|
|
|
t.Fatalf("session %s should be valid", newId)
|
2020-06-24 17:40:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:50:28 +02:00
|
|
|
func TestSession_Delete(t *testing.T) {
|
|
|
|
s := New(time.Hour, "testdata")
|
|
|
|
s.Delete("abc")
|
2019-12-28 09:48:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:50:28 +02:00
|
|
|
func TestSession_Get(t *testing.T) {
|
|
|
|
s := New(time.Hour, "testdata")
|
2020-06-25 01:20:58 +02:00
|
|
|
data := Data{
|
2020-06-26 14:26:36 +02:00
|
|
|
User: entity.Guest,
|
2020-06-25 14:54:04 +02:00
|
|
|
Shares: UIDs{"a000000000000001"},
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
id := s.Create(data)
|
|
|
|
t.Logf("id: %s", id)
|
|
|
|
assert.Equal(t, 48, len(id))
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
cachedData := s.Get(id)
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if cachedData.Invalid() {
|
|
|
|
t.Fatal("cachedData should be valid")
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
assert.Equal(t, data, cachedData)
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
s.Delete(id)
|
2019-12-28 09:48:36 +01:00
|
|
|
|
2020-06-25 14:54:04 +02:00
|
|
|
if sess := s.Get(id); sess.Valid() {
|
|
|
|
t.Fatal("session should be invalid")
|
2020-06-25 01:20:58 +02:00
|
|
|
}
|
2019-12-28 09:48:36 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 13:50:28 +02:00
|
|
|
func TestSession_Exists(t *testing.T) {
|
|
|
|
s := New(time.Hour, "testdata")
|
|
|
|
assert.False(t, s.Exists("xyz"))
|
2020-06-25 01:20:58 +02:00
|
|
|
data := Data{
|
|
|
|
User: entity.Guest,
|
|
|
|
}
|
2020-06-25 14:54:04 +02:00
|
|
|
id := s.Create(data)
|
|
|
|
t.Logf("id: %s", id)
|
|
|
|
assert.Equal(t, 48, len(id))
|
|
|
|
assert.True(t, s.Exists(id))
|
|
|
|
s.Delete(id)
|
|
|
|
assert.False(t, s.Exists(id))
|
2019-12-28 09:48:36 +01:00
|
|
|
}
|