93 lines
3.5 KiB
Go
93 lines
3.5 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func TestDicom3DPreferencesAreSelfEditableAndValidated(t *testing.T) {
|
|
st := scopedUserStorage(t, t.TempDir(), users.Permissions{}, []byte("key"))
|
|
for _, test := range []struct {
|
|
body string
|
|
status int
|
|
}{
|
|
{`{"what":"user","which":["dicom3D"],"data":{"id":1,"dicom3D":{"memoryMiB":512,"quality":"auto","opacity":50}}}`, 200},
|
|
{`{"what":"user","which":["dicom3D"],"data":{"id":1,"dicom3D":{"memoryMiB":2048,"quality":"auto","opacity":50}}}`, 400},
|
|
{`{"what":"user","which":["dicom3D"],"data":{"id":1,"dicom3D":{"quality":"invalid"}}}`, 400},
|
|
} {
|
|
r := mux.SetURLVars(httptest.NewRequest("PUT", "/users/1", strings.NewReader(test.body)), map[string]string{"id": "1"})
|
|
r = authorizeTestRequest(r, testSession(1, "u", false))
|
|
w := httptest.NewRecorder()
|
|
handle(userPutHandler, "", st, &settings.Server{}).ServeHTTP(w, r)
|
|
if w.Code != test.status {
|
|
t.Fatalf("status %d, want %d: %s", w.Code, test.status, w.Body.String())
|
|
}
|
|
}
|
|
u, err := st.Users.Get("", false, uint(1))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.Dicom3D.MemoryMiB != 512 || u.Dicom3D.Opacity != 50 || u.Dicom3D.Quality != "auto" {
|
|
t.Fatalf("settings not persisted: %+v", u.Dicom3D)
|
|
}
|
|
if u.Perm.Admin {
|
|
t.Fatal("preferences changed permissions")
|
|
}
|
|
// Check the same profile representation returned on subsequent SSO sessions.
|
|
if ssoUserInfo(u).Dicom3D != u.Dicom3D {
|
|
t.Fatal("session profile lost preferences")
|
|
}
|
|
}
|
|
|
|
func TestLocalAdminCannotOverrideSSORole(t *testing.T) {
|
|
st := scopedUserStorage(t, t.TempDir(), users.Permissions{Admin: true}, []byte("key"))
|
|
r := authorizeTestRequest(httptest.NewRequest("GET", "/", nil), testSession(1, "u", false))
|
|
w := httptest.NewRecorder()
|
|
handle(withAdmin(func(http.ResponseWriter, *http.Request, *data) (int, error) {
|
|
t.Fatal("local admin bypassed SSO role")
|
|
return 0, nil
|
|
}), "", st, &settings.Server{}).ServeHTTP(w, r)
|
|
if w.Code != 403 {
|
|
t.Fatalf("got %d", w.Code)
|
|
}
|
|
}
|
|
|
|
func TestLocalPasswordMutationsRejected(t *testing.T) {
|
|
st := scopedUserStorage(t, t.TempDir(), users.Permissions{}, []byte("key"))
|
|
for _, body := range []string{`{"what":"user","which":["password"],"data":{"id":1,"password":"password"}}`, `{"what":"user","which":["all"],"data":{"id":1}}`, `{"what":"user","which":["locale"],"data":null}`} {
|
|
r := httptest.NewRequest("PUT", "/users/1", strings.NewReader(body))
|
|
r = mux.SetURLVars(r, map[string]string{"id": "1"})
|
|
r = authorizeTestRequest(r, testSession(1, "u", true))
|
|
w := httptest.NewRecorder()
|
|
handle(userPutHandler, "", st, &settings.Server{}).ServeHTTP(w, r)
|
|
if w.Code != 400 && w.Code != 403 {
|
|
t.Fatalf("mutation accepted: %d", w.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIndexNonceChangesPerResponse(t *testing.T) {
|
|
f := fstest.MapFS{"public/index.html": &fstest.MapFile{Data: []byte(`<script>window.FileBrowser=[{[ .Json ]}];</script>`)}}
|
|
d := &data{settings: &settings.Settings{}, server: &settings.Server{}}
|
|
var previous string
|
|
for i := 0; i < 2; i++ {
|
|
w := httptest.NewRecorder()
|
|
status, err := handleWithStaticData(w, httptest.NewRequest("GET", "/", nil), d, f, "public/index.html", "text/html")
|
|
if err != nil || status != 0 {
|
|
t.Fatal(status, err)
|
|
}
|
|
csp := w.Header().Get("Content-Security-Policy")
|
|
if !strings.Contains(csp, "'nonce-") || csp == previous || !strings.Contains(w.Body.String(), "<script nonce=") {
|
|
t.Fatal("missing or reused nonce")
|
|
}
|
|
previous = csp
|
|
}
|
|
}
|