tercul-backend/internal/testutil/mock_jwt_manager.go
google-labs-jules[bot] 80cfe71e59 refactor: Refactor GraphQL tests to use mock-based unit tests
This commit refactors the GraphQL test suite to resolve persistent build failures and establish a stable, mock-based unit testing environment.

The key changes include:
- Consolidating all GraphQL test helper functions into a single, canonical file (`internal/adapters/graphql/graphql_test_utils_test.go`).
- Removing duplicated test helper code from `integration_test.go` and other test files.
- Creating a new, dedicated unit test file for the `like` and `unlike` mutations (`internal/adapters/graphql/like_resolvers_unit_test.go`) using a mock-based approach.
- Introducing mock services (`MockLikeService`, `MockAnalyticsService`) and updating mock repositories (`MockLikeRepository`, `MockWorkRepository`) in the `internal/testutil` package to support `testify/mock`.
- Adding a `ContextWithUserID` helper function to `internal/platform/auth/middleware.go` to facilitate testing of authenticated resolvers.

These changes resolve the `redeclared in this block` and package collision errors, resulting in a clean and passing test suite. This provides a solid foundation for future Test-Driven Development.
2025-10-03 09:21:41 +00:00

40 lines
1.1 KiB
Go

package testutil
import (
"tercul/internal/domain"
"tercul/internal/platform/auth"
"time"
"github.com/golang-jwt/jwt/v5"
)
// MockJWTManager is a mock implementation of the JWTManagement interface.
type MockJWTManager struct{}
// NewMockJWTManager creates a new MockJWTManager.
func NewMockJWTManager() auth.JWTManagement {
return &MockJWTManager{}
}
// GenerateToken generates a dummy token for a user.
func (m *MockJWTManager) GenerateToken(user *domain.User) (string, error) {
return "dummy-token-for-" + user.Username, nil
}
// ValidateToken validates a dummy token.
func (m *MockJWTManager) ValidateToken(tokenString string) (*auth.Claims, error) {
if tokenString != "" {
// A real implementation would parse the user from the token.
// For this mock, we'll just return a generic user.
return &auth.Claims{
UserID: 1,
Username: "testuser",
Email: "test@test.com",
Role: "reader",
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)),
},
}, nil
}
return nil, auth.ErrInvalidToken
}