mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
Twelve more hand-built statements, leaving zero application-level raw SQL in base. What remains raw is raw for a reason: DDL in migrations, PRAGMA wal_checkpoint, and apis/sql.go — which is the SQL console, where executing user-supplied SQL IS the feature. scheduler (5): plain UPDATEs. claimFunction keeps its guard on the current status, which is what makes the claim exactly-once — two schedulers racing both issue the statement and only the one whose UPDATE matched a still-pending row sees a row affected. The builder emits the same guarded statement. private (6): the interesting one is the upsert. It was hand-written INSERT … ON CONFLICT because SqliteBuilder.Upsert answered "not supported" — that was never a constraint, only an unimplemented override, and dbx v1.17.2 implements it. This is one of the callers that error sent to raw SQL. The table name is now a constant instead of six string literals. And a test for the branch that had none: every PUT in the suite inserted a NEW tag, so nothing exercised ON CONFLICT. That is the one statement here whose two halves can disagree, and the failure is quiet in the worst way — an insert that does not update leaves the OLD ciphertext readable under a tag the client believes it just replaced. The new case seeds a row, overwrites it, and asserts one row holding the new bytes. apis and plugins suites green. Co-authored-by: Hanzo Dev <[email protected]>
213 lines
7.0 KiB
Go
213 lines
7.0 KiB
Go
package apis_test
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tests"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
// Reuse the auth tokens baked into tests/data — same user the rest of the
|
|
// suite authenticates as.
|
|
const (
|
|
privateUserAToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2xsZWN0aW9uSWQiOiJfdXNlcnNfYXV0aF8iLCJleHAiOjI1MjQ2MDQ0NjEsImlkIjoiNHExeGxjbG1mbG9rdTMzIiwicmVmcmVzaGFibGUiOnRydWUsInR5cGUiOiJhdXRoIn0.AuFTIzCsdLEy-5adFzpjZzbqAdTP6Iu9B1wPBAxLBgo"
|
|
privateTestUserID = "4q1xlclmfloku33"
|
|
)
|
|
|
|
// seedPrivateRow inserts a ciphertext row for the test user. Creates the
|
|
// table first since the endpoint does so lazily on first use.
|
|
func seedPrivateRow(app *tests.TestApp, tag string, ct []byte) error {
|
|
_, err := app.DB().NewQuery(`
|
|
CREATE TABLE IF NOT EXISTS _private (
|
|
user_id TEXT NOT NULL,
|
|
tag TEXT NOT NULL,
|
|
ct BLOB NOT NULL,
|
|
updated_at INTEGER NOT NULL,
|
|
PRIMARY KEY (user_id, tag)
|
|
) WITHOUT ROWID
|
|
`).Execute()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = app.DB().NewQuery(`
|
|
INSERT INTO _private (user_id, tag, ct, updated_at)
|
|
VALUES ({:u}, {:t}, {:c}, {:n})
|
|
`).Bind(dbx.Params{"u": privateTestUserID, "t": tag, "c": ct, "n": int64(1700000000000)}).Execute()
|
|
return err
|
|
}
|
|
|
|
func TestPrivateAPI(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ciphertext := []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe}
|
|
|
|
scenarios := []tests.ApiScenario{
|
|
// --- Auth gating ---
|
|
{
|
|
Name: "PUT private tag as guest → 401",
|
|
Method: http.MethodPut,
|
|
URL: "/v1/private/watchlist",
|
|
Body: bytes.NewReader(ciphertext),
|
|
ExpectedStatus: 401,
|
|
ExpectedContent: []string{`"status":401`},
|
|
},
|
|
{
|
|
Name: "GET private tag as guest → 401",
|
|
Method: http.MethodGet,
|
|
URL: "/v1/private/watchlist",
|
|
ExpectedStatus: 401,
|
|
ExpectedContent: []string{`"status":401`},
|
|
},
|
|
{
|
|
Name: "LIST private tags as guest → 401",
|
|
Method: http.MethodGet,
|
|
URL: "/v1/private",
|
|
ExpectedStatus: 401,
|
|
ExpectedContent: []string{`"status":401`},
|
|
},
|
|
|
|
// --- Tag validation ---
|
|
{
|
|
Name: "PUT with dotdot tag → 400",
|
|
Method: http.MethodPut,
|
|
URL: "/v1/private/..bad",
|
|
Body: bytes.NewReader(ciphertext),
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
ExpectedStatus: 400,
|
|
ExpectedContent: []string{`"status":400`, "Invalid tag"},
|
|
},
|
|
|
|
// --- Happy path writes ---
|
|
{
|
|
Name: "PUT ciphertext returns size + timestamp",
|
|
Method: http.MethodPut,
|
|
URL: "/v1/private/watchlist",
|
|
Body: bytes.NewReader(ciphertext),
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
ExpectedStatus: 200,
|
|
ExpectedContent: []string{
|
|
`"tag":"watchlist"`,
|
|
`"size":8`,
|
|
},
|
|
},
|
|
{
|
|
// The overwrite branch. Every other PUT here inserts a new row, so
|
|
// nothing exercised ON CONFLICT — and the upsert is the one
|
|
// statement in this file whose two halves can disagree: an insert
|
|
// that silently fails to update leaves the OLD ciphertext readable
|
|
// under a tag the client believes it just replaced.
|
|
Name: "PUT over an existing tag replaces it",
|
|
Method: http.MethodPut,
|
|
URL: "/v1/private/watchlist",
|
|
Body: bytes.NewReader([]byte("REPLACED-ciphertext")),
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
|
|
if err := seedPrivateRow(app, "watchlist", ciphertext); err != nil {
|
|
t.Fatalf("seed failed: %v", err)
|
|
}
|
|
},
|
|
ExpectedStatus: 200,
|
|
ExpectedContent: []string{
|
|
`"tag":"watchlist"`,
|
|
`"size":19`, // len("REPLACED-ciphertext"), not the 8-byte seed
|
|
},
|
|
AfterTestFunc: func(t testing.TB, app *tests.TestApp, res *http.Response) {
|
|
// One row, not two, and holding the new bytes.
|
|
var rows []struct {
|
|
CT []byte `db:"ct"`
|
|
}
|
|
if err := app.DB().Select("ct").From("_private").
|
|
Where(dbx.HashExp{"tag": "watchlist"}).All(&rows); err != nil {
|
|
t.Fatalf("readback failed: %v", err)
|
|
}
|
|
if len(rows) != 1 {
|
|
t.Fatalf("upsert left %d rows, want 1", len(rows))
|
|
}
|
|
if string(rows[0].CT) != "REPLACED-ciphertext" {
|
|
t.Fatalf("ciphertext not replaced: got %q", rows[0].CT)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: "PUT empty body → 400 (must DELETE)",
|
|
Method: http.MethodPut,
|
|
URL: "/v1/private/empty",
|
|
Body: bytes.NewReader([]byte{}),
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
ExpectedStatus: 400,
|
|
ExpectedContent: []string{`"status":400`, "Empty ciphertext"},
|
|
},
|
|
|
|
// --- Reads (seed pre-row via BeforeTestFunc) ---
|
|
{
|
|
Name: "LIST returns seeded tag",
|
|
Method: http.MethodGet,
|
|
URL: "/v1/private",
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
|
|
if err := seedPrivateRow(app, "watchlist", ciphertext); err != nil {
|
|
t.Fatalf("seed failed: %v", err)
|
|
}
|
|
},
|
|
ExpectedStatus: 200,
|
|
ExpectedContent: []string{
|
|
`"tag":"watchlist"`,
|
|
`"size":8`,
|
|
`"updated_at":1700000000000`,
|
|
},
|
|
},
|
|
{
|
|
Name: "GET returns seeded ciphertext bytes",
|
|
Method: http.MethodGet,
|
|
URL: "/v1/private/watchlist",
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
|
|
if err := seedPrivateRow(app, "watchlist", ciphertext); err != nil {
|
|
t.Fatalf("seed failed: %v", err)
|
|
}
|
|
},
|
|
AfterTestFunc: func(t testing.TB, app *tests.TestApp, res *http.Response) {
|
|
if ct := res.Header.Get("Content-Type"); ct != "application/octet-stream" {
|
|
t.Errorf("expected octet-stream, got %q", ct)
|
|
}
|
|
if ua := res.Header.Get("X-Private-Updated-At"); ua != "1700000000000" {
|
|
t.Errorf("expected updated_at header, got %q", ua)
|
|
}
|
|
},
|
|
ExpectedStatus: 200,
|
|
// The body is raw binary — bypass the content string check by
|
|
// asserting that the response doesn't contain the JSON envelope.
|
|
NotExpectedContent: []string{`"status"`, `"data"`},
|
|
},
|
|
{
|
|
Name: "GET non-existent tag → 404",
|
|
Method: http.MethodGet,
|
|
URL: "/v1/private/nope",
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
ExpectedStatus: 404,
|
|
ExpectedContent: []string{`"status":404`},
|
|
},
|
|
|
|
// --- Delete ---
|
|
{
|
|
Name: "DELETE seeded tag → 204",
|
|
Method: http.MethodDelete,
|
|
URL: "/v1/private/watchlist",
|
|
Headers: map[string]string{"Authorization": privateUserAToken},
|
|
ExpectedStatus: 204,
|
|
BeforeTestFunc: func(t testing.TB, app *tests.TestApp, e *core.ServeEvent) {
|
|
if err := seedPrivateRow(app, "watchlist", ciphertext); err != nil {
|
|
t.Fatalf("seed failed: %v", err)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, s := range scenarios {
|
|
s.Test(t)
|
|
}
|
|
}
|