Improve installer extraction, login failures, and upload handling

This commit is contained in:
2026-07-27 00:17:30 +08:00
parent 97ea6fb7aa
commit 73555cd04c
14 changed files with 199 additions and 31 deletions
@@ -678,6 +678,31 @@ func TestAdminReleasePackageUploadRejectsMissingAndOversizedFile(t *testing.T) {
}
}
func TestAdminReleasePackageUploadReportsInterruptedMultipart(t *testing.T) {
handler, cleanup := testRouter(t)
defer cleanup()
session, csrf, err := loginForTest(handler)
if err != nil {
t.Fatal(err)
}
const boundary = "ymhut-interrupted-upload"
body := bytes.NewBufferString("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"package.exe\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\npartial package")
req := httptest.NewRequest(http.MethodPost, "/api/admin/releases/packages", body)
req.Header.Set("Content-Type", "multipart/form-data; boundary="+boundary)
req.AddCookie(&http.Cookie{Name: auth.SessionCookie, Value: session})
req.Header.Set("X-CSRF-Token", csrf)
res := httptest.NewRecorder()
handler.ServeHTTP(res, req)
if res.Code != http.StatusBadRequest || !strings.Contains(res.Body.String(), "UPLOAD_INTERRUPTED") {
t.Fatalf("interrupted upload returned %d %s", res.Code, res.Body.String())
}
}
func newReleaseUploadRequest(t *testing.T, name string, data []byte, includeFile bool) *http.Request {
t.Helper()
var body bytes.Buffer
@@ -950,7 +975,7 @@ func TestAdminLoginFailureReturnsImmediatelyAndIsNotCached(t *testing.T) {
if time.Since(started) > time.Second {
t.Fatal("invalid login request did not return promptly")
}
if res.Code != http.StatusOK || !strings.Contains(res.Body.String(), "LOGIN_FAILED") {
if res.Code != http.StatusOK || !strings.Contains(res.Body.String(), "CAPTCHA_INVALID") {
t.Fatalf("invalid login returned %d: %s", res.Code, res.Body.String())
}
if got := res.Header().Get("Cache-Control"); got != "no-store" {
@@ -958,6 +983,15 @@ func TestAdminLoginFailureReturnsImmediatelyAndIsNotCached(t *testing.T) {
}
}
func TestRemoteHostRemovesEphemeralPort(t *testing.T) {
if got := remoteHost("127.0.0.1:51842"); got != "127.0.0.1" {
t.Fatalf("remoteHost returned %q", got)
}
if got := remoteHost("[::1]:51842"); got != "::1" {
t.Fatalf("remoteHost returned %q", got)
}
}
func readTestCaptcha(dataURL string) (string, error) {
const prefix = "data:image/png;base64,"
raw, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(dataURL, prefix))