Skip to content

Commit

Permalink
add api version header to fixtures (#903)
Browse files Browse the repository at this point in the history
* add api version header to fixtures

* fixing tests

* forgot a comma :(

* spacing issue

* fixing formatting of tests again

* adding flag

* fixing test

* setting version in createParams instead
  • Loading branch information
joyluu-stripe committed Jul 5, 2022
1 parent 77c7c03 commit d38eedd
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 26 deletions.
1 change: 1 addition & 0 deletions docs/rpc/commands.md
Expand Up @@ -858,6 +858,7 @@
| add | [string](#string) | repeated | Add parameters in the fixture |
| remove | [string](#string) | repeated | Remove parameters from the fixture |
| raw | [string](#string) | | Raw fixture string |
| api_version | [string](#string) | | Specify API Version for fixture |



Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/fixtures.go
Expand Up @@ -18,6 +18,7 @@ type FixturesCmd struct {
Cfg *config.Config

stripeAccount string
apiVersion string
skip []string
override []string
add []string
Expand All @@ -42,6 +43,7 @@ func newFixturesCmd(cfg *config.Config) *FixturesCmd {
fixturesCmd.Cmd.Flags().StringArrayVar(&fixturesCmd.override, "override", []string{}, "Override parameters in the fixture")
fixturesCmd.Cmd.Flags().StringArrayVar(&fixturesCmd.add, "add", []string{}, "Add parameters in the fixture")
fixturesCmd.Cmd.Flags().StringArrayVar(&fixturesCmd.remove, "remove", []string{}, "Remove parameters from the fixture")
fixturesCmd.Cmd.Flags().StringVar(&fixturesCmd.apiVersion, "api-version", "", "Specify API version in the fixture")

return fixturesCmd
}
Expand Down Expand Up @@ -73,7 +75,7 @@ func (fc *FixturesCmd) runFixturesCmd(cmd *cobra.Command, args []string) error {
return err
}

_, err = fixture.Execute(cmd.Context())
_, err = fixture.Execute(cmd.Context(), fc.apiVersion)

if err != nil {
return err
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/trigger.go
Expand Up @@ -18,6 +18,7 @@ type triggerCmd struct {

fs afero.Fs
stripeAccount string
apiVersion string
skip []string
override []string
add []string
Expand Down Expand Up @@ -54,6 +55,7 @@ needed to create the triggered event as well as the corresponding API objects.
tc.cmd.Flags().StringArrayVar(&tc.add, "add", []string{}, "Add params to the trigger")
tc.cmd.Flags().StringArrayVar(&tc.remove, "remove", []string{}, "Remove params from the trigger")
tc.cmd.Flags().StringVar(&tc.raw, "raw", "", "Raw fixture in string format to replace all default fixtures")
tc.cmd.Flags().StringVar(&tc.apiVersion, "api-version", "", "Specify API version for trigger")

// Hidden configuration flags, useful for dev/debugging
tc.cmd.Flags().StringVar(&tc.apiBaseURL, "api-base", stripe.DefaultAPIBaseURL, "Sets the API base URL")
Expand All @@ -78,7 +80,7 @@ func (tc *triggerCmd) runTriggerCmd(cmd *cobra.Command, args []string) error {

event := args[0]

_, err = fixtures.Trigger(cmd.Context(), event, tc.stripeAccount, tc.apiBaseURL, apiKey, tc.skip, tc.override, tc.add, tc.remove, tc.raw)
_, err = fixtures.Trigger(cmd.Context(), event, tc.stripeAccount, tc.apiBaseURL, apiKey, tc.skip, tc.override, tc.add, tc.remove, tc.raw, tc.apiVersion)
if err != nil {
return err
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/fixtures/fixtures.go
Expand Up @@ -252,7 +252,7 @@ func (fxt *Fixture) Remove(removals []string) error {

// Execute takes the parsed fixture file and runs through all the requests
// defined to populate the user's account
func (fxt *Fixture) Execute(ctx context.Context) ([]string, error) {
func (fxt *Fixture) Execute(ctx context.Context, apiVersion string) ([]string, error) {
requestNames := make([]string, len(fxt.fixture.Fixtures))
for i, data := range fxt.fixture.Fixtures {
if isNameIn(data.Name, fxt.Skip) {
Expand All @@ -264,14 +264,13 @@ func (fxt *Fixture) Execute(ctx context.Context) ([]string, error) {
requestNames[i] = data.Name

fmt.Printf("Running fixture for: %s\n", data.Name)
resp, err := fxt.makeRequest(ctx, data)
resp, err := fxt.makeRequest(ctx, data, apiVersion)
if err != nil && !errWasExpected(err, data.ExpectedErrorType) {
return nil, err
}

fxt.responses[data.Name] = gjson.ParseBytes(resp)
}

return requestNames, nil
}

Expand All @@ -292,7 +291,7 @@ func (fxt *Fixture) UpdateEnv() error {
return nil
}

func (fxt *Fixture) makeRequest(ctx context.Context, data fixture) ([]byte, error) {
func (fxt *Fixture) makeRequest(ctx context.Context, data fixture, apiVersion string) ([]byte, error) {
var rp requests.RequestParameters

if data.Method == "post" && !fxt.fixture.Meta.ExcludeMetadata {
Expand All @@ -314,7 +313,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data fixture) ([]byte, erro
return make([]byte, 0), err
}

params, err := fxt.createParams(data.Params)
params, err := fxt.createParams(data.Params, apiVersion)

if err != nil {
return make([]byte, 0), err
Expand All @@ -323,7 +322,7 @@ func (fxt *Fixture) makeRequest(ctx context.Context, data fixture) ([]byte, erro
return req.MakeRequest(ctx, fxt.APIKey, path, params, true)
}

func (fxt *Fixture) createParams(params interface{}) (*requests.RequestParameters, error) {
func (fxt *Fixture) createParams(params interface{}, apiVersion string) (*requests.RequestParameters, error) {
requestParams := requests.RequestParameters{}
parsed, err := fxt.parseInterface(params)
if err != nil {
Expand All @@ -333,6 +332,10 @@ func (fxt *Fixture) createParams(params interface{}) (*requests.RequestParameter

requestParams.SetStripeAccount(fxt.StripeAccount)

if apiVersion != "" {
requestParams.SetVersion(apiVersion)
}

return &requestParams, nil
}

Expand Down
18 changes: 9 additions & 9 deletions pkg/fixtures/fixtures_test.go
Expand Up @@ -107,7 +107,7 @@ func TestMakeRequest(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, file, []string{}, []string{}, []string{}, []string{})
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)

require.NotNil(t, fxt.responses["cust_bender"])
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestMakeRequestWithStringFixture(t *testing.T) {
fxt, err := NewFixtureFromRawString(fs, apiKey, "", ts.URL, testFixture)
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)

require.NotNil(t, fxt.responses["cust_bender"])
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestWithSkipMakeRequest(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, file, []string{"char_bender", "capt_bender"}, []string{}, []string{}, []string{})
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)

require.True(t, fxt.responses["cust_bender"].Exists())
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestMakeRequestWithOverride(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, file, []string{}, []string{"cust_bender:name=Fry", "char_bender:amount=3000"}, []string{}, []string{})
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -255,7 +255,7 @@ func TestMakeRequestWithAdd(t *testing.T) {
)
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)
}

Expand Down Expand Up @@ -293,7 +293,7 @@ func TestMakeRequestWithRemove(t *testing.T) {
)
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)
}

Expand All @@ -309,7 +309,7 @@ func TestMakeRequestExpectedFailure(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, "failured_test_fixture.json", []string{}, []string{}, []string{}, []string{})
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NoError(t, err)
require.NotNil(t, fxt.responses["charge_expected_failure"])
}
Expand All @@ -326,7 +326,7 @@ func TestMakeRequestUnexpectedFailure(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, "failured_test_fixture.json", []string{}, []string{}, []string{}, []string{})
require.NoError(t, err)

_, err = fxt.Execute(context.Background())
_, err = fxt.Execute(context.Background(), "")
require.NotNil(t, err)
}

Expand Down Expand Up @@ -440,7 +440,7 @@ func TestExecuteReturnsRequestNames(t *testing.T) {
fxt, err := NewFixtureFromFile(fs, apiKey, "", ts.URL, file, []string{}, []string{}, []string{}, []string{})
require.NoError(t, err)

requestNames, err := fxt.Execute(context.Background())
requestNames, err := fxt.Execute(context.Background(), "")
require.NoError(t, err)

require.NotNil(t, fxt.responses["cust_bender"])
Expand Down
4 changes: 2 additions & 2 deletions pkg/fixtures/triggers.go
Expand Up @@ -132,7 +132,7 @@ func EventNames() []string {
}

// Trigger triggers a Stripe event.
func Trigger(ctx context.Context, event string, stripeAccount string, baseURL string, apiKey string, skip, override, add, remove []string, raw string) ([]string, error) {
func Trigger(ctx context.Context, event string, stripeAccount string, baseURL string, apiKey string, skip, override, add, remove []string, raw string, apiVersion string) ([]string, error) {
var fixture *Fixture
var err error
fs := afero.NewOsFs()
Expand Down Expand Up @@ -167,7 +167,7 @@ func Trigger(ctx context.Context, event string, stripeAccount string, baseURL st
}
}

requestNames, err := fixture.Execute(ctx)
requestNames, err := fixture.Execute(ctx, apiVersion)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("Trigger failed: %s\n", err))
}
Expand Down
1 change: 1 addition & 0 deletions pkg/rpcservice/trigger.go
Expand Up @@ -31,6 +31,7 @@ func (srv *RPCService) Trigger(ctx context.Context, req *rpc.TriggerRequest) (*r
req.Add,
req.Remove,
req.Raw,
req.ApiVersion,
)
if err != nil {
return nil, err
Expand Down
25 changes: 18 additions & 7 deletions rpc/trigger.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/trigger.proto
Expand Up @@ -25,6 +25,9 @@ message TriggerRequest {

// Raw fixture string
string raw = 7;

// Specify API Version for fixture
string api_version = 8;
}

message TriggerResponse {
Expand Down

0 comments on commit d38eedd

Please sign in to comment.