forked from Nixius/authelia
1
0
Fork 0
ATLAS/docker/ss-atlas/internal/handlers/handlers_test.go

98 lines
2.3 KiB
Go

package handlers
import (
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"testing"
"git.nixc.us/a250/ss-atlas/internal/config"
)
func TestContains(t *testing.T) {
tests := []struct {
s string
sub string
want bool
}{
{"customers,admins", "customers", true},
{"admins,customers", "customers", true},
{"customers", "customers", true},
{"admins,users", "customers", false},
{"", "customers", false},
{"cus", "customers", false},
{"customersx", "customers", true},
}
for _, tt := range tests {
if got := contains(tt.s, tt.sub); got != tt.want {
t.Errorf("contains(%q, %q) = %v, want %v", tt.s, tt.sub, got, tt.want)
}
}
}
func TestSearchString(t *testing.T) {
tests := []struct {
s string
sub string
want bool
}{
{"customers,admins", "customers", true},
{"admins", "admins", true},
{"a", "a", true},
{"abc", "b", true},
{"abc", "d", false},
{"", "a", false},
}
for _, tt := range tests {
if got := searchString(tt.s, tt.sub); got != tt.want {
t.Errorf("searchString(%q, %q) = %v, want %v", tt.s, tt.sub, got, tt.want)
}
}
}
func TestSanitizeUsername(t *testing.T) {
tests := []struct {
email string
want string
}{
{"user@example.com", "user-example"},
{"User.Name@domain.com", "user-name-domain"},
{"user_name@domain.com", "user_name-domain"},
{"user123@domain.com", "user123-domain"},
{"UPPER@domain.com", "upper-domain"},
{"a.b.c@x.com", "a-b-c-x"},
{"spécial@x.com", "sp-cial-x"},
{"alice@nixc.us", "alice-nixc"},
}
for _, tt := range tests {
if got := sanitizeUsername(tt.email); got != tt.want {
t.Errorf("sanitizeUsername(%q) = %q, want %q", tt.email, got, tt.want)
}
}
}
func TestHealthRoute(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
rootDir := filepath.Join(filepath.Dir(filename), "..", "..")
templatePath := filepath.Join(rootDir, "templates")
cfg := &config.Config{
Port: "8080",
TemplatePath: templatePath,
}
router := NewRouter(cfg, nil, nil, nil)
req := httptest.NewRequest(http.MethodGet, "/health", nil)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("GET /health status = %d, want 200", rec.Code)
}
if body := rec.Body.String(); body != "ok" {
t.Errorf("GET /health body = %q, want ok", body)
}
}