tercul-backend/internal/adapters/graphql/like_resolvers_unit_test.go
google-labs-jules[bot] 9fd2331eb4 feat: Implement production-ready API patterns
This commit introduces a comprehensive set of foundational improvements to make the API more robust, secure, and observable.

The following features have been implemented:

- **Observability Stack:** A new `internal/observability` package has been added, providing structured logging with `zerolog`, Prometheus metrics, and OpenTelemetry tracing. This stack is fully integrated into the application's request pipeline.

- **Centralized Authorization:** A new `internal/app/authz` service has been created to centralize authorization logic. This service is now used by the `user`, `work`, and `comment` services to protect all Create, Update, and Delete operations.

- **Standardized Input Validation:** The previous ad-hoc validation has been replaced with a more robust, struct-tag-based system using the `go-playground/validator` library. This has been applied to all GraphQL input models.

- **Structured Error Handling:** A new set of custom error types has been introduced in the `internal/domain` package. A custom `gqlgen` error presenter has been implemented to map these domain errors to structured GraphQL error responses with specific error codes.

- **`updateUser` Endpoint:** The `updateUser` mutation has been fully implemented as a proof of concept for the new patterns, including support for partial updates and comprehensive authorization checks.

- **Test Refactoring:** The test suite has been significantly improved by decoupling mock repositories from the shared `testutil` package, resolving circular dependency issues and making the tests more maintainable.
2025-10-04 18:16:08 +00:00

120 lines
3.6 KiB
Go

package graphql_test
import (
"context"
"fmt"
"strconv"
"testing"
"tercul/internal/adapters/graphql"
"tercul/internal/adapters/graphql/model"
"tercul/internal/app"
"tercul/internal/app/analytics"
"tercul/internal/app/like"
"tercul/internal/domain"
platform_auth "tercul/internal/platform/auth"
"tercul/internal/testutil"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)
// LikeResolversUnitSuite is a unit test suite for the like resolvers.
type LikeResolversUnitSuite struct {
suite.Suite
resolver *graphql.Resolver
mockLikeRepo *testutil.MockLikeRepository
mockWorkRepo *mockWorkRepository
mockAnalyticsSvc *testutil.MockAnalyticsService
}
func (s *LikeResolversUnitSuite) SetupTest() {
// 1. Create mock repositories
s.mockLikeRepo = new(testutil.MockLikeRepository)
s.mockWorkRepo = new(mockWorkRepository)
s.mockAnalyticsSvc = new(testutil.MockAnalyticsService)
// 2. Create real services with mock repositories
likeService := like.NewService(s.mockLikeRepo)
analyticsService := analytics.NewService(s.mockAnalyticsSvc, nil, nil, nil, nil)
// 3. Create the resolver with the services
s.resolver = &graphql.Resolver{
App: &app.Application{
Like: likeService,
Analytics: analyticsService,
},
}
}
func TestLikeResolversUnitSuite(t *testing.T) {
suite.Run(t, new(LikeResolversUnitSuite))
}
func (s *LikeResolversUnitSuite) TestCreateLike() {
// 1. Setup
workIDStr := "1"
workIDUint64, _ := strconv.ParseUint(workIDStr, 10, 32)
workIDUint := uint(workIDUint64)
userID := uint(123)
// Mock repository responses
s.mockWorkRepo.On("Exists", mock.Anything, workIDUint).Return(true, nil)
s.mockLikeRepo.On("Create", mock.Anything, mock.AnythingOfType("*domain.Like")).Run(func(args mock.Arguments) {
arg := args.Get(1).(*domain.Like)
arg.ID = 1 // Simulate database assigning an ID
}).Return(nil)
s.mockAnalyticsSvc.On("IncrementWorkCounter", mock.Anything, workIDUint, "likes", 1).Return(nil)
// Create a context with an authenticated user
ctx := platform_auth.ContextWithUserID(context.Background(), userID)
// 2. Execution
likeInput := model.LikeInput{
WorkID: &workIDStr,
}
createdLike, err := s.resolver.Mutation().CreateLike(ctx, likeInput)
// 3. Assertions
s.Require().NoError(err)
s.Require().NotNil(createdLike)
s.Equal("1", createdLike.ID)
s.Equal(fmt.Sprintf("%d", userID), createdLike.User.ID)
// Verify that the repository's Create method was called
s.mockLikeRepo.AssertCalled(s.T(), "Create", mock.Anything, mock.MatchedBy(func(l *domain.Like) bool {
return *l.WorkID == workIDUint && l.UserID == userID
}))
// Verify that analytics was called
s.mockAnalyticsSvc.AssertCalled(s.T(), "IncrementWorkCounter", mock.Anything, workIDUint, "likes", 1)
}
func (s *LikeResolversUnitSuite) TestDeleteLike() {
// 1. Setup
likeIDStr := "1"
likeIDUint, _ := strconv.ParseUint(likeIDStr, 10, 32)
userID := uint(123)
// Mock the repository response for the initial 'find'
s.mockLikeRepo.On("GetByID", mock.Anything, uint(likeIDUint)).Return(&domain.Like{
BaseModel: domain.BaseModel{ID: uint(likeIDUint)},
UserID: userID,
}, nil)
// Mock the repository response for the 'delete'
s.mockLikeRepo.On("Delete", mock.Anything, uint(likeIDUint)).Return(nil)
// Create a context with an authenticated user
ctx := platform_auth.ContextWithUserID(context.Background(), userID)
// 2. Execution
deleted, err := s.resolver.Mutation().DeleteLike(ctx, likeIDStr)
// 3. Assertions
s.Require().NoError(err)
s.True(deleted)
// Verify that the repository's Delete method was called
s.mockLikeRepo.AssertCalled(s.T(), "Delete", mock.Anything, uint(likeIDUint))
}