Skip to content

[UBP] Use a realistic event payload in Stripe webhook automated tests #11985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions components/public-api-server/pkg/webhooks/stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package webhooks

import (
"encoding/json"
"fmt"
"net/http"

"github.com/gitpod-io/gitpod/common-go/log"
Expand All @@ -28,6 +27,9 @@ func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

// TODO: verify webhook signature.
// Conditional on there being a secret configured.

req.Body = http.MaxBytesReader(w, req.Body, maxBodyBytes)

event := stripe.Event{}
Expand All @@ -44,8 +46,11 @@ func (h *webhookHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

// TODO: verify webhook signature.
// Conditional on there being a secret configured.
invoiceId, ok := event.Data.Object["id"].(string)
if !ok {
log.Error("failed to find invoice id in Stripe event payload")
w.WriteHeader(http.StatusBadRequest)
}

fmt.Fprintf(w, "event type: %s", event.Type)
log.Infof("finalizing invoice for invoice id: %s", invoiceId)
}
36 changes: 24 additions & 12 deletions components/public-api-server/pkg/webhooks/stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
package webhooks

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"testing"

"github.com/gitpod-io/gitpod/common-go/baseserver"
"github.com/stretchr/testify/require"
"github.com/stripe/stripe-go/v72"
)

// https://stripe.com/docs/api/events/types
Expand Down Expand Up @@ -44,15 +43,13 @@ func TestWebhookAcceptsPostRequests(t *testing.T) {

srv := baseServerWithStripeWebhook(t)

ev := stripe.Event{Type: invoiceFinalizedEventType}
payload, err := json.Marshal(ev)
require.NoError(t, err)
payload := payloadForStripeEvent(t, invoiceFinalizedEventType)

url := fmt.Sprintf("%s%s", srv.HTTPAddress(), "/webhook")

for _, scenario := range scenarios {
t.Run(scenario.HttpMethod, func(t *testing.T) {
req, err := http.NewRequest(scenario.HttpMethod, url, bytes.NewBuffer(payload))
req, err := http.NewRequest(scenario.HttpMethod, url, payload)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
Expand Down Expand Up @@ -88,11 +85,8 @@ func TestWebhookIgnoresIrrelevantEvents(t *testing.T) {

for _, scenario := range scenarios {
t.Run(scenario.EventType, func(t *testing.T) {
ev := stripe.Event{Type: scenario.EventType}
payload, err := json.Marshal(ev)
require.NoError(t, err)

req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(payload))
payload := payloadForStripeEvent(t, scenario.EventType)
req, err := http.NewRequest(http.MethodPost, url, payload)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
Expand All @@ -115,3 +109,21 @@ func baseServerWithStripeWebhook(t *testing.T) *baseserver.Server {

return srv
}

func payloadForStripeEvent(t *testing.T, eventType string) io.Reader {
t.Helper()

if eventType != invoiceFinalizedEventType {
return strings.NewReader(`{}`)
}
return strings.NewReader(`
{
"data": {
"object": {
"id": "in_1LUQi7GadRXm50o36jWK7ehs"
}
},
"type": "invoice.finalized"
}
`)
}
6 changes: 6 additions & 0 deletions components/usage/pkg/apiv1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func (s *BillingService) UpdateInvoices(ctx context.Context, in *v1.UpdateInvoic
return &v1.UpdateInvoicesResponse{}, nil
}

func (s *BillingService) FinalizeInvoice(ctx context.Context, in *v1.FinalizeInvoiceRequest) (*v1.FinalizeInvoiceResponse, error) {
log.Infof("Finalizing invoice for invoice %q", in.GetInvoiceId())

return &v1.FinalizeInvoiceResponse{}, nil
}

func (s *BillingService) creditSummaryForTeams(sessions []*v1.BilledSession) (map[string]int64, error) {
creditsPerTeamID := map[string]float64{}

Expand Down
9 changes: 9 additions & 0 deletions components/usage/pkg/apiv1/billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package apiv1

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -115,3 +116,11 @@ func TestCreditSummaryForTeams(t *testing.T) {
})
}
}

func TestFinalizeInvoice(t *testing.T) {
b := BillingService{}

_, err := b.FinalizeInvoice(context.Background(), &v1.FinalizeInvoiceRequest{})

require.NoError(t, err)
}