|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "path/filepath" |
4 | 8 | "testing" |
5 | 9 | "time" |
| 10 | + |
| 11 | + "github.com/JeremiahM37/librarr/internal/config" |
| 12 | + "github.com/JeremiahM37/librarr/internal/db" |
6 | 13 | ) |
7 | 14 |
|
8 | 15 | func TestSessionStore_CreateAndGet(t *testing.T) { |
@@ -219,3 +226,91 @@ func TestIsExempt(t *testing.T) { |
219 | 226 | }) |
220 | 227 | } |
221 | 228 | } |
| 229 | + |
| 230 | +func TestHandleAuthStatus_OIDCHints(t *testing.T) { |
| 231 | + // /api/auth/status is the canonical pre-auth endpoint the login modal |
| 232 | + // hits to decide whether to render the SSO button. /api/config is gated |
| 233 | + // behind the multi-user middleware once any user exists, so the modal |
| 234 | + // MUST be able to read the OIDC hint here or the button silently |
| 235 | + // disappears after the first user registers. |
| 236 | + dir := t.TempDir() |
| 237 | + database, err := db.New(filepath.Join(dir, "test.db")) |
| 238 | + if err != nil { |
| 239 | + t.Fatalf("create test db: %v", err) |
| 240 | + } |
| 241 | + t.Cleanup(func() { database.Close() }) |
| 242 | + sessions := NewSessionStore() |
| 243 | + |
| 244 | + tests := []struct { |
| 245 | + name string |
| 246 | + cfg *config.Config |
| 247 | + wantEnabled bool |
| 248 | + wantProviderName string |
| 249 | + }{ |
| 250 | + { |
| 251 | + name: "nil config", |
| 252 | + cfg: nil, |
| 253 | + wantEnabled: false, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "OIDC disabled", |
| 257 | + cfg: &config.Config{OIDCEnabled: false}, |
| 258 | + wantEnabled: false, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "OIDC partially configured (no client secret)", |
| 262 | + cfg: &config.Config{ |
| 263 | + OIDCEnabled: true, |
| 264 | + OIDCIssuer: "https://idp.example.com", |
| 265 | + OIDCClientID: "client", |
| 266 | + OIDCProviderName: "Ignored", |
| 267 | + }, |
| 268 | + wantEnabled: false, |
| 269 | + }, |
| 270 | + { |
| 271 | + name: "OIDC fully configured", |
| 272 | + cfg: &config.Config{ |
| 273 | + OIDCEnabled: true, |
| 274 | + OIDCIssuer: "https://idp.example.com", |
| 275 | + OIDCClientID: "client", |
| 276 | + OIDCClientSecret: "secret", |
| 277 | + OIDCProviderName: "PocketID", |
| 278 | + }, |
| 279 | + wantEnabled: true, |
| 280 | + wantProviderName: "PocketID", |
| 281 | + }, |
| 282 | + } |
| 283 | + |
| 284 | + for _, tt := range tests { |
| 285 | + t.Run(tt.name, func(t *testing.T) { |
| 286 | + req := httptest.NewRequest(http.MethodGet, "/api/auth/status", nil) |
| 287 | + rr := httptest.NewRecorder() |
| 288 | + handleAuthStatus(tt.cfg, database, sessions)(rr, req) |
| 289 | + |
| 290 | + if rr.Code != http.StatusOK { |
| 291 | + t.Fatalf("status = %d, want 200; body=%s", rr.Code, rr.Body.String()) |
| 292 | + } |
| 293 | + var resp map[string]any |
| 294 | + if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil { |
| 295 | + t.Fatalf("decode body: %v", err) |
| 296 | + } |
| 297 | + enabled, _ := resp["oidc_enabled"].(bool) |
| 298 | + if enabled != tt.wantEnabled { |
| 299 | + t.Errorf("oidc_enabled = %v, want %v", enabled, tt.wantEnabled) |
| 300 | + } |
| 301 | + if tt.wantEnabled { |
| 302 | + if got, _ := resp["oidc_provider_name"].(string); got != tt.wantProviderName { |
| 303 | + t.Errorf("oidc_provider_name = %q, want %q", got, tt.wantProviderName) |
| 304 | + } |
| 305 | + // MUST NOT leak the client secret on the public endpoint. |
| 306 | + for k, v := range resp { |
| 307 | + if s, ok := v.(string); ok && s == "secret" { |
| 308 | + t.Errorf("response leaks secret-like value at key %q", k) |
| 309 | + } |
| 310 | + } |
| 311 | + } else if _, present := resp["oidc_provider_name"]; present { |
| 312 | + t.Errorf("oidc_provider_name MUST be absent when OIDC is disabled, got %v", resp["oidc_provider_name"]) |
| 313 | + } |
| 314 | + }) |
| 315 | + } |
| 316 | +} |
0 commit comments