@Test public void getPaymentMethodNonces_returnsAnEmptyListIfEmpty() { BraintreeFragment fragment = new MockFragmentBuilder() .successResponse(stringFromFixture("payment_methods/get_payment_methods_empty_response.json")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); ArgumentCaptor<List<PaymentMethodNonce>> captor = ArgumentCaptor.forClass((Class) List.class); verify(fragment).postCallback(captor.capture()); assertEquals(0, captor.getValue().size()); }
@Test public void getPaymentMethodNonces_doesNotParseAndroidPayPaymentMethods() { BraintreeFragment fragment = new MockFragmentBuilder() .successResponse(stringFromFixture("payment_methods/get_payment_methods_android_pay_response.json")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); ArgumentCaptor<List<PaymentMethodNonce>> captor = ArgumentCaptor.forClass((Class) List.class); verify(fragment).postCallback(captor.capture()); List<PaymentMethodNonce> paymentMethodNonces = captor.getValue(); assertEquals(0, paymentMethodNonces.size()); }
@Test public void getPaymentMethodNonces_fetchesPaymentMethods() { BraintreeFragment fragment = new MockFragmentBuilder() .successResponse(stringFromFixture("payment_methods/get_payment_methods_response.json")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); ArgumentCaptor<List<PaymentMethodNonce>> captor = ArgumentCaptor.forClass((Class) List.class); verify(fragment).postCallback(captor.capture()); List<PaymentMethodNonce> paymentMethodNonces = captor.getValue(); assertEquals(3, paymentMethodNonces.size()); assertEquals("11", ((CardNonce) paymentMethodNonces.get(0)).getLastTwo()); assertEquals("PayPal", paymentMethodNonces.get(1).getTypeLabel()); assertEquals("happy-venmo-joe", ((VenmoAccountNonce) paymentMethodNonces.get(2)).getUsername()); }
@Test public void requestOneTimePayment_userAction_canBeSetToCommit() { BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").userAction(PayPalRequest.USER_ACTION_COMMIT)); ArgumentCaptor<Intent> dataCaptor = ArgumentCaptor.forClass(Intent.class); verify(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), dataCaptor.capture()); Uri uri = dataCaptor.getValue().getData(); assertEquals("commit", uri.getQueryParameter("useraction")); }
@Test public void requestOneTimePayment_userAction_setsUserActionToBlankStringonDefault() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1").userAction(PayPalRequest.USER_ACTION_DEFAULT)); ArgumentCaptor<Intent> dataCaptor = ArgumentCaptor.forClass(Intent.class); verify(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), dataCaptor.capture()); Uri uri = dataCaptor.getValue().getData(); assertEquals("", uri.getQueryParameter("useraction")); }
@Test public void enroll_callsListenerWithUnionPayEnrollmentIdAdded() throws JSONException { String expectedEnrollmentId = "some-enrollment-id"; boolean expectedSmsCodeRequired = true; JSONObject response = new JSONObject(); response.put("unionPayEnrollmentId", expectedEnrollmentId); response.put("smsCodeRequired", expectedSmsCodeRequired); BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfigurationWithUnionPay) .successResponse(response.toString()) .build(); UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder(); UnionPay.enroll(fragment, unionPayCardBuilder); verify(fragment).postUnionPayCallback(expectedEnrollmentId, expectedSmsCodeRequired); }
@Test(timeout = 1000) public void requestOneTimePayment_customHandlerIsCalledCorrectly() throws InterruptedException { BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); final CountDownLatch latch = new CountDownLatch(1); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"), new PayPalApprovalHandler() { @Override public void handleApproval(Request request, PayPalApprovalCallback paypalApprovalCallback) { latch.countDown(); } }); latch.await(); }
@Test public void fetchCapabilities_callsListenerWithCapabilitiesOnSuccess() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfigurationWithUnionPay) .successResponse(stringFromFixture("unionpay_capabilities_success_response.json")) .build(); UnionPay.fetchCapabilities(fragment, UNIONPAY_CREDIT); ArgumentCaptor<UnionPayCapabilities> argumentCaptor = ArgumentCaptor.forClass(UnionPayCapabilities.class); verify(fragment).postCallback(argumentCaptor.capture()); UnionPayCapabilities capabilities = argumentCaptor.getValue(); assertNotNull(capabilities); assertTrue(capabilities.isUnionPay()); assertFalse(capabilities.isDebit()); assertTrue(capabilities.supportsTwoStepAuthAndCapture()); assertTrue(capabilities.isSupported()); }
@Test public void enroll_sendsAnalyticsEventOnSuccess() throws JSONException { UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder() .cardNumber("some-card-number"); JSONObject successObject = new JSONObject() .put("unionPayEnrollmentId", "unionPayEnrollmentId") .put("smsCodeRequired", true); BraintreeFragment braintreeFragment = new MockFragmentBuilder() .configuration(mConfigurationWithUnionPay) .successResponse(successObject.toString()) .build(); UnionPay.enroll(braintreeFragment, unionPayCardBuilder); verify(braintreeFragment).sendAnalyticsEvent("union-pay.enrollment-succeeded"); }
@Test public void getPaymentMethodNonces_sendsAnAnalyticsEventForParsingErrors() { BraintreeFragment fragment = new MockFragmentBuilder() .successResponse("{}") .build(); PaymentMethod.getPaymentMethodNonces(fragment); verify(fragment).sendAnalyticsEvent("get-payment-methods.failed"); }
@Test public void getRewardsBalance_callsListenerWithRewardsBalanceOnIneligibleCard() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfiguration) .successResponse(stringFromFixture("response/amex_rewards_balance_ineligible_card.json")) .build(); AmericanExpress.getRewardsBalance(fragment, "fake-nonce", "USD"); ArgumentCaptor<AmericanExpressRewardsBalance> argumentCaptor = ArgumentCaptor.forClass(AmericanExpressRewardsBalance.class); verify(fragment).postAmericanExpressCallback(argumentCaptor.capture()); AmericanExpressRewardsBalance rewardsBalance = argumentCaptor.getValue(); assertNotNull(rewardsBalance); assertNull(rewardsBalance.getConversionRate()); assertNull(rewardsBalance.getCurrencyAmount()); assertNull(rewardsBalance.getCurrencyIsoCode()); assertNull(rewardsBalance.getRequestId()); assertNull(rewardsBalance.getRewardsAmount()); assertNull(rewardsBalance.getRewardsUnit()); assertEquals("INQ2002", rewardsBalance.getErrorCode()); assertEquals("Card is ineligible", rewardsBalance.getErrorMessage()); }
@Test public void getRewardsBalance_callsListenerWithRewardsBalanceOnInsufficientPoints() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfiguration) .successResponse(stringFromFixture("response/amex_rewards_balance_insufficient_points.json")) .build(); AmericanExpress.getRewardsBalance(fragment, "fake-nonce", "USD"); ArgumentCaptor<AmericanExpressRewardsBalance> argumentCaptor = ArgumentCaptor.forClass(AmericanExpressRewardsBalance.class); verify(fragment).postAmericanExpressCallback(argumentCaptor.capture()); AmericanExpressRewardsBalance rewardsBalance = argumentCaptor.getValue(); assertNotNull(rewardsBalance); assertNull(rewardsBalance.getConversionRate()); assertNull(rewardsBalance.getCurrencyAmount()); assertNull(rewardsBalance.getCurrencyIsoCode()); assertNull(rewardsBalance.getRequestId()); assertNull(rewardsBalance.getRewardsAmount()); assertNull(rewardsBalance.getRewardsUnit()); assertEquals("INQ2003", rewardsBalance.getErrorCode()); assertEquals("Insufficient points on card", rewardsBalance.getErrorMessage()); }
@Test public void getRewardsBalance_sendsAnalyticsEventOnParseError() { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfiguration) .successResponse("-- not json --") .build(); AmericanExpress.getRewardsBalance(fragment, "fake-nonce", "USD"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.start"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.parse.failed"); } }
@Test public void getPaymentMethodNonces_sendsAnAnalyticsEventForSuccess() { BraintreeFragment fragment = new MockFragmentBuilder() .successResponse(stringFromFixture("payment_methods/get_payment_methods_response.json")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); verify(fragment).sendAnalyticsEvent("get-payment-methods.succeeded"); }
@Test public void requestBillingAgreement_cancelUrlTriggersCancelListener() { final BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Intent intent = new Intent() .setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/cancel")); PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent); return null; } }).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class)); PayPal.requestBillingAgreement(fragment, new PayPalRequest()); verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL); }
@Test public void requestOneTimePayment_customHandlerCancelCallbackIsInvoked() throws InterruptedException { BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1"), new PayPalApprovalHandler() { @Override public void handleApproval(Request request, PayPalApprovalCallback paypalApprovalCallback) { paypalApprovalCallback.onCancel(); } }); verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL); }
@Test public void checkout_cancelUrlTriggersCancelListener() { final BraintreeFragment fragment = mMockFragmentBuilder .successResponse(stringFromFixture("paypal_hermes_response.json")) .build(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Intent intent = new Intent() .setData(Uri.parse("com.braintreepayments.api.test.braintree://onetouch/v1/cancel")); PayPal.onActivityResult(fragment, Activity.RESULT_OK, intent); return null; } }).when(fragment).browserSwitch(eq(BraintreeRequestCodes.PAYPAL), any(Intent.class)); PayPal.requestOneTimePayment(fragment, new PayPalRequest("1")); verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL); }
@Test public void getRewardsBalance_sendsAnalyticsEventOnSuccess() { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfiguration) .successResponse(stringFromFixture("response/amex_rewards_balance_success.json")) .build(); AmericanExpress.getRewardsBalance(fragment, "fake-nonce", "USD"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.start"); verify(fragment).sendAnalyticsEvent("amex.rewards-balance.success"); }
@Test public void fetchCapabilities_sendsAnalyticsEventOnSuccess() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(mConfigurationWithUnionPay) .successResponse(stringFromFixture("unionpay_capabilities_success_response.json")) .build(); UnionPay.fetchCapabilities(fragment, UNIONPAY_CREDIT); verify(fragment).sendAnalyticsEvent("union-pay.capabilities-received"); }
@Test public void onActivityResult_withSuccessfulVaultCall_sendsAnalyticsEvent() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization clientToken = Authorization.fromString(stringFromFixture("base_64_client_token.txt")); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(clientToken) .sessionId("session-id") .successResponse(stringFromFixture("payment_methods/venmo_account_response.json")) .build(); Venmo.authorizeAccount(fragment, true); Intent responseIntent = new Intent() .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "nonce"); Venmo.onActivityResult(fragment, Activity.RESULT_OK, responseIntent); verify(fragment).sendAnalyticsEvent(endsWith("pay-with-venmo.vault.success")); }