github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/bank/services/jpmorgan/jpmorgan_test.go (about)

     1  package jpmorgan
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/mundipagg/boleto-api/certificate"
     8  	"github.com/mundipagg/boleto-api/mock"
     9  	"github.com/mundipagg/boleto-api/models"
    10  	"github.com/mundipagg/boleto-api/test"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  var boletoResponseFailParameters = []test.Parameter{
    15  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(0).Build(), Expected: models.ErrorResponse{Code: `MPAmountInCents`, Message: `Valor não pode ser menor do que 1 centavo`}},
    16  	{Input: newStubBoletoRequestJPMorgan().WithExpirationDate(time.Date(2021, 10, 17, 12, 12, 12, 12, time.Local)).Build(), Expected: models.ErrorResponse{Code: `MPExpireDate`, Message: `Data de expiração não pode ser menor que a data de hoje`}},
    17  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(201).Build(), Expected: models.ErrorResponse{Code: `Internal Error - Contact Service Provider`, Message: `Internal Error - Contact Service Provider`}},
    18  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(202).Build(), Expected: models.ErrorResponse{Code: `GCA-010`, Message: `The account was not found.`}},
    19  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(205).Build(), Expected: models.ErrorResponse{Code: `MPOurNumberFail`, Message: `our number was not returned by the bank`}},
    20  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(203).Build(), Expected: models.ErrorResponse{Code: `BOL-3`, Message: `Não foi possível processar a requisição por inconsistencia nos campos abaixo`}},
    21  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(204).Build(), Expected: models.ErrorResponse{Code: `BOL-144`, Message: `Boleto já Existente. Detalhes abaixo:`}},
    22  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(206).Build(), Expected: models.ErrorResponse{Code: `GCA-111`, Message: `codContaCorrente/codAgencia is required`}},
    23  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(207).Build(), Expected: models.ErrorResponse{Code: `Signature Verification Failure`, Message: `Signature Verification Failure`}},
    24  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(209).Build(), Expected: models.ErrorResponse{Code: `BOL-144`, Message: `Boleto já Existente. Detalhes abaixo:`}},
    25  	{Input: newStubBoletoRequestJPMorgan().WithAmountInCents(300).Build(), Expected: models.ErrorResponse{Code: `MPTimeout`, Message: `GatewayTimeout`}},
    26  }
    27  
    28  func Test_ProcessBoleto_WhenServiceRespondsSuccessfully_ShouldHasSuccessfulBoletoResponse(t *testing.T) {
    29  	mock.StartMockService("9003")
    30  	certificate.LoadMockCertificates()
    31  	input := newStubBoletoRequestJPMorgan().Build()
    32  	bank, _ := New()
    33  
    34  	output, err := bank.ProcessBoleto(input)
    35  
    36  	assert.Nil(t, err, "Não deve haver um erro")
    37  	assert.Equal(t, 12, len(output.OurNumber))
    38  	assert.Equal(t, "123456789012", output.OurNumber)
    39  	test.AssertProcessBoletoWithSuccess(t, output)
    40  }
    41  
    42  func Test_ProcessBoleto_WhenServiceRespondsWithShortOurNUmber_ShouldHasPadZerosAndSuccessfulBoletoResponse(t *testing.T) {
    43  	mock.StartMockService("9005")
    44  	certificate.LoadMockCertificates()
    45  	input := newStubBoletoRequestJPMorgan().WithAmountInCents(211).Build()
    46  	bank, _ := New()
    47  
    48  	output, err := bank.ProcessBoleto(input)
    49  
    50  	assert.Nil(t, err, "Não deve haver um erro")
    51  	assert.Equal(t, 12, len(output.OurNumber))
    52  	assert.Equal(t, "000000123456", output.OurNumber)
    53  	test.AssertProcessBoletoWithSuccess(t, output)
    54  }
    55  
    56  func Test_ProcessBoleto_WhenServiceRespondsUnsuccessful_ShouldHasErrorResponse(t *testing.T) {
    57  	mock.StartMockService("9004")
    58  	certificate.LoadMockCertificates()
    59  	bank, _ := New()
    60  
    61  	for _, fact := range boletoResponseFailParameters {
    62  		request := fact.Input.(*models.BoletoRequest)
    63  		response, err := bank.ProcessBoleto(request)
    64  		assert.Nil(t, err, "Não deve haver um erro fora do objeto de response")
    65  
    66  		test.AssertProcessBoletoFailed(t, response)
    67  		assert.Equal(t, fact.Expected.(models.ErrorResponse).Code, response.Errors[0].Code)
    68  		assert.Contains(t, response.Errors[0].Message, fact.Expected.(models.ErrorResponse).Message)
    69  	}
    70  }
    71  
    72  func TestTemplateResponse_WhenRequestHasSpecialCharacter_ShouldBeParsedSuccessful(t *testing.T) {
    73  	mock.StartMockService("9006")
    74  	certificate.LoadMockCertificates()
    75  	input := newStubBoletoRequestJPMorgan().WithBuyerName("Nome do \tComprador (Cliente)").Build()
    76  	bank, _ := New()
    77  
    78  	output, _ := bank.ProcessBoleto(input)
    79  
    80  	test.AssertProcessBoletoWithSuccess(t, output)
    81  }