tercul-backend/internal/app/user/commands_test.go
google-labs-jules[bot] f675c98e80 Fix: Correct authorization logic in integration tests
The integration tests for admin-only mutations were failing due to an authorization issue. The root cause was that the JWT token used in the tests did not reflect the user's admin role, which was being set directly in the database.

This commit fixes the issue by:
1.  Updating the `CreateAuthenticatedUser` test helper to generate a new JWT token after a user's role is changed. This ensures the token contains the correct, up-to-date role.
2.  Removing all uses of `auth.ContextWithAdminUser` from the integration tests, making the JWT token the single source of truth for authorization.

This change also removes unused imports and variables that were causing build failures after the refactoring. All integration tests now pass.
2025-10-04 23:48:44 +00:00

102 lines
2.7 KiB
Go

package user
import (
"context"
"testing"
"tercul/internal/app/authz"
"tercul/internal/domain"
platform_auth "tercul/internal/platform/auth"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type UserCommandsSuite struct {
suite.Suite
repo *mockUserRepository
authzSvc *authz.Service
commands *UserCommands
}
func (s *UserCommandsSuite) SetupTest() {
s.repo = &mockUserRepository{}
workRepo := &mockWorkRepoForUserTests{}
s.authzSvc = authz.NewService(workRepo, nil) // Translation repo not needed for user tests
s.commands = NewUserCommands(s.repo, s.authzSvc)
}
func TestUserCommandsSuite(t *testing.T) {
suite.Run(t, new(UserCommandsSuite))
}
func (s *UserCommandsSuite) TestUpdateUser_Success_Self() {
// Arrange
ctx := platform_auth.ContextWithUserID(context.Background(), 1)
input := UpdateUserInput{ID: 1, Username: strPtr("new_username")}
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.User, error) {
return &domain.User{BaseModel: domain.BaseModel{ID: 1}}, nil
}
// Act
updatedUser, err := s.commands.UpdateUser(ctx, input)
// Assert
assert.NoError(s.T(), err)
assert.NotNil(s.T(), updatedUser)
assert.Equal(s.T(), "new_username", updatedUser.Username)
}
func (s *UserCommandsSuite) TestUpdateUser_Success_Admin() {
// Arrange
ctx := platform_auth.ContextWithAdminUser(context.Background(), 99) // Admin user
input := UpdateUserInput{ID: 1, Username: strPtr("new_username_by_admin")}
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.User, error) {
return &domain.User{BaseModel: domain.BaseModel{ID: 1}}, nil
}
// Act
updatedUser, err := s.commands.UpdateUser(ctx, input)
// Assert
assert.NoError(s.T(), err)
assert.NotNil(s.T(), updatedUser)
assert.Equal(s.T(), "new_username_by_admin", updatedUser.Username)
}
func (s *UserCommandsSuite) TestUpdateUser_Forbidden() {
// Arrange
ctx := platform_auth.ContextWithUserID(context.Background(), 2) // Different user
input := UpdateUserInput{ID: 1, Username: strPtr("forbidden_username")}
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.User, error) {
return &domain.User{BaseModel: domain.BaseModel{ID: 1}}, nil
}
// Act
_, err := s.commands.UpdateUser(ctx, input)
// Assert
assert.Error(s.T(), err)
assert.ErrorIs(s.T(), err, domain.ErrForbidden)
}
func (s *UserCommandsSuite) TestUpdateUser_Unauthorized() {
// Arrange
ctx := context.Background() // No user in context
input := UpdateUserInput{ID: 1, Username: strPtr("unauthorized_username")}
// Act
_, err := s.commands.UpdateUser(ctx, input)
// Assert
assert.Error(s.T(), err)
assert.ErrorIs(s.T(), err, domain.ErrUnauthorized)
}
// Helper to get a pointer to a string
func strPtr(s string) *string {
return &s
}