Skip to content

Commit

Permalink
Create client_test and fix tokenization_test (#2)
Browse files Browse the repository at this point in the history
* Improving credit card tokenize tests

* Adjust with env variables

* Rollback vars

* Create client_test

* Remove .env

Co-authored-by: Walter Junior <[email protected]>
  • Loading branch information
WalterPaes and Walter Junior authored Nov 26, 2020
1 parent 29c911b commit 002bacb
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 28 deletions.
43 changes: 43 additions & 0 deletions test/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package test

import (
"fmt"
"github.com/vasconcelosvcd/go-cielo"
"os"
"testing"
)

func getNewClient(success bool, t *testing.T) (*cielo.Client, error) {
t.Helper()
if success {
return cielo.NewClient(os.Getenv("MERCHANT_ID"), os.Getenv("MERCHANT_KEY"), cielo.SandboxEnvironment)
}
return cielo.NewClient("", "", cielo.SandboxEnvironment)
}

func Test_NewClient(t *testing.T) {
t.Run("SUCCESS", func(t *testing.T) {
client, err := getNewClient(true, t)
if err != nil {
t.Fatalf("Errors was not expected. Err: %v", err.Error())
}

expected := "*cielo.Client"
got := fmt.Sprintf("%T", client)

if got != expected {
t.Errorf("Was expected '%v', but got '%v'", expected, got)
}
})

t.Run("FAIL", func(t *testing.T) {
client, err := getNewClient(false, t)
if err == nil {
t.Error("Errors was expected.")
}

if client != nil {
t.Error("Client must be nil")
}
})
}
70 changes: 42 additions & 28 deletions test/tokenization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,76 @@ package test

import (
"github.com/vasconcelosvcd/go-cielo"
"os"
"testing"
)

func TestTokenize(t *testing.T){
c,err := main.NewClient(os.Getenv("MERCHANT_ID"),os.Getenv("MERCHANT_KEY"), main.SandboxEnvironment)
if err!= nil{
println(err.Error())
func TestTokenize(t *testing.T) {
c, err := getNewClient(true, t)
if err != nil {
t.Error(err.Error())
}
if c == nil {
t.Fatal("Client must not be nil")
}
cc:= main.CreditCard{

cc := cielo.CreditCard{
CardNumber: "5247712516640978",
CustomerName: "Tester Name",
Holder: "Teste Holder",
ExpirationDate: "11/2026",
SaveCard: true,
Brand: "Master",
}
_,err =c.CreateTokenizeCard(&cc)


if err!=nil{
print(err.Error())
token, err := c.CreateTokenizeCard(&cc)
if err != nil {
t.Error(err.Error())
}
if token == nil {
t.Fatal("Token must not be nil")
}
if len(cc.CardToken)<=0 {
t.Error("Não foi gerado o token do cartao \n "+ err.Error())
if len(token.CardToken) <= 0 {
t.Error("Não foi gerado o token do cartao")
}
}

func TestGetTokenized(t *testing.T){
c,err := main.NewClient(os.Getenv("MERCHANT_ID"),os.Getenv("MERCHANT_KEY"), main.SandboxEnvironment)
if err!= nil{
println(err.Error())
func TestGetTokenized(t *testing.T) {
c, err := getNewClient(true, t)
if err != nil {
t.Error(err.Error())
}
cc:= main.CreditCard{
if c == nil {
t.Fatal("Client must not be nil")
}

cc := cielo.CreditCard{
CardNumber: "5247712516640978",
CustomerName: "Tester Name",
Holder: "Teste Holder",
ExpirationDate: "11/2026",
SaveCard: true,
Brand: "Master",
}
_,err =c.CreateTokenizeCard(&cc)


//t := http.NewRequest("GET", "www.google.com","")
if err!=nil{
token, err := c.CreateTokenizeCard(&cc)
if err != nil {
t.Error(err.Error())
}
if len(cc.CardToken)<=0 {
t.Error("Não foi gerado o token do cartao \n "+ err.Error())
if token == nil {
t.Fatal("Token must not be nil")
}
if len(token.CardToken) <= 0 {
t.Error("Não foi gerado o token do cartao")
}
getCard,err:= c.GetTokenizeCard(cc.CardToken)
if err!=nil{

tokenizedCard, err := c.GetTokenizeCard(token.CardToken)
if err != nil {
t.Error(err.Error())
}
if len(getCard.CardNumber)<=0 {
t.Error("Não foi gerado o token do cartao \n "+ err.Error())
if tokenizedCard == nil {
t.Fatal("Tokenized card must not be nil")
}
if len(tokenizedCard.CardNumber) <= 0 {
t.Error("Não foi gerado o token do cartao")
}
}
}

0 comments on commit 002bacb

Please sign in to comment.