mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-27 00:49:41 -05:00
+5








8f2e8add9a
Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: spatiumstas <79056064+spatiumstas@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Andreas Fehn <fehnomenal@fehn.systems> Co-authored-by: Matt Van Horn <mvanhorn@users.noreply.github.com> Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: Takahiro Shimizu <anatofuz@gmail.com> Co-authored-by: Graham Steffaniak <gtsteffaniak@users.noreply.github.com> Co-authored-by: Joel Town Road <13539685+hippi345@users.noreply.github.com> Co-authored-by: beemines <3083452174@qq.com> Co-authored-by: Bee mines <Beemines82@gmail.com> Co-authored-by: dweebzxx <134675545+dweebzxx@users.noreply.github.com> Co-authored-by: Stephan Paternotte <Stephan-P@users.noreply.github.com>
69 lines
2.1 KiB
Go
69 lines
2.1 KiB
Go
package web
|
|
|
|
import (
|
|
"archive/zip"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/archiveencoding"
|
|
)
|
|
|
|
func TestUnarchiveEncodingPreviewDoesNotWriteOrDelete(t *testing.T) {
|
|
root, user := setupUploadHTTPTest(t)
|
|
archivePath := filepath.Join(root, "test.zip")
|
|
f, err := os.Create(archivePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zw := zip.NewWriter(f)
|
|
w, err := zw.CreateHeader(&zip.FileHeader{Name: "\x83\x65\x83\x58\x83\x67.txt", NonUTF8: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = w.Write([]byte("hello")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
body := `{"fromSource":"uploads","path":"/test.zip","destination":"/","preview":true,"deleteAfter":true}`
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/resources/unarchive", strings.NewReader(body))
|
|
status, err := unarchiveHandler(rec, req, &Context{User: user})
|
|
if err != nil || status != http.StatusOK {
|
|
t.Fatalf("status=%d err=%v body=%s", status, err, rec.Body.String())
|
|
}
|
|
var preview archiveencoding.Preview
|
|
if err = json.Unmarshal(rec.Body.Bytes(), &preview); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if preview.Suggested != "cp932" {
|
|
t.Fatalf("preview: %+v", preview)
|
|
}
|
|
entries, err := os.ReadDir(root)
|
|
if err != nil || len(entries) != 1 || entries[0].Name() != "test.zip" {
|
|
t.Fatalf("preview modified source: %v, %v", entries, err)
|
|
}
|
|
|
|
// The preview goes through the same source permission checks as extraction.
|
|
perms := user.BackendSourcePermissions[root]
|
|
perms.Download = false
|
|
user.BackendSourcePermissions[root] = perms
|
|
user.BackendScopes[0].Permissions.Download = false
|
|
rec = httptest.NewRecorder()
|
|
req = httptest.NewRequest(http.MethodPost, "/api/resources/unarchive", strings.NewReader(body))
|
|
status, err = unarchiveHandler(rec, req, &Context{User: user})
|
|
if status != http.StatusForbidden || err == nil {
|
|
t.Fatalf("expected denied preview, got %d, %v", status, err)
|
|
}
|
|
}
|