Files
deklarix/internal/auth/auth_test.go
noroot 2a16dc2200 feat: add auth and multi-tenancy (Schritt 2, part 1/2)
internal/auth is pure logic (bcrypt hashing, session token generation)
with no DB access — persistence for account/app_user/session lives in
internal/store like everything else, via migration 0003.

account is the tenant (Mandant); app_user is a login inside one account;
session is a real server-side row (not a signed stateless token) so
logout can actually end a session rather than the client just
forgetting a JWT. submission.account_id is NOT NULL — added directly
rather than the nullable-then-backfill dance, since no submission rows
exist anywhere yet (verified empty on the test server before writing
the migration). Added as migration 0003 (new file), not folded into an
earlier one, since 0001/0002 are already applied on the test server.

store.ErrNotFound lets callers distinguish "wrong email" / "unknown
session" from a genuine DB error — matters for login, where those two
cases should both fail closed but for different reasons.

Not yet wired into internal/web — that's the next commit. All of this
is tested against real Postgres (14 store tests green) but isn't
reachable from any HTTP handler yet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-27 15:48:40 +02:00

51 lines
1.3 KiB
Go

package auth_test
import (
"testing"
"github.com/netcell-it/deklarix/internal/auth"
)
func TestHashPasswordRejectsShortPassword(t *testing.T) {
if _, err := auth.HashPassword("kurz"); err == nil {
t.Fatal("expected error for a too-short password, got nil")
}
}
func TestHashAndVerifyRoundTrip(t *testing.T) {
hash, err := auth.HashPassword("ein-langes-passwort")
if err != nil {
t.Fatalf("HashPassword: %v", err)
}
if err := auth.VerifyPassword(hash, "ein-langes-passwort"); err != nil {
t.Fatalf("VerifyPassword: %v", err)
}
}
func TestVerifyPasswordRejectsWrongPassword(t *testing.T) {
hash, err := auth.HashPassword("ein-langes-passwort")
if err != nil {
t.Fatalf("HashPassword: %v", err)
}
if err := auth.VerifyPassword(hash, "falsches-passwort"); err == nil {
t.Fatal("expected error for a wrong password, got nil")
}
}
func TestNewSessionTokenIsRandomAndCorrectLength(t *testing.T) {
a, err := auth.NewSessionToken()
if err != nil {
t.Fatalf("NewSessionToken: %v", err)
}
b, err := auth.NewSessionToken()
if err != nil {
t.Fatalf("NewSessionToken: %v", err)
}
if a == b {
t.Fatal("expected two distinct tokens, got the same value twice")
}
if len(a) != 64 {
t.Fatalf("token length = %d, want 64 (hex of 32 random bytes)", len(a))
}
}