Files
RenewCMS/test/get_all_error_propagation_test.go
T

75 lines
2.3 KiB
Go

package test
import (
"RenewCMS/internal/infrastructure/useCases"
"errors"
"net/http"
"testing"
"time"
)
func TestGetAllFailuresReachTheCaller(t *testing.T) {
boom := errors.New("storage down")
t.Run("registration status answers 500 instead of reporting open", func(t *testing.T) {
userRepo := newMockUserRepo()
userRepo.fail.failNextWith("GetAll", boom)
router, _ := newRegistrationStatusRouter(userRepo, newMockMailRepo())
rec := registrationStatusRequest(router, nil)
if rec.Code != http.StatusInternalServerError {
t.Errorf("status = %d, want 500", rec.Code)
}
})
t.Run("IsRegistrationOpen returns the error", func(t *testing.T) {
userRepo := newMockUserRepo()
userRepo.fail.failNextWith("GetAll", boom)
uc := useCases.NewCreateUserUseCase(userRepo, newSeededMockRoleRepo(), &stubHasher{}, newMockMailRepo(), "https://cms.example.com", discardRecorder)
if _, err := uc.IsRegistrationOpen(); !errors.Is(err, boom) {
t.Errorf("err = %v, want %v", err, boom)
}
})
t.Run("GetInvite returns the error instead of an invalid invite", func(t *testing.T) {
invites := newInviteFakeRepo()
invites.fail.failNextWith("GetAll", boom)
uc := useCases.NewGetInviteUseCase(invites, &stubHasher{})
if _, err := uc.GetInvite("code"); !errors.Is(err, boom) {
t.Errorf("err = %v, want %v", err, boom)
}
})
t.Run("ListUsers returns the error", func(t *testing.T) {
userRepo := newMockUserRepo()
userRepo.fail.failNextWith("GetAll", boom)
if _, err := useCases.NewListUsersUseCase(userRepo).ListUsers(); !errors.Is(err, boom) {
t.Errorf("err = %v, want %v", err, boom)
}
})
t.Run("retention stops on a user listing failure", func(t *testing.T) {
userRepo := newMockUserRepo()
userRepo.fail.failNextWith("GetAll", boom)
uc := useCases.NewEnforceRetentionUseCase(userRepo, newInviteFakeRepo(), &memorySessionRepo{})
if _, err := uc.Tick(time.Now()); !errors.Is(err, boom) {
t.Errorf("err = %v, want %v", err, boom)
}
})
t.Run("retention stops on an invite listing failure", func(t *testing.T) {
invites := newInviteFakeRepo()
invites.fail.failNextWith("GetAll", boom)
uc := useCases.NewEnforceRetentionUseCase(newMockUserRepo(), invites, &memorySessionRepo{})
if _, err := uc.Tick(time.Now()); !errors.Is(err, boom) {
t.Errorf("err = %v, want %v", err, boom)
}
})
}