mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-10-20 07:31:56 +00:00
srv.Shutdown never cancelled the request context, so a SIGTERM mid-export left the plaintext dump and pg_service.conf in TMPDIR. The handler now derives its context from the app context and the router removes staging dirs older than 20 minutes at startup.
40 lines
1.2 KiB
Go
40 lines
1.2 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package router
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/api/handlers"
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/backup"
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/config"
|
|
)
|
|
|
|
func TestBackupAuthenticationBeforeExport(t *testing.T) {
|
|
for _, key := range []string{"", "fixture-key"} {
|
|
handler := New(nil, nil, nil, Options{Auth: config.AuthConfig{APIKey: key}, AdminRoutes: map[string]http.Handler{"/backup": handlers.BackupRouter(backup.Options{ConnectionService: "fixture"}, context.Background())}})
|
|
for _, token := range []string{"", "wrong", "fixture-key"} {
|
|
r := httptest.NewRequest("GET", "/api/v1/admin/backup?database=other", nil)
|
|
if token != "" {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
handler.ServeHTTP(w, r)
|
|
want := 503
|
|
if key != "" {
|
|
want = 401
|
|
if token == key {
|
|
want = 400
|
|
}
|
|
}
|
|
if w.Code != want || w.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatalf("status=%d want=%d", w.Code, want)
|
|
}
|
|
}
|
|
}
|
|
}
|