feat: implement license platform business flows and delivery tooling
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-09 15:33:22 +08:00
parent 3ab651d3a8
commit 54c318c615
142 changed files with 11105 additions and 178 deletions

View File

@@ -0,0 +1,99 @@
package httpapi
import (
"database/sql/driver"
"encoding/json"
"github.com/gin-gonic/gin"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestAuditListSnapshot(t *testing.T) {
now := time.Date(2026, 10, 9, 2, 0, 0, 0, time.UTC)
base := " FROM audit_events e JOIN admins a ON a.id=e.actor_admin_id WHERE 1=1 AND e.action=?"
db, stub := newDashboardTestDB(t, []dashboardQuery{
{sql: "SELECT COUNT(*)" + base, args: []any{"license.issued"}, rows: [][]driver.Value{{int64(21)}}},
{sql: "SELECT e.id,e.actor_admin_id,a.email,e.action,e.resource_type,e.resource_id,e.details,e.created_at" + base + " ORDER BY e.created_at DESC,e.id DESC LIMIT ? OFFSET ?", args: []any{"license.issued", 20, 20}, rows: [][]driver.Value{{int64(1), int64(2), "admin@example.test", "license.issued", "license", int64(3), []byte(`{"license_id":3,"password":"hidden"}`), now}}},
})
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/?action=license.issued&page=2", nil)
(&BusinessAPI{db: db}).listAuditEvents(c)
if w.Code != 200 || strings.Contains(w.Body.String(), "hidden") {
t.Fatalf("%d %s", w.Code, w.Body.String())
}
var result struct {
Data []auditRecord `json:"data"`
Meta struct {
Total int `json:"total"`
Page int `json:"page"`
} `json:"meta"`
}
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
t.Fatal(err)
}
if result.Meta.Total != 21 || result.Meta.Page != 2 || len(result.Data) != 1 || !stub.committed || !stub.txOptions.ReadOnly {
t.Fatalf("unexpected snapshot: %s", w.Body.String())
}
}
func TestPlatformSettingsValidation(t *testing.T) {
for _, tc := range []struct {
s platformSettings
ok bool
}{
{platformSettings{SiteName: " 管理台 "}, true},
{platformSettings{SiteName: ""}, false},
{platformSettings{SiteName: strings.Repeat("中", 101)}, false},
{platformSettings{SiteName: "平台", SupportEmail: "admin@example.test"}, true},
{platformSettings{SiteName: "平台", SupportEmail: "Name <admin@example.test>"}, false},
{platformSettings{SiteName: "平台", Announcement: strings.Repeat("中", 2001)}, false},
} {
if got := validateSettings(&tc.s); got != tc.ok {
t.Fatalf("validation=%v want %v", got, tc.ok)
}
}
}
func TestAuditDetailsAllowlist(t *testing.T) {
out := publicAuditDetails([]byte(`{"license_id":3,"password":"private","token":"private","instance_secret":"private","state":{"password":"private"},"fields":["private"]}`))
if len(out) != 2 || out["license_id"] != float64(3) {
t.Fatalf("unsafe details: %#v", out)
}
if len(publicAuditDetails([]byte(`invalid`))) != 0 {
t.Fatal("invalid JSON exposed")
}
}
func TestAuditFilterValidation(t *testing.T) {
for _, tc := range []struct {
query string
ok bool
}{
{"?action=license.issued&resource_type=license&q=admin", true},
{"?from=2026-10-09T00:00:00Z&to=2026-10-10T00:00:00Z", true},
{"?from=not-a-date", false},
{"?from=2026-10-10T00:00:00Z&to=2026-10-09T00:00:00Z", false},
{"?action=" + strings.Repeat("x", 65), false},
} {
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request = httptest.NewRequest("GET", "/"+tc.query, nil)
where, _, ok := auditFilter(c)
if ok != tc.ok {
t.Fatalf("%s got %v", tc.query, ok)
}
if ok && where == "" {
t.Fatal("missing predicate")
}
}
}
func TestAdminModuleAuthentication(t *testing.T) {
r := gin.New()
registerBusinessRoutes(r, &BusinessAPI{})
for _, tc := range []struct{ method, path string }{{"GET", "/api/v1/audit-events"}, {"GET", "/api/v1/settings"}, {"PUT", "/api/v1/settings"}, {"POST", "/api/v1/settings/password"}} {
w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(tc.method, tc.path, nil))
if w.Code != 401 {
t.Fatalf("%s %s: %d", tc.method, tc.path, w.Code)
}
}
}