Skip to content

Commit

Permalink
Adding ablity to check for expected failures with a trigger execution (
Browse files Browse the repository at this point in the history
  • Loading branch information
gracegoo-stripe authored Jan 25, 2021
1 parent f6c9d04 commit 6f44102
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 16 deletions.
18 changes: 13 additions & 5 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ type fixtureFile struct {
}

type fixture struct {
Name string `json:"name"`
Path string `json:"path"`
Method string `json:"method"`
Params interface{} `json:"params"`
Name string `json:"name"`
ExpectedErrorType string `json:"expected_error_type"`
Path string `json:"path"`
Method string `json:"method"`
Params interface{} `json:"params"`
}

type fixtureQuery struct {
Expand Down Expand Up @@ -108,7 +109,7 @@ func (fxt *Fixture) Execute() error {
fmt.Printf("Setting up fixture for: %s\n", data.Name)

resp, err := fxt.makeRequest(data)
if err != nil {
if err != nil && !errWasExpected(err, data.ExpectedErrorType) {
return err
}

Expand All @@ -118,6 +119,13 @@ func (fxt *Fixture) Execute() error {
return nil
}

func errWasExpected(err error, expectedErrorType string) bool {
if rerr, ok := err.(requests.RequestError); ok {
return rerr.ErrorType == expectedErrorType
}
return false
}

// UpdateEnv uses the results of the fixtures command just executed and
// updates a local .env with the resulting data
func (fxt *Fixture) UpdateEnv() error {
Expand Down
54 changes: 54 additions & 0 deletions pkg/fixtures/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ const testFixture = `
]
}`

const failureTestFixture = `
{
"_meta": {
"template_version": 0
},
"fixtures": [
{
"name": "charge_expected_failure",
"expected_error_type": "card_error",
"path": "/v1/charges",
"method": "post",
"params": {
"source": "tok_chargeDeclined",
"amount": 100,
"currency": "usd",
"description": "(created by Stripe CLI)"
}
}
]
}`

func TestParseInterface(t *testing.T) {
address := make(map[string]interface{})
address["line1"] = "1 Planet Express St"
Expand Down Expand Up @@ -208,6 +229,39 @@ func TestMakeRequest(t *testing.T) {
require.True(t, fxt.responses["char_bender"].Find("charge").(bool))
}

func TestMakeRequestExpectedFailure(t *testing.T) {
fs := afero.NewMemMapFs()
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(402)
res.Write([]byte(`{"error": {"type": "card_error"}}`))
}))

defer func() { ts.Close() }()
afero.WriteFile(fs, "failured_test_fixture.json", []byte(failureTestFixture), os.ModePerm)
fxt, err := NewFixture(fs, "sk_test_1234", "", ts.URL, "failured_test_fixture.json")
require.NoError(t, err)

err = fxt.Execute()
require.NoError(t, err)
require.NotNil(t, fxt.responses["charge_expected_failure"])
}

func TestMakeRequestUnexpectedFailure(t *testing.T) {
fs := afero.NewMemMapFs()
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(500)
res.Write([]byte(`{"error": "Internal Failure Occurred."}`))
}))

defer func() { ts.Close() }()
afero.WriteFile(fs, "failured_test_fixture.json", []byte(failureTestFixture), os.ModePerm)
fxt, err := NewFixture(fs, "sk_test_1234", "", ts.URL, "failured_test_fixture.json")
require.NoError(t, err)

err = fxt.Execute()
require.NotNil(t, err)
}

func TestParsePathDoNothing(t *testing.T) {
fxt := Fixture{}
http := fixture{
Expand Down
20 changes: 10 additions & 10 deletions pkg/fixtures/fs_vfsdata.go

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

Loading

0 comments on commit 6f44102

Please sign in to comment.