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

Ref(SPV-1216) Fix unit tests status codes #24

Merged
merged 13 commits into from
Dec 2, 2024
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
8 changes: 8 additions & 0 deletions internal/api/v1/admin/users/userstest/xpub_api_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ func NewBadRequestSPVError() models.SPVError {
}
}

func NewInternalServerSPVError() models.SPVError {
return models.SPVError{
Message: http.StatusText(http.StatusInternalServerError),
StatusCode: http.StatusInternalServerError,
Code: models.UnknownErrorCode,
}
}

func ExpectedXPub(t *testing.T) *response.Xpub {
return &response.Xpub{
Model: response.Model{
Expand Down
20 changes: 12 additions & 8 deletions internal/api/v1/admin/users/xpubs_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ func TestXPubsAPI_CreateXPub(t *testing.T) {
URL := spvwallettest.TestAPIAddr + "/api/v1/admin/users"
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// when:
// given:
wallet, transport := spvwallettest.GivenSPVAdminAPI(t)
transport.RegisterResponder(http.MethodPost, URL, tc.responder)

// then:
// when:
got, err := wallet.CreateXPub(context.Background(), &commands.CreateUserXpub{
Metadata: map[string]any{},
XPub: "",
})

// then:
require.ErrorIs(t, err, tc.expectedErr)
require.EqualValues(t, tc.expectedResponse, got)
require.Equal(t, tc.expectedResponse, got)
})
}
}
Expand All @@ -68,22 +70,24 @@ func TestXPubsAPI_XPubs(t *testing.T) {
responder: httpmock.NewJsonResponderOrPanic(http.StatusBadRequest, userstest.NewBadRequestSPVError()),
},
"HTTP GET /api/v1/admin/users str response: 500": {
expectedErr: errors.ErrUnrecognizedAPIResponse,
responder: httpmock.NewStringResponder(http.StatusInternalServerError, "unexpected internal server failure"),
expectedErr: userstest.NewInternalServerSPVError(),
responder: httpmock.NewJsonResponderOrPanic(http.StatusInternalServerError, userstest.NewInternalServerSPVError()),
},
}

URL := spvwallettest.TestAPIAddr + "/api/v1/admin/users"
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// when:
// given:
wallet, transport := spvwallettest.GivenSPVAdminAPI(t)
transport.RegisterResponder(http.MethodGet, URL, tc.responder)

// then:
// when:
got, err := wallet.XPubs(context.Background())

// then:
require.ErrorIs(t, err, tc.expectedErr)
require.EqualValues(t, tc.expectedResponse, got)
require.Equal(t, tc.expectedResponse, got)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package querybuilderstest

import (
"net/http"
"testing"
"time"

"github.com/bitcoin-sv/spv-wallet/models"
)

func ParseTime(t *testing.T, s string) time.Time {
Expand All @@ -19,11 +16,3 @@ func ParseTime(t *testing.T, s string) time.Time {
func Ptr[T any](value T) *T {
return &value
}

func NewBadRequestSPVError() *models.SPVError {
return &models.SPVError{
Message: http.StatusText(http.StatusBadRequest),
StatusCode: http.StatusBadRequest,
Code: "invalid-data-format",
}
}
31 changes: 10 additions & 21 deletions internal/api/v1/user/configs/configs_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import (
"net/http"
"testing"

"github.com/bitcoin-sv/spv-wallet-go-client/errors"
"github.com/bitcoin-sv/spv-wallet-go-client/internal/api/v1/user/configs/configstest"
"github.com/bitcoin-sv/spv-wallet-go-client/internal/spvwallettest"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/response"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
)

func TestConfigsAPI_SharedConfig_APIResponses(t *testing.T) {
tests := map[string]struct {
statusCode int
expectedResponse *response.SharedConfig
expectedErr error
responder httpmock.Responder
Expand All @@ -28,38 +26,29 @@ func TestConfigsAPI_SharedConfig_APIResponses(t *testing.T) {
"pikePaymentEnabled": true,
},
},
statusCode: http.StatusOK,
responder: httpmock.NewJsonResponderOrPanic(http.StatusOK, httpmock.File("configstest/response_200_status_code.json")),
responder: httpmock.NewJsonResponderOrPanic(http.StatusOK, httpmock.File("configstest/response_200_status_code.json")),
},
"HTTP GET /api/v1/configs/shared response: 400": {
expectedErr: models.SPVError{
Message: http.StatusText(http.StatusBadRequest),
StatusCode: http.StatusBadRequest,
Code: "invalid-data-format",
},
statusCode: http.StatusOK,
responder: httpmock.NewJsonResponderOrPanic(http.StatusBadRequest, &models.SPVError{
Message: http.StatusText(http.StatusBadRequest),
StatusCode: http.StatusBadRequest,
Code: "invalid-data-format",
}),
expectedErr: configstest.NewBadRequestSPVError(),
responder: httpmock.NewJsonResponderOrPanic(http.StatusBadRequest, configstest.NewBadRequestSPVError()),
},
"HTTP GET /api/v1/configs/shared str response: 500": {
expectedErr: errors.ErrUnrecognizedAPIResponse,
statusCode: http.StatusInternalServerError,
responder: httpmock.NewStringResponder(http.StatusInternalServerError, "unexpected internal server failure"),
expectedErr: configstest.NewInternalServerSPVError(),
responder: httpmock.NewJsonResponderOrPanic(http.StatusInternalServerError, configstest.NewInternalServerSPVError()),
},
}

url := spvwallettest.TestAPIAddr + "/api/v1/configs/shared"
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// when:
// given:
wallet, transport := spvwallettest.GivenSPVUserAPI(t)
transport.RegisterResponder(http.MethodGet, url, tc.responder)

// then:
// when:
got, err := wallet.SharedConfig(context.Background())

// then:
require.ErrorIs(t, err, tc.expectedErr)
chris-4chain marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, tc.expectedResponse, got)
})
Expand Down
23 changes: 23 additions & 0 deletions internal/api/v1/user/configs/configstest/configs_api_fixtures.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package configstest

import (
"net/http"

"github.com/bitcoin-sv/spv-wallet/models"
)

func NewBadRequestSPVError() models.SPVError {
return models.SPVError{
Message: http.StatusText(http.StatusBadRequest),
StatusCode: http.StatusBadRequest,
Code: "invalid-data-format",
}
}

func NewInternalServerSPVError() models.SPVError {
return models.SPVError{
Message: http.StatusText(http.StatusInternalServerError),
StatusCode: http.StatusInternalServerError,
Code: models.UnknownErrorCode,
}
}
Loading
Loading