mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-06 21:36:37 +08:00
prefer openlist api credentials
This commit is contained in:
@@ -146,6 +146,83 @@ func TestStorageConfigUploadLocalToOpenListAPI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageConfigUploadLocalToOpenListAPIWithUsernamePassword(t *testing.T) {
|
||||
var loginSeen bool
|
||||
var uploaded []string
|
||||
openlist := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.URL.Path {
|
||||
case "/api/auth/login":
|
||||
loginSeen = true
|
||||
var body map[string]string
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
t.Fatalf("decode login body: %v", err)
|
||||
}
|
||||
if body["username"] != "alice" || body["password"] != "secret" {
|
||||
t.Fatalf("login body = %#v", body)
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"code":200,"data":{"token":"openlist-session-token"}}`))
|
||||
case "/api/fs/mkdir":
|
||||
if r.Header.Get("Authorization") != "openlist-session-token" {
|
||||
t.Fatalf("mkdir authorization = %q", r.Header.Get("Authorization"))
|
||||
}
|
||||
_, _ = w.Write([]byte(`{"code":200,"message":"success"}`))
|
||||
case "/api/fs/get":
|
||||
if r.Header.Get("Authorization") != "openlist-session-token" {
|
||||
t.Fatalf("get authorization = %q", r.Header.Get("Authorization"))
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"code":404,"message":"not found"}`))
|
||||
case "/api/fs/put":
|
||||
if r.Header.Get("Authorization") != "openlist-session-token" {
|
||||
t.Fatalf("put authorization = %q", r.Header.Get("Authorization"))
|
||||
}
|
||||
decoded, err := url.PathUnescape(r.Header.Get("File-Path"))
|
||||
if err != nil {
|
||||
t.Fatalf("decode file path: %v", err)
|
||||
}
|
||||
uploaded = append(uploaded, decoded)
|
||||
_, _ = w.Write([]byte(`{"code":200,"message":"success"}`))
|
||||
case "/dav":
|
||||
t.Fatal("OpenList username/password upload should use API, not WebDAV")
|
||||
default:
|
||||
t.Fatalf("unexpected openlist path %s", r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer openlist.Close()
|
||||
|
||||
_, storage := newStorageUploadTestService(t)
|
||||
if _, err := storage.Save(t.Context(), StorageInput{
|
||||
Type: "openlist",
|
||||
Config: map[string]any{
|
||||
"server": openlist.URL,
|
||||
"username": "alice",
|
||||
"password": "secret",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(source, "Movie.2026.mkv"), []byte("movie"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := storage.UploadLocal(t.Context(), CloudUploadInput{
|
||||
Type: "openlist",
|
||||
SourcePath: source,
|
||||
DestPath: "/OpenList",
|
||||
Recursive: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upload local: %v", err)
|
||||
}
|
||||
if !loginSeen {
|
||||
t.Fatal("expected OpenList API login")
|
||||
}
|
||||
if res.Uploaded != 1 || len(uploaded) != 1 || uploaded[0] != "/OpenList/Movie.2026.mkv" {
|
||||
t.Fatalf("result = %+v uploaded=%#v", res, uploaded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStorageConfigOpenListHTTPSAgainstHTTPHint(t *testing.T) {
|
||||
openlist := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"code":200}`))
|
||||
|
||||
Reference in New Issue
Block a user