forked from baron/baron-sso
81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSharedLinkBeforeCreate(t *testing.T) {
|
|
t.Run("generates id and token when missing", func(t *testing.T) {
|
|
link := SharedLink{}
|
|
|
|
if err := link.BeforeCreate(nil); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if link.ID == "" {
|
|
t.Fatalf("expected generated id")
|
|
}
|
|
if len(link.Token) != 64 {
|
|
t.Fatalf("expected 64-character token, got %q", link.Token)
|
|
}
|
|
if _, err := hex.DecodeString(link.Token); err != nil {
|
|
t.Fatalf("expected hex token: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("preserves existing id and token", func(t *testing.T) {
|
|
link := SharedLink{
|
|
ID: "existing-id",
|
|
Token: "existing-token",
|
|
}
|
|
|
|
if err := link.BeforeCreate(nil); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if link.ID != "existing-id" || link.Token != "existing-token" {
|
|
t.Fatalf("expected existing fields to be preserved: %#v", link)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSharedLinkIsValid(t *testing.T) {
|
|
future := time.Now().Add(time.Hour)
|
|
past := time.Now().Add(-time.Hour)
|
|
|
|
tests := []struct {
|
|
name string
|
|
link SharedLink
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "active link without expiration is valid",
|
|
link: SharedLink{IsActive: true},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "active link with future expiration is valid",
|
|
link: SharedLink{IsActive: true, ExpiresAt: &future},
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "inactive link is invalid",
|
|
link: SharedLink{IsActive: false, ExpiresAt: &future},
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "expired link is invalid",
|
|
link: SharedLink{IsActive: true, ExpiresAt: &past},
|
|
expected: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := tc.link.IsValid(); got != tc.expected {
|
|
t.Fatalf("unexpected validity: got=%v expected=%v", got, tc.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|