Production state
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.michelsen.id/phill/preface-tools/internal/auth"
|
||||
"git.michelsen.id/phill/preface-tools/internal/tools"
|
||||
)
|
||||
|
||||
type testTool struct{}
|
||||
|
||||
func (testTool) Definition() tools.Definition { return tools.Definition{Key: "test", Name: "Test"} }
|
||||
func (testTool) StudentHandler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`<section id="tool-fragment">Tool</section>`))
|
||||
})
|
||||
return mux
|
||||
}
|
||||
func (testTool) InstructorHandler() http.Handler { return testTool{}.StudentHandler() }
|
||||
|
||||
func TestNewWithToolRoutesDoesNotPanic(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(testTool{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authService := auth.New("student", "instructor", string(make([]byte, 32)), time.Hour, false)
|
||||
server := New(authService, registry, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
if server.Handler() == nil {
|
||||
t.Fatal("missing handler")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToolRootUsesApplicationShell(t *testing.T) {
|
||||
registry := tools.NewRegistry()
|
||||
if err := registry.Register(testTool{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authService := auth.New("student", "instructor", string(make([]byte, 32)), time.Hour, false)
|
||||
claims, err := authService.Authenticate(auth.Student, "student", "test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server := New(authService, registry, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
request := httptest.NewRequest(http.MethodGet, "/tools/test/", nil)
|
||||
request.AddCookie(&http.Cookie{Name: auth.CookieName, Value: authService.Sign(claims)})
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
for _, expected := range []string{"<!doctype html>", `data-theme="silk"`, "daisyui@5.6.3", "Preface Tools", `id="tool-fragment"`} {
|
||||
if !strings.Contains(response.Body.String(), expected) {
|
||||
t.Errorf("rendered tool page missing %q", expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user