mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-26 16:39: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>
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package auth_test
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/gtsteffaniak/filebrowser/backend/internal/auth"
|
|
)
|
|
|
|
func TestExtractGroupsFromHeader_SingleValue(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-Cosmos-Role", "2")
|
|
|
|
groups, present := auth.ExtractGroupsFromHeader(req, "x-cosmos-role")
|
|
if !present {
|
|
t.Fatal("expected groups to be present")
|
|
}
|
|
if len(groups) != 1 || groups[0] != "2" {
|
|
t.Fatalf("groups = %#v, want [\"2\"]", groups)
|
|
}
|
|
}
|
|
|
|
func TestExtractGroupsFromHeader_CommaSeparated(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-Groups", "1, 2")
|
|
|
|
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
|
|
if !present {
|
|
t.Fatal("expected groups to be present")
|
|
}
|
|
if len(groups) != 2 || groups[0] != "1" || groups[1] != "2" {
|
|
t.Fatalf("groups = %#v, want [\"1\", \"2\"]", groups)
|
|
}
|
|
}
|
|
|
|
func TestExtractGroupsFromHeader_JSONArray(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-Groups", `["admin","users"]`)
|
|
|
|
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
|
|
if !present {
|
|
t.Fatal("expected groups to be present")
|
|
}
|
|
if len(groups) != 2 || groups[0] != "admin" || groups[1] != "users" {
|
|
t.Fatalf("groups = %#v, want [\"admin\", \"users\"]", groups)
|
|
}
|
|
}
|
|
|
|
func TestExtractGroupsFromHeader_Missing(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
groups, present := auth.ExtractGroupsFromHeader(req, "X-Groups")
|
|
if present {
|
|
t.Fatalf("expected groups to be absent, got %#v", groups)
|
|
}
|
|
}
|
|
|
|
func TestExtractGroupsFromHeader_EmptyHeaderName(t *testing.T) {
|
|
req, err := http.NewRequest(http.MethodGet, "/", http.NoBody)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("X-Groups", "admin")
|
|
|
|
groups, present := auth.ExtractGroupsFromHeader(req, "")
|
|
if present {
|
|
t.Fatalf("expected groups to be absent, got %#v", groups)
|
|
}
|
|
}
|