code cleanup

This commit is contained in:
Fran Jurmanović
2025-09-18 13:33:51 +02:00
parent 901dbe697e
commit 5e7c96697a
83 changed files with 2832 additions and 2186 deletions

View File

@@ -12,30 +12,24 @@ import (
)
func TestJWT_GenerateAndValidateToken(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
jwtHandler := jwt.NewJWTHandler(os.Getenv("JWT_SECRET"))
// Create test user
user := &model.User{
ID: uuid.New(),
Username: "testuser",
RoleID: uuid.New(),
}
// Test JWT generation
token, err := jwtHandler.GenerateToken(user.ID.String())
tests.AssertNoError(t, err)
tests.AssertNotNil(t, token)
// Verify token is not empty
if token == "" {
t.Fatal("Expected non-empty token, got empty string")
}
// Test JWT validation
claims, err := jwtHandler.ValidateToken(token)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, claims)
@@ -43,80 +37,66 @@ func TestJWT_GenerateAndValidateToken(t *testing.T) {
}
func TestJWT_ValidateToken_InvalidToken(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
jwtHandler := jwt.NewJWTHandler(os.Getenv("JWT_SECRET"))
// Test with invalid token
claims, err := jwtHandler.ValidateToken("invalid-token")
if err == nil {
t.Fatal("Expected error for invalid token, got nil")
}
// Direct nil check to avoid the interface wrapping issue
if claims != nil {
t.Fatalf("Expected nil claims, got %v", claims)
}
}
func TestJWT_ValidateToken_EmptyToken(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
jwtHandler := jwt.NewJWTHandler(os.Getenv("JWT_SECRET"))
// Test with empty token
claims, err := jwtHandler.ValidateToken("")
if err == nil {
t.Fatal("Expected error for empty token, got nil")
}
// Direct nil check to avoid the interface wrapping issue
if claims != nil {
t.Fatalf("Expected nil claims, got %v", claims)
}
}
func TestUser_VerifyPassword_Success(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test user
user := &model.User{
ID: uuid.New(),
Username: "testuser",
RoleID: uuid.New(),
}
// Hash password manually (simulating what BeforeCreate would do)
plainPassword := "password123"
hashedPassword, err := password.HashPassword(plainPassword)
tests.AssertNoError(t, err)
user.Password = hashedPassword
// Test password verification - should succeed
err = user.VerifyPassword(plainPassword)
tests.AssertNoError(t, err)
}
func TestUser_VerifyPassword_Failure(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create test user
user := &model.User{
ID: uuid.New(),
Username: "testuser",
RoleID: uuid.New(),
}
// Hash password manually
hashedPassword, err := password.HashPassword("correct_password")
tests.AssertNoError(t, err)
user.Password = hashedPassword
// Test password verification with wrong password - should fail
err = user.VerifyPassword("wrong_password")
if err == nil {
t.Fatal("Expected error for wrong password, got nil")
@@ -124,11 +104,9 @@ func TestUser_VerifyPassword_Failure(t *testing.T) {
}
func TestUser_Validate_Success(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create valid user
user := &model.User{
ID: uuid.New(),
Username: "testuser",
@@ -136,25 +114,21 @@ func TestUser_Validate_Success(t *testing.T) {
RoleID: uuid.New(),
}
// Test validation - should succeed
err := user.Validate()
tests.AssertNoError(t, err)
}
func TestUser_Validate_MissingUsername(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create user without username
user := &model.User{
ID: uuid.New(),
Username: "", // Missing username
Username: "",
Password: "password123",
RoleID: uuid.New(),
}
// Test validation - should fail
err := user.Validate()
if err == nil {
t.Fatal("Expected error for missing username, got nil")
@@ -162,19 +136,16 @@ func TestUser_Validate_MissingUsername(t *testing.T) {
}
func TestUser_Validate_MissingPassword(t *testing.T) {
// Setup
helper := tests.NewTestHelper(t)
defer helper.Cleanup()
// Create user without password
user := &model.User{
ID: uuid.New(),
Username: "testuser",
Password: "", // Missing password
Password: "",
RoleID: uuid.New(),
}
// Test validation - should fail
err := user.Validate()
if err == nil {
t.Fatal("Expected error for missing password, got nil")
@@ -182,24 +153,19 @@ func TestUser_Validate_MissingPassword(t *testing.T) {
}
func TestPassword_HashAndVerify(t *testing.T) {
// Test password hashing and verification directly
plainPassword := "test_password_123"
// Hash password
hashedPassword, err := password.HashPassword(plainPassword)
tests.AssertNoError(t, err)
tests.AssertNotNil(t, hashedPassword)
// Verify hashed password is not the same as plain password
if hashedPassword == plainPassword {
t.Fatal("Hashed password should not equal plain password")
}
// Verify correct password
err = password.VerifyPassword(hashedPassword, plainPassword)
tests.AssertNoError(t, err)
// Verify wrong password fails
err = password.VerifyPassword(hashedPassword, "wrong_password")
if err == nil {
t.Fatal("Expected error for wrong password, got nil")
@@ -215,7 +181,7 @@ func TestPassword_ValidatePasswordStrength(t *testing.T) {
{"Valid password", "StrongPassword123!", false},
{"Too short", "123", true},
{"Empty password", "", true},
{"Medium password", "password123", false}, // Depends on validation rules
{"Medium password", "password123", false},
}
for _, tc := range testCases {
@@ -235,7 +201,6 @@ func TestPassword_ValidatePasswordStrength(t *testing.T) {
}
func TestRole_Model(t *testing.T) {
// Test Role model structure
permissions := []model.Permission{
{ID: uuid.New(), Name: "read"},
{ID: uuid.New(), Name: "write"},
@@ -248,7 +213,6 @@ func TestRole_Model(t *testing.T) {
Permissions: permissions,
}
// Verify role structure
tests.AssertEqual(t, "Test Role", role.Name)
tests.AssertEqual(t, 3, len(role.Permissions))
tests.AssertEqual(t, "read", role.Permissions[0].Name)
@@ -257,19 +221,16 @@ func TestRole_Model(t *testing.T) {
}
func TestPermission_Model(t *testing.T) {
// Test Permission model structure
permission := &model.Permission{
ID: uuid.New(),
Name: "test_permission",
}
// Verify permission structure
tests.AssertEqual(t, "test_permission", permission.Name)
tests.AssertNotNil(t, permission.ID)
}
func TestUser_WithRole_Model(t *testing.T) {
// Test User model with Role relationship
permissions := []model.Permission{
{ID: uuid.New(), Name: "read"},
{ID: uuid.New(), Name: "write"},
@@ -289,7 +250,6 @@ func TestUser_WithRole_Model(t *testing.T) {
Role: role,
}
// Verify user-role relationship
tests.AssertEqual(t, "testuser", user.Username)
tests.AssertEqual(t, role.ID, user.RoleID)
tests.AssertEqual(t, "User", user.Role.Name)