tercul-backend/internal/data/sql/user_repository_test.go
google-labs-jules[bot] c2e9a118e2 feat(testing): Increase test coverage and fix authz bugs
This commit significantly increases the test coverage across the application and fixes several underlying bugs that were discovered while writing the new tests.

The key changes include:

- **New Tests:** Added extensive integration and unit tests for GraphQL resolvers, application services, and data repositories, substantially increasing the test coverage for packages like `graphql`, `user`, `translation`, and `analytics`.

- **Authorization Bug Fixes:**
  - Fixed a critical bug where a user creating a `Work` was not correctly associated as its author, causing subsequent permission failures.
  - Corrected the authorization logic in `authz.Service` to properly check for entity ownership by non-admin users.

- **Test Refactoring:**
  - Refactored numerous test suites to use `testify/mock` instead of manual mocks, improving test clarity and maintainability.
  - Isolated integration tests by creating a fresh admin user and token for each test run, eliminating test pollution.
  - Centralized domain errors into `internal/domain/errors.go` and updated repositories to use them, making error handling more consistent.

- **Code Quality Improvements:**
  - Replaced manual mock implementations with `testify/mock` for better consistency.
  - Cleaned up redundant and outdated test files.

These changes stabilize the test suite, improve the overall quality of the codebase, and move the project closer to the goal of 80% test coverage.
2025-10-09 07:03:45 +00:00

114 lines
3.1 KiB
Go

package sql_test
import (
"context"
"testing"
"tercul/internal/data/sql"
"tercul/internal/domain"
"tercul/internal/platform/config"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type UserRepositoryTestSuite struct {
testutil.IntegrationTestSuite
UserRepo domain.UserRepository
}
func (s *UserRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
cfg, err := config.LoadConfig()
s.Require().NoError(err)
s.UserRepo = sql.NewUserRepository(s.DB, cfg)
}
func (s *UserRepositoryTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()
s.DB.Exec("DELETE FROM users")
}
func TestUserRepository(t *testing.T) {
suite.Run(t, new(UserRepositoryTestSuite))
}
func (s *UserRepositoryTestSuite) createUser(username, email string, role domain.UserRole) *domain.User {
user := &domain.User{
Username: username,
Email: email,
Role: role,
}
err := s.UserRepo.Create(context.Background(), user)
s.Require().NoError(err)
return user
}
func (s *UserRepositoryTestSuite) TestFindByUsername() {
s.Run("should find a user by username", func() {
// Arrange
expectedUser := s.createUser("testuser", "test@test.com", domain.UserRoleReader)
// Act
foundUser, err := s.UserRepo.FindByUsername(context.Background(), "testuser")
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundUser)
s.Equal(expectedUser.ID, foundUser.ID)
s.Equal("testuser", foundUser.Username)
})
s.Run("should return error if user not found", func() {
_, err := s.UserRepo.FindByUsername(context.Background(), "nonexistent")
s.Require().Error(err)
s.ErrorIs(err, domain.ErrEntityNotFound)
})
}
func (s *UserRepositoryTestSuite) TestFindByEmail() {
s.Run("should find a user by email", func() {
// Arrange
expectedUser := s.createUser("testuser", "test@test.com", domain.UserRoleReader)
// Act
foundUser, err := s.UserRepo.FindByEmail(context.Background(), "test@test.com")
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundUser)
s.Equal(expectedUser.ID, foundUser.ID)
s.Equal("test@test.com", foundUser.Email)
})
s.Run("should return error if user not found", func() {
_, err := s.UserRepo.FindByEmail(context.Background(), "nonexistent@test.com")
s.Require().Error(err)
s.ErrorIs(err, domain.ErrEntityNotFound)
})
}
func (s *UserRepositoryTestSuite) TestListByRole() {
s.Run("should return all users for a given role", func() {
// Arrange
s.createUser("reader1", "reader1@test.com", domain.UserRoleReader)
s.createUser("reader2", "reader2@test.com", domain.UserRoleReader)
s.createUser("admin1", "admin1@test.com", domain.UserRoleAdmin)
// Act
readers, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleReader)
s.Require().NoError(err)
admins, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleAdmin)
s.Require().NoError(err)
// Assert
s.Len(readers, 2)
s.Len(admins, 1)
})
s.Run("should return empty slice if no users for role", func() {
users, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleContributor)
s.Require().NoError(err)
s.Len(users, 0)
})
}