Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
57b241d
docs: Console Redesign Batch 1 implementation plan
Jul 10, 2026
93fb92b
feat(backend): optional reason on lifecycle + impersonation, persiste…
Jul 10, 2026
595b3f7
chore(web): add vitest + Testing Library test infrastructure
Jul 10, 2026
27a0be4
fix(web): vitest passWithNoTests for bootstrap commit, tsc coverage f…
Jul 10, 2026
b884846
fix(web): narrow vitest.config.ts plugins cast from any to Plugin[] (…
Jul 10, 2026
edb3b22
feat(web): shared meter tone/percent utility for usage-vs-limit displays
Jul 10, 2026
874acfe
feat(web): add trial status to StatusBadge's 4-way status system
Jul 10, 2026
9a38a22
feat(web): add shadcn Sheet primitive (for Batch 2's Archive side-sheet)
Jul 10, 2026
71bb260
feat(web): add console dark-chrome and trial-status design tokens
Jul 10, 2026
d651f5a
feat(web): dark-chrome top-nav shell for the Platform Console
Jul 10, 2026
f276c4e
feat(web): shared tenant-queue filters and promote BarRow to a shared…
Jul 10, 2026
53d139d
feat(web): reskin Tenants list — saved-queue chips, usage meters, cus…
Jul 10, 2026
46c04d0
feat(web): reskin Platform Console dashboard — real KPI tiles, queues…
Jul 10, 2026
b308565
fix(web): Dashboard load() error handling + scope over-limit queue te…
Jul 10, 2026
973cd0e
fix(web): console header toggle buttons stay visible in light mode (f…
Jul 10, 2026
f177443
test(backend): assert audit callback actually ran in no-body test
Jul 11, 2026
9f76cf9
fix(web): isActiveNavPath prefix-collision + move for fast-refresh
Jul 11, 2026
3a57887
fix(web): meterPercent zero-limit consistency + overLimitTenants cove…
Jul 11, 2026
9bd8a3e
fix(web): localize Sheet's close button accessible label
Jul 11, 2026
8a60f88
fix(web): drop false "recently" claim from suspended-tenants queue label
Jul 11, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions backend/internal/handler/super_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ func (h *Handler) setTenantStatus(c echo.Context, action string) error {
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid tenant ID"})
}
var body struct {
Reason string `json:"reason"`
}
//nolint:errcheck
_ = c.Bind(&body) // optional body; malformed/absent JSON leaves body.Reason == ""
current, err := h.Store.GetTenantStatus(c.Request().Context(), tenantID)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to load tenant"})
Expand All @@ -414,7 +419,11 @@ func (h *Handler) setTenantStatus(c echo.Context, action string) error {
return writeErr(c, err)
}
adminID := uuid.MustParse(claims.UserID)
if err := h.Store.LogAdminAction(c.Request().Context(), adminID, action+"_tenant", "tenant", tenantID, map[string]interface{}{"from": current, "to": tr.to}, c.RealIP(), c.Request().UserAgent()); err != nil {
changes := map[string]interface{}{"from": current, "to": tr.to}
if body.Reason != "" {
changes["reason"] = body.Reason
}
if err := h.Store.LogAdminAction(c.Request().Context(), adminID, action+"_tenant", "tenant", tenantID, changes, c.RealIP(), c.Request().UserAgent()); err != nil {
log.Printf("Failed to log admin action: %v", err)
}
return c.JSON(http.StatusOK, map[string]string{"status": tr.to})
Expand All @@ -438,6 +447,11 @@ func (h *Handler) ImpersonateTenant(c echo.Context) error {
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid tenant ID"})
}
var body struct {
Reason string `json:"reason"`
}
//nolint:errcheck
_ = c.Bind(&body)
status, err := h.Store.GetTenantStatus(c.Request().Context(), tenantID)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to load tenant"})
Expand All @@ -457,7 +471,11 @@ func (h *Handler) ImpersonateTenant(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to mint impersonation token"})
}
adminID := uuid.MustParse(claims.UserID)
if err := h.Store.LogAdminAction(c.Request().Context(), adminID, "impersonate_tenant", "tenant", tenantID, map[string]interface{}{"expires_at": expiresAt}, c.RealIP(), c.Request().UserAgent()); err != nil {
changes := map[string]interface{}{"expires_at": expiresAt}
if body.Reason != "" {
changes["reason"] = body.Reason
}
if err := h.Store.LogAdminAction(c.Request().Context(), adminID, "impersonate_tenant", "tenant", tenantID, changes, c.RealIP(), c.Request().UserAgent()); err != nil {
log.Printf("Failed to log admin action: %v", err)
}
return c.JSON(http.StatusOK, map[string]interface{}{
Expand Down
163 changes: 163 additions & 0 deletions backend/internal/handler/super_admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package handler

import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"idento/backend/internal/models"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

func TestSetTenantStatus_ReasonPersistedToAuditChanges(t *testing.T) {
e := echo.New()
tenantID := uuid.New()
adminID := uuid.New()
var capturedChanges map[string]interface{}

fs := &fakeStore{
getTenantStatus: func(id uuid.UUID) (string, error) {
if id == tenantID {
return "active", nil
}
return "", nil
},
updateTenantStatus: func(id uuid.UUID, status string) error {
return nil
},
logAdminAction: func(audID uuid.UUID, action, targetType string, targetID uuid.UUID, changes interface{}, ip, userAgent string) error {
capturedChanges = changes.(map[string]interface{})
return nil
},
}

h := &Handler{Store: fs}
body, _ := json.Marshal(map[string]string{"reason": "Spring Summit 2026, approved by JR"})
req := httptest.NewRequest(http.MethodPost, "/api/super-admin/tenants/"+tenantID.String()+"/suspend", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(tenantID.String())
c.Set("user", &models.JWTCustomClaims{
UserID: adminID.String(),
TenantID: uuid.New().String(),
Role: "admin",
})

if err := h.SuspendTenant(c); err != nil {
t.Fatalf("SuspendTenant returned error: %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
if capturedChanges == nil {
t.Fatalf("expected changes to be captured, got nil")
}
if capturedChanges["reason"] != "Spring Summit 2026, approved by JR" {
t.Fatalf("expected reason in audit changes, got %#v", capturedChanges)
}
if capturedChanges["from"] != "active" || capturedChanges["to"] != "suspended" {
t.Fatalf("expected from/to preserved alongside reason, got %#v", capturedChanges)
}
}
Comment on lines +16 to +67

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Use table-driven subtests and parallel execution.

These three cases duplicate the same Echo/fake-store setup and none uses t.Parallel(). Consolidate the scenarios into table-driven subtests; hoist shared JWT-secret setup before parallel subtests because t.Setenv cannot be called from a parallel test.

As per coding guidelines: Write unit tests using table-driven patterns and parallel execution.

Also applies to: 69-112, 114-160

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/internal/handler/super_admin_test.go` around lines 16 - 67, Refactor
the related audit-change tests, including
TestSetTenantStatus_ReasonPersistedToAuditChanges and the additional cases, into
one table-driven test with subtests and t.Parallel(). Hoist any shared
JWT-secret setup before entering parallel subtests because t.Setenv cannot run
in parallel tests, and parameterize each scenario’s request, expected status,
and captured audit changes while preserving the existing assertions.

Source: Coding guidelines


func TestSetTenantStatus_NoBodyStillWorks(t *testing.T) {
e := echo.New()
tenantID := uuid.New()
adminID := uuid.New()
var capturedChanges map[string]interface{}

fs := &fakeStore{
getTenantStatus: func(id uuid.UUID) (string, error) {
if id == tenantID {
return "suspended", nil
}
return "", nil
},
updateTenantStatus: func(id uuid.UUID, status string) error {
return nil
},
logAdminAction: func(audID uuid.UUID, action, targetType string, targetID uuid.UUID, changes interface{}, ip, userAgent string) error {
capturedChanges = changes.(map[string]interface{})
return nil
},
}

h := &Handler{Store: fs}
req := httptest.NewRequest(http.MethodPost, "/api/super-admin/tenants/"+tenantID.String()+"/reactivate", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(tenantID.String())
c.Set("user", &models.JWTCustomClaims{
UserID: adminID.String(),
TenantID: uuid.New().String(),
Role: "admin",
})

if err := h.ReactivateTenant(c); err != nil {
t.Fatalf("ReactivateTenant returned error: %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
if capturedChanges == nil {
t.Fatalf("expected logAdminAction to be invoked with captured changes, got nil")
}
if _, hasReason := capturedChanges["reason"]; hasReason {
t.Fatalf("expected no reason key when body omits it, got %#v", capturedChanges)
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

func TestImpersonateTenant_ReasonPersistedToAuditChanges(t *testing.T) {
t.Setenv("JWT_SECRET", "test-secret")
e := echo.New()
tenantID := uuid.New()
adminID := uuid.New()
var capturedChanges map[string]interface{}

fs := &fakeStore{
getTenantStatus: func(id uuid.UUID) (string, error) {
if id == tenantID {
return "active", nil
}
return "", nil
},
logAdminAction: func(audID uuid.UUID, action, targetType string, targetID uuid.UUID, changes interface{}, ip, userAgent string) error {
capturedChanges = changes.(map[string]interface{})
return nil
},
}

h := &Handler{Store: fs}
body, _ := json.Marshal(map[string]string{"reason": "Reproduce badge-print bug for support ticket #4821"})
req := httptest.NewRequest(http.MethodPost, "/api/super-admin/tenants/"+tenantID.String()+"/impersonate", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetParamNames("id")
c.SetParamValues(tenantID.String())
c.Set("user", &models.JWTCustomClaims{
UserID: adminID.String(),
TenantID: uuid.New().String(),
Role: "admin",
})

if err := h.ImpersonateTenant(c); err != nil {
t.Fatalf("ImpersonateTenant returned error: %v", err)
}
if rec.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
}
if capturedChanges == nil {
t.Fatalf("expected changes to be captured, got nil")
}
if capturedChanges["reason"] != "Reproduce badge-print bug for support ticket #4821" {
t.Fatalf("expected reason in audit changes, got %#v", capturedChanges)
}
}
Loading
Loading