From d27234f7e6c1a891fc30626ef334e7b0b7564279 Mon Sep 17 00:00:00 2001 From: Cyrill Schumacher Date: Fri, 5 Mar 2021 16:53:18 +0100 Subject: [PATCH] check for invalid internalIDs --- client.go | 9 ++++++++- client_test.go | 28 +++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/client.go b/client.go index 816d155..f123ba6 100644 --- a/client.go +++ b/client.go @@ -71,6 +71,7 @@ type Client struct { doFn OptionHTTPRequestFn merchants map[string]OptionMerchant // string = your custom merchant ID currentInternalID string + internalIDFound bool } type Option interface { @@ -99,7 +100,8 @@ func MakeClient(opts ...Option) (Client, error) { }, }).Do } - + // see if we have a default one, otherwise you always have to call WithMerchant. + _, c.internalIDFound = c.merchants[""] return c, nil } @@ -107,11 +109,16 @@ func MakeClient(opts ...Option) (Client, error) { func (c *Client) WithMerchant(internalID string) *Client { c2 := *c c2.currentInternalID = internalID + _, c2.internalIDFound = c2.merchants[internalID] return &c2 } func (c *Client) do(req *http.Request, v interface{}) error { internalID := c.currentInternalID + if !c.internalIDFound { + return fmt.Errorf("ClientID %q not found in list of merchants", internalID) + } + req.SetBasicAuth(c.merchants[internalID].MerchantID, c.merchants[internalID].Password) resp, err := c.doFn(req) defer closeResponse(resp) diff --git a/client_test.go b/client_test.go index ce182c4..9d915b6 100644 --- a/client_test.go +++ b/client_test.go @@ -101,7 +101,7 @@ func TestClient_Initialize(t *testing.T) { ) must(t, err) - rs, err := c.Initialize(context.Background(), datatrans.RequestInitialize{ + ri := datatrans.RequestInitialize{ Currency: "CHF", RefNo: "872732", Amount: 1337, @@ -115,13 +115,27 @@ func TestClient_Initialize(t *testing.T) { CustomFields: map[string]interface{}{ "alp": true, }, - }) - must(t, err) - - want := &datatrans.ResponseInitialize{TransactionId: "210215103033478409", RawJSONBody: datatrans.RawJSONBody{0x7b, 0x22, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x22, 0x3a, 0x20, 0x22, 0x32, 0x31, 0x30, 0x32, 0x31, 0x35, 0x31, 0x30, 0x33, 0x30, 0x33, 0x33, 0x34, 0x37, 0x38, 0x34, 0x30, 0x39, 0x22, 0x7d}} - if !reflect.DeepEqual(rs, want) { - t.Error("invalid response") } + t.Run("successful request", func(t *testing.T) { + rs, err := c.Initialize(context.Background(), ri) + must(t, err) + + want := &datatrans.ResponseInitialize{TransactionId: "210215103033478409", RawJSONBody: datatrans.RawJSONBody{0x7b, 0x22, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x49, 0x64, 0x22, 0x3a, 0x20, 0x22, 0x32, 0x31, 0x30, 0x32, 0x31, 0x35, 0x31, 0x30, 0x33, 0x30, 0x33, 0x33, 0x34, 0x37, 0x38, 0x34, 0x30, 0x39, 0x22, 0x7d}} + if !reflect.DeepEqual(rs, want) { + t.Error("invalid response") + } + }) + + t.Run("invalid merchant", func(t *testing.T) { + rs, err := c.WithMerchant("sadfasdf").Initialize(context.Background(), ri) + if rs != nil { + t.Error("response should be nil") + } + if err == nil { + t.Fatal("expected an error") + } + t.Log(err) + }) } func TestMarshalJSON(t *testing.T) {