Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
client/tier: change Report to default to Stripe's now (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmizerany committed Mar 3, 2023
1 parent 4753d71 commit bf6fc2e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 1 addition & 2 deletions client/tier/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,13 @@ func (c *Client) Report(ctx context.Context, org, feature string, n int) error {
Org: org,
Feature: fn,
N: n,
At: time.Now(),
})

return err
}

type ReportParams struct {
At time.Time // default is time.Now()
At time.Time // default is 'now' at Stripe
Clobber bool // default is false
}

Expand Down
37 changes: 37 additions & 0 deletions client/tier/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package tier

import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

"kr.dev/diff"
"tier.run/api/apitypes"
"tier.run/refs"
)

func TestUserPassword(t *testing.T) {
Expand Down Expand Up @@ -37,3 +41,36 @@ func TestUserPassword(t *testing.T) {
want := []string{"testkey"}
diff.Test(t, t.Errorf, got, want)
}

func TestReportNow(t *testing.T) {
var mu sync.Mutex
var got []apitypes.ReportRequest
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
var v apitypes.ReportRequest
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
t.Error(err)
}
got = append(got, v)
io.WriteString(w, "{}")
}))

c := &Client{BaseURL: s.URL}
if err := c.Report(context.Background(), "org:foo", "feature:x", 1); err != nil {
t.Fatal(err)
}

want := []apitypes.ReportRequest{
{
Org: "org:foo",
Feature: refs.MustParseName("feature:x"),
N: 1,

// Check that At is unset causing use to use Stripe's 'now'.
At: time.Time{},
},
}

diff.Test(t, t.Errorf, got, want)
}

0 comments on commit bf6fc2e

Please sign in to comment.