feat: delete never-activated licenses and bundle deployment verification keys
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
86
backend/internal/httpapi/license_delete_test.go
Normal file
86
backend/internal/httpapi/license_delete_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type deleteDB struct {
|
||||
*dashboardDB
|
||||
commands []string
|
||||
failAudit bool
|
||||
}
|
||||
|
||||
func (d *deleteDB) Connect(context.Context) (driver.Conn, error) { return d, nil }
|
||||
func (d *deleteDB) ExecContext(_ context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
||||
d.commands = append(d.commands, query)
|
||||
if strings.Contains(query, "INSERT INTO audit_events") {
|
||||
if len(args) != 3 {
|
||||
d.t.Fatal("audit arguments missing")
|
||||
}
|
||||
var details map[string]any
|
||||
if err := json.Unmarshal(args[2].Value.([]byte), &details); err != nil || details["removed_instances"] != float64(1) || details["detached_orders"] != float64(1) {
|
||||
d.t.Fatal("missing deletion audit context", err, details)
|
||||
}
|
||||
if d.failAudit {
|
||||
return nil, errors.New("audit unavailable")
|
||||
}
|
||||
}
|
||||
return driver.RowsAffected(1), nil
|
||||
}
|
||||
|
||||
func TestDeleteLicense(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
activated int64
|
||||
failAudit bool
|
||||
want int
|
||||
}{
|
||||
{"provisioned with order and operations", 0, false, 204},
|
||||
{"activated", 1, false, 409},
|
||||
{"audit failure", 0, true, 500},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
const activationQuery = `SELECT COUNT(*) FROM license_instances i WHERE i.license_id=? AND (i.status='activated' OR i.activated_at IS NOT NULL OR i.certificate_payload IS NOT NULL OR i.certificate_signature IS NOT NULL OR EXISTS (SELECT 1 FROM activation_operations o WHERE o.instance_id=i.id))`
|
||||
queries := []dashboardQuery{
|
||||
{sql: "SELECT status,license_key FROM licenses WHERE id=? FOR UPDATE", args: []any{int64(2)}, rows: [][]driver.Value{{"revoked", "test-key"}}},
|
||||
{sql: activationQuery, args: []any{int64(2)}, rows: [][]driver.Value{{tc.activated}}},
|
||||
}
|
||||
if tc.activated == 0 {
|
||||
queries = append(queries,
|
||||
dashboardQuery{sql: "SELECT COUNT(*) FROM license_instances WHERE license_id=?", args: []any{int64(2)}, rows: [][]driver.Value{{int64(1)}}},
|
||||
dashboardQuery{sql: "SELECT COUNT(*) FROM offline_orders WHERE issued_license_id=?", args: []any{int64(2)}, rows: [][]driver.Value{{int64(1)}}},
|
||||
dashboardQuery{sql: "SELECT COUNT(*) FROM license_operations WHERE license_id=?", args: []any{int64(2)}, rows: [][]driver.Value{{int64(2)}}},
|
||||
)
|
||||
}
|
||||
stub := &deleteDB{dashboardDB: &dashboardDB{t: t, queries: queries}, failAudit: tc.failAudit}
|
||||
db := sql.OpenDB(stub)
|
||||
defer db.Close()
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest("DELETE", "/licenses/2", nil)
|
||||
c.Params = gin.Params{{Key: "id", Value: "2"}}
|
||||
c.Set("admin_id", uint64(1))
|
||||
(&BusinessAPI{db: db}).deleteLicense(c)
|
||||
c.Writer.WriteHeaderNow()
|
||||
if w.Code != tc.want || len(stub.queries) != 0 || stub.committed != (tc.want == 204) {
|
||||
t.Fatalf("response=%d body=%s remaining=%d committed=%v", w.Code, w.Body.String(), len(stub.queries), stub.committed)
|
||||
}
|
||||
if tc.want == 204 {
|
||||
if len(stub.commands) != 8 || !strings.Contains(stub.commands[0], "audit_events") || !strings.Contains(stub.commands[1], "UPDATE offline_orders") || !strings.Contains(stub.commands[7], "DELETE FROM licenses") {
|
||||
t.Fatalf("unexpected deletion order: %v", stub.commands)
|
||||
}
|
||||
} else if len(stub.commands) > 1 {
|
||||
t.Fatalf("unexpected cleanup on failure: %v", stub.commands)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user