2021-04-25 21:53:17 +03:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-04-03 15:40:57 +03:00
|
|
|
|
|
|
|
conf "github.com/muety/wakapi/config"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-04-25 21:53:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUser_TZ(t *testing.T) {
|
|
|
|
sut1, sut2 := &User{Location: ""}, &User{Location: "America/Los_Angeles"}
|
|
|
|
pst, _ := time.LoadLocation("America/Los_Angeles")
|
2021-11-28 14:40:46 +03:00
|
|
|
_, offset1 := time.Now().Zone()
|
|
|
|
_, offset2 := time.Now().In(pst).Zone()
|
2021-04-25 21:53:17 +03:00
|
|
|
|
|
|
|
assert.Equal(t, time.Local, sut1.TZ())
|
|
|
|
assert.Equal(t, pst, sut2.TZ())
|
|
|
|
|
2021-11-28 14:40:46 +03:00
|
|
|
assert.InDelta(t, time.Duration(offset1*int(time.Second)), sut1.TZOffset(), float64(1*time.Second))
|
|
|
|
assert.InDelta(t, time.Duration(offset2*int(time.Second)), sut2.TZOffset(), float64(1*time.Second))
|
2021-04-25 21:53:17 +03:00
|
|
|
}
|
2022-12-29 14:06:54 +03:00
|
|
|
|
|
|
|
func TestUser_MinDataAge(t *testing.T) {
|
2023-04-03 15:40:57 +03:00
|
|
|
c := conf.Load("", "")
|
2022-12-29 14:06:54 +03:00
|
|
|
|
|
|
|
var sut *User
|
|
|
|
|
|
|
|
// test with unlimited retention time / clean-up disabled
|
|
|
|
c.App.DataRetentionMonths = -1
|
|
|
|
c.Subscriptions.Enabled = false
|
|
|
|
sut = &User{}
|
|
|
|
assert.Zero(t, sut.MinDataAge())
|
|
|
|
|
|
|
|
// test with limited retention time / clean-up enabled, and subscriptions disabled
|
|
|
|
c.App.DataRetentionMonths = 1
|
|
|
|
c.Subscriptions.Enabled = false
|
|
|
|
sut = &User{}
|
|
|
|
assert.WithinRange(t, sut.MinDataAge(), time.Now().AddDate(0, -1, -1), time.Now().AddDate(0, -1, 1))
|
|
|
|
|
|
|
|
// test with limited retention time, subscriptions enabled, and user hasn't got one
|
|
|
|
c.App.DataRetentionMonths = 1
|
|
|
|
c.Subscriptions.Enabled = true
|
|
|
|
sut = &User{}
|
|
|
|
assert.WithinRange(t, sut.MinDataAge(), time.Now().AddDate(0, -1, -1), time.Now().AddDate(0, -1, 1))
|
|
|
|
|
|
|
|
// test with limited retention time, subscriptions disabled, but user still got (an expired) one
|
|
|
|
c.App.DataRetentionMonths = 1
|
|
|
|
c.Subscriptions.Enabled = false
|
|
|
|
until2 := CustomTime(time.Now().AddDate(0, 0, -1))
|
|
|
|
sut = &User{SubscribedUntil: &until2}
|
|
|
|
assert.WithinRange(t, sut.MinDataAge(), time.Now().AddDate(0, -1, -1), time.Now().AddDate(0, -1, 1))
|
|
|
|
|
|
|
|
// test with limited retention time, subscriptions enabled, and user has got one
|
|
|
|
c.App.DataRetentionMonths = 1
|
|
|
|
c.Subscriptions.Enabled = true
|
|
|
|
until1 := CustomTime(time.Now().AddDate(0, 1, 0))
|
|
|
|
sut = &User{SubscribedUntil: &until1}
|
|
|
|
assert.Zero(t, sut.MinDataAge())
|
|
|
|
}
|