2020-05-08 11:46:09 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-08 11:46:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLink(t *testing.T) {
|
2020-06-22 15:16:26 +02:00
|
|
|
link := NewLink("st9lxuqxpogaaba1", true, false)
|
|
|
|
assert.Equal(t, "st9lxuqxpogaaba1", link.ShareUID)
|
2020-05-08 11:46:09 +02:00
|
|
|
assert.Equal(t, false, link.CanEdit)
|
|
|
|
assert.Equal(t, true, link.CanComment)
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.Equal(t, 10, len(link.LinkToken))
|
2020-06-22 15:16:26 +02:00
|
|
|
assert.Equal(t, 16, len(link.LinkUID))
|
2020-05-08 11:46:09 +02:00
|
|
|
}
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
func TestLink_Expired(t *testing.T) {
|
|
|
|
const oneDay = 60 * 60 * 24
|
|
|
|
|
|
|
|
link := NewLink("st9lxuqxpogaaba1", true, false)
|
|
|
|
|
2020-06-26 14:26:36 +02:00
|
|
|
link.ModifiedAt = Timestamp().Add(-7 * Day)
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = 0
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
assert.False(t, link.Expired())
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = oneDay
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
assert.False(t, link.Expired())
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = oneDay * 8
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
assert.True(t, link.Expired())
|
2020-06-26 12:24:05 +02:00
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
link.LinkExpires = oneDay
|
|
|
|
link.LinkViews = 9
|
2020-06-26 12:24:05 +02:00
|
|
|
link.MaxViews = 10
|
|
|
|
|
|
|
|
assert.False(t, link.Expired())
|
|
|
|
|
|
|
|
link.Redeem()
|
|
|
|
|
|
|
|
assert.True(t, link.Expired())
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLink_Redeem(t *testing.T) {
|
|
|
|
link := NewLink(rnd.PPID('a'), false, false)
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.Equal(t, uint(0), link.LinkViews)
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
link.Redeem()
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.Equal(t, uint(1), link.LinkViews)
|
2020-06-26 12:16:13 +02:00
|
|
|
|
|
|
|
if err := link.Save(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
link.Redeem()
|
|
|
|
|
2020-06-28 14:52:26 +02:00
|
|
|
assert.Equal(t, uint(2), link.LinkViews)
|
2020-06-26 12:16:13 +02:00
|
|
|
}
|