Skip to content

Commit

Permalink
Card token - add integrated test
Browse files Browse the repository at this point in the history
  • Loading branch information
meliguilhermefernandes committed Feb 8, 2024
1 parent 43d9453 commit c7986e6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 103 deletions.
16 changes: 2 additions & 14 deletions pkg/cardtoken/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@ package cardtoken
import (
"context"
"fmt"
"strings"

"github.com/mercadopago/sdk-go/pkg/config"
"github.com/mercadopago/sdk-go/pkg/internal/httpclient"
)

const (
baseURL = "https://api.mercadopago.com/v1/"
urlGet = baseURL + "/v1/card_tokens/{id}"
urlPost = baseURL + "/v1/card_tokens"
url = "https://api.mercadopago.com/v1/card_tokens"
)

type Client interface {
Get(ctx context.Context, id string) (*Response, error)
Create(ctx context.Context, request Request) (*Response, error)
}

Expand All @@ -28,16 +24,8 @@ func NewClient(c *config.Config) Client {
return &client{cfg: c}
}

func (c *client) Get(ctx context.Context, id string) (*Response, error) {
res, err := httpclient.Get[Response](ctx, c.cfg, strings.Replace(urlGet, "{id}", id, 1))
if err != nil {
return nil, fmt.Errorf("error get card token: %w", err)
}
return res, nil
}

func (c *client) Create(ctx context.Context, request Request) (*Response, error) {
res, err := httpclient.Post[Response](ctx, c.cfg, urlPost, request)
res, err := httpclient.Post[Response](ctx, c.cfg, url, request)
if err != nil {
return nil, fmt.Errorf("error create card token: %w", err)
}
Expand Down
76 changes: 0 additions & 76 deletions pkg/cardtoken/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,79 +93,3 @@ func TestCreate(t *testing.T) {
})
}
}

func TestGet(t *testing.T) {
type fields struct {
cfg *config.Config
}
type args struct {
ctx context.Context
id string
}
tests := []struct {
name string
fields fields
args args
want *Response
wantErr string
}{
{
name: "should_get_card_token",
fields: fields{
cfg: &config.Config{
Requester: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
stringReader := strings.NewReader(string(cardTokenResponse))
stringReadCloser := io.NopCloser(stringReader)
return &http.Response{
Body: stringReadCloser,
}, nil
},
},
},
},
args: args{
ctx: context.Background(),
id: "123",
},
want: mockCardToken(),
wantErr: "",
},
{
name: "should_fail_get_card_token",
fields: fields{
cfg: &config.Config{
Requester: &httpclient.Mock{
DoMock: func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("some error")
},
},
},
},
args: args{
ctx: context.Background(),
},
want: nil,
wantErr: "error get card token: transport level error: some error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &client{
cfg: tt.fields.cfg,
}
got, err := c.Get(tt.args.ctx, tt.args.id)
gotErr := ""
if err != nil {
gotErr = err.Error()
}

if gotErr != tt.wantErr {
t.Errorf("card token client.Get() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("card token client.Get() = %v, want %v", got, tt.want)
}
})
}
}
29 changes: 23 additions & 6 deletions pkg/cardtoken/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (

func mockCardToken() *Response {
return &Response{
Id: "3d40b34eb41a6d0923e5bc545927c2e9",
FirstSixDigits: "123456",
ID: "3d40b34eb41a6d0923e5bc545927c2e9",
FirstSixDigits: "503143",
ExpirationMonth: 11,
ExpirationYear: 2030,
LastFourDigits: "1234",
ExpirationYear: 2025,
LastFourDigits: "6351",
Cardholder: Cardholder{
Identification: Identification{
Number: "12345678901",
Number: "70383868084",
Type: "CPF",
},
Name: "APRO",
Name: "MASTER TEST",
},
Status: "active",
DateCreated: parseDate("2024-02-08T09:05:42.725-04:00"),
Expand All @@ -29,6 +29,23 @@ func mockCardToken() *Response {
}
}

func MockCardTokenRequest() Request {
return Request{
SiteID: "Teste",
CardNumber: "5031433215406351",
ExpirationMonth: "11",
ExpirationYear: "2025",
SecurityCode: "123",
Cardholder: Cardholder{
Identification: Identification{
Type: "CPF",
Number: "70383868084",
},
Name: "MASTER TEST",
},
}
}

func parseDate(s string) time.Time {
d, _ := time.Parse(time.RFC3339, s)
return d
Expand Down
2 changes: 1 addition & 1 deletion pkg/cardtoken/request.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cardtoken

type Request struct {
SiteId string `json:"site_id"`
SiteID string `json:"site_id"`
CardNumber string `json:"card_number"`
ExpirationYear string `json:"expiration_year"`
ExpirationMonth string `json:"expiration_month"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/cardtoken/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cardtoken
import "time"

type Response struct {
Id string `json:"id"`
ID string `json:"id"`
FirstSixDigits string `json:"first_six_digits"`
LastFourDigits string `json:"last_four_digits"`
Status string `json:"status"`
Expand Down
10 changes: 5 additions & 5 deletions resources/mocks/cardtoken/response.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"id": "3d40b34eb41a6d0923e5bc545927c2e9",
"first_six_digits": "123456",
"first_six_digits": "503143",
"expiration_month": 11,
"expiration_year": 2030,
"last_four_digits": "1234",
"expiration_year": 2025,
"last_four_digits": "6351",
"cardholder": {
"identification": {
"number": "12345678901",
"number": "70383868084",
"type": "CPF"
},
"name": "APRO"
"name": "MASTER TEST"
},
"status": "active",
"date_created": "2024-02-08T09:05:42.725-04:00",
Expand Down
29 changes: 29 additions & 0 deletions test/integration/cardtoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package integration

import (
"context"
"os"
"testing"

"github.com/mercadopago/sdk-go/pkg/cardtoken"
"github.com/mercadopago/sdk-go/pkg/config"
)

func TestCardToken(t *testing.T) {
t.Run("should_create_card_token", func(t *testing.T) {
cfg, err := config.New(os.Getenv("ACCESS_TOKEN"))
if err != nil {
t.Fatal(err)
}

client := cardtoken.NewClient(cfg)
res, err := client.Create(context.Background(), cardtoken.MockCardTokenRequest())

if res == nil {
t.Error("res can't be nil")
}
if err != nil {
t.Errorf(err.Error())
}
})
}

0 comments on commit c7986e6

Please sign in to comment.