docs(cooler,diary,festival,fetchguard): compile README Go examples via src= (quick-261118-qsd)
- cooler: ExampleFromApp and the articlesController purge declarations; AllowExport run by TestDocsAllowExport on miniredis - diary: blogLog and ExampleFor; the generated main's run function, run by TestDocsRun in a temp directory with diary.Reset on cleanup - festival: PostPublished/ExampleBus_Fire and PostFormExtended/Collected/ ExampleBus_Collect, with a TestDocsDeclarations for Collected - fetchguard: fetch region run by TestDocsFetchImage (loopback refused as private_ip), client region run by TestDocsSyncItems on a stub transport
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
@@ -15,6 +16,8 @@ import (
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
"git.golem15.com/golem15/summercms/modules/cooler"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
"git.golem15.com/golem15/summercms/modules/surf"
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
)
|
||||
|
||||
func ExampleFromApp() {
|
||||
@@ -157,3 +160,42 @@ func ExampleSiteCacheFromApp() {
|
||||
}
|
||||
// Output: POST /api/__site__/cache/purge {"kind":"article","slug":"hello-summer"}
|
||||
}
|
||||
|
||||
// AllowExport admits five exports a minute per user across all instances.
|
||||
func AllowExport(ctx context.Context, app *backpack.App, fallback cooler.Attempter, userKey string) (bool, error) {
|
||||
client, err := cooler.OpenRedis(ctx, app.Config.String(cooler.RedisURLKey))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer client.Close()
|
||||
store := cooler.NewRateLimitStore(client, "acme:", fallback)
|
||||
allowed, _, _ := store.Attempt("export|"+userKey, 5, time.Minute)
|
||||
return allowed, nil
|
||||
}
|
||||
|
||||
// TestDocsAllowExport runs AllowExport on miniredis: a user's sixth export
|
||||
// in a minute is refused, and another user's first one passes.
|
||||
func TestDocsAllowExport(t *testing.T) {
|
||||
mr := miniredis.RunT(t)
|
||||
cfg, err := compass.Open(compass.Options{Dir: t.TempDir(), Environ: []string{"SUMMER_REDIS__URL=redis://" + mr.Addr() + "/0"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app := backpack.New(cfg)
|
||||
fallback := surf.NewMemoryStore(0)
|
||||
for i := 1; i <= 6; i++ {
|
||||
allowed, err := AllowExport(t.Context(), app, fallback, "user:7")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if allowed != (i <= 5) {
|
||||
t.Fatalf("export %d allowed = %v", i, allowed)
|
||||
}
|
||||
}
|
||||
if allowed, err := AllowExport(t.Context(), app, fallback, "user:8"); err != nil || !allowed {
|
||||
t.Fatalf("another user's export = %v, %v", allowed, err)
|
||||
}
|
||||
if len(mr.Keys()) == 0 {
|
||||
t.Fatal("the store wrote no key to Redis")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user