Files
TtsSpeaker/server/admin_test.go
T

111 lines
3.1 KiB
Go

package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"tts-server/internal/tts"
)
func TestAdminSeparateAuthentication(t *testing.T) {
s := testServer(t)
s.cfg.adminToken = "admin-secret"
s.cfg.apiToken = "synthesis-secret"
for _, token := range []string{"", "synthesis-secret"} {
r := httptest.NewRequest("GET", "/admin/api/voices", nil)
r.Header.Set("Authorization", "Bearer "+token)
w := httptest.NewRecorder()
s.routes().ServeHTTP(w, r)
if w.Code != 401 {
t.Fatalf("synthesis token granted admin access: %d", w.Code)
}
}
r := httptest.NewRequest("GET", "/admin/api/voices", nil)
r.Header.Set("Authorization", "Bearer admin-secret")
w := httptest.NewRecorder()
s.routes().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatal(w.Code)
}
}
func TestAdminDefaultAndPreview(t *testing.T) {
s := testServer(t)
s.cfg.adminToken = "admin-secret"
s.cfg.apiToken = "synthesis-secret"
r := httptest.NewRequest("POST", "/admin/api/default", bytes.NewBufferString(`{"voice":"a"}`))
r.Header.Set("Authorization", "Bearer admin-secret")
w := httptest.NewRecorder()
s.routes().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatal(w.Code, w.Body.String())
}
repo := s.repository()
key := s.cache.Key(repo.Version, "a", 1, 1, "hello")
if err := s.cache.Put(key, tts.EncodeWAV([]float32{0.1, 0.2}, 22050)); err != nil {
t.Fatal(err)
}
r = httptest.NewRequest("POST", "/admin/api/preview", bytes.NewBufferString(`{"text":"hello"}`))
r.Header.Set("Authorization", "Bearer admin-secret")
w = httptest.NewRecorder()
s.routes().ServeHTTP(w, r)
if w.Code != 200 || w.Header().Get("Content-Type") != "audio/wav" {
t.Fatal(w.Code, w.Body.String())
}
}
type pausedUpload struct {
started chan struct{}
release chan struct{}
once sync.Once
}
func (p *pausedUpload) Read(b []byte) (int, error) {
p.once.Do(func() { close(p.started) })
<-p.release
return 0, io.EOF
}
func (p *pausedUpload) Close() error { return nil }
func TestUploadDoesNotBlockHealth(t *testing.T) {
s := testServer(t)
s.cfg.adminToken = "admin-secret"
body := &pausedUpload{started: make(chan struct{}), release: make(chan struct{})}
r := httptest.NewRequest("POST", "/admin/api/import", body)
r.Header.Set("Authorization", "Bearer admin-secret")
done := make(chan struct{})
go func() { defer close(done); s.routes().ServeHTTP(httptest.NewRecorder(), r) }()
<-body.started
health := make(chan int, 1)
go func() {
w := httptest.NewRecorder()
s.routes().ServeHTTP(w, httptest.NewRequest("GET", "/healthz", nil))
health <- w.Code
}()
select {
case code := <-health:
if code != http.StatusOK {
t.Error(code)
}
case <-time.After(time.Second):
t.Error("upload blocked health")
}
close(body.release)
<-done
}
func TestAdminPageServedWithoutSecrets(t *testing.T) {
s := testServer(t)
w := httptest.NewRecorder()
s.routes().ServeHTTP(w, httptest.NewRequest("GET", "/admin/", nil))
if w.Code != 200 || w.Header().Get("Content-Security-Policy") == "" {
t.Fatal(w.Code)
}
var parsed any
if json.Unmarshal(w.Body.Bytes(), &parsed) == nil {
t.Fatal("expected HTML")
}
}