82 lines
2.7 KiB
Go
82 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/calendar"
|
|
)
|
|
|
|
func TestCalendarUpcomingDefaultsAndBounds(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
s.registerCalendarRoutes()
|
|
repo, e := s.calendars()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
owner, e := s.store.PrincipalForSession(domain, token)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
c, e := repo.Create(context.Background(), domain, owner.UUID, "Default", "default")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
other, e := repo.Create(context.Background(), domain, owner.UUID, "Other", "other")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = repo.SaveDefaults(context.Background(), domain, owner.UUID, []string{c.ID}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for i := 0; i < 25; i++ {
|
|
start := time.Now().UTC().Add(time.Duration(i+1) * time.Hour).Truncate(time.Second)
|
|
b, _ := json.Marshal(map[string]any{"summary": fmt.Sprintf("Event %02d", i), "start": start, "end": start.Add(time.Hour)})
|
|
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", string(b), ""); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
}
|
|
start := time.Now().UTC().Add(time.Hour).Truncate(time.Second)
|
|
b, _ := json.Marshal(map[string]any{"summary": "Not selected", "start": start, "end": start.Add(time.Hour)})
|
|
if w := calendarRequest(s, token, "POST", calendarAPI+"calendars/"+other.ID+"/events", string(b), ""); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
w := calendarRequest(s, token, "GET", calendarAPI+"upcoming", "", "")
|
|
var result struct {
|
|
Events []struct {
|
|
calendar.Event
|
|
Href string
|
|
}
|
|
}
|
|
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Events) != 20 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
if !strings.Contains(result.Events[0].Href, "calendar=default") || result.Events[0].Summary != "Event 00" || result.Events[19].Summary != "Event 19" {
|
|
t.Fatal(result.Events)
|
|
}
|
|
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
|
|
t.Fatal("private response cached")
|
|
}
|
|
w = calendarRequest(s, token, "GET", calendarAPI+"upcoming?period=day", "", "")
|
|
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
now := time.Now().In(s.getLocation(domain))
|
|
end := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, s.getLocation(domain))
|
|
for _, event := range result.Events {
|
|
if !event.Start.Before(end) {
|
|
t.Fatal("tomorrow event in today's widget", event.Start)
|
|
}
|
|
}
|
|
if w = calendarRequest(s, "", "GET", calendarAPI+"upcoming", "", ""); w.Code != 403 {
|
|
t.Fatal("anonymous access", w.Code)
|
|
}
|
|
if w = calendarRequest(s, token, "GET", calendarAPI+"upcoming?date=1900-01-01", "", ""); w.Code != 422 {
|
|
t.Fatal("unbounded dates", w.Code)
|
|
}
|
|
}
|