test: cover the ICS writer and the calendar feed routes

This commit is contained in:
Jakub Zych
2026-10-02 17:38:48 +02:00
parent 90247a460d
commit b39f627fe3
6 changed files with 665 additions and 5 deletions

View File

@@ -73,11 +73,11 @@ func TestPluginActivates(t *testing.T) {
if p.ID() != "jz.bm" || !slices.Equal(p.Requires(), []string{"golem15.user"}) {
t.Errorf("ID %q Requires %v", p.ID(), p.Requires())
}
if n := len(p.Models()); n != 9 {
t.Errorf("Models = %d, want 9", n)
if n := len(p.Models()); n != 10 {
t.Errorf("Models = %d, want 10", n)
}
if n := len(p.Migrations()); n != 10 {
t.Errorf("Migrations = %d, want 10", n)
if n := len(p.Migrations()); n != 11 {
t.Errorf("Migrations = %d, want 11", n)
}
if !slices.ContainsFunc(p.Commands(), func(c bonfire.Command) bool { return c.Name == "bm:seed" }) {
t.Error("bm:seed is not registered")
@@ -88,6 +88,9 @@ func TestPluginActivates(t *testing.T) {
if got := app.Config.Int("jz.bm.dashboard_limit"); got != 5 {
t.Errorf("jz.bm.dashboard_limit = %d, want the default 5", got)
}
if got := app.Config.String("jz.bm.public_url"); got != "" {
t.Errorf("jz.bm.public_url = %q, want the default empty", got)
}
}
func TestRoutes(t *testing.T) {
@@ -109,8 +112,13 @@ func TestRoutes(t *testing.T) {
"DELETE /_bm/api/v1/readings/{id}/progress",
"GET /_bm/api/v1/materials",
"GET /_bm/api/v1/materials/{id}",
"GET /_bm/api/v1/calendar-feed",
"PUT /_bm/api/v1/calendar-feed",
"POST /_bm/api/v1/calendar-feed/rotate",
"DELETE /_bm/api/v1/calendar-feed",
}
var got []string
sawFeed := false
for _, rt := range router.Routes() {
if rt.PluginID != "jz.bm" {
continue
@@ -122,11 +130,24 @@ func TestRoutes(t *testing.T) {
}
continue
}
if rt.Method+" "+rt.Pattern == "GET /_bm/calendar/{token}" {
// The calendar feed: public (the token is the credential) but
// throttled per IP and per token.
want := []string{"throttle:jz.bm.calendar-ip", "throttle:jz.bm.calendar-token"}
if !slices.Equal(rt.Middleware, want) {
t.Errorf("the calendar feed has middleware %v, want %v", rt.Middleware, want)
}
sawFeed = true
continue
}
got = append(got, rt.Method+" "+rt.Pattern)
if !slices.Contains(rt.Middleware, "jwt.auth") {
t.Errorf("%s %s is not behind jwt.auth: %v", rt.Method, rt.Pattern, rt.Middleware)
}
}
if !sawFeed {
t.Error("GET /_bm/calendar/{token} is not registered")
}
if !slices.Equal(got, want) {
t.Errorf("routes =\n%v\nwant\n%v", got, want)
}