package oltp
import (
"strings"
"testing"
)
// The forge's own binding lives under the EMPTY name, which is the
// same convention the fleet default for capability categories uses.
// BindBucket refused it, so the forge's own storage could not be bound
// at all: the page offered the form, the probe passed, and the write
// came back saying a binding "needs a repository" — on a page that has
// none.
func TestTheForgesOwnBindingCanActuallyBeBound(t *testing.T) {
d := testDB(t)
b := BucketBinding{Endpoint: "https://acct.r2.cloudflarestorage.com",
Bucket: "forge", Region: "auto", AccessKey: "k", Secret: "s"}
if err := d.BindBucket(ForgeBucket, b); err != nil {
t.Fatalf("FAIL: the forge's own bucket was refused: %v", err)
}
got, ok, err := d.Bucket(ForgeBucket)
if err != nil || !ok {
t.Fatalf("FAIL: it did not come back (ok=%v err=%v)", ok, err)
}
if got.Bucket != "forge" || got.Endpoint != b.Endpoint {
t.Errorf("FAIL: stored %+v", got)
}
// The secret round-trips through the seal rather than being lost.
if got.Secret != "s" {
t.Errorf("FAIL: secret came back %q", got.Secret)
}
}
// Every other field is still required, and the message no longer names
// a field this row does not have.
func TestABindingStillNeedsItsEndpointAndKeys(t *testing.T) {
d := testDB(t)
full := BucketBinding{Endpoint: "https://e", Bucket: "b", AccessKey: "k", Secret: "s"}
for _, c := range []struct {
name string
b BucketBinding
}{
{"no endpoint", BucketBinding{Bucket: "b", AccessKey: "k", Secret: "s"}},
{"no bucket", BucketBinding{Endpoint: "https://e", AccessKey: "k", Secret: "s"}},
{"no key", BucketBinding{Endpoint: "https://e", Bucket: "b", Secret: "s"}},
{"no secret", BucketBinding{Endpoint: "https://e", Bucket: "b", AccessKey: "k"}},
} {
if err := d.BindBucket(ForgeBucket, c.b); err == nil {
t.Errorf("FAIL: %s was accepted", c.name)
} else if strings.Contains(err.Error(), "repository") {
t.Errorf("FAIL: %s names a repository: %v", c.name, err)
}
}
// And a repository's own binding is unaffected.
if err := d.BindBucket("quarry", full); err != nil {
t.Errorf("FAIL: a repository's binding broke: %v", err)
}
}