Files
archie/internal/webui/usage_test.go
T
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

70 lines
2.2 KiB
Go

package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/usage"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
)
// usageTotalsStore answers the usage read with no rows: these cases test the
// access decision, not the report.
type usageTotalsStore struct{}
func (usageTotalsStore) UsageTotals(context.Context, int) ([]usage.TotalRow, error) {
return nil, nil
}
// TestUsageNeedsAdminOrOwner pins the grant: usage names who spent what, so it
// carries its own action like the audit feed. A viewer's or developer's broad
// read must not open it.
func TestUsageNeedsAdminOrOwner(t *testing.T) {
tests := []struct {
role org.Role
want int
}{
{org.RoleViewer, http.StatusForbidden},
{org.RoleDeveloper, http.StatusForbidden},
{org.RoleAdmin, http.StatusOK},
{org.RoleOwner, http.StatusOK},
}
for _, tt := range tests {
t.Run(string(tt.role), func(t *testing.T) {
principal := access.Principal{
IdentityID: "alice", Org: "acme",
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
}
srv := &Server{
Usage: usageTotalsStore{},
Accounts: accountBoundaryStore{},
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
Principals: principalOf(principal),
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example/api/usage", nil)
req.Header.Set("X-Archie-CSRF", "1")
sendCookie(req, "archie_session", "valid")
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != tt.want {
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
}
if tt.want != http.StatusOK {
return
}
var report usageReportView
if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil {
t.Fatalf("decode report: %v", err)
}
if report.Days != 30 || report.ByDay == nil || report.ByIdentity == nil {
t.Fatalf("report = %+v, want days 30 with empty slices", report)
}
})
}
}