Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

1.5 KB
Raw Blame
package api

import (
	"context"
	"net/http/httptest"
	"testing"

	"github.com/rcourtman/pulse-go-rewrite/internal/config"
	"github.com/rcourtman/pulse-go-rewrite/internal/monitoring"
	"github.com/stretchr/testify/assert"
)

// TestStateIsolation_Permanent verifies that tenant context is respected by router helper.
func TestStateIsolation_Permanent(t *testing.T) {
	// Setup config with MT enabled
	cfg := &config.Config{
		MultiTenantEnabled: true,
	}

	// Create router with nil monitors initially
	router := &Router{
		config: cfg,
	}

	_ = router // Suppress unused

	// Simulate request with Tenant Context
	req := httptest.NewRequest("GET", "/api/state", nil)
	ctx := context.WithValue(req.Context(), OrgIDContextKey, "tenant-1")
	req = req.WithContext(ctx)

	// Verify GetOrgID helper correctly extracts it
	assert.Equal(t, "tenant-1", GetOrgID(req.Context()))
}

func TestGetTenantMonitor_Permanent(t *testing.T) {
	cfg := &config.Config{MultiTenantEnabled: true}
	defaultMon := &monitoring.Monitor{}

	router := &Router{
		config:  cfg,
		monitor: defaultMon,
	}

	// Case 1: Default Context -> Default Monitor
	req := httptest.NewRequest("GET", "/", nil)
	mon := router.getTenantMonitor(req.Context())
	assert.Equal(t, defaultMon, mon)

	// Case 2: Tenant Context but NO MT Monitor -> fail closed
	ctx := context.WithValue(req.Context(), OrgIDContextKey, "tenant-1")
	mon = router.getTenantMonitor(ctx)
	assert.Nil(t, mon, "Should fail closed for non-default org when tenant monitor is unavailable")
}