Changeset 0.9.0 (#50)
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/config"
|
||||
"git.gobha.me/xcaliber/chat-switchboard/store"
|
||||
)
|
||||
|
||||
func testConfig() *config.Config {
|
||||
@@ -23,12 +24,16 @@ func testConfig() *config.Config {
|
||||
}
|
||||
}
|
||||
|
||||
// testAuthHandler creates an AuthHandler with nil stores (safe for non-DB tests).
|
||||
func testAuthHandler() *AuthHandler {
|
||||
return NewAuthHandler(testConfig(), store.Stores{})
|
||||
}
|
||||
|
||||
// ── JWT Token Tests ─────────────────────────
|
||||
|
||||
func TestJWTGeneration(t *testing.T) {
|
||||
cfg := testConfig()
|
||||
|
||||
// Create a token the same way the handler does
|
||||
now := time.Now()
|
||||
claims := Claims{
|
||||
UserID: "550e8400-e29b-41d4-a716-446655440000",
|
||||
@@ -130,13 +135,11 @@ func TestBcryptHash(t *testing.T) {
|
||||
t.Fatalf("Failed to hash: %v", err)
|
||||
}
|
||||
|
||||
// Correct password should match
|
||||
err = bcrypt.CompareHashAndPassword(hash, []byte(password))
|
||||
if err != nil {
|
||||
t.Error("Correct password should match hash")
|
||||
}
|
||||
|
||||
// Wrong password should not match
|
||||
err = bcrypt.CompareHashAndPassword(hash, []byte("wrongPassword"))
|
||||
if err == nil {
|
||||
t.Error("Wrong password should not match hash")
|
||||
@@ -153,7 +156,6 @@ func TestBcryptDifferentHashesForSamePassword(t *testing.T) {
|
||||
t.Error("Same password should produce different hashes (salt)")
|
||||
}
|
||||
|
||||
// Both should still verify
|
||||
if bcrypt.CompareHashAndPassword(hash1, []byte(password)) != nil {
|
||||
t.Error("hash1 should verify")
|
||||
}
|
||||
@@ -168,22 +170,18 @@ func TestTokenHash(t *testing.T) {
|
||||
token := "abc123refreshtoken"
|
||||
hash := hashToken(token)
|
||||
|
||||
// Should be deterministic
|
||||
if hashToken(token) != hash {
|
||||
t.Error("hashToken should be deterministic")
|
||||
}
|
||||
|
||||
// Different token should produce different hash
|
||||
if hashToken("different") == hash {
|
||||
t.Error("Different tokens should produce different hashes")
|
||||
}
|
||||
|
||||
// Should be hex-encoded SHA-256 (64 chars)
|
||||
if len(hash) != 64 {
|
||||
t.Errorf("Expected 64 char hex hash, got %d chars", len(hash))
|
||||
}
|
||||
|
||||
// Verify it's actually SHA-256
|
||||
expected := sha256.Sum256([]byte(token))
|
||||
expectedHex := hex.EncodeToString(expected[:])
|
||||
if hash != expectedHex {
|
||||
@@ -194,7 +192,7 @@ func TestTokenHash(t *testing.T) {
|
||||
// ── Request Validation Tests ────────────────
|
||||
|
||||
func TestRegisterValidation(t *testing.T) {
|
||||
h := NewAuthHandler(testConfig())
|
||||
h := testAuthHandler()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -211,21 +209,11 @@ func TestRegisterValidation(t *testing.T) {
|
||||
body: `{"username":"test","email":"test@example.com"}`,
|
||||
wantCode: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "invalid email",
|
||||
body: `{"username":"test","email":"notanemail","password":"12345678"}`,
|
||||
wantCode: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "password too short",
|
||||
body: `{"username":"test","email":"test@example.com","password":"short"}`,
|
||||
wantCode: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
name: "username too short",
|
||||
body: `{"username":"ab","email":"test@example.com","password":"12345678"}`,
|
||||
wantCode: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -247,7 +235,7 @@ func TestRegisterValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLoginValidation(t *testing.T) {
|
||||
h := NewAuthHandler(testConfig())
|
||||
h := testAuthHandler()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -289,7 +277,7 @@ func TestLoginValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRefreshValidation(t *testing.T) {
|
||||
h := NewAuthHandler(testConfig())
|
||||
h := testAuthHandler()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
@@ -305,7 +293,7 @@ func TestRefreshValidation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLogoutWithoutToken(t *testing.T) {
|
||||
h := NewAuthHandler(testConfig())
|
||||
h := testAuthHandler()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
Reference in New Issue
Block a user