feat(go-crm): full auth, routing, middleware, and supporting infra
- Add auth handlers (signup, login, logout, account management) with bcrypt - Add client, customer, service, scheduling, payment, question, answer handlers - Add dashboard, monthly report, and lead pipeline pages - Add UTF-8 middleware to force charset on HTML responses - Add config package with env-based overrides for DB path, secrets, endpoints - Add parser package for WhatsApp message ingestion - Add clean-arch layers: pkg/domain, pkg/repo, pkg/usecase for leads - Add cmd/migrate utility for DB migrations - Add Makefile, README, run-tests.sh, and dev scripts - Update docker-compose.yml with memory limits - Update .air.toml to exclude DB files and stop on errors - Update whatsapp-sync dependencies and add src/index.js entrypoint - Add whatsme standalone WhatsApp reader app (source only) - Untrack .opencode-sandbox/data/go-crm.db from git history - Expand root .gitignore: ngrok, tmp dirs, sandbox DBs, compiled binaries
This commit is contained in:
@@ -210,4 +210,128 @@ func TestLeadsQRPolling(t *testing.T) {
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListClientsHasBackToHomeLink(t *testing.T) {
|
||||
tmpfile, err := os.CreateTemp("", "test_*.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(tmpfile.Name())
|
||||
tmpfile.Close()
|
||||
|
||||
testDB, err := db.Init(tmpfile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to init db: %v", err)
|
||||
}
|
||||
defer testDB.Close()
|
||||
|
||||
DB = testDB
|
||||
WAConnector = whatsapp.NewFakeConnector()
|
||||
|
||||
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)
|
||||
_, err = testDB.Exec(
|
||||
"INSERT INTO accounts (email, name, password, created_at) VALUES (?, ?, ?, ?)",
|
||||
"test@example.com", "Test Account", string(hashedPassword), time.Now().Unix(),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create account: %v", err)
|
||||
}
|
||||
|
||||
var accountID int64
|
||||
err = testDB.QueryRow("SELECT account_id FROM accounts WHERE email = ?", "test@example.com").Scan(&accountID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get account id: %v", err)
|
||||
}
|
||||
|
||||
sessionID := "test-session-nav"
|
||||
expires := time.Now().Add(time.Hour).Unix()
|
||||
_, err = testDB.Exec(
|
||||
"INSERT INTO sessions (session_id, account_id, expires) VALUES (?, ?, ?)",
|
||||
sessionID, accountID, expires,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create session: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/clients", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "session", Value: sessionID})
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ListClients(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
|
||||
if !strings.Contains(body, `href="/"`) {
|
||||
t.Error("expected clients page to have Back to Home link")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListClientsShowsConnectButtonForUnconnectedClients(t *testing.T) {
|
||||
tmpfile, err := os.CreateTemp("", "test_*.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(tmpfile.Name())
|
||||
tmpfile.Close()
|
||||
|
||||
testDB, err := db.Init(tmpfile.Name())
|
||||
if err != nil {
|
||||
t.Fatalf("failed to init db: %v", err)
|
||||
}
|
||||
defer testDB.Close()
|
||||
|
||||
DB = testDB
|
||||
WAConnector = whatsapp.NewFakeConnector()
|
||||
|
||||
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("password"), bcrypt.DefaultCost)
|
||||
_, err = testDB.Exec(
|
||||
"INSERT INTO accounts (email, name, password, created_at) VALUES (?, ?, ?, ?)",
|
||||
"test@example.com", "Test Account", string(hashedPassword), time.Now().Unix(),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create account: %v", err)
|
||||
}
|
||||
|
||||
var accountID int64
|
||||
err = testDB.QueryRow("SELECT account_id FROM accounts WHERE email = ?", "test@example.com").Scan(&accountID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get account id: %v", err)
|
||||
}
|
||||
|
||||
var clientID int64
|
||||
testDB.QueryRow(
|
||||
"INSERT INTO clients (account_id, name, phone, created_at) VALUES (?, ?, ?, ?) RETURNING client_id",
|
||||
accountID, "Unconnected Client", "+5521987654321", time.Now().Unix(),
|
||||
).Scan(&clientID)
|
||||
|
||||
sessionID := "test-session-connect-btn"
|
||||
expires := time.Now().Add(time.Hour).Unix()
|
||||
_, err = testDB.Exec(
|
||||
"INSERT INTO sessions (session_id, account_id, expires) VALUES (?, ?, ?)",
|
||||
sessionID, accountID, expires,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create session: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/clients", nil)
|
||||
req.AddCookie(&http.Cookie{Name: "session", Value: sessionID})
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ListClients(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
body := w.Body.String()
|
||||
|
||||
if !strings.Contains(body, "leads/connect?client_id=") {
|
||||
t.Error("expected clients page to show Connect link for unconnected clients")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user