package goncfiles_test import ( "encoding/base64" "encoding/json" "io" "net/http" "net/http/httptest" "strings" "testing" gonexapp "gitea.neitzel.de/konrad/go-nc-exapp" "gitea.neitzel.de/konrad/go-nc-files" ) const testPrefKey = "savedDefault" // appAPIPathStore adapts go-nc-exapp AppAPIPreferences to DefaultPathStore (ExApp wiring pattern). type appAPIPathStore struct { gonexapp.AppAPIPreferences } func (a appAPIPathStore) GetDefault() (string, error) { return a.Get() } func (a appAPIPathStore) SetDefault(v string) error { return a.Set(v) } func TestWebDAVModeUsesPreferencesNotSettingsFile(t *testing.T) { dirs := map[string]bool{} prefsVals := map[string]string{} srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { case strings.Contains(r.URL.Path, "ex-app/preference/get-values"): _ = json.NewEncoder(w).Encode(map[string]any{ "ocs": map[string]any{ "data": []map[string]string{{ "configkey": testPrefKey, "configvalue": prefsVals[testPrefKey], }}, }, }) case strings.HasSuffix(r.URL.Path, "ex-app/preference") && r.Method == http.MethodPost: var body struct { ConfigKey string `json:"configKey"` ConfigValue string `json:"configValue"` } _ = json.NewDecoder(r.Body).Decode(&body) prefsVals[body.ConfigKey] = body.ConfigValue w.WriteHeader(http.StatusOK) _ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) case r.Method == "MKCOL": dirs[strings.TrimSuffix(r.URL.Path, "/")] = true w.WriteHeader(http.StatusCreated) case r.Method == "PROPFIND": p := strings.TrimSuffix(r.URL.Path, "/") if !dirs[p] { http.NotFound(w, r) return } w.WriteHeader(http.StatusMultiStatus) _, _ = io.WriteString(w, ``+r.URL.Path+``) default: http.Error(w, "unhandled "+r.Method+" "+r.URL.Path, 500) } })) t.Cleanup(srv.Close) cred := gonexapp.Credentials{ BaseURL: srv.URL, AppID: "myexapp", AppVersion: "0.1.0", AAVersion: "1.0.0", AppSecret: "s", UserID: "alice", } root := goncfiles.WebDAVRoot{Cred: cred, Client: srv.Client()} prefs := gonexapp.NewAppAPIPreferences(cred, "myexapp", testPrefKey) prefs.Client = srv.Client() prefs.OCS.Client = srv.Client() store := appAPIPathStore{AppAPIPreferences: prefs} visit, err := goncfiles.Open(root, store, testInitialFolder, "") if err != nil { t.Fatal(err) } if visit.SavedDefault != testInitialFolder { t.Fatalf("saved %s", visit.SavedDefault) } if err := visit.SetDefault("Zones"); err != nil { t.Fatal(err) } if prefsVals[testPrefKey] != "Zones" { t.Fatalf("prefs not written: %#v", prefsVals) } again, err := goncfiles.Open(root, store, testInitialFolder, "") if err != nil { t.Fatal(err) } if again.SavedDefault != "Zones" { t.Fatalf("got %s", again.SavedDefault) } } func TestWebDAVVisitForbiddenFailsClosed(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "no", http.StatusForbidden) })) t.Cleanup(srv.Close) cred := gonexapp.Credentials{ BaseURL: srv.URL, AppID: "myexapp", AppVersion: "0.1.0", AAVersion: "1.0.0", AppSecret: "s", UserID: "alice", } _, err := goncfiles.Open( goncfiles.WebDAVRoot{Cred: cred, Client: srv.Client()}, &goncfiles.Memory{}, testInitialFolder, "Secret", ) if err == nil || !goncfiles.IsForbidden(err) { t.Fatalf("got %v", err) } } func TestUserFromRequestRoundTrip(t *testing.T) { token := base64.StdEncoding.EncodeToString([]byte("alice:secret")) r := httptest.NewRequest(http.MethodGet, "/", nil) r.Header.Set("AUTHORIZATION-APP-API", token) user, err := gonexapp.UserFromRequest(r) if err != nil || user != "alice" { t.Fatalf("user=%q err=%v", user, err) } }