|
| 1 | +package midtrans_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + pg "github.com/pandudpn/go-payment-gateway" |
| 10 | + "github.com/pandudpn/go-payment-gateway/gateway/midtrans" |
| 11 | + "github.com/pandudpn/go-payment-gateway/mocks" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +func TestCreateLinkPayAccount_SuccessSandBox(t *testing.T) { |
| 17 | + // mockApiRequest interface |
| 18 | + // for mocking Call API |
| 19 | + mockApiRequest := mocks.NewApiRequestInterface(t) |
| 20 | + |
| 21 | + // mock request |
| 22 | + mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes() |
| 23 | + mockUrl := getMockUrlCreatePayAccountSandBox() |
| 24 | + mockHeader := getMockHeaderSandBox() |
| 25 | + expectedResult := midtrans.LinkAccountPayResponse{} |
| 26 | + |
| 27 | + // doing mock call |
| 28 | + mockApiRequest. |
| 29 | + On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil) |
| 30 | + |
| 31 | + // mock options |
| 32 | + mockOptions := getMockOptionsSandBox() |
| 33 | + mockOptions.ApiCall = mockApiRequest |
| 34 | + |
| 35 | + opts, _ := pg.NewOption(mockOptions) |
| 36 | + |
| 37 | + lap := getMockParamsLinkAccountPay() |
| 38 | + result, err := midtrans.CreateLinkPayAccount(lap, opts) |
| 39 | + |
| 40 | + assert.Nil(t, err, "error should be nil") |
| 41 | + assert.Equal(t, &expectedResult, result) |
| 42 | +} |
| 43 | + |
| 44 | +func TestCreateLinkPayAccountWithContext_SuccessSandBox(t *testing.T) { |
| 45 | + ctx := context.Background() |
| 46 | + // mockApiRequest interface |
| 47 | + // for mocking Call API |
| 48 | + mockApiRequest := mocks.NewApiRequestInterface(t) |
| 49 | + |
| 50 | + // mock request |
| 51 | + mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes() |
| 52 | + mockUrl := getMockUrlCreatePayAccountSandBox() |
| 53 | + mockHeader := getMockHeaderSandBox() |
| 54 | + expectedResult := midtrans.LinkAccountPayResponse{} |
| 55 | + |
| 56 | + // doing mock call |
| 57 | + mockApiRequest. |
| 58 | + On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil) |
| 59 | + |
| 60 | + // mock options |
| 61 | + mockOptions := getMockOptionsSandBox() |
| 62 | + mockOptions.ApiCall = mockApiRequest |
| 63 | + |
| 64 | + opts, _ := pg.NewOption(mockOptions) |
| 65 | + |
| 66 | + lap := getMockParamsLinkAccountPay() |
| 67 | + result, err := midtrans.CreateLinkPayAccountWithContext(ctx, lap, opts) |
| 68 | + |
| 69 | + assert.Nil(t, err, "error should be nil") |
| 70 | + assert.Equal(t, &expectedResult, result) |
| 71 | +} |
| 72 | + |
| 73 | +func TestCreateLinkPayAccount_ErrorRequest(t *testing.T) { |
| 74 | + // mockApiRequest interface |
| 75 | + // for mocking Call API |
| 76 | + mockApiRequest := mocks.NewApiRequestInterface(t) |
| 77 | + |
| 78 | + // mock request |
| 79 | + mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes() |
| 80 | + mockUrl := getMockUrlCreatePayAccountSandBox() |
| 81 | + mockHeader := getMockHeaderSandBox() |
| 82 | + expectedResult := midtrans.LinkAccountPayResponse{} |
| 83 | + |
| 84 | + // doing mock call |
| 85 | + mockApiRequest. |
| 86 | + On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(errors.New("error request")) |
| 87 | + |
| 88 | + // mock options |
| 89 | + mockOptions := getMockOptionsSandBox() |
| 90 | + mockOptions.ApiCall = mockApiRequest |
| 91 | + |
| 92 | + opts, _ := pg.NewOption(mockOptions) |
| 93 | + |
| 94 | + lap := getMockParamsLinkAccountPay() |
| 95 | + result, err := midtrans.CreateLinkPayAccount(lap, opts) |
| 96 | + |
| 97 | + assert.NotNil(t, err, "error should not to be nil") |
| 98 | + assert.Nil(t, result, "result should be nil") |
| 99 | +} |
| 100 | + |
| 101 | +func TestCreateLinkPayAccount_DefaultPaymentType(t *testing.T) { |
| 102 | + // mockApiRequest interface |
| 103 | + // for mocking Call API |
| 104 | + mockApiRequest := mocks.NewApiRequestInterface(t) |
| 105 | + |
| 106 | + // mock request |
| 107 | + mockParamLinkAccountPay := getMockParamsLinkAccountPayBytes() |
| 108 | + mockUrl := getMockUrlCreatePayAccountSandBox() |
| 109 | + mockHeader := getMockHeaderSandBox() |
| 110 | + expectedResult := midtrans.LinkAccountPayResponse{} |
| 111 | + |
| 112 | + // doing mock call |
| 113 | + mockApiRequest. |
| 114 | + On("Call", mock.Anything, http.MethodPost, mockUrl, mockHeader, mockParamLinkAccountPay, &expectedResult).Return(nil) |
| 115 | + |
| 116 | + // mock options |
| 117 | + mockOptions := getMockOptionsSandBox() |
| 118 | + mockOptions.ApiCall = mockApiRequest |
| 119 | + |
| 120 | + opts, _ := pg.NewOption(mockOptions) |
| 121 | + |
| 122 | + lap := getMockParamsLinkAccountPay() |
| 123 | + lap.PaymentType = "" |
| 124 | + result, err := midtrans.CreateLinkPayAccount(lap, opts) |
| 125 | + |
| 126 | + assert.Nil(t, err, "error should be nil") |
| 127 | + assert.Equal(t, &expectedResult, result) |
| 128 | +} |
| 129 | + |
| 130 | +func TestCreateLinkPayAccount_ErrorValidationPaymentTypeNotMatch(t *testing.T) { |
| 131 | + mockOptions := getMockOptionsSandBox() |
| 132 | + |
| 133 | + opts, _ := pg.NewOption(mockOptions) |
| 134 | + |
| 135 | + lap := getMockParamsLinkAccountPay() |
| 136 | + lap.PaymentType = midtrans.PaymentTypeBCA |
| 137 | + result, err := midtrans.CreateLinkPayAccount(lap, opts) |
| 138 | + |
| 139 | + assert.NotNil(t, err, "error should not to be nil") |
| 140 | + assert.Nil(t, result, "result should be nil") |
| 141 | +} |
| 142 | + |
| 143 | +func TestCreateLinkPayAccountWithContext_ErrorValidationPaymentTypeNotMatch(t *testing.T) { |
| 144 | + ctx := context.Background() |
| 145 | + mockOptions := getMockOptionsSandBox() |
| 146 | + |
| 147 | + opts, _ := pg.NewOption(mockOptions) |
| 148 | + |
| 149 | + lap := getMockParamsLinkAccountPay() |
| 150 | + lap.PaymentType = midtrans.PaymentTypeBCA |
| 151 | + result, err := midtrans.CreateLinkPayAccountWithContext(ctx, lap, opts) |
| 152 | + |
| 153 | + assert.NotNil(t, err, "error should not to be nil") |
| 154 | + assert.Nil(t, result, "result should be nil") |
| 155 | +} |
| 156 | + |
| 157 | +func TestCreateLinkPayAccount_ErrorValidationParamsGopayPartnerIsNil(t *testing.T) { |
| 158 | + mockOptions := getMockOptionsSandBox() |
| 159 | + |
| 160 | + opts, _ := pg.NewOption(mockOptions) |
| 161 | + |
| 162 | + lap := getMockParamsLinkAccountPay() |
| 163 | + lap.GopayPartner = nil |
| 164 | + result, err := midtrans.CreateLinkPayAccount(lap, opts) |
| 165 | + |
| 166 | + assert.NotNil(t, err, "error should not to be nil") |
| 167 | + assert.Nil(t, result, "result should be nil") |
| 168 | +} |
0 commit comments