Compare commits

..

2 Commits

Author SHA1 Message Date
4faf16c84b refactor: use switch on vote.Type 2025-11-13 08:02:52 +01:00
028bde4737 refactor: extract common setup logic 2025-11-13 08:02:45 +01:00
2 changed files with 66 additions and 51 deletions

View File

@@ -11,7 +11,6 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/crypto/bcrypt"
"goyco/internal/database" "goyco/internal/database"
"goyco/internal/handlers" "goyco/internal/handlers"
"goyco/internal/middleware" "goyco/internal/middleware"
@@ -19,6 +18,8 @@ import (
"goyco/internal/server" "goyco/internal/server"
"goyco/internal/services" "goyco/internal/services"
"goyco/internal/testutils" "goyco/internal/testutils"
"golang.org/x/crypto/bcrypt"
) )
type testContext struct { type testContext struct {
@@ -27,10 +28,19 @@ type testContext struct {
AuthService *services.AuthFacade AuthService *services.AuthFacade
} }
func setupTestContext(t *testing.T) *testContext { type commonHandlers struct {
AuthService *services.AuthFacade
VoteService *services.VoteService
MetadataService services.TitleFetcher
AuthHandler *handlers.AuthHandler
PostHandler *handlers.PostHandler
VoteHandler *handlers.VoteHandler
UserHandler *handlers.UserHandler
APIHandler *handlers.APIHandler
}
func setupCommonHandlers(t *testing.T, suite *testutils.ServiceSuite, useMonitoring bool) *commonHandlers {
t.Helper() t.Helper()
middleware.StopAllRateLimiters()
suite := testutils.NewServiceSuite(t)
authService, err := services.NewAuthFacadeForTest(testutils.AppTestConfig, suite.UserRepo, suite.PostRepo, suite.DeletionRepo, suite.RefreshTokenRepo, suite.EmailSender) authService, err := services.NewAuthFacadeForTest(testutils.AppTestConfig, suite.UserRepo, suite.PostRepo, suite.DeletionRepo, suite.RefreshTokenRepo, suite.EmailSender)
if err != nil { if err != nil {
@@ -44,32 +54,61 @@ func setupTestContext(t *testing.T) *testContext {
postHandler := handlers.NewPostHandler(suite.PostRepo, metadataService, voteService) postHandler := handlers.NewPostHandler(suite.PostRepo, metadataService, voteService)
voteHandler := handlers.NewVoteHandler(voteService) voteHandler := handlers.NewVoteHandler(voteService)
userHandler := handlers.NewUserHandler(suite.UserRepo, authService) userHandler := handlers.NewUserHandler(suite.UserRepo, authService)
apiHandler := handlers.NewAPIHandlerWithMonitoring(testutils.AppTestConfig, suite.PostRepo, suite.UserRepo, voteService, suite.DB, middleware.NewInMemoryDBMonitor())
staticDir := t.TempDir() var apiHandler *handlers.APIHandler
robotsFile := filepath.Join(staticDir, "robots.txt") if useMonitoring {
os.WriteFile(robotsFile, []byte("User-agent: *\nDisallow: /"), 0644) apiHandler = handlers.NewAPIHandlerWithMonitoring(testutils.AppTestConfig, suite.PostRepo, suite.UserRepo, voteService, suite.DB, middleware.NewInMemoryDBMonitor())
} else {
apiHandler = handlers.NewAPIHandler(testutils.AppTestConfig, suite.PostRepo, suite.UserRepo, voteService)
}
router := server.NewRouter(server.RouterConfig{ return &commonHandlers{
AuthService: authService,
VoteService: voteService,
MetadataService: metadataService,
AuthHandler: authHandler, AuthHandler: authHandler,
PostHandler: postHandler, PostHandler: postHandler,
VoteHandler: voteHandler, VoteHandler: voteHandler,
UserHandler: userHandler, UserHandler: userHandler,
APIHandler: apiHandler, APIHandler: apiHandler,
AuthService: authService, }
PageHandler: nil, }
func buildRouterConfig(h *commonHandlers, staticDir string, pageHandler *handlers.PageHandler) server.RouterConfig {
return server.RouterConfig{
AuthHandler: h.AuthHandler,
PostHandler: h.PostHandler,
VoteHandler: h.VoteHandler,
UserHandler: h.UserHandler,
APIHandler: h.APIHandler,
AuthService: h.AuthService,
PageHandler: pageHandler,
StaticDir: staticDir, StaticDir: staticDir,
Debug: false, Debug: false,
DisableCache: false, DisableCache: false,
DisableCompression: false, DisableCompression: false,
DBMonitor: middleware.NewInMemoryDBMonitor(), DBMonitor: middleware.NewInMemoryDBMonitor(),
RateLimitConfig: testutils.AppTestConfig.RateLimit, RateLimitConfig: testutils.AppTestConfig.RateLimit,
}) }
}
func setupTestContext(t *testing.T) *testContext {
t.Helper()
middleware.StopAllRateLimiters()
suite := testutils.NewServiceSuite(t)
h := setupCommonHandlers(t, suite, true)
staticDir := t.TempDir()
robotsFile := filepath.Join(staticDir, "robots.txt")
os.WriteFile(robotsFile, []byte("User-agent: *\nDisallow: /"), 0644)
router := server.NewRouter(buildRouterConfig(h, staticDir, nil))
return &testContext{ return &testContext{
Router: router, Router: router,
Suite: suite, Suite: suite,
AuthService: authService, AuthService: h.AuthService,
} }
} }
@@ -78,19 +117,7 @@ func setupPageHandlerTestContext(t *testing.T) *testContext {
middleware.StopAllRateLimiters() middleware.StopAllRateLimiters()
suite := testutils.NewServiceSuite(t) suite := testutils.NewServiceSuite(t)
authService, err := services.NewAuthFacadeForTest(testutils.AppTestConfig, suite.UserRepo, suite.PostRepo, suite.DeletionRepo, suite.RefreshTokenRepo, suite.EmailSender) h := setupCommonHandlers(t, suite, false)
if err != nil {
t.Fatalf("Failed to create auth service: %v", err)
}
voteService := services.NewVoteService(suite.VoteRepo, suite.PostRepo, suite.DB)
metadataService := suite.TitleFetcher
authHandler := handlers.NewAuthHandler(authService, suite.UserRepo)
postHandler := handlers.NewPostHandler(suite.PostRepo, metadataService, voteService)
voteHandler := handlers.NewVoteHandler(voteService)
userHandler := handlers.NewUserHandler(suite.UserRepo, authService)
apiHandler := handlers.NewAPIHandler(testutils.AppTestConfig, suite.PostRepo, suite.UserRepo, voteService)
staticDir := t.TempDir() staticDir := t.TempDir()
templatesDir := t.TempDir() templatesDir := t.TempDir()
@@ -162,31 +189,17 @@ func setupPageHandlerTestContext(t *testing.T) *testContext {
confirmDeleteTemplate := `{{define "content"}}<h1>Confirm Delete</h1>{{end}}` confirmDeleteTemplate := `{{define "content"}}<h1>Confirm Delete</h1>{{end}}`
os.WriteFile(filepath.Join(templatesDir, "confirm_delete.gohtml"), []byte(confirmDeleteTemplate), 0644) os.WriteFile(filepath.Join(templatesDir, "confirm_delete.gohtml"), []byte(confirmDeleteTemplate), 0644)
pageHandler, err := handlers.NewPageHandler(templatesDir, authService, suite.PostRepo, voteService, suite.UserRepo, metadataService, testutils.AppTestConfig) pageHandler, err := handlers.NewPageHandler(templatesDir, h.AuthService, suite.PostRepo, h.VoteService, suite.UserRepo, h.MetadataService, testutils.AppTestConfig)
if err != nil { if err != nil {
t.Fatalf("Failed to create page handler: %v", err) t.Fatalf("Failed to create page handler: %v", err)
} }
router := server.NewRouter(server.RouterConfig{ router := server.NewRouter(buildRouterConfig(h, staticDir, pageHandler))
AuthHandler: authHandler,
PostHandler: postHandler,
VoteHandler: voteHandler,
UserHandler: userHandler,
APIHandler: apiHandler,
AuthService: authService,
PageHandler: pageHandler,
StaticDir: staticDir,
Debug: false,
DisableCache: false,
DisableCompression: false,
DBMonitor: middleware.NewInMemoryDBMonitor(),
RateLimitConfig: testutils.AppTestConfig.RateLimit,
})
return &testContext{ return &testContext{
Router: router, Router: router,
Suite: suite, Suite: suite,
AuthService: authService, AuthService: h.AuthService,
} }
} }

View File

@@ -4,9 +4,10 @@ import (
"fmt" "fmt"
"testing" "testing"
"golang.org/x/crypto/bcrypt"
"goyco/internal/database" "goyco/internal/database"
"goyco/internal/repositories" "goyco/internal/repositories"
"golang.org/x/crypto/bcrypt"
) )
func TestIntegration_Repositories(t *testing.T) { func TestIntegration_Repositories(t *testing.T) {
@@ -320,9 +321,10 @@ func TestIntegration_Repositories(t *testing.T) {
var upVotes, downVotes int64 var upVotes, downVotes int64
for _, vote := range votes { for _, vote := range votes {
if vote.Type == database.VoteUp { switch vote.Type {
case database.VoteUp:
upVotes++ upVotes++
} else if vote.Type == database.VoteDown { case database.VoteDown:
downVotes++ downVotes++
} }
} }