redeem_service_redeem_test.go 3.2 KB

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