|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors" |
| 8 | + "github.com/Wei-Shaw/sub2api/internal/pkg/pagination" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +type redeemRejectRepo struct { |
| 13 | + code RedeemCode |
| 14 | + useCalled bool |
| 15 | +} |
| 16 | + |
| 17 | +func (r *redeemRejectRepo) Create(ctx context.Context, code *RedeemCode) error { |
| 18 | + panic("unexpected Create call") |
| 19 | +} |
| 20 | + |
| 21 | +func (r *redeemRejectRepo) CreateBatch(ctx context.Context, codes []RedeemCode) error { |
| 22 | + panic("unexpected CreateBatch call") |
| 23 | +} |
| 24 | + |
| 25 | +func (r *redeemRejectRepo) GetByID(ctx context.Context, id int64) (*RedeemCode, error) { |
| 26 | + if r.code.ID != id { |
| 27 | + return nil, ErrRedeemCodeNotFound |
| 28 | + } |
| 29 | + clone := r.code |
| 30 | + return &clone, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (r *redeemRejectRepo) GetByCode(ctx context.Context, code string) (*RedeemCode, error) { |
| 34 | + if r.code.Code != code { |
| 35 | + return nil, ErrRedeemCodeNotFound |
| 36 | + } |
| 37 | + clone := r.code |
| 38 | + return &clone, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (r *redeemRejectRepo) Update(ctx context.Context, code *RedeemCode) error { |
| 42 | + panic("unexpected Update call") |
| 43 | +} |
| 44 | + |
| 45 | +func (r *redeemRejectRepo) BatchUpdate(ctx context.Context, ids []int64, fields RedeemCodeBatchUpdateFields) (int64, error) { |
| 46 | + panic("unexpected BatchUpdate call") |
| 47 | +} |
| 48 | + |
| 49 | +func (r *redeemRejectRepo) Delete(ctx context.Context, id int64) error { |
| 50 | + panic("unexpected Delete call") |
| 51 | +} |
| 52 | + |
| 53 | +func (r *redeemRejectRepo) Use(ctx context.Context, id, userID int64) error { |
| 54 | + r.useCalled = true |
| 55 | + r.code.Status = StatusUsed |
| 56 | + r.code.UsedBy = &userID |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (r *redeemRejectRepo) List(ctx context.Context, params pagination.PaginationParams) ([]RedeemCode, *pagination.PaginationResult, error) { |
| 61 | + panic("unexpected List call") |
| 62 | +} |
| 63 | + |
| 64 | +func (r *redeemRejectRepo) ListWithFilters(ctx context.Context, params pagination.PaginationParams, codeType, status, search string) ([]RedeemCode, *pagination.PaginationResult, error) { |
| 65 | + panic("unexpected ListWithFilters call") |
| 66 | +} |
| 67 | + |
| 68 | +func (r *redeemRejectRepo) ListByUser(ctx context.Context, userID int64, limit int) ([]RedeemCode, error) { |
| 69 | + panic("unexpected ListByUser call") |
| 70 | +} |
| 71 | + |
| 72 | +func (r *redeemRejectRepo) ListByUserPaginated(ctx context.Context, userID int64, params pagination.PaginationParams, codeType string) ([]RedeemCode, *pagination.PaginationResult, error) { |
| 73 | + panic("unexpected ListByUserPaginated call") |
| 74 | +} |
| 75 | + |
| 76 | +func (r *redeemRejectRepo) SumPositiveBalanceByUser(ctx context.Context, userID int64) (float64, error) { |
| 77 | + panic("unexpected SumPositiveBalanceByUser call") |
| 78 | +} |
| 79 | + |
| 80 | +func TestRedeemRejectsInvitationCodeBeforeTransaction(t *testing.T) { |
| 81 | + ctx := context.Background() |
| 82 | + redeemRepo := &redeemRejectRepo{ |
| 83 | + code: RedeemCode{ |
| 84 | + ID: 1, |
| 85 | + Code: "INVITE-001", |
| 86 | + Type: RedeemTypeInvitation, |
| 87 | + Status: StatusUnused, |
| 88 | + }, |
| 89 | + } |
| 90 | + redeemService := NewRedeemService(redeemRepo, nil, nil, nil, nil, nil, nil, nil) |
| 91 | + |
| 92 | + got, err := redeemService.Redeem(ctx, 2, redeemRepo.code.Code) |
| 93 | + |
| 94 | + require.Nil(t, got) |
| 95 | + require.Error(t, err) |
| 96 | + require.True(t, infraerrors.IsBadRequest(err)) |
| 97 | + require.Equal(t, "REDEEM_CODE_UNSUPPORTED_TYPE", infraerrors.Reason(err)) |
| 98 | + require.Equal(t, "invitation codes can only be used during registration", infraerrors.Message(err)) |
| 99 | + require.False(t, redeemRepo.useCalled) |
| 100 | + require.Equal(t, StatusUnused, redeemRepo.code.Status) |
| 101 | + require.Nil(t, redeemRepo.code.UsedBy) |
| 102 | +} |
0 commit comments