github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/bb/bb_test.go (about)

     1  package bb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/mundipagg/boleto-api/mock"
     7  	"github.com/mundipagg/boleto-api/models"
     8  	"github.com/mundipagg/boleto-api/test"
     9  	"github.com/mundipagg/boleto-api/util"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  const baseMockJSON = `
    15  {
    16  	
    17  	"BankNumber": 1,
    18  	"Authentication": {
    19  		"Username": "xxx",
    20  		"Password": "xxx"
    21  	},
    22  	"Agreement": {
    23  		"AgreementNumber": 5555555,
    24  		"WalletVariation": 19,
    25  		"Wallet":17,
    26  		"Agency":"5555",
    27  		"Account":"55555"
    28  	},
    29  	"Title": {
    30  		"ExpireDate": "2029-10-01",
    31  		"AmountInCents": 200,
    32  		"OurNumber": 1,
    33  		"Instructions": "Senhor caixa, não receber após o vencimento",
    34  		"DocumentNumber": "123456"
    35  	},
    36  	"Buyer": {
    37  		"Name": "TESTE",
    38  		"Document": {
    39  			"Type": "CNPJ",
    40  			"Number": "55555555550140"
    41  		},
    42  		"Address": {
    43  			"Street": "Teste",
    44  			"Number": "123",
    45  			"Complement": "Apto",
    46  			"ZipCode": "55555555",
    47  			"City": "Rio de Janeiro",
    48  			"District": "Teste",
    49  			"StateCode": "RJ"
    50  		}
    51  	},
    52  	"Recipient": {
    53  		"Name": "TESTE",
    54  		"Document": {
    55  			"Type": "CNPJ",
    56  			"Number": "55555555555555"
    57  		},
    58  		"Address": {
    59  			"Street": "TESTE",
    60  			"Number": "555",
    61  			"Complement": "Teste",
    62  			"ZipCode": "0455555",
    63  			"City": "São Paulo",
    64  			"District": "",
    65  			"StateCode": "SP"
    66  		}
    67  	}
    68  }
    69  `
    70  
    71  var boletoTypeParameters = []test.Parameter{
    72  	{Input: models.Title{BoletoType: ""}, Expected: "19"},
    73  	{Input: models.Title{BoletoType: "NSA"}, Expected: "19"},
    74  	{Input: models.Title{BoletoType: "BDP"}, Expected: "19"},
    75  	{Input: models.Title{BoletoType: "CH"}, Expected: "01"},
    76  	{Input: models.Title{BoletoType: "DM"}, Expected: "02"},
    77  	{Input: models.Title{BoletoType: "DS"}, Expected: "04"},
    78  	{Input: models.Title{BoletoType: "NP"}, Expected: "12"},
    79  	{Input: models.Title{BoletoType: "RC"}, Expected: "17"},
    80  	{Input: models.Title{BoletoType: "ND"}, Expected: "19"},
    81  }
    82  
    83  func TestProcessBoleto_WhenServiceRespondsSuccessfully_ShouldHasSuccessfulBoletoResponse(t *testing.T) {
    84  	mock.StartMockService("9009")
    85  	input := new(models.BoletoRequest)
    86  	util.FromJSON(baseMockJSON, input)
    87  	bank := New()
    88  
    89  	output, _ := bank.ProcessBoleto(input)
    90  
    91  	test.AssertProcessBoletoWithSuccess(t, output)
    92  }
    93  
    94  func TestProcessBoleto_WhenServiceRespondsFailed_ShouldHasFailedBoletoResponse(t *testing.T) {
    95  	mock.StartMockService("9008")
    96  	input := new(models.BoletoRequest)
    97  	util.FromJSON(baseMockJSON, input)
    98  	input.Title.AmountInCents = 400
    99  	bank := New()
   100  
   101  	output, _ := bank.ProcessBoleto(input)
   102  
   103  	test.AssertProcessBoletoFailed(t, output)
   104  }
   105  
   106  func TestProcessBoleto_WhenAccountInvalid_ShouldHasFailedBoletoResponse(t *testing.T) {
   107  	mock.StartMockService("9007")
   108  	input := new(models.BoletoRequest)
   109  	util.FromJSON(baseMockJSON, input)
   110  	input.Agreement.Account = ""
   111  	bank := New()
   112  
   113  	output, _ := bank.ProcessBoleto(input)
   114  
   115  	test.AssertProcessBoletoFailed(t, output)
   116  }
   117  
   118  func TestShouldCalculateAgencyDigitFromBb(t *testing.T) {
   119  	test.ExpectTrue(bbAgencyDigitCalculator("0137") == "6", t)
   120  
   121  	test.ExpectTrue(bbAgencyDigitCalculator("3418") == "5", t)
   122  
   123  	test.ExpectTrue(bbAgencyDigitCalculator("3324") == "3", t)
   124  
   125  	test.ExpectTrue(bbAgencyDigitCalculator("5797") == "5", t)
   126  }
   127  
   128  func TestShouldCalculateAccountDigitFromBb(t *testing.T) {
   129  	test.ExpectTrue(bbAccountDigitCalculator("", "00006685") == "0", t)
   130  
   131  	test.ExpectTrue(bbAccountDigitCalculator("", "00025619") == "6", t)
   132  
   133  	test.ExpectTrue(bbAccountDigitCalculator("", "00006842") == "X", t)
   134  
   135  	test.ExpectTrue(bbAccountDigitCalculator("", "00000787") == "0", t)
   136  }
   137  
   138  func TestGetBoletoType_WhenCalled_ShouldBeMapTypeSuccessful(t *testing.T) {
   139  	request := new(models.BoletoRequest)
   140  	for _, fact := range boletoTypeParameters {
   141  		request.Title = fact.Input.(models.Title)
   142  		_, result := getBoletoType(request)
   143  		assert.Equal(t, fact.Expected, result, "Deve mapear o boleto type corretamente")
   144  	}
   145  }