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



![coderabbitai[bot]](/avatar/4a08c485fbe908b864e4cf902fddaff20a4dd8a58faf8f65f051648bd2ef3153?size=40)




45a14bf2f2
Signed-off-by: MichaIng <micha@dietpi.com> Co-authored-by: Kevin B. <kurami32_kbm@proton.me> Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Mattijs Bliek <mattijs@mattijsbliek.com> Co-authored-by: GammelSami <39372285+GammelSami@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: yzxcj797 <54314860+yzxcj797@users.noreply.github.com> Co-authored-by: yzxcj797 <yzxcj797@users.noreply.github.com> Co-authored-by: DireMunchkin <dire.munchkin@proton.me> Co-authored-by: Chris <39135018+Chri6s@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Aditya Raj Singh <github@adityaraj.com> Co-authored-by: tbsagdic <86909795+tbsagdic@users.noreply.github.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: Ethan Lee Qi Yang <pluscompanyhqDEPT@gmail.com> Co-authored-by: Aditya Raj Singh <aditya@bncw.in> Co-authored-by: MichaIng <micha@dietpi.com> Co-authored-by: FandacqkaCorp <david@davidyloreto.com> Co-authored-by: FandacqkaCorp <275647464+FandacqkaCorp@users.noreply.github.com> Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: connorjfarrell <connorjfarrell@users.noreply.github.com>
139 lines
3.7 KiB
Go
139 lines
3.7 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/state"
|
|
"github.com/gtsteffaniak/filebrowser/backend/pkg/settings"
|
|
)
|
|
|
|
func configureCosmosProxyAuth(t *testing.T) {
|
|
t.Helper()
|
|
settings.Config.Auth.Methods.ProxyAuth = settings.ProxyAuthConfig{
|
|
AuthCommon: settings.AuthCommon{
|
|
Enabled: true,
|
|
GroupsClaim: "x-cosmos-role",
|
|
UserGroups: []string{"2", "1"},
|
|
AdminGroup: "2",
|
|
},
|
|
Header: "X-Cosmos-User",
|
|
}
|
|
t.Cleanup(func() {
|
|
settings.Config.Auth.Methods.ProxyAuth = settings.ProxyAuthConfig{}
|
|
})
|
|
}
|
|
|
|
func proxyRequest(t *testing.T, username, role string) *http.Request {
|
|
t.Helper()
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if username != "" {
|
|
req.Header.Set("X-Cosmos-User", username)
|
|
}
|
|
if role != "" {
|
|
req.Header.Set("X-Cosmos-Role", role)
|
|
}
|
|
return req
|
|
}
|
|
|
|
func TestSetupProxyUser_CosmosGuestDenied(t *testing.T) {
|
|
setupTestEnv(t)
|
|
configureCosmosProxyAuth(t)
|
|
|
|
_, err := SetupProxyUser(proxyRequest(t, "guest-user", "0"), &Context{}, "guest-user")
|
|
if err == nil {
|
|
t.Fatal("expected guest role to be denied")
|
|
}
|
|
if _, getErr := state.GetUserByUsername("guest-user"); getErr == nil {
|
|
t.Fatal("guest user should not be created")
|
|
}
|
|
}
|
|
|
|
func TestSetupProxyUser_CosmosRegularUserAllowed(t *testing.T) {
|
|
setupTestEnv(t)
|
|
configureCosmosProxyAuth(t)
|
|
|
|
user, err := SetupProxyUser(proxyRequest(t, "regular-user", "1"), &Context{}, "regular-user")
|
|
if err != nil {
|
|
t.Fatalf("SetupProxyUser: %v", err)
|
|
}
|
|
if user.Permissions.Admin {
|
|
t.Fatal("expected regular user without admin")
|
|
}
|
|
}
|
|
|
|
func TestSetupProxyUser_CosmosAdminPromoted(t *testing.T) {
|
|
setupTestEnv(t)
|
|
configureCosmosProxyAuth(t)
|
|
|
|
user, err := SetupProxyUser(proxyRequest(t, "admin-user", "2"), &Context{}, "admin-user")
|
|
if err != nil {
|
|
t.Fatalf("SetupProxyUser: %v", err)
|
|
}
|
|
if !user.Permissions.Admin {
|
|
t.Fatal("expected admin user")
|
|
}
|
|
}
|
|
|
|
func TestSetupProxyUser_CosmosMissingRoleDenied(t *testing.T) {
|
|
setupTestEnv(t)
|
|
configureCosmosProxyAuth(t)
|
|
|
|
_, err := SetupProxyUser(proxyRequest(t, "no-role-user", ""), &Context{}, "no-role-user")
|
|
if err == nil {
|
|
t.Fatal("expected missing role header to be denied when userGroups is set")
|
|
}
|
|
}
|
|
|
|
func TestSetupProxyUser_NoGroupConfigAllowsAll(t *testing.T) {
|
|
setupTestEnv(t)
|
|
settings.Config.Auth.Methods.ProxyAuth = settings.ProxyAuthConfig{
|
|
AuthCommon: settings.AuthCommon{Enabled: true},
|
|
Header: "X-Cosmos-User",
|
|
}
|
|
t.Cleanup(func() {
|
|
settings.Config.Auth.Methods.ProxyAuth = settings.ProxyAuthConfig{}
|
|
})
|
|
|
|
user, err := SetupProxyUser(proxyRequest(t, "legacy-user", ""), &Context{}, "legacy-user")
|
|
if err != nil {
|
|
t.Fatalf("SetupProxyUser: %v", err)
|
|
}
|
|
if user.LoginMethod != users.LoginMethodProxy {
|
|
t.Fatalf("login method = %q, want proxy", user.LoginMethod)
|
|
}
|
|
}
|
|
|
|
func TestSetupProxyUser_CosmosAdminRevokedOnRoleDowngrade(t *testing.T) {
|
|
setupTestEnv(t)
|
|
configureCosmosProxyAuth(t)
|
|
|
|
user, err := SetupProxyUser(proxyRequest(t, "downgrade-user", "2"), &Context{}, "downgrade-user")
|
|
if err != nil {
|
|
t.Fatalf("SetupProxyUser admin: %v", err)
|
|
}
|
|
if !user.Permissions.Admin {
|
|
t.Fatal("expected admin after role 2")
|
|
}
|
|
|
|
user, err = SetupProxyUser(proxyRequest(t, "downgrade-user", "1"), &Context{}, "downgrade-user")
|
|
if err != nil {
|
|
t.Fatalf("SetupProxyUser user: %v", err)
|
|
}
|
|
if user.Permissions.Admin {
|
|
t.Fatal("expected admin revoked after downgrade to role 1")
|
|
}
|
|
|
|
reloaded, err := state.GetUserByUsername("downgrade-user")
|
|
if err != nil {
|
|
t.Fatalf("GetUserByUsername: %v", err)
|
|
}
|
|
if reloaded.Permissions.Admin {
|
|
t.Fatal("expected admin revoked in persisted user record")
|
|
}
|
|
}
|