feat: implement license platform business flows and delivery tooling
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:
150
backend/internal/httpapi/orders_integration_test.go
Normal file
150
backend/internal/httpapi/orders_integration_test.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testOrdersMySQL(t *testing.T, db *sql.DB, r *gin.Engine, cookie *http.Cookie, customerID, productID uint64) {
|
||||
request := func(method, path, body, key string, authenticated bool) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
if authenticated {
|
||||
req.AddCookie(cookie)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
check := func(w *httptest.ResponseRecorder, code int) {
|
||||
t.Helper()
|
||||
if w.Code != code {
|
||||
t.Fatalf("code=%d want=%d body=%s", w.Code, code, w.Body)
|
||||
}
|
||||
}
|
||||
exec := func(query string, args ...any) {
|
||||
t.Helper()
|
||||
if _, err := db.Exec(query, args...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
body := fmt.Sprintf(`{"customer_id":%d,"product_id":%d,"amount_cents":12800,"currency":"CNY"}`, customerID, productID)
|
||||
check(request("POST", "/api/v1/orders", body, "create", false), 401)
|
||||
exec("UPDATE admins SET role='viewer' WHERE email='integration@example.test'")
|
||||
check(request("POST", "/api/v1/orders", body, "create", true), 403)
|
||||
exec("UPDATE admins SET role='admin' WHERE email='integration@example.test'")
|
||||
check(request("POST", "/api/v1/orders", body, "", true), 400)
|
||||
check(request("POST", "/api/v1/orders", strings.Replace(body, "12800", "0", 1), "invalid", true), 400)
|
||||
exec("UPDATE customers SET status='inactive' WHERE id=?", customerID)
|
||||
check(request("POST", "/api/v1/orders", body, "inactive-customer", true), 400)
|
||||
exec("UPDATE customers SET status='active' WHERE id=?", customerID)
|
||||
w := request("POST", "/api/v1/orders", body, "create", true)
|
||||
check(w, 201)
|
||||
var result struct {
|
||||
Data orderRecord `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id := result.Data.ID
|
||||
path := fmt.Sprintf("/api/v1/orders/%d", id)
|
||||
check(request("POST", "/api/v1/orders", body, "create", true), 200)
|
||||
check(request("POST", "/api/v1/orders", strings.Replace(body, "12800", "12900", 1), "create", true), 409)
|
||||
check(request("GET", path, "", "", true), 200)
|
||||
check(request("GET", "/api/v1/orders?page=1&page_size=20", "", "", true), 200)
|
||||
dates := `{"starts_at":"2026-09-30T00:00:00Z","expires_at":"2027-09-30T00:00:00Z"}`
|
||||
check(request("POST", path+"/issue", dates, "issue", true), 409)
|
||||
payment := `{"amount_cents":12800,"payment_reference":"BANK-TEST-001"}`
|
||||
check(request("POST", path+"/confirm-payment", strings.Replace(payment, "12800", "12799", 1), "payment", true), 400)
|
||||
exec("CREATE TRIGGER order_audit_failure BEFORE INSERT ON audit_events FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='test failure'")
|
||||
t.Cleanup(func() { db.Exec("DROP TRIGGER IF EXISTS order_audit_failure") })
|
||||
check(request("POST", path+"/confirm-payment", payment, "payment", true), 500)
|
||||
exec("DROP TRIGGER order_audit_failure")
|
||||
var status string
|
||||
if err := db.QueryRow("SELECT status FROM offline_orders WHERE id=?", id).Scan(&status); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if status != "pending_payment" {
|
||||
t.Fatal("failed payment changed order")
|
||||
}
|
||||
check(request("POST", path+"/confirm-payment", payment, "payment", true), 200)
|
||||
check(request("POST", path+"/confirm-payment", payment, "payment", true), 200)
|
||||
check(request("POST", path+"/confirm-payment", payment, "different-payment", true), 409)
|
||||
check(request("POST", path+"/confirm-payment", strings.Replace(payment, "001", "002", 1), "payment", true), 409)
|
||||
exec("UPDATE products SET active=0 WHERE id=?", productID)
|
||||
check(request("POST", path+"/issue", dates, "issue", true), 400)
|
||||
exec("UPDATE products SET active=1 WHERE id=?", productID)
|
||||
var before int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM licenses").Scan(&before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exec("CREATE TRIGGER order_audit_failure BEFORE INSERT ON audit_events FOR EACH ROW SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT='test failure'")
|
||||
check(request("POST", path+"/issue", dates, "issue", true), 500)
|
||||
exec("DROP TRIGGER order_audit_failure")
|
||||
var after int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM licenses").Scan(&after); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if before != after {
|
||||
t.Fatal("failed audit leaked license")
|
||||
}
|
||||
if err := db.QueryRow("SELECT status FROM offline_orders WHERE id=?", id).Scan(&status); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if status != "paid" {
|
||||
t.Fatal("failed issuance changed order")
|
||||
}
|
||||
results := make(chan *httptest.ResponseRecorder, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() { results <- request("POST", path+"/issue", dates, "issue", true) }()
|
||||
}
|
||||
replay := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
w := <-results
|
||||
check(w, 200)
|
||||
if strings.Contains(w.Body.String(), `"idempotent_replay":true`) {
|
||||
replay++
|
||||
}
|
||||
}
|
||||
if replay != 1 {
|
||||
t.Fatalf("replays=%d", replay)
|
||||
}
|
||||
check(request("POST", path+"/issue", dates, "different-issue", true), 409)
|
||||
check(request("POST", path+"/issue", strings.Replace(dates, "2027", "2028", 1), "issue", true), 409)
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM licenses").Scan(&after); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if after != before+1 {
|
||||
t.Fatal("duplicate issuance")
|
||||
}
|
||||
var count int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM audit_events WHERE resource_type='order' AND resource_id=?", id).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("order audits=%d", count)
|
||||
}
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM order_operations WHERE order_id=?", id).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 3 {
|
||||
t.Fatalf("operations=%d", count)
|
||||
}
|
||||
var licenseID uint64
|
||||
if err := db.QueryRow("SELECT issued_license_id FROM offline_orders WHERE id=? AND status='issued'", id).Scan(&licenseID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM licenses WHERE id=? AND customer_id=? AND product_id=?", licenseID, customerID, productID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatal("license scope mismatch")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user