Skip to content

Commit

Permalink
test: implement tests for checkOrderReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbrm committed Feb 1, 2024
1 parent 05819c5 commit 332b891
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 12 deletions.
22 changes: 11 additions & 11 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func Router(
// Receipt validation.
{
valid := validator.New()
r.Method(http.MethodPost, "/{orderID}/submit-receipt", metricsMwr("SubmitReceipt", corsMwrPost(submitReceipt(svc, valid))))
r.Method(http.MethodPost, "/receipt", metricsMwr("createOrderFromReceipt", corsMwrPost(createOrderFromReceipt(svc, valid))))
r.Method(http.MethodPatch, "/{orderID}/receipt", metricsMwr("checkOrderReceipt", authMwr(checkOrderReceipt(svc, valid))))
r.Method(http.MethodPost, "/{orderID}/submit-receipt", metricsMwr("SubmitReceipt", corsMwrPost(submitReceiptH(svc, valid))))
r.Method(http.MethodPost, "/receipt", metricsMwr("createOrderFromReceipt", corsMwrPost(createOrderFromReceiptH(svc, valid))))
r.Method(http.MethodPatch, "/{orderID}/receipt", metricsMwr("checkOrderReceipt", authMwr(checkOrderReceiptH(svc, valid))))
}

r.Route("/{orderID}/credentials", func(cr chi.Router) {
Expand Down Expand Up @@ -1400,8 +1400,8 @@ func HandleStripeWebhook(service *Service) handlers.AppHandler {
}
}

// submitReceipt handles receipt submission requests.
func submitReceipt(svc *Service, valid *validator.Validate) handlers.AppHandler {
// submitReceiptH handles receipt submission requests.
func submitReceiptH(svc *Service, valid *validator.Validate) handlers.AppHandler {
return handlers.AppHandler(func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
ctx := r.Context()

Expand Down Expand Up @@ -1481,13 +1481,13 @@ func submitReceipt(svc *Service, valid *validator.Validate) handlers.AppHandler
})
}

func createOrderFromReceipt(svc *Service, valid *validator.Validate) handlers.AppHandler {
func createOrderFromReceiptH(svc *Service, valid *validator.Validate) handlers.AppHandler {
return func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
return createOrderFromReceiptH(w, r, svc, valid)
return createOrderFromReceipth(w, r, svc, valid)
}
}

func createOrderFromReceiptH(w http.ResponseWriter, r *http.Request, svc *Service, valid *validator.Validate) *handlers.AppError {
func createOrderFromReceipth(w http.ResponseWriter, r *http.Request, svc *Service, valid *validator.Validate) *handlers.AppError {
ctx := r.Context()

lg := logging.Logger(ctx, "skus").With().Str("func", "createOrderFromReceipt").Logger()
Expand Down Expand Up @@ -1550,13 +1550,13 @@ func createOrderFromReceiptH(w http.ResponseWriter, r *http.Request, svc *Servic

}

func checkOrderReceipt(svc *Service, valid *validator.Validate) handlers.AppHandler {
func checkOrderReceiptH(svc *Service, valid *validator.Validate) handlers.AppHandler {
return func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
return checkOrderReceiptH(w, r, svc, valid)
return checkOrderReceipth(w, r, svc, valid)
}
}

func checkOrderReceiptH(w http.ResponseWriter, r *http.Request, svc *Service, valid *validator.Validate) *handlers.AppError {
func checkOrderReceipth(w http.ResponseWriter, r *http.Request, svc *Service, valid *validator.Validate) *handlers.AppError {
ctx := r.Context()

lg := logging.Logger(ctx, "skus").With().Str("func", "checkOrderReceipt").Logger()
Expand Down
6 changes: 5 additions & 1 deletion services/skus/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,11 @@ func (s *Service) createOrderWithReceipt(ctx context.Context, req model.ReceiptR
}

func (s *Service) checkOrderReceipt(ctx context.Context, orderID uuid.UUID, extID string) error {
ord, err := s.orderRepo.GetByExternalID(ctx, s.Datastore.RawDB(), extID)
return checkOrderReceipt(ctx, s.Datastore.RawDB(), s.orderRepo, orderID, extID)
}

func checkOrderReceipt(ctx context.Context, dbi sqlx.QueryerContext, repo orderStoreSvc, orderID uuid.UUID, extID string) error {
ord, err := repo.GetByExternalID(ctx, dbi, extID)
if err != nil {
return err
}
Expand Down
92 changes: 92 additions & 0 deletions services/skus/service_nonint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
uuid "github.com/satori/go.uuid"
"github.com/shopspring/decimal"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/brave-intl/bat-go/libs/datastore"

"github.com/brave-intl/bat-go/services/skus/model"
"github.com/brave-intl/bat-go/services/skus/storage/repository"
)

func TestCheckNumBlindedCreds(t *testing.T) {
Expand Down Expand Up @@ -850,6 +852,96 @@ func TestCreateOrderWithReceipt(t *testing.T) {
}
}

func TestService_checkOrderReceipt(t *testing.T) {
type tcGiven struct {
orderID uuid.UUID
extID string
repo *repository.MockOrder
}

type testCase struct {
name string
given tcGiven
exp error
}

tests := []testCase{
{
name: "order_not_found",
given: tcGiven{
orderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
extID: "extID_01",
repo: &repository.MockOrder{
FnGetByExternalID: func(ctx context.Context, dbi sqlx.QueryerContext, extID string) (*model.Order, error) {
return nil, model.ErrOrderNotFound
},
},
},
exp: model.ErrOrderNotFound,
},

{
name: "some_error",
given: tcGiven{
orderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
extID: "extID_01",
repo: &repository.MockOrder{
FnGetByExternalID: func(ctx context.Context, dbi sqlx.QueryerContext, extID string) (*model.Order, error) {
return nil, model.Error("some error")
},
},
},
exp: model.Error("some error"),
},

{
name: "order_receipt_dont_match",
given: tcGiven{
orderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
extID: "extID_01",
repo: &repository.MockOrder{
FnGetByExternalID: func(ctx context.Context, dbi sqlx.QueryerContext, extID string) (*model.Order, error) {
result := &model.Order{
ID: uuid.Must(uuid.FromString("decade00-0000-4000-a000-000000000000")),
}

return result, nil
},
},
},
exp: model.ErrNoMatchOrderReceipt,
},

{
name: "happy_path",
given: tcGiven{
orderID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
extID: "extID_01",
repo: &repository.MockOrder{
FnGetByExternalID: func(ctx context.Context, dbi sqlx.QueryerContext, extID string) (*model.Order, error) {
result := &model.Order{
ID: uuid.Must(uuid.FromString("facade00-0000-4000-a000-000000000000")),
}

return result, nil
},
},
},
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()

actual := checkOrderReceipt(ctx, nil, tc.given.repo, tc.given.orderID, tc.given.extID)
should.Equal(t, tc.exp, actual)
})
}
}

type mockPaidOrderCreator struct {
fnCreateOrder func(ctx context.Context, req *model.CreateOrderRequestNew, ordNew *model.OrderNew, items []model.OrderItem) (*model.Order, error)
fnUpdateOrderStatusPaidWithMetadata func(ctx context.Context, oid *uuid.UUID, mdata datastore.Metadata) error
Expand Down

0 comments on commit 332b891

Please sign in to comment.