package web
import (
"strings"
"testing"
)
// The attach form's own action is a real multipart POST that uploads
// through quarry. attach.js replaces it with the direct
// browser-to-bucket road — faster, and the forge never holds the bytes
// — but that road is not always open: a bucket that does not allow this
// origin refuses the preflight and the browser reports a bare network
// error with no status to read.
//
// The listener used to preventDefault unconditionally and then show
// that error, so the page offered a picker and an Upload button, took
// the press, and failed, while the form underneath would have worked.
func TestTheAttachFormIsUsedWhenTheDirectRoadIsShut(t *testing.T) {
js := string(attachJS)
for what, want := range map[string]string{
"leaves a known-blocked form alone": "if (section.querySelector('[data-hook=attach-cors]')) return;",
"marks an unreachable bucket": "e.unreachable = true",
"hands the press back to the form": "form.submit()",
"only falls back on an unreachable": "e.unreachable",
} {
if !strings.Contains(js, want) {
t.Errorf("FAIL: the uploader no longer %s (%q)", what, want)
} else {
t.Logf("ok: the uploader %s", what)
}
}
// A refusal that HAS a status is a real one the form would meet
// too, so it is still reported rather than retried.
if !strings.Contains(js, "'the bucket answered ' + xhr.status") {
t.Error("FAIL: a real refusal is no longer reported")
} else {
t.Log("ok: a refusal with a status is still shown")
}
}
// The note beside a working form must not say uploading is blocked.
// What is blocked is the direct road; the form goes through quarry.
func TestTheCorsNoteDoesNotContradictTheFormBesideIt(t *testing.T) {
if strings.Contains(attachSource, "Browser uploads are blocked") {
t.Error("FAIL: the note says uploads are blocked, beside a picker and an Upload button")
} else {
t.Log("ok: the note no longer says uploading is blocked")
}
if !strings.Contains(attachSource, "Uploads go through quarry") {
t.Error("FAIL: the note does not say what still works")
} else {
t.Log("ok: the note says the form still uploads")
}
}