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

     1  //go:build !unit
     2  // +build !unit
     3  
     4  package test
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mundipagg/boleto-api/models"
    13  	"github.com/pact-foundation/pact-go/dsl"
    14  	"github.com/pact-foundation/pact-go/types"
    15  	"go.mongodb.org/mongo-driver/bson/primitive"
    16  )
    17  
    18  var boleto *models.BoletoView
    19  
    20  // The actual Provider test itself
    21  func TestMessageProvider_Success(t *testing.T) {
    22  	pact := createPact()
    23  
    24  	// Map test descriptions to message producer (handlers)
    25  	functionMappings := dsl.MessageHandlers{
    26  		"a boleto": func(m dsl.Message) (interface{}, error) {
    27  			if boleto != nil {
    28  				return boleto, nil
    29  			} else {
    30  				return map[string]string{
    31  					"message": "not found",
    32  				}, nil
    33  			}
    34  		},
    35  	}
    36  
    37  	stateMappings := dsl.StateHandlers{
    38  		"boleto with id exists": func(s dsl.State) error {
    39  			id := primitive.NewObjectID()
    40  			boleto = &models.BoletoView{
    41  				ID:        id,
    42  				UID:       "d4862472-0f5e-11ec-844b-00059a3c7a00",
    43  				SecretKey: "d4862472-0f5e-11ec-844b-00059a3c7a00",
    44  				PublicKey: "00c9acbc0ad001fd0316c83aeb8ef8d6dccefe2a76698963b305f478f7b0e263",
    45  				Format:    "json",
    46  				Boleto: models.BoletoRequest{
    47  					Authentication: models.Authentication{
    48  						Username:           "eyJpZCI6IjgwNDNiNTMtZjQ5Mi00YyIsImNvZGlnb1B1YmxpY2Fkb3IiOjEwOSwiY29kaWdvU29mdHdhcmUiOjEsInNlcXVlbmNpYWxJbnN0YWxhY2FvIjoxfQ",
    49  						Password:           "eyJpZCI6IjBjZDFlMGQtN2UyNC00MGQyLWI0YSIsImNvZGlnb1B1YmxpY2Fkb3IiOjEwOSwiY29kaWdvU29mdHdhcmUiOjEsInNlcXVlbmNpYWxJbnN0YWxhY2FvIjoxLCJzZXF1ZW5jaWFsQ3JlZGVuY2lhbCI6MX0",
    50  						AuthorizationToken: "NWVjNWI5MGI3M2Y0OGM4YTVjNDRlYzBkOmFiNjE3YzM4LTliNzAtNGE1OS1hMzhmLTMzMTU0ZmFiMDEwYw==",
    51  						AccessKey:          "3aaa712f-8b31-457d-abf3-56cdf7f71fe4",
    52  					},
    53  					Agreement: models.Agreement{
    54  						AgreementNumber: 1660590,
    55  						Wallet:          1,
    56  						WalletVariation: 35,
    57  						Agency:          "1234",
    58  						AgencyDigit:     "3",
    59  						Account:         "01234567",
    60  						AccountDigit:    "9",
    61  					},
    62  					Title: models.Title{
    63  						CreateDate:     time.Time{},
    64  						ExpireDateTime: time.Time{},
    65  						ExpireDate:     "2021-09-11",
    66  						AmountInCents:  200,
    67  						OurNumber:      94614469,
    68  						Instructions:   "NÃO RECEBER APÓS O VENCIMENTO. O prazo de compensação de boleto é de até 3 dias úteis após o pagamento, o valor do limite poderá ficar bloqueado até o processamento.",
    69  						DocumentNumber: "1234567890",
    70  						NSU:            "123",
    71  						BoletoType:     "ND",
    72  						Fees: &models.Fees{
    73  							Fine: &models.Fine{
    74  								DaysAfterExpirationDate: 1,
    75  								AmountInCents:           200,
    76  								PercentageOnTotal:       1,
    77  							},
    78  							Interest: &models.Interest{
    79  								DaysAfterExpirationDate: 1,
    80  								AmountPerDayInCents:     200,
    81  								PercentagePerMonth:      1,
    82  							},
    83  						},
    84  						BoletoTypeCode: "19",
    85  					},
    86  					Recipient: models.Recipient{
    87  						Name: "Nome do Recebedor (Loja)",
    88  						Document: models.Document{
    89  							Type:   "CNPJ",
    90  							Number: "11123123000199",
    91  						},
    92  						Address: models.Address{
    93  							Street:     "Logradouro do Recebedor",
    94  							Number:     "1000",
    95  							Complement: "Sala 01",
    96  							ZipCode:    "00000000",
    97  							City:       "Cidade do Recebedor",
    98  							District:   "Bairro do Recebdor",
    99  							StateCode:  "RJ",
   100  						},
   101  					},
   102  					PayeeGuarantor: &models.PayeeGuarantor{
   103  						Name: "Nome do PayeeGuarantor (Loja)",
   104  						Document: models.Document{
   105  							Type:   "CPF",
   106  							Number: "11282705792",
   107  						},
   108  					},
   109  					Buyer: models.Buyer{
   110  						Name:  "Nome do Comprador (Cliente)",
   111  						Email: "teste@pagar.me",
   112  						Document: models.Document{
   113  							Type:   "CPF",
   114  							Number: "12332279717",
   115  						},
   116  						Address: models.Address{
   117  							Street:     "Logradouro do Comprador",
   118  							Number:     "1000",
   119  							Complement: "Casa 01",
   120  							ZipCode:    "01001000",
   121  							City:       "Cidade do Comprador",
   122  							District:   "Bairro do Comprador",
   123  							StateCode:  "SC",
   124  						},
   125  					},
   126  					BankNumber: 1,
   127  					RequestKey: "d3296f2d-781f-4caf-85e6-c30a7d85b30f",
   128  				},
   129  				BankID:        1,
   130  				CreateDate:    time.Time{},
   131  				BankNumber:    "001-9",
   132  				DigitableLine: "00190.00009 01014.051005 00066.673179 9 71340000010000",
   133  				OurNumber:     "12345",
   134  				Barcode:       "00199713400000100000000001014051000006667317",
   135  				Barcode64:     "asdasd",
   136  				Links: []models.Link{
   137  					{
   138  						Href:   "http://localhost:3000/boleto?fmt=html&id=6136912227b87a73be9fb9cc&pk=00c9acbc0ad001fd0316c83aeb8ef8d6dccefe2a76698963b305f478f7b0e263",
   139  						Rel:    "html",
   140  						Method: "GET",
   141  					},
   142  					{
   143  						Href:   "http://localhost:3000/boleto?fmt=html&id=6136912227b87a73be9fb9cc&pk=00c9acbc0ad001fd0316c83aeb8ef8d6dccefe2a76698963b305f478f7b0e263",
   144  						Rel:    "html",
   145  						Method: "GET",
   146  					},
   147  				},
   148  			}
   149  
   150  			return nil
   151  		},
   152  	}
   153  
   154  	// Verify the Provider with Pactflow publish contracts
   155  	//nolint
   156  	pact.VerifyMessageProvider(t, dsl.VerifyMessageRequest{
   157  		BrokerURL: os.Getenv("PACT_BROKER_URL"),
   158  		ConsumerVersionSelectors: []types.ConsumerVersionSelector{
   159  			types.ConsumerVersionSelector{
   160  				Tag:         os.Getenv("GITHUB_BRANCH"),
   161  				FallbackTag: "master",
   162  				Latest:      true,
   163  			},
   164  			types.ConsumerVersionSelector{
   165  				Tag:    "production",
   166  				Latest: true,
   167  			},
   168  		},
   169  		BrokerToken:                os.Getenv("PACT_BROKER_TOKEN"),
   170  		PublishVerificationResults: true,
   171  		ProviderVersion:            os.Getenv("GITHUB_COMMIT"),
   172  		ProviderTags:               []string{os.Getenv("GITHUB_BRANCH")},
   173  		MessageHandlers:            functionMappings,
   174  		StateHandlers:              stateMappings,
   175  	})
   176  }
   177  
   178  // Configuration / Test Data
   179  var dir, _ = os.Getwd()
   180  
   181  var logDir = fmt.Sprintf("%s/../log", dir)
   182  
   183  // Setup the Pact client.
   184  func createPact() dsl.Pact {
   185  	return dsl.Pact{
   186  		Provider: "boleto-api",
   187  		LogDir:   logDir,
   188  	}
   189  }