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

     1  //go:build integration || !unit
     2  // +build integration !unit
     3  
     4  package db_test
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mundipagg/boleto-api/caixa"
    13  	"github.com/mundipagg/boleto-api/db"
    14  	"github.com/mundipagg/boleto-api/mock"
    15  	"github.com/mundipagg/boleto-api/models"
    16  	"github.com/stretchr/testify/assert"
    17  	"go.mongodb.org/mongo-driver/bson/primitive"
    18  )
    19  
    20  func TestCreateMongo(t *testing.T) {
    21  	mock.StartMockService("9026")
    22  	conn, err := db.CreateMongo()
    23  
    24  	assert.Nil(t, err)
    25  	assert.NotNil(t, conn)
    26  }
    27  
    28  func TestCreateMongoMustNoBeRecreated(t *testing.T) {
    29  	mock.StartMockService("9027")
    30  	conn1, err := db.CreateMongo()
    31  
    32  	assert.Nil(t, err)
    33  	assert.NotNil(t, conn1)
    34  
    35  	conn2, err := db.CreateMongo()
    36  
    37  	assert.Nil(t, err)
    38  	assert.NotNil(t, conn2)
    39  
    40  	assert.Equal(t, conn1, conn2)
    41  }
    42  
    43  func TestCheckMongo(t *testing.T) {
    44  	mock.StartMockService("9025")
    45  	err := db.CheckMongo()
    46  
    47  	assert.Nil(t, err)
    48  }
    49  
    50  func TestGetBoletoById(t *testing.T) {
    51  
    52  	mock.StartMockService("9028")
    53  
    54  	bank := caixa.New()
    55  	input := newStubBoletoRequestDb(models.Caixa).Build()
    56  	resp, err := bank.ProcessBoleto(input)
    57  	assert.Nil(t, err)
    58  
    59  	boView := models.NewBoletoView(*input, resp, bank.GetBankNameIntegration())
    60  	// Set values in order to force attributes that can be checked
    61  	oldID := boView.ID.Hex()
    62  	oldPk := boView.PublicKey
    63  	newID := "60be988b3193b131b8061835"
    64  	newPk := "4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2"
    65  	boView.ID, _ = primitive.ObjectIDFromHex(newID)
    66  	boView.UID = "b32ace53-c7dc-11eb-86b5-00059a3c7a00"
    67  	boView.SecretKey = "b32ace53-c7dc-11eb-86b5-00059a3c7a00"
    68  	boView.PublicKey = newPk
    69  	boView.Boleto.RequestKey = "b3279a06-c7dc-11eb-86b5-00059a3c7a00"
    70  	boView.Links[0].Href = strings.ReplaceAll(boView.Links[0].Href, oldID, newID)
    71  	boView.Links[0].Href = strings.ReplaceAll(boView.Links[0].Href, oldPk, newPk)
    72  	boView.Links[1].Href = strings.ReplaceAll(boView.Links[1].Href, oldID, newID)
    73  	boView.Links[1].Href = strings.ReplaceAll(boView.Links[1].Href, oldPk, newPk)
    74  
    75  	expectedExpireDateTime := time.Now().Add(5 * time.Hour * 24)
    76  	boView.Boleto.Title.ExpireDateTime = expectedExpireDateTime
    77  	boView.Boleto.Title.ExpireDate = expectedExpireDateTime.Format("2006-01-02")
    78  
    79  	expectedCreateDate := time.Now()
    80  	boView.CreateDate = expectedCreateDate
    81  
    82  	mID := boView.ID.Hex()
    83  	resp.ID = string(mID)
    84  	resp.Links = boView.Links
    85  
    86  	err = deleteBoletoById(newID)
    87  	assert.Nil(t, err)
    88  
    89  	err = db.SaveBoleto(boView)
    90  	assert.Nil(t, err)
    91  
    92  	b, _, err := db.GetBoletoByID(newID, newPk)
    93  	assert.Nil(t, err)
    94  	createDateZone, _ := b.CreateDate.Zone()
    95  	expireDateTimeZone, _ := b.Boleto.Title.ExpireDateTime.Zone()
    96  	titleCreateDateZone, _ := b.Boleto.Title.CreateDate.Zone()
    97  
    98  	assert.Equal(t, "60be988b3193b131b8061835", b.ID.Hex(), fmt.Sprintf("RecoveryBoleto ID error: expected [%s] got [%s]", "60be988b3193b131b8061835", b.ID.Hex()))
    99  	assert.Equal(t, "b32ace53-c7dc-11eb-86b5-00059a3c7a00", b.UID, fmt.Sprintf("RecoveryBoleto UID error: expected [%s] got [%s]", "b32ace53-c7dc-11eb-86b5-00059a3c7a00", b.UID))
   100  	assert.Equal(t, "b32ace53-c7dc-11eb-86b5-00059a3c7a00", b.SecretKey, fmt.Sprintf("RecoveryBoleto SecretKey error: expected [%s] got [%s]", "b32ace53-c7dc-11eb-86b5-00059a3c7a00", b.SecretKey))
   101  	assert.Equal(t, "4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.PublicKey, fmt.Sprintf("RecoveryBoleto PublicKey error: expected [%s] got [%s]", "4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.PublicKey))
   102  	assert.Equal(t, uint(123456), b.Boleto.Agreement.AgreementNumber, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.AgreementNumber error: expected [%d] got [%d]", uint(123456), b.Boleto.Agreement.AgreementNumber))
   103  	assert.Equal(t, uint16(0), b.Boleto.Agreement.Wallet, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.Wallet error: expected [%d] got [%d]", uint16(0), b.Boleto.Agreement.Wallet))
   104  	assert.Equal(t, "1234", b.Boleto.Agreement.Agency, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.Agency error: expected [%s] got [%s]", "1234", b.Boleto.Agreement.Agency))
   105  	assert.Equal(t, "", b.Boleto.Agreement.AgencyDigit, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.AgencyDigit error: expected [%s] got [%s]", "", b.Boleto.Agreement.AgencyDigit))
   106  	assert.Equal(t, "", b.Boleto.Agreement.Account, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.Account error: expected [%s] got [%s]", "01234567", b.Boleto.Agreement.Account))
   107  	assert.Equal(t, "", b.Boleto.Agreement.AccountDigit, fmt.Sprintf("RecoveryBoleto Boleto.Agreement.AccountDigit error: expected [%s] got [%s]", "", b.Boleto.Agreement.AccountDigit))
   108  	// Check b.Boleto.Title.CreateDate attributes
   109  	assert.Equal(t, boView.Boleto.Title.CreateDate.Unix(), b.Boleto.Title.CreateDate.Unix(), fmt.Sprintf("RecoveryBoleto Boleto.Title.CreateDate[timestamp] error: expected [%s] got [%s]", boView.Boleto.Title.CreateDate, b.Boleto.Title.CreateDate))
   110  	assert.Equal(t, "-03", titleCreateDateZone, fmt.Sprintf("RecoveryBoleto Boleto.Title.CreateDate error: expected [%s] got [%s]", "-03", titleCreateDateZone))
   111  	assert.Equal(t, "Local", b.Boleto.Title.CreateDate.Location().String(), fmt.Sprintf("RecoveryBoleto Boleto.Title.CreateDate[Location] error: expected [%s] got [%s]", "Local", b.Boleto.Title.CreateDate.Location().String()))
   112  	// Check b.Boleto.Title.ExpireDateTime attributes
   113  	assert.Equal(t, expectedExpireDateTime.Unix(), b.Boleto.Title.ExpireDateTime.Unix(), fmt.Sprintf("RecoveryBoleto Boleto.Title.ExpireDateTime[timestamp] error: expected [%s] got [%s]", expectedExpireDateTime, b.Boleto.Title.ExpireDateTime))
   114  	assert.Equal(t, "-03", expireDateTimeZone, fmt.Sprintf("RecoveryBoleto Boleto.Title.ExpireDateTime[Zone] error: expected [%s] got [%s]", "-03", expireDateTimeZone))
   115  	assert.Equal(t, "Local", b.Boleto.Title.ExpireDateTime.Location().String(), fmt.Sprintf("RecoveryBoleto Boleto.Title.ExpireDateTime[location] error: expected [%s] got [%s]", "Local", b.Boleto.Title.ExpireDateTime.Location().String()))
   116  	assert.Equal(t, expectedExpireDateTime.Format("2006-01-02"), b.Boleto.Title.ExpireDate, fmt.Sprintf("RecoveryBoleto Boleto.Title.ExpireDate error: expected [%s] got [%s]", expectedExpireDateTime.Format("2006-01-02"), b.Boleto.Title.ExpireDate))
   117  	assert.Equal(t, uint64(200), b.Boleto.Title.AmountInCents, fmt.Sprintf("RecoveryBoleto Boleto.Title.AmountInCents error: expected [%d] got [%d]", uint64(200), b.Boleto.Title.AmountInCents))
   118  	assert.Equal(t, uint(14012345678901234), b.Boleto.Title.OurNumber, fmt.Sprintf("RecoveryBoleto Boleto.Title.OurNumber error: expected [%d] got [%d]", uint(14012345678901234), b.Boleto.Title.OurNumber))
   119  	assert.Equal(t, "Campo de instruções -  max 40 caracteres", b.Boleto.Title.Instructions, fmt.Sprintf("RecoveryBoleto Boleto.Title.Instructions error: expected [%s] got [%s]", "Campo de instruções -  max 40 caracteres", b.Boleto.Title.Instructions))
   120  	assert.Equal(t, "1234567890A", b.Boleto.Title.DocumentNumber, fmt.Sprintf("RecoveryBoleto Boleto.Title.DocumentNumber error: expected [%s] got [%s]", "1234567890A", b.Boleto.Title.DocumentNumber))
   121  	assert.Equal(t, "OUT", b.Boleto.Title.BoletoType, fmt.Sprintf("RecoveryBoleto Boleto.Title.BoletoType error: expected [%s] got [%s]", "OUT", b.Boleto.Title.BoletoType))
   122  	assert.Equal(t, "99", b.Boleto.Title.BoletoTypeCode, fmt.Sprintf("RecoveryBoleto Boleto.Title.BoletoTypeCode error: expected [%s] got [%s]", "99", b.Boleto.Title.BoletoTypeCode))
   123  	assert.Equal(t, "", b.Boleto.Recipient.Name, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Name error: expected [%s] got [%s]", "", b.Boleto.Recipient.Name))
   124  	assert.Equal(t, "CNPJ", b.Boleto.Recipient.Document.Type, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Document.Type error: expected [%s] got [%s]", "CNPJ", b.Boleto.Recipient.Document.Type))
   125  	assert.Equal(t, "12123123000112", b.Boleto.Recipient.Document.Number, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Document.Number error: expected [%s] got [%s]", "12123123000112", b.Boleto.Recipient.Document.Number))
   126  	assert.Equal(t, "", b.Boleto.Recipient.Address.Street, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.Street error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.Street))
   127  	assert.Equal(t, "", b.Boleto.Recipient.Address.Number, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.Number error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.Number))
   128  	assert.Equal(t, "", b.Boleto.Recipient.Address.Complement, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.Complement error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.Complement))
   129  	assert.Equal(t, "", b.Boleto.Recipient.Address.ZipCode, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.ZipCode error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.ZipCode))
   130  	assert.Equal(t, "", b.Boleto.Recipient.Address.City, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.City error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.City))
   131  	assert.Equal(t, "", b.Boleto.Recipient.Address.District, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.District error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.District))
   132  	assert.Equal(t, "", b.Boleto.Recipient.Address.StateCode, fmt.Sprintf("RecoveryBoleto Boleto.Recipient.Address.StateCode error: expected [%s] got [%s]", "", b.Boleto.Recipient.Address.StateCode))
   133  	assert.Equal(t, "Willian Jadson Bezerra Menezes Tupinambá", b.Boleto.Buyer.Name, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Name error: expected [%s] got [%s]", "Willian Jadson Bezerra Menezes Tupinambá", b.Boleto.Buyer.Name))
   134  	assert.Equal(t, "CPF", b.Boleto.Buyer.Document.Type, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Document.Type error: expected [%s] got [%s]", "CPF", b.Boleto.Buyer.Document.Type))
   135  	assert.Equal(t, "12312312312", b.Boleto.Buyer.Document.Number, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Document.Number error: expected [%s] got [%s]", "12312312312", b.Boleto.Buyer.Document.Number))
   136  	assert.Equal(t, "Rua da Assunção de Sá", b.Boleto.Buyer.Address.Street, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.Street error: expected [%s] got [%s]", "Rua da Assunção de Sá", b.Boleto.Buyer.Address.Street))
   137  	assert.Equal(t, "123", b.Boleto.Buyer.Address.Number, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.Number error: expected [%s] got [%s]", "123", b.Boleto.Buyer.Address.Number))
   138  	assert.Equal(t, "Seção A, s 02", b.Boleto.Buyer.Address.Complement, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.Complement error: expected [%s] got [%s]", "Seção A, s 02", b.Boleto.Buyer.Address.Complement))
   139  	assert.Equal(t, "20520051", b.Boleto.Buyer.Address.ZipCode, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.ZipCode error: expected [%s] got [%s]", "20520051", b.Boleto.Buyer.Address.ZipCode))
   140  	assert.Equal(t, "Belém do Pará", b.Boleto.Buyer.Address.City, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.City error: expected [%s] got [%s]", "Belém do Pará", b.Boleto.Buyer.Address.City))
   141  	assert.Equal(t, "Açaí", b.Boleto.Buyer.Address.District, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.District error: expected [%s] got [%s]", "Açaí", b.Boleto.Buyer.Address.District))
   142  	assert.Equal(t, "PA", b.Boleto.Buyer.Address.StateCode, fmt.Sprintf("RecoveryBoleto Boleto.Buyer.Address.StateCode error: expected [%s] got [%s]", "PA", b.Boleto.Buyer.Address.StateCode))
   143  	assert.Equal(t, models.BankNumber(104), b.Boleto.BankNumber, fmt.Sprintf("RecoveryBoleto Boleto.BankNumber error: expected [%d] got [%d]", models.BankNumber(104), b.Boleto.BankNumber))
   144  	assert.Equal(t, "b3279a06-c7dc-11eb-86b5-00059a3c7a00", b.Boleto.RequestKey, fmt.Sprintf("RecoveryBoleto Boleto.RequestKey error: expected [%s] got [%s]", "b3279a06-c7dc-11eb-86b5-00059a3c7a00", b.Boleto.RequestKey))
   145  	assert.Equal(t, models.BankNumber(104), b.BankID, fmt.Sprintf("RecoveryBoleto BankID error: expected [%d] got [%d]", models.BankNumber(104), b.BankID))
   146  	// Check CreateDate attributes
   147  	assert.Equal(t, expectedCreateDate.Unix(), b.CreateDate.Unix(), fmt.Sprintf("RecoveryBoleto CreateDate error: expected [%s] got [%s]", expectedCreateDate, b.CreateDate))
   148  	assert.Equal(t, "-03", createDateZone, fmt.Sprintf("RecoveryBoleto CreateDate error: expected [%s] got [%s]", "-03", createDateZone))
   149  	assert.Equal(t, "Local", b.CreateDate.Location().String(), fmt.Sprintf("RecoveryBoleto CreateDate error: expected [%s] got [%s]", "Local", b.CreateDate.Location().String()))
   150  	assert.Equal(t, "104-0", b.BankNumber, fmt.Sprintf("RecoveryBoleto BankNumber error: expected [%s] got [%s]", "104-0", b.BankNumber))
   151  	assert.Equal(t, "10492.00650 61000.100042 09922.269841 3 72670000001000", b.DigitableLine, fmt.Sprintf("RecoveryBoleto DigitableLine error: expected [%s] got [%s]", "10492.00650 61000.100042 09922.269841 3 72670000001000", b.DigitableLine))
   152  	assert.Equal(t, "10493726700000010002006561000100040992226984", b.Barcode, fmt.Sprintf("RecoveryBoleto Barcode error: expected [%s] got [%s]", "10493726700000010002006561000100040992226984", b.Barcode))
   153  	assert.Equal(t, "http://localhost:3000/boleto?fmt=html&id=60be988b3193b131b8061835&pk=4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.Links[0].Href, fmt.Sprintf("RecoveryBoleto Links[0].Href error: expected [%s] got [%s]", "http://localhost:3000/boleto?fmt=html&id=60be988b3193b131b8061835&pk=4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.Links[0].Href))
   154  	assert.Equal(t, "html", b.Links[0].Rel, fmt.Sprintf("RecoveryBoleto Links[0].Rel error: expected [%s] got [%s]", "html", b.Links[0].Rel))
   155  	assert.Equal(t, "GET", b.Links[0].Method, fmt.Sprintf("RecoveryBoleto Links[0].Method error: expected [%s] got [%s]", "GET", b.Links[0].Method))
   156  	assert.Equal(t, "http://localhost:3000/boleto?fmt=pdf&id=60be988b3193b131b8061835&pk=4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.Links[1].Href, fmt.Sprintf("RecoveryBoleto Links[1].Href error: expected [%s] got [%s]", "http://localhost:3000/boleto?fmt=pdf&id=60be988b3193b131b8061835&pk=4f5316763275cc72e6313017171512a5c0d6f4710436a8a9cb506c36655be7f2", b.Links[1].Href))
   157  	assert.Equal(t, "pdf", b.Links[1].Rel, fmt.Sprintf("RecoveryBoleto Links[1].Rel error: expected [%s] got [%s]", "pdf", b.Links[1].Rel))
   158  	assert.Equal(t, "GET", b.Links[1].Method, fmt.Sprintf("RecoveryBoleto Links[1].Method error: expected [%s] got [%s]", "GET", b.Links[1].Method))
   159  }
   160  
   161  func TestMongoDb_GetUserCredentials(t *testing.T) {
   162  	mock.StartMockService("9029")
   163  
   164  	gandalfID := "60c293944808daa6fdf2f3b1"
   165  	gID, _ := primitive.ObjectIDFromHex(gandalfID)
   166  	cGandalf := models.Credentials{
   167  		ID:       gID,
   168  		Username: "gandalf",
   169  		Password: "grey",
   170  	}
   171  	deleteCredentialById(gandalfID)
   172  	err := saveCredential(cGandalf)
   173  	assert.Nil(t, err)
   174  
   175  	sarumanID := "60c293944808daa6fdf2f3b3"
   176  	sID, _ := primitive.ObjectIDFromHex(sarumanID)
   177  	cSaruman := models.Credentials{
   178  		ID:       sID,
   179  		Username: "saruman",
   180  		Password: "white",
   181  	}
   182  	deleteCredentialById(sarumanID)
   183  	err = saveCredential(cSaruman)
   184  	assert.Nil(t, err)
   185  
   186  	c, err := db.GetUserCredentials()
   187  	assert.Nil(t, err)
   188  	assert.GreaterOrEqual(t, len(c), 2)
   189  
   190  	g, err := getUserCredentialByID(gandalfID)
   191  	assert.Nil(t, err)
   192  
   193  	assert.Equal(t, "60c293944808daa6fdf2f3b1", g.ID.Hex(), fmt.Sprintf("Gandalf ID error: expected [%s] got [%s]", "60c293944808daa6fdf2f3b1", g.ID.Hex()))
   194  	assert.Equal(t, "gandalf", g.Username, fmt.Sprintf("Gandalf Username error: expected [%s] got [%s]", "gandalf", g.Username))
   195  	assert.Equal(t, "grey", g.Password, fmt.Sprintf("Gandalf Password error: expected [%s] got [%s]", "grey", g.Password))
   196  
   197  	s, err := getUserCredentialByID(sarumanID)
   198  	assert.Nil(t, err)
   199  	assert.Equal(t, "60c293944808daa6fdf2f3b3", s.ID.Hex(), fmt.Sprintf("Saruman ID error: expected [%s] got [%s]", "60c293944808daa6fdf2f3b3", s.ID.Hex()))
   200  	assert.Equal(t, "saruman", s.Username, fmt.Sprintf("Saruman Username error: expected [%s] got [%s]", "saruman", s.Username))
   201  	assert.Equal(t, "white", s.Password, fmt.Sprintf("Saruman Password error: expected [%s] got [%s]", "white", s.Password))
   202  
   203  	deleteCredentialById(gandalfID)
   204  	deleteCredentialById(sarumanID)
   205  }