85 lines
2.7 KiB
Go
85 lines
2.7 KiB
Go
// app/admin/admin_test.go
|
|
// Focused tests for UI auth and scoping
|
|
// Created: 2026-04-12 09:28 MSK
|
|
// Updated: 2026-04-12 09:28 MSK — demo UI token and tenant-scoped UI responses
|
|
package admin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"shared-sqs/app/tenant"
|
|
)
|
|
|
|
// TestJWTAuth_AllowsDemoToken verifies that the public demo token authenticates into the seeded demo tenant.
|
|
func TestJWTAuth_AllowsDemoToken(t *testing.T) {
|
|
t.Setenv("SHARED_SQS_UI_DEMO_TOKEN", "demo-token-for-test")
|
|
t.Setenv("SHARED_SQS_UI_DEMO_TENANT_ID", "t-demo-test")
|
|
|
|
store := tenant.NewTenantStore()
|
|
demoTenant, err := store.CreateFixed("demo-service", 10, "t-demo-test", "SSAK-demo-test", "demo-secret")
|
|
if err != nil {
|
|
t.Fatalf("CreateFixed(): %v", err)
|
|
}
|
|
|
|
h := NewHandler(store, "admin-token")
|
|
req := httptest.NewRequest(http.MethodPost, "/ui/api/auth", strings.NewReader(`{"token":"demo-token-for-test"}`))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
|
|
h.jwtAuth(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("jwtAuth() status = %d, body = %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]any
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if got := resp["tenant_id"]; got != demoTenant.ID {
|
|
t.Fatalf("tenant_id = %v, want %s", got, demoTenant.ID)
|
|
}
|
|
if got := resp["access_key"]; got != demoTenant.AccessKey {
|
|
t.Fatalf("access_key = %v, want %s", got, demoTenant.AccessKey)
|
|
}
|
|
}
|
|
|
|
// TestListTenants_UIContextReturnsOnlyOwnTenant verifies that UI API listing is scoped to the authenticated tenant.
|
|
func TestListTenants_UIContextReturnsOnlyOwnTenant(t *testing.T) {
|
|
store := tenant.NewTenantStore()
|
|
firstTenant, err := store.CreateFixed("demo-service", 10, "t-demo-test", "SSAK-demo-test", "demo-secret")
|
|
if err != nil {
|
|
t.Fatalf("CreateFixed(first): %v", err)
|
|
}
|
|
if _, err := store.CreateFixed("other-service", 10, "t-other-test", "SSAK-other-test", "other-secret"); err != nil {
|
|
t.Fatalf("CreateFixed(second): %v", err)
|
|
}
|
|
|
|
h := NewHandler(store, "admin-token")
|
|
req := httptest.NewRequest(http.MethodGet, "/ui/api/tenants", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), uiTenantContextKey, firstTenant))
|
|
w := httptest.NewRecorder()
|
|
|
|
h.listTenants(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("listTenants() status = %d, body = %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp []map[string]any
|
|
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
|
t.Fatalf("decode response: %v", err)
|
|
}
|
|
if len(resp) != 1 {
|
|
t.Fatalf("len(response) = %d, want 1", len(resp))
|
|
}
|
|
if got := resp[0]["id"]; got != firstTenant.ID {
|
|
t.Fatalf("response[0].id = %v, want %s", got, firstTenant.ID)
|
|
}
|
|
}
|