Skip to content

Commit

Permalink
Merge pull request #2604 from brave-intl/master
Browse files Browse the repository at this point in the history
Production 2024-07-18_01
  • Loading branch information
pavelbrm authored Jul 17, 2024
2 parents 817237e + 1eda1a0 commit 70fa812
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion services/grant/cmd/grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func GrantServer(
srv := http.Server{
Addr: ":3333",
Handler: chi.ServerBaseContext(ctx, r),
ReadTimeout: 3 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
}
err = srv.ListenAndServe()
Expand Down
16 changes: 10 additions & 6 deletions services/skus/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Router(

// For now, this endpoint is placed directly under /credentials.
// It would make sense to put it under /items/item_id, had the caller known the item id.
// However, the caller of this endpoit does not posses that knowledge, and it would have to call the order endpoint to get it.
// However, the caller of this endpoint does not possess that knowledge, and it would have to call the order endpoint to get it.
// This extra round-trip currently does not make sense.
// So until Bundles came along we can benefit from the fact that there is one item per order.
// By the time Bundles arrive, the caller would either have to fetch order anyway, or this can be communicated in another way.
Expand Down Expand Up @@ -623,15 +623,19 @@ type createItemCredsRequest struct {
// createItemCreds handles requests for creating credentials for an item.
func createItemCreds(svc *Service) handlers.AppHandler {
return func(w http.ResponseWriter, r *http.Request) *handlers.AppError {
ctx := r.Context()
lg := logging.Logger(ctx, "skus.createItemCreds")
b, err := io.ReadAll(io.LimitReader(r.Body, reqBodyLimit10MB))
if err != nil {
return handlers.WrapError(err, "error reading body", http.StatusBadRequest)
}

req := &createItemCredsRequest{}
if err := requestutils.ReadJSON(ctx, r.Body, req); err != nil {
lg.Error().Err(err).Msg("failed to read body payload")
return handlers.WrapError(err, "Error in request body", http.StatusBadRequest)
if err := json.Unmarshal(b, req); err != nil {
return handlers.WrapError(err, "error decoding body", http.StatusBadRequest)
}

ctx := r.Context()
lg := logging.Logger(ctx, "skus.createItemCreds")

if _, err := govalidator.ValidateStruct(req); err != nil {
lg.Error().Err(err).Msg("failed to validate struct")
return handlers.WrapValidationError(err)
Expand Down
4 changes: 2 additions & 2 deletions services/skus/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ func (s *Service) processAppStoreNotificationTx(ctx context.Context, dbi sqlx.Ex

switch {
case ntf.shouldRenew():
expt := time.UnixMilli(txn.ExpiresDate).UTC()
expt := time.UnixMilli(txn.ExpiresDate).UTC().Add(24 * time.Hour)
paidt := time.Now()

return s.renewOrderWithExpPaidTime(ctx, dbi, ord.ID, expt, paidt)
Expand Down Expand Up @@ -1748,7 +1748,7 @@ func (s *Service) processPlayStoreNotificationTx(ctx context.Context, dbi sqlx.E
return err
}

expt := time.UnixMilli(sub.ExpiryTimeMillis).UTC()
expt := time.UnixMilli(sub.ExpiryTimeMillis).UTC().Add(24 * time.Hour)
paidt := time.Now()

return s.renewOrderWithExpPaidTime(ctx, dbi, ord.ID, expt, paidt)
Expand Down
33 changes: 29 additions & 4 deletions services/skus/service_nonint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,26 @@ func TestService_processPlayStoreNotificationTx(t *testing.T) {
SubID: "nightly.bravevpn.monthly",
},
},
orepo: &repository.MockOrder{},
orepo: &repository.MockOrder{
FnSetExpiresAt: func(ctx context.Context, dbi sqlx.ExecerContext, id uuid.UUID, when time.Time) error {
if when.Equal(time.Date(2024, time.July, 2, 0, 0, 0, 0, time.UTC)) {
return nil
}

return model.Error("unexpected")
},
},
prepo: &repository.MockOrderPayHistory{},
pscl: &mockPSClient{},
pscl: &mockPSClient{
fnVerifySubscription: func(ctx context.Context, pkgName, subID, token string) (*androidpublisher.SubscriptionPurchase, error) {
result := &androidpublisher.SubscriptionPurchase{
PaymentState: ptrTo[int64](1),
ExpiryTimeMillis: time.Date(2024, time.July, 1, 0, 0, 0, 0, time.UTC).UnixMilli(),
}

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

Expand Down Expand Up @@ -558,10 +575,18 @@ func TestService_processAppStoreNotificationTx(t *testing.T) {
},
txn: &appstore.JWSTransactionDecodedPayload{
OriginalTransactionId: "123456789000001",
ExpiresDate: 1704067201000,
ExpiresDate: 1704067200000,
},

orepo: &repository.MockOrder{},
orepo: &repository.MockOrder{
FnSetExpiresAt: func(ctx context.Context, dbi sqlx.ExecerContext, id uuid.UUID, when time.Time) error {
if when.Equal(time.Date(2024, time.January, 2, 0, 0, 0, 0, time.UTC)) {
return nil
}

return model.Error("unexpected")
},
},
prepo: &repository.MockOrderPayHistory{},
},
},
Expand Down

0 comments on commit 70fa812

Please sign in to comment.