package gonexapp_test import ( "encoding/base64" "fmt" "io" "net/http" "net/http/httptest" "strings" gonexapp "gitea.neitzel.de/konrad/go-nc-exapp" ) func ExampleUserFromRequest() { token := base64.StdEncoding.EncodeToString([]byte("alice:secret")) req := httptest.NewRequest(http.MethodGet, "/api", nil) req.Header.Set("AUTHORIZATION-APP-API", token) user, err := gonexapp.UserFromRequest(req) if err != nil { fmt.Println("err:", err) return } fmt.Println(user) // Output: alice } func ExampleCredentials_AuthHeaders() { cred := gonexapp.Credentials{ BaseURL: "https://nextcloud.example", AppID: "myexapp", AppVersion: "0.1.0", AAVersion: "1.0.0", AppSecret: "s", UserID: "alice", } h := cred.AuthHeaders() fmt.Println(h.Get("EX-APP-ID"), h.Get("OCS-APIRequest") != "") // Output: myexapp true } func ExampleNewAppAPIPreferences() { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if strings.Contains(r.URL.Path, "get-values") { _, _ = io.WriteString(w, `{"ocs":{"data":[{"configkey":"savedDefault","configvalue":"Zones"}]}}`) return } w.WriteHeader(http.StatusOK) _, _ = io.WriteString(w, `{"ocs":{"data":{}}}`) })) defer srv.Close() cred := gonexapp.Credentials{ BaseURL: srv.URL, AppID: "myexapp", AppVersion: "0.1.0", AAVersion: "1.0.0", AppSecret: "s", UserID: "alice", } prefs := gonexapp.NewAppAPIPreferences(cred, "myexapp", "savedDefault") prefs.Client = srv.Client() prefs.OCS.Client = srv.Client() value, err := prefs.Get() if err != nil { fmt.Println("err:", err) return } if err := prefs.Set("Zones"); err != nil { fmt.Println("set:", err) return } fmt.Println(value) // Output: Zones } func ExampleAccessGate_Wrap() { inner := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "ok") }) h := gonexapp.AccessGate{}.Wrap(inner) rec := httptest.NewRecorder() h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/api", nil)) fmt.Println(rec.Code, rec.Body.String()) // Output: 200 ok } func ExampleResolveRequiredGroups() { fmt.Println(gonexapp.ResolveRequiredGroups("", false, nil)) fmt.Println(len(gonexapp.ResolveRequiredGroups("", true, []string{"ops"}))) // Output: // [] // 0 } func ExampleTopMenuAdminRequired() { fmt.Println(gonexapp.TopMenuAdminRequired("0", gonexapp.DefaultTopMenuAdminRequired)) fmt.Println(gonexapp.TopMenuAdminRequired("maybe", gonexapp.DefaultTopMenuAdminRequired)) // Output: // 0 // 1 }