tercul-backend/internal/app/work/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

149 lines
4.6 KiB
Go

package work
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"tercul/internal/app/authz"
"tercul/internal/domain"
workdomain "tercul/internal/domain/work"
platform_auth "tercul/internal/platform/auth"
"testing"
)
type WorkCommandsSuite struct {
suite.Suite
repo *mockWorkRepository
searchClient *mockSearchClient
authzSvc *authz.Service
commands *WorkCommands
}
func (s *WorkCommandsSuite) SetupTest() {
s.repo = &mockWorkRepository{}
s.searchClient = &mockSearchClient{}
s.authzSvc = authz.NewService(s.repo, nil)
s.commands = NewWorkCommands(s.repo, s.searchClient, s.authzSvc)
}
func TestWorkCommandsSuite(t *testing.T) {
suite.Run(t, new(WorkCommandsSuite))
}
func (s *WorkCommandsSuite) TestCreateWork_Success() {
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
_, err := s.commands.CreateWork(context.Background(), work)
assert.NoError(s.T(), err)
}
func (s *WorkCommandsSuite) TestCreateWork_Nil() {
_, err := s.commands.CreateWork(context.Background(), nil)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestCreateWork_EmptyTitle() {
work := &workdomain.Work{TranslatableModel: domain.TranslatableModel{Language: "en"}}
_, err := s.commands.CreateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestCreateWork_EmptyLanguage() {
work := &workdomain.Work{Title: "Test Work"}
_, err := s.commands.CreateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestCreateWork_RepoError() {
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
s.repo.createFunc = func(ctx context.Context, w *workdomain.Work) error {
return errors.New("db error")
}
_, err := s.commands.CreateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_Success() {
ctx := platform_auth.ContextWithAdminUser(context.Background(), 1)
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
work.ID = 1
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*workdomain.Work, error) {
return work, nil
}
s.repo.isAuthorFunc = func(ctx context.Context, workID uint, authorID uint) (bool, error) {
return true, nil
}
err := s.commands.UpdateWork(ctx, work)
assert.NoError(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_Nil() {
err := s.commands.UpdateWork(context.Background(), nil)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_ZeroID() {
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
err := s.commands.UpdateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_EmptyTitle() {
work := &workdomain.Work{TranslatableModel: domain.TranslatableModel{Language: "en"}}
work.ID = 1
err := s.commands.UpdateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_EmptyLanguage() {
work := &workdomain.Work{Title: "Test Work"}
work.ID = 1
err := s.commands.UpdateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestUpdateWork_RepoError() {
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
work.ID = 1
s.repo.updateFunc = func(ctx context.Context, w *workdomain.Work) error {
return errors.New("db error")
}
err := s.commands.UpdateWork(context.Background(), work)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestDeleteWork_Success() {
ctx := platform_auth.ContextWithAdminUser(context.Background(), 1)
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
work.ID = 1
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*workdomain.Work, error) {
return work, nil
}
s.repo.isAuthorFunc = func(ctx context.Context, workID uint, authorID uint) (bool, error) {
return true, nil
}
err := s.commands.DeleteWork(ctx, 1)
assert.NoError(s.T(), err)
}
func (s *WorkCommandsSuite) TestDeleteWork_ZeroID() {
err := s.commands.DeleteWork(context.Background(), 0)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestDeleteWork_RepoError() {
s.repo.deleteFunc = func(ctx context.Context, id uint) error {
return errors.New("db error")
}
err := s.commands.DeleteWork(context.Background(), 1)
assert.Error(s.T(), err)
}
func (s *WorkCommandsSuite) TestAnalyzeWork_Success() {
err := s.commands.AnalyzeWork(context.Background(), 1)
assert.NoError(s.T(), err)
}