Skip to content
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

fix: Resolve failing HTTP API tests via cleanup #557

Merged
merged 1 commit into from
Jun 23, 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
9 changes: 9 additions & 0 deletions api/http/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import (
"github.com/stretchr/testify/assert"
)

func CleanupEnv() {
env = ""
}

func TestFormatError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "prod"
s := formatError(errors.New("test error"))
assert.Equal(t, "", s)
Expand All @@ -33,6 +38,7 @@ func TestFormatError(t *testing.T) {
}

func TestHandleErrOnBadRequest(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusBadRequest)
Expand Down Expand Up @@ -65,6 +71,7 @@ func TestHandleErrOnBadRequest(t *testing.T) {
}

func TestHandleErrOnInternalServerError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusInternalServerError)
Expand Down Expand Up @@ -96,6 +103,7 @@ func TestHandleErrOnInternalServerError(t *testing.T) {
}

func TestHandleErrOnNotFound(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusNotFound)
Expand Down Expand Up @@ -128,6 +136,7 @@ func TestHandleErrOnNotFound(t *testing.T) {
}

func TestHandleErrOnDefault(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("Unauthorized"), http.StatusUnauthorized)
Expand Down
29 changes: 29 additions & 0 deletions api/http/handlerfuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ func TestDumpHandlerWithNoError(t *testing.T) {
}

func TestDumpHandlerWithDBError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand All @@ -132,6 +134,8 @@ func TestDumpHandlerWithDBError(t *testing.T) {
}

func TestExecGQLWithNilBody(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand All @@ -150,6 +154,8 @@ func TestExecGQLWithNilBody(t *testing.T) {
}

func TestExecGQLWithEmptyBody(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand Down Expand Up @@ -177,6 +183,8 @@ func (m *mockReadCloser) Read(p []byte) (n int, err error) {
}

func TestExecGQLWithMockBody(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
mockReadCloser := mockReadCloser{}
// if Read is called, it will return error
mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
Expand All @@ -199,6 +207,8 @@ func TestExecGQLWithMockBody(t *testing.T) {
}

func TestExecGQLWithNoDB(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
stmt := `
mutation {
Expand All @@ -224,6 +234,8 @@ mutation {
}

func TestExecGQLHandlerContentTypeJSONWithJSONError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
// statement with JSON formatting error
stmt := `
[
Expand Down Expand Up @@ -302,6 +314,8 @@ func TestExecGQLHandlerContentTypeJSON(t *testing.T) {
}

func TestExecGQLHandlerContentTypeFormURLEncoded(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand Down Expand Up @@ -388,6 +402,8 @@ mutation {
}

func TestLoadSchemaHandlerWithReadBodyError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
mockReadCloser := mockReadCloser{}
// if Read is called, it will return error
mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
Expand All @@ -410,6 +426,8 @@ func TestLoadSchemaHandlerWithReadBodyError(t *testing.T) {
}

func TestLoadSchemaHandlerWithoutDB(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
stmt := `
type user {
name: String
Expand Down Expand Up @@ -438,6 +456,8 @@ type user {
}

func TestLoadSchemaHandlerWithAddSchemaError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
ctx := context.Background()
defra := testNewInMemoryDB(t, ctx)

Expand Down Expand Up @@ -508,6 +528,8 @@ type user {
}

func TestGetBlockHandlerWithMultihashError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand All @@ -524,7 +546,10 @@ func TestGetBlockHandlerWithMultihashError(t *testing.T) {
assert.Equal(t, "Bad Request", errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "illegal base32 data at input byte 0", errResponse.Errors[0].Message)
}

func TestGetBlockHandlerWithDSKeyWithNoDB(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
cID, err := cid.Parse("bafybeidembipteezluioakc2zyke4h5fnj4rr3uaougfyxd35u3qzefzhm")
if err != nil {
t.Fatal(err)
Expand All @@ -549,6 +574,8 @@ func TestGetBlockHandlerWithDSKeyWithNoDB(t *testing.T) {
}

func TestGetBlockHandlerWithNoDB(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
errResponse := errorResponse{}
testRequest(testOptions{
Testing: t,
Expand All @@ -567,6 +594,8 @@ func TestGetBlockHandlerWithNoDB(t *testing.T) {
}

func TestGetBlockHandlerWithGetBlockstoreError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
ctx := context.Background()
defra := testNewInMemoryDB(t, ctx)

Expand Down