claude-web-chat/config_test.go
djuka 6c0ca3a96f
All checks were successful
Tests / unit-tests (push) Successful in 25s
Dodato kreiranje projekta kroz UI i change-password template
- CreateProject() sa validacijom imena (regex, duplikati, prazno)
- POST /projects/create ruta sa AuthMiddleware
- Modal forma na projects stranici za unos imena
- 10 unit testova za CreateProject
- change-password.html template i testovi
- Ažuriran TESTING.md sa novom sekcijom

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-18 06:27:10 +00:00

180 lines
4.2 KiB
Go

package main
import (
"os"
"path/filepath"
"testing"
)
func TestLoadConfig(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.json")
t.Run("valid config", func(t *testing.T) {
data := `{
"port": 9100,
"mode": "cli",
"projects_path": "/tmp/projects",
"username": "admin",
"password": "secret",
"session_secret": "abc123"
}`
os.WriteFile(cfgPath, []byte(data), 0644)
cfg, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Port != 9100 {
t.Errorf("port = %d, want 9100", cfg.Port)
}
if cfg.Mode != "cli" {
t.Errorf("mode = %q, want cli", cfg.Mode)
}
if cfg.ProjectsPath != "/tmp/projects" {
t.Errorf("projects_path = %q, want /tmp/projects", cfg.ProjectsPath)
}
if !cfg.CheckPassword("secret") {
t.Error("CheckPassword should return true for original password")
}
})
t.Run("password hashed on load", func(t *testing.T) {
data := `{
"username": "admin",
"password": "myplainpass",
"session_secret": "abc123"
}`
os.WriteFile(cfgPath, []byte(data), 0644)
cfg, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !isHashed(cfg.Password) {
t.Error("password should be hashed after load")
}
if !cfg.CheckPassword("myplainpass") {
t.Error("CheckPassword should work with original password")
}
})
t.Run("already hashed password not re-hashed", func(t *testing.T) {
// Prvo učitaj da se hešuje
data := `{
"username": "admin",
"password": "testpass",
"session_secret": "abc123"
}`
os.WriteFile(cfgPath, []byte(data), 0644)
cfg1, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("first load: %v", err)
}
hash1 := cfg1.Password
// Ponovo učitaj — heš treba da ostane isti
cfg2, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("second load: %v", err)
}
if cfg2.Password != hash1 {
t.Error("already hashed password should not be re-hashed")
}
})
t.Run("defaults applied", func(t *testing.T) {
data := `{
"username": "admin",
"password": "secret",
"session_secret": "abc123"
}`
os.WriteFile(cfgPath, []byte(data), 0644)
cfg, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Port != 9100 {
t.Errorf("port = %d, want 9100", cfg.Port)
}
if cfg.Mode != "cli" {
t.Errorf("mode = %q, want cli", cfg.Mode)
}
if cfg.ProjectsPath != "/root/projects" {
t.Errorf("projects_path = %q, want /root/projects", cfg.ProjectsPath)
}
})
t.Run("missing username", func(t *testing.T) {
data := `{"password": "secret", "session_secret": "abc"}`
os.WriteFile(cfgPath, []byte(data), 0644)
_, err := LoadConfig(cfgPath)
if err == nil {
t.Fatal("expected error for missing username")
}
})
t.Run("missing password", func(t *testing.T) {
data := `{"username": "admin", "session_secret": "abc"}`
os.WriteFile(cfgPath, []byte(data), 0644)
_, err := LoadConfig(cfgPath)
if err == nil {
t.Fatal("expected error for missing password")
}
})
t.Run("missing session_secret", func(t *testing.T) {
data := `{"username": "admin", "password": "secret"}`
os.WriteFile(cfgPath, []byte(data), 0644)
_, err := LoadConfig(cfgPath)
if err == nil {
t.Fatal("expected error for missing session_secret")
}
})
t.Run("file not found", func(t *testing.T) {
_, err := LoadConfig("/nonexistent/config.json")
if err == nil {
t.Fatal("expected error for missing file")
}
})
t.Run("invalid json", func(t *testing.T) {
os.WriteFile(cfgPath, []byte("{invalid"), 0644)
_, err := LoadConfig(cfgPath)
if err == nil {
t.Fatal("expected error for invalid JSON")
}
})
t.Run("api config", func(t *testing.T) {
data := `{
"username": "admin",
"password": "secret",
"session_secret": "abc123",
"api": {
"key": "sk-ant-xxx",
"model": "claude-sonnet-4-20250514"
}
}`
os.WriteFile(cfgPath, []byte(data), 0644)
cfg, err := LoadConfig(cfgPath)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.API.Key != "sk-ant-xxx" {
t.Errorf("api.key = %q, want sk-ant-xxx", cfg.API.Key)
}
if cfg.API.Model != "claude-sonnet-4-20250514" {
t.Errorf("api.model = %q", cfg.API.Model)
}
})
}