Skip to content

Commit

Permalink
test: create account
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Oct 7, 2023
1 parent 42cacce commit 759f429
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 41 deletions.
12 changes: 10 additions & 2 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,21 @@ func (c *client) FetchAccounts(ctx context.Context, pageNum int, pageSize int) (
return data, c.do(ctx, req)
}

type AccountUsageType string

const (
AccountUsageTypeYourFunds AccountUsageType = "YourFunds"
AccountUsageTypeSegregatedDesignated AccountUsageType = "SegregatedDesignated"
AccountUsageTypeSegregatedPooled AccountUsageType = "SegregatedPooled"
)

type CreateAccountPayload struct {
Name string `json:"accountName"`
Owner struct {
Name string `json:"name"`
} `json:"owner"`
SortCode string `json:"sortCode"`
UsageType string `json:"usageType,omitempty"`
SortCode string `json:"sortCode"`
UsageType AccountUsageType `json:"usageType,omitempty"`
}

func (c *client) CreateAccount(ctx context.Context, payload CreateAccountPayload) (data AccountResponse, err error) {
Expand Down
24 changes: 22 additions & 2 deletions accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/brokeyourbike/clearbank-api-client-go"
"github.com/brokeyourbike/clearbank-api-client-go/signature"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
Expand All @@ -21,11 +22,14 @@ var accountSuccess []byte
//go:embed testdata/accounts-success.json
var accountsSuccess []byte

//go:embed testdata/account-create-success.json
var accountCreateSuccess []byte

func TestFetchAccount(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", nil, clearbank.WithHTTPClient(mockHttpClient))

resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(accountSuccess))}
resp := &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader(accountSuccess))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

accountID := uuid.MustParse("a85002e3-0116-4b14-b7fa-427e60f4f6bc")
Expand All @@ -41,11 +45,27 @@ func TestFetchAccounts(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", nil, clearbank.WithHTTPClient(mockHttpClient))

resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(accountsSuccess))}
resp := &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(bytes.NewReader(accountsSuccess))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

got, err := client.FetchAccounts(context.TODO(), 1, 100)
require.NoError(t, err)

assert.Len(t, got.Accounts, 1)
}

func TestCreateAccount(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

resp := &http.Response{StatusCode: http.StatusCreated, Body: io.NopCloser(bytes.NewReader(accountCreateSuccess))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()

got, err := client.CreateAccount(ctx, clearbank.CreateAccountPayload{})
require.NoError(t, err)
assert.Equal(t, uuid.MustParse("a85002e3-0116-4b14-b7fa-427e60f4f6bc"), got.Account.ID)
}
10 changes: 0 additions & 10 deletions rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestFetchMarketrate(t *testing.T) {

func TestFetchMarketrate_Fail(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)

client := clearbank.NewClient("token", nil, clearbank.WithHTTPClient(mockHttpClient))

resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(marketrateFail))}
Expand All @@ -53,15 +52,6 @@ func TestFetchMarketrate_Fail(t *testing.T) {
require.Error(t, err)
}

func TestFetchMarketrate_FailedHttpRequest(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", nil, clearbank.WithHTTPClient(mockHttpClient))
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("cannot do")).Once()

_, err := client.FetchMarketrate(context.TODO(), clearbank.MarketrateParams{FixedSide: clearbank.FixedSideBuy})
assert.Error(t, err)
}

func TestNegotiate(t *testing.T) {
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", nil, clearbank.WithHTTPClient(mockHttpClient))
Expand Down
27 changes: 0 additions & 27 deletions statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
_ "embed"
"errors"
"io"
"net/http"
"testing"
Expand All @@ -29,19 +28,6 @@ func TestRequestStatement(t *testing.T) {
assert.NoError(t, client.RequestStatement(ctx, clearbank.StatementPayload{}))
}

func TestRequestStatement_FailedHttpRequest(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("cannot do")).Once()

assert.Error(t, client.RequestStatement(ctx, clearbank.StatementPayload{}))
}

func TestRequestStatementFor(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
Expand All @@ -55,16 +41,3 @@ func TestRequestStatementFor(t *testing.T) {

assert.NoError(t, client.RequestStatementFor(ctx, "IBAN12345", clearbank.StatementPayload{}))
}

func TestRequestStatementFor_FailedHttpRequest(t *testing.T) {
mockSigner := signature.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))

ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()

mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(nil, errors.New("cannot do")).Once()

assert.Error(t, client.RequestStatementFor(ctx, "IBAN12345", clearbank.StatementPayload{}))
}
40 changes: 40 additions & 0 deletions testdata/account-create-success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"account": {
"id": "a85002e3-0116-4b14-b7fa-427e60f4f6bc",
"name": "string",
"label": "string",
"type": "CACC",
"currency": [
"AED"
],
"balances": [
{
"name": "string",
"amount": 0,
"currency": "AED",
"status": "CLBD",
"lastCommittedTransaction": "string"
}
],
"minimumBalance": {
"name": "string",
"amount": 0,
"currency": "AED",
"status": "CLBD",
"lastCommittedTransaction": "string"
},
"status": "NotProvided",
"statusReason": "NotProvided",
"iban": "string",
"bban": "string",
"upic": "string",
"cuid": "string"
},
"halLinks": [
{
"name": "string",
"href": "string",
"templated": true
}
]
}

0 comments on commit 759f429

Please sign in to comment.