accounts and email
This commit is contained in:
@@ -414,3 +414,135 @@ func multipartUploadRequest(t *testing.T, filename string, content []byte, folde
|
||||
request.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
return request
|
||||
}
|
||||
|
||||
func TestHandleHealthzReturnsOK(t *testing.T) {
|
||||
server := setupHealthTestServer(t)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
server.handleHealthz(recorder, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if payload["status"] != "ok" {
|
||||
t.Fatalf("status = %q, want ok", payload["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleReadyzReportsOKWhenDatabaseAndStoreAreReachable(t *testing.T) {
|
||||
server := setupHealthTestServer(t)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
server.handleReadyz(recorder, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if payload["status"] != "ok" {
|
||||
t.Fatalf("status = %q, want ok", payload["status"])
|
||||
}
|
||||
if payload["database"] != "ok" {
|
||||
t.Fatalf("database = %q, want ok", payload["database"])
|
||||
}
|
||||
if payload["contentStore"] != "ok" {
|
||||
t.Fatalf("contentStore = %q, want ok", payload["contentStore"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleReadyzReportsUnavailableWhenContentStoreMissing(t *testing.T) {
|
||||
server := setupHealthTestServer(t)
|
||||
server.config.Content.StoreDir = filepath.Join(t.TempDir(), "does-not-exist")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
server.handleReadyz(recorder, httptest.NewRequest(http.MethodGet, "/readyz", nil))
|
||||
|
||||
if recorder.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if payload["status"] != "unavailable" {
|
||||
t.Fatalf("status = %q, want unavailable", payload["status"])
|
||||
}
|
||||
if payload["contentStore"] == "ok" {
|
||||
t.Fatalf("contentStore = %q, want non-ok", payload["contentStore"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleHealthAliasForReadyz(t *testing.T) {
|
||||
server := setupHealthTestServer(t)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
server.handleHealth(recorder, httptest.NewRequest(http.MethodGet, "/health", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
|
||||
}
|
||||
var payload map[string]string
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if payload["status"] != "ok" {
|
||||
t.Fatalf("status = %q, want ok", payload["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthzAndReadyzAreRegisteredOnRouter(t *testing.T) {
|
||||
server := newAPITestServer(t)
|
||||
|
||||
for _, path := range []string{"/healthz", "/readyz", "/health"} {
|
||||
t.Run(path, func(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
server.handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d; body=%s", recorder.Code, http.StatusOK, recorder.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func setupHealthTestServer(t *testing.T) *Server {
|
||||
t.Helper()
|
||||
|
||||
root := t.TempDir()
|
||||
storeDir := filepath.Join(root, "files")
|
||||
if err := os.MkdirAll(storeDir, 0o755); err != nil {
|
||||
t.Fatalf("create store dir: %v", err)
|
||||
}
|
||||
|
||||
db, err := sql.Open("libsql", "file:"+filepath.Join(root, "test.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("open database: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
if err := database.ApplyMigrations(context.Background(), db); err != nil {
|
||||
t.Fatalf("apply migrations: %v", err)
|
||||
}
|
||||
|
||||
contentStore, err := store.New(storeDir)
|
||||
if err != nil {
|
||||
t.Fatalf("create content store: %v", err)
|
||||
}
|
||||
repository := docs.NewRepository(db)
|
||||
documents := docs.NewService(t.TempDir(), contentStore, markdown.NewRenderer(), repository, slog.Default())
|
||||
|
||||
return &Server{
|
||||
config: config.Config{
|
||||
Content: config.ContentConfig{StoreDir: storeDir},
|
||||
},
|
||||
documents: documents,
|
||||
repository: repository,
|
||||
contentStore: contentStore,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user