backend barebone done, big chunk of work done
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Package auth_test tests password hashing and verification.
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"messenger/internal/auth"
|
||||
)
|
||||
|
||||
func TestHashAndVerify(t *testing.T) {
|
||||
pwd := "secret"
|
||||
hash, err := auth.HashPassword(pwd)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword error: %v", err)
|
||||
}
|
||||
ok, err := auth.VerifyPassword(hash, pwd)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword error: %v", err)
|
||||
}
|
||||
if !ok {
|
||||
t.Fatal("expected password to verify")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrongPassword(t *testing.T) {
|
||||
hash, _ := auth.HashPassword("correct")
|
||||
ok, err := auth.VerifyPassword(hash, "wrong")
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword error on wrong password: %v", err)
|
||||
}
|
||||
if ok {
|
||||
t.Fatal("wrong password should not verify")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidHashFormat(t *testing.T) {
|
||||
_, err := auth.VerifyPassword("invalidformat", "pwd")
|
||||
if err == nil {
|
||||
t.Fatal("expected error on invalid hash format")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDifferentSalts(t *testing.T) {
|
||||
h1, _ := auth.HashPassword("pass")
|
||||
h2, _ := auth.HashPassword("pass")
|
||||
if h1 == h2 {
|
||||
t.Fatal("expected different hashes for same password due to salt")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user