From 365e622e8a841967b153b99d9294a24ac9e0b74b Mon Sep 17 00:00:00 2001 From: Gabriel Cataldo Date: Mon, 4 Dec 2023 16:33:55 -0300 Subject: [PATCH] developing unit test --- asaas/invoice_test.go | 104 ++++++++++++++++++++- asaas/main_test.go | 179 +++++++++++++++++++++++++++++++++---- asaas/mobile_phone_test.go | 53 ++++++++++- asaas/negativity_test.go | 146 +++++++++++++++++++++++++++--- asaas/notification.go | 2 +- asaas/notification_test.go | 58 ++++++++++-- internal/test/dough.go | 21 ----- 7 files changed, 503 insertions(+), 60 deletions(-) diff --git a/asaas/invoice_test.go b/asaas/invoice_test.go index 36ebf88..bdce09d 100644 --- a/asaas/invoice_test.go +++ b/asaas/invoice_test.go @@ -6,12 +6,114 @@ import ( "time" ) +func TestInvoiceSchedule(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initCreditCardCharge(true, false) + chargeId := getEnvValue(EnvCreditCardChargeId) + assertFatalStringBlank(t, chargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.Schedule(ctx, ScheduleInvoiceRequest{ + Payment: chargeId, + Installment: "", + Customer: "", + ServiceDescription: "Unit test go", + Observations: "Unit test go", + ExternalReference: "", + Value: 100, + Deductions: 0, + EffectiveDate: Date{}, + MunicipalServiceId: "", + MunicipalServiceCode: "", + MunicipalServiceName: "", + UpdatePayment: false, + Taxes: InvoiceTaxesRequest{}, + }) + assertResponseSuccess(t, resp, err) +} + +func TestInvoiceAuthorizeById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initInvoice() + invoiceId := getEnvValue(EnvCreditCardChargeId) + assertFatalStringBlank(t, invoiceId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.AuthorizeById(ctx, invoiceId) + assertResponseSuccess(t, resp, err) +} + +func TestInvoiceUpdateById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initInvoice() + invoiceId := getEnvValue(EnvCreditCardChargeId) + assertFatalStringBlank(t, invoiceId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.UpdateById(ctx, invoiceId, UpdateInvoiceRequest{ + ServiceDescription: "Unit test golang", + Observations: "", + ExternalReference: nil, + Value: 0, + Deductions: nil, + EffectiveDate: Date{}, + MunicipalServiceId: nil, + MunicipalServiceCode: nil, + MunicipalServiceName: nil, + UpdatePayment: nil, + Taxes: nil, + }) + assertResponseSuccess(t, resp, err) +} + +func TestInvoiceCancelById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initInvoice() + invoiceId := getEnvValue(EnvCreditCardChargeId) + assertFatalStringBlank(t, invoiceId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.CancelById(ctx, invoiceId) + assertResponseSuccess(t, resp, err) +} + +func TestInvoiceGetById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initInvoice() + invoiceId := getEnvValue(EnvCreditCardChargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.GetById(ctx, invoiceId) + assertResponseSuccess(t, resp, err) +} + func TestInvoiceGetAll(t *testing.T) { accessToken := getEnvValue(EnvAccessToken) assertFatalStringBlank(t, accessToken) + initInvoice() ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() nInvoice := NewInvoice(EnvSandbox, accessToken) - resp, errAsaas := nInvoice.GetAll(ctx, GetAllInvoicesRequest{}) + resp, errAsaas := nInvoice.GetAll(ctx, GetAllInvoicesRequest{ + EffectiveDateGE: Date{}, + EffectiveDateLE: Date{}, + Payment: "", + Installment: "", + Customer: "", + ExternalReference: "", + Status: "", + Offset: 0, + Limit: 10, + }) assertResponseSuccess(t, resp, errAsaas) } diff --git a/asaas/main_test.go b/asaas/main_test.go index 223a110..1d72c3e 100644 --- a/asaas/main_test.go +++ b/asaas/main_test.go @@ -30,6 +30,10 @@ const EnvChargeDocumentId = "ASAAS_CHARGE_DOCUMENT_ID" const EnvAnticipationId = "ASAAS_ANTICIPATION_ID" const EnvBillPaymentId = "ASAAS_BILL_PAYMENT_ID" const EnvCreditBureauReportId = "ASAAS_CREDIT_BUREAU_REPORT_ID" +const EnvInvoiceId = "ASAAS_INVOICE_ID" +const EnvMobilePhoneRechargeId = "ASAAS_MOBILE_PHONE_RECHARGE_ID" +const EnvNegativityId = "ASAAS_NEGATIVITY_ID" +const EnvNotificationId = "ASAAS_NOTIFICATION_ID" func init() { initFile() @@ -46,6 +50,10 @@ func TestMain(m *testing.M) { clearUndefinedChargeId() clearBillPaymentId() clearFileName() + clearInvoiceId() + clearMobilePhoneRechargeId() + clearNegativityId() + logInfo(EnvSandbox, "clean all envs successfully") os.Exit(code) } @@ -173,7 +181,6 @@ func initCreditCardCharge(capture bool, withInstallment bool) { func initPixCharge() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } clearPixChargeId() @@ -211,7 +218,6 @@ func initPixCharge() { func initBankSlipCharge(withInstallment bool) { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } clearBankSlipChargeId() @@ -257,7 +263,6 @@ func initBankSlipCharge(withInstallment bool) { func initUndefinedCharge() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } clearUndefinedChargeId() @@ -287,7 +292,6 @@ func initUndefinedCharge() { func initChargeDeleted() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } initUndefinedCharge() @@ -309,7 +313,6 @@ func initChargeDeleted() { func initChargeReceivedInCash() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } initUndefinedCharge() @@ -363,7 +366,6 @@ func initChargeDocumentId() { func initAnticipation() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } initCreditCardCharge(true, false) @@ -387,7 +389,6 @@ func initAnticipation() { func initBillPayment() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } clearBillPaymentId() @@ -436,7 +437,6 @@ func initImage() { func initCreditBureauReport() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } initCustomer() @@ -462,7 +462,6 @@ func initCreditBureauReport() { func initFiscalInfo() { accessToken := getEnvValue(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -491,10 +490,117 @@ func initFiscalInfo() { } } +func initInvoice() { + accessToken := getEnvValue(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + clearInvoiceId() + initCreditCardCharge(true, false) + chargeId := getEnvValue(EnvCreditCardChargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nInvoice := NewInvoice(EnvSandbox, accessToken) + resp, err := nInvoice.Schedule(ctx, ScheduleInvoiceRequest{ + Payment: chargeId, + Installment: "", + Customer: "", + ServiceDescription: "Unit test go", + Observations: "Unit test go", + ExternalReference: "", + Value: 100, + Deductions: 0, + EffectiveDate: Date{}, + MunicipalServiceId: "", + MunicipalServiceCode: "", + MunicipalServiceName: "", + UpdatePayment: false, + Taxes: InvoiceTaxesRequest{}, + }) + if err != nil || resp.IsFailure() { + logError("error resp:", resp, "err: ", err) + return + } + setEnv(EnvInvoiceId, resp.Id) +} + +func initMobilePhoneRecharge() { + accessToken := getEnvValue(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + clearMobilePhoneRechargeId() + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) + resp, err := nMobilePhone.Recharge(ctx, MobilePhoneRechargeRequest{ + PhoneNumber: "47997576130", + Value: 20, + }) + if err != nil || resp.IsFailure() { + logError("error resp:", resp, "err: ", err) + return + } + setEnv(EnvMobilePhoneRechargeId, resp.Id) +} + +func initNegativity() { + accessToken := getEnvValue(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + clearNegativityId() + initBankSlipCharge(false) + chargeId := getEnvValue(EnvBankSlipChargeId) + if util.IsBlank(&chargeId) { + return + } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.Create(ctx, CreateNegativityRequest{ + Payment: chargeId, + Type: NegativityTypeCreditBureau, + Description: "Unit test golang", + CustomerName: "Unit test golang", + CustomerCpfCnpj: "24971563792", + CustomerPrimaryPhone: "47999376637", + CustomerPostalCode: "01310-000", + CustomerAddress: "Av. Paulista", + CustomerAddressNumber: "150", + CustomerProvince: "Centro", + }) + if err != nil || resp.IsFailure() { + logError("error resp:", resp, "err: ", err) + return + } + setEnv(EnvNegativityId, resp.Id) +} + +func initNotification() { + accessToken := getEnvValue(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + initCreditCardCharge(false, false) + customerId := getEnvValue(EnvCustomerId) + if util.IsBlank(&customerId) { + return + } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNotification := NewNotification(EnvSandbox, accessToken) + resp, err := nNotification.GetAllByCustomer(ctx, customerId) + if err != nil || resp.IsFailure() || resp.IsNoContent() { + logError("error resp:", resp, "err: ", err) + return + } + setEnv(EnvNotificationId, resp.Data[0].Id) +} + func clearCustomerId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -511,7 +617,6 @@ func clearCustomerId() { func clearCreditCardChargeId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -531,7 +636,6 @@ func clearCreditCardChargeId() { func clearPixChargeId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -548,7 +652,6 @@ func clearPixChargeId() { func clearBankSlipChargeId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -565,7 +668,6 @@ func clearBankSlipChargeId() { func clearUndefinedChargeId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -582,7 +684,6 @@ func clearUndefinedChargeId() { func clearBillPaymentId() { accessToken := getEnvValueWithoutLogger(EnvAccessToken) if util.IsBlank(&accessToken) { - return } ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) @@ -605,6 +706,54 @@ func clearFileName() { _ = os.Unsetenv(EnvFileName) } +func clearInvoiceId() { + accessToken := getEnvValueWithoutLogger(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + invoiceId := getEnvValueWithoutLogger(EnvInvoiceId) + if util.IsBlank(&invoiceId) { + return + } + invoiceAsaas := NewInvoice(EnvSandbox, accessToken) + _, _ = invoiceAsaas.CancelById(ctx, invoiceId) + _ = os.Unsetenv(EnvInvoiceId) +} + +func clearMobilePhoneRechargeId() { + accessToken := getEnvValueWithoutLogger(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + rechargeId := getEnvValueWithoutLogger(EnvMobilePhoneRechargeId) + if util.IsBlank(&rechargeId) { + return + } + mobilePhoneAsaas := NewMobilePhone(EnvSandbox, accessToken) + _, _ = mobilePhoneAsaas.CancelRechargeById(ctx, rechargeId) + _ = os.Unsetenv(EnvMobilePhoneRechargeId) +} + +func clearNegativityId() { + accessToken := getEnvValueWithoutLogger(EnvAccessToken) + if util.IsBlank(&accessToken) { + return + } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + negativityId := getEnvValueWithoutLogger(EnvNegativityId) + if util.IsBlank(&negativityId) { + return + } + negativityAsaas := NewNegativity(EnvSandbox, accessToken) + _, _ = negativityAsaas.CancelById(ctx, negativityId) + _ = os.Unsetenv(EnvNegativityId) +} + func removeFileTest(fileName string) { err := os.Remove(fileName) if err != nil { diff --git a/asaas/mobile_phone_test.go b/asaas/mobile_phone_test.go index 9e09e6a..a24181a 100644 --- a/asaas/mobile_phone_test.go +++ b/asaas/mobile_phone_test.go @@ -13,8 +13,57 @@ func TestMobilePhoneRecharge(t *testing.T) { defer cancel() nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) resp, errAsaas := nMobilePhone.Recharge(ctx, MobilePhoneRechargeRequest{ - PhoneNumber: "47997576130", - Value: 15, + PhoneNumber: "47997576131", + Value: 20, }) assertResponseSuccess(t, resp, errAsaas) } + +func TestMobilePhoneCancelRechargeById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initMobilePhoneRecharge() + rechargeId := getEnvValue(EnvMobilePhoneRechargeId) + assertFatalStringBlank(t, rechargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) + resp, errAsaas := nMobilePhone.CancelRechargeById(ctx, rechargeId) + assertResponseSuccess(t, resp, errAsaas) +} + +func TestMobilePhoneGetRechargeById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initMobilePhoneRecharge() + rechargeId := getEnvValue(EnvMobilePhoneRechargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) + resp, errAsaas := nMobilePhone.GetRechargeById(ctx, rechargeId) + assertResponseSuccess(t, resp, errAsaas) +} + +func TestMobilePhoneGetAllRecharges(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initMobilePhoneRecharge() + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) + resp, errAsaas := nMobilePhone.GetAllRecharges(ctx, PageableDefaultRequest{ + Offset: 0, + Limit: 10, + }) + assertResponseSuccess(t, resp, errAsaas) +} + +func TestMobilePhoneGetProviderByPhoneNumber(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nMobilePhone := NewMobilePhone(EnvSandbox, accessToken) + resp, errAsaas := nMobilePhone.GetProviderByPhoneNumber(ctx, "47997576131") + assertResponseSuccess(t, resp, errAsaas) +} diff --git a/asaas/negativity_test.go b/asaas/negativity_test.go index 962eabb..99c572e 100644 --- a/asaas/negativity_test.go +++ b/asaas/negativity_test.go @@ -2,8 +2,6 @@ package asaas import ( "context" - "encoding/json" - "github.com/GabrielHCataldo/go-asaas/internal/test" "os" "testing" "time" @@ -12,23 +10,142 @@ import ( func TestNegativityCreate(t *testing.T) { accessToken := getEnvValue(EnvAccessToken) assertFatalStringBlank(t, accessToken) + initBankSlipCharge(false) + chargeId := getEnvValue(EnvBankSlipChargeId) + assertFatalStringBlank(t, chargeId) f, err := os.Open(getEnvValue(EnvFileName)) assertFatalErrorNonnull(t, err) v, err := os.ReadFile(f.Name()) assertFatalErrorNonnull(t, err) ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() - req := &CreateNegativityRequest{} - err = json.Unmarshal(test.GetCreateNegativitySuccess(), req) + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.Create(ctx, CreateNegativityRequest{ + Payment: chargeId, + Type: NegativityTypeCreditBureau, + Description: "Unit test golang", + CustomerName: "Unit test golang", + CustomerCpfCnpj: "24971563792", + CustomerPrimaryPhone: "47999376637", + CustomerSecondaryPhone: "", + CustomerPostalCode: "01310-000", + CustomerAddress: "Av. Paulista", + CustomerAddressNumber: "150", + CustomerComplement: "", + CustomerProvince: "Centro", + Documents: &FileRequest{ + Name: f.Name(), + Mime: FileMimeTypeText, + Data: v, + }, + }) + assertResponseSuccess(t, resp, err) +} + +func TestNegativitySimulate(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initBankSlipCharge(false) + chargeId := getEnvValue(EnvBankSlipChargeId) + assertFatalStringBlank(t, chargeId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.Simulate(ctx, chargeId) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityResendDocumentsById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + negativityId := getEnvValue(EnvNegativityId) + assertFatalStringBlank(t, negativityId) + f, err := os.Open(getEnvValue(EnvFileName)) assertFatalErrorNonnull(t, err) - req.Documents = &FileRequest{ - Name: f.Name(), - Mime: FileMimeTypeText, - Data: v, - } + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.ResendDocumentsById(ctx, negativityId, NegativityResendDocumentsRequest{ + Documents: f, + }) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityCancelById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + negativityId := getEnvValue(EnvNegativityId) + assertFatalStringBlank(t, negativityId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.CancelById(ctx, negativityId) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityGetById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + negativityId := getEnvValue(EnvNegativityId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.GetById(ctx, negativityId) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityGetAll(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.GetAll(ctx, GetAllNegativitiesRequest{ + Status: "", + Type: "", + Payment: "", + RequestStartDate: Date{}, + RequestEndDate: Date{}, + Offset: 0, + Limit: 10, + }) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityGetHistoryById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + negativityId := getEnvValue(EnvNegativityId) + assertFatalStringBlank(t, negativityId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNegativity := NewNegativity(EnvSandbox, accessToken) + resp, err := nNegativity.GetHistoryById(ctx, negativityId, PageableDefaultRequest{ + Offset: 0, + Limit: 10, + }) + assertResponseSuccess(t, resp, err) +} + +func TestNegativityGetPaymentsById(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNegativity() + negativityId := getEnvValue(EnvNegativityId) + assertFatalStringBlank(t, negativityId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() nNegativity := NewNegativity(EnvSandbox, accessToken) - resp, errAsaas := nNegativity.Create(ctx, *req) - assertResponseSuccess(t, resp, errAsaas) + resp, err := nNegativity.GetHistoryById(ctx, negativityId, PageableDefaultRequest{ + Offset: 0, + Limit: 10, + }) + assertResponseSuccess(t, resp, err) } func TestNegativityGetChargesAvailableForDunning(t *testing.T) { @@ -37,6 +154,9 @@ func TestNegativityGetChargesAvailableForDunning(t *testing.T) { ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() nNegativity := NewNegativity(EnvSandbox, accessToken) - resp, errAsaas := nNegativity.GetChargesAvailableForDunning(ctx, PageableDefaultRequest{}) - assertResponseSuccess(t, resp, errAsaas) + resp, err := nNegativity.GetChargesAvailableForDunning(ctx, PageableDefaultRequest{ + Offset: 0, + Limit: 10, + }) + assertResponseSuccess(t, resp, err) } diff --git a/asaas/notification.go b/asaas/notification.go index bc0e413..2060efc 100644 --- a/asaas/notification.go +++ b/asaas/notification.go @@ -50,7 +50,7 @@ type UpdateManyNotificationRequest struct { // Habilita/desabilita a mensagem de WhatsApp para seu cliente WhatsappEnabledForCustomer *bool `json:"whatsappEnabledForCustomer,omitempty"` // Especifica quantos dias antes do vencimento a notificação deve se enviada. Válido somente para o evento PAYMENT_DUEDATE_WARNING - ScheduleOffset int `json:"scheduleOffset,omitempty"` + ScheduleOffset *int `json:"scheduleOffset,omitempty"` } type NotificationResponse struct { diff --git a/asaas/notification_test.go b/asaas/notification_test.go index 1399120..f5f9779 100644 --- a/asaas/notification_test.go +++ b/asaas/notification_test.go @@ -2,27 +2,71 @@ package asaas import ( "context" - "github.com/GabrielHCataldo/go-asaas/internal/test" "testing" "time" ) -func TestNotificationGetAllByCustomer(t *testing.T) { +func TestNotificationUpdateById(t *testing.T) { accessToken := getEnvValue(EnvAccessToken) assertFatalStringBlank(t, accessToken) + initNotification() + notificationId := getEnvValue(EnvNotificationId) + assertFatalStringBlank(t, notificationId) ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() nNotification := NewNotification(EnvSandbox, accessToken) - resp, errAsaas := nNotification.GetAllByCustomer(ctx, test.GetCustomerIdDefault()) - assertResponseSuccess(t, resp, errAsaas) + resp, err := nNotification.UpdateById(ctx, notificationId, UpdateNotificationRequest{ + Enabled: Pointer(true), + EmailEnabledForProvider: nil, + SmsEnabledForProvider: nil, + EmailEnabledForCustomer: nil, + SmsEnabledForCustomer: nil, + PhoneCallEnabledForCustomer: nil, + WhatsappEnabledForCustomer: nil, + ScheduleOffset: nil, + }) + assertResponseSuccess(t, resp, err) } -func TestNotificationGetAllByCustomerNoContent(t *testing.T) { +func TestNotificationUpdateManyByCustomer(t *testing.T) { + accessToken := getEnvValue(EnvAccessToken) + assertFatalStringBlank(t, accessToken) + initNotification() + customerId := getEnvValue(EnvCustomerId) + assertFatalStringBlank(t, customerId) + notificationId := getEnvValue(EnvNotificationId) + assertFatalStringBlank(t, notificationId) + ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + nNotification := NewNotification(EnvSandbox, accessToken) + resp, err := nNotification.UpdateManyByCustomer(ctx, UpdateManyNotificationsRequest{ + Customer: customerId, + Notifications: []UpdateManyNotificationRequest{ + { + Id: notificationId, + Enabled: Pointer(true), + EmailEnabledForProvider: nil, + SmsEnabledForProvider: nil, + EmailEnabledForCustomer: nil, + SmsEnabledForCustomer: nil, + PhoneCallEnabledForCustomer: nil, + WhatsappEnabledForCustomer: nil, + ScheduleOffset: nil, + }, + }, + }) + assertResponseSuccess(t, resp, err) +} + +func TestNotificationGetAllByCustomer(t *testing.T) { accessToken := getEnvValue(EnvAccessToken) assertFatalStringBlank(t, accessToken) + initCreditCardCharge(false, false) + customerId := getEnvValue(EnvCustomerId) + assertFatalStringBlank(t, customerId) ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) defer cancel() nNotification := NewNotification(EnvSandbox, accessToken) - resp, errAsaas := nNotification.GetAllByCustomer(ctx, "") - assertResponseNoContent(t, resp, errAsaas) + resp, err := nNotification.GetAllByCustomer(ctx, customerId) + assertResponseSuccess(t, resp, err) } diff --git a/internal/test/dough.go b/internal/test/dough.go index 4a57bea..fae1a0f 100644 --- a/internal/test/dough.go +++ b/internal/test/dough.go @@ -7,10 +7,6 @@ import ( "time" ) -func GetCustomerIdDefault() string { - return "cus_000005791749" -} - func GetPaymentLinkIdDefault() string { return "le5jqxz8as7pgwn9" } @@ -79,20 +75,3 @@ func GetTransferToBankFailureRequestDefault() []byte { } `) } - -func GetCreateNegativitySuccess() []byte { - return []byte(` - { - "payment": "pay_8129071930338672", - "type": "CREDIT_BUREAU", - "description": "Unit test Golang", - "customerName": "Marcelo Almeida", - "customerCpfCnpj": "24971563792", - "customerPrimaryPhone": "47999376637", - "customerPostalCode": "01310-000", - "customerAddress": "Av. Paulista", - "customerAddressNumber": "150", - "customerProvince": "Centro" - } -`) -}