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,102 @@
package httpapi
import (
"database/sql/driver"
"encoding/json"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestCurrentLicenseStatus(t *testing.T) {
now := time.Date(2026, 9, 30, 8, 0, 0, 0, time.UTC)
for _, tc := range []struct {
name, status string
starts, expires time.Time
want string
}{
{"scheduled", "active", now.Add(time.Hour), now.Add(2 * time.Hour), "scheduled"},
{"start boundary", "active", now, now.Add(time.Hour), "active"},
{"expiry boundary", "active", now.Add(-time.Hour), now, "expired"},
{"revoked", "revoked", now.Add(time.Hour), now.Add(2 * time.Hour), "revoked"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := currentLicenseStatus(tc.status, tc.starts, tc.expires, now); got != tc.want {
t.Fatalf("got %s want %s", got, tc.want)
}
})
}
}
func TestLicenseFilterPredicates(t *testing.T) {
now := time.Date(2026, 10, 8, 10, 0, 0, 0, time.UTC)
for _, tc := range []struct {
status, predicate string
times []any
}{
{"", "", nil},
{"active", " AND l.status<>'revoked' AND l.starts_at<=? AND l.expires_at>?", []any{now, now}},
{"pending", " AND l.status<>'revoked' AND l.starts_at>? AND l.expires_at>?", []any{now, now}},
{"expired", " AND l.status<>'revoked' AND l.expires_at<=?", []any{now}},
{"revoked", " AND l.status='revoked'", nil},
{"expiring", " AND l.status<>'revoked' AND l.starts_at<=? AND l.expires_at>? AND l.expires_at<=?", []any{now, now, now.Add(30 * 24 * time.Hour)}},
} {
t.Run(tc.status, func(t *testing.T) {
where, args, ok := licenseFilter(tc.status, "customer' OR 1=1", now)
want := " WHERE 1=1 AND (l.license_key LIKE ? OR c.name LIKE ? OR p.name LIKE ? OR p.code LIKE ?)" + tc.predicate
if !ok || where != want {
t.Fatalf("predicate %s", where)
}
expected := []any{"%customer' OR 1=1%", "%customer' OR 1=1%", "%customer' OR 1=1%", "%customer' OR 1=1%"}
expected = append(expected, tc.times...)
if !reflect.DeepEqual(args, expected) {
t.Fatalf("args %#v", args)
}
})
}
for _, status := range []string{"scheduled", "unknown", "ACTIVE"} {
if _, _, ok := licenseFilter(status, "", now); ok {
t.Errorf("accepted %s", status)
}
}
}
func TestListLicensesFilteredPage(t *testing.T) {
// No time-dependent predicate is needed here; each status predicate is checked
// above with exact boundaries. Count and page must both join all search fields.
where := " WHERE 1=1 AND (l.license_key LIKE ? OR c.name LIKE ? OR p.name LIKE ? OR p.code LIKE ?) AND l.status='revoked'"
args := []any{"%Acme%", "%Acme%", "%Acme%", "%Acme%"}
now := time.Now().UTC()
db, _ := newDashboardTestDB(t, []dashboardQuery{
{sql: "SELECT COUNT(*)" + licenseFrom + where, args: args, rows: [][]driver.Value{{int64(31)}}},
{sql: licenseSelect + where + " ORDER BY l.id DESC LIMIT ? OFFSET ?", args: append(append([]any{}, args...), 10, 10), rows: [][]driver.Value{testLicenseRow(25, "revoked", now.Add(-time.Hour), now.Add(time.Hour), now)}},
})
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/api/v1/licenses?q=%20Acme%20&status=%20revoked%20&page=2&page_size=10", nil)
(&BusinessAPI{db: db}).listLicenses(c)
var out struct {
Data []licenseRecord `json:"data"`
Meta struct {
Total, Page int
PageSize int `json:"page_size"`
} `json:"meta"`
}
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &out) != nil || out.Meta.Total != 31 || out.Meta.Page != 2 || out.Meta.PageSize != 10 || len(out.Data) != 1 || out.Data[0].Status != "revoked" {
t.Fatalf("%d %s", w.Code, w.Body)
}
}
func TestListLicensesInvalidStatus(t *testing.T) {
db, _ := newDashboardTestDB(t, nil)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = httptest.NewRequest("GET", "/api/v1/licenses?status=invalid", nil)
(&BusinessAPI{db: db}).listLicenses(c)
if w.Code != 400 || !strings.Contains(w.Body.String(), "invalid_request") {
t.Fatalf("%d %s", w.Code, w.Body)
}
}