@Test public void collectDeviceData() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder().build(); String deviceData = DataCollector.collectDeviceData(fragment); JSONObject json = new JSONObject(deviceData); assertFalse(TextUtils.isEmpty(json.getString("device_session_id"))); assertEquals("600000", json.getString("fraud_merchant_id")); assertNotNull(json.getString("correlation_id")); }
@Test public void collectDeviceData_usesDirectMerchantId() throws JSONException { BraintreeFragment fragment = new MockFragmentBuilder().build(); String deviceData = DataCollector.collectDeviceData(fragment, "100"); JSONObject json = new JSONObject(deviceData); assertFalse(TextUtils.isEmpty(json.getString("device_session_id"))); assertEquals("100", json.getString("fraud_merchant_id")); }
@Test public void requestBillingAgreement_postsExceptionWhenAmountIsIncluded() { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestBillingAgreement(fragment, new PayPalRequest("1")); ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); verify(fragment).postCallback(captor.capture()); assertTrue(captor.getValue() instanceof BraintreeException); assertEquals("There must be no amount specified for the Billing Agreement flow", captor.getValue().getMessage()); }
@Test public void authorizeAccount_postsExceptionWhenNotEnabled() { BraintreeFragment fragment = new MockFragmentBuilder().build(); PayPal.authorizeAccount(fragment); ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); verify(fragment).postCallback(captor.capture()); assertTrue(captor.getValue() instanceof BraintreeException); assertEquals("PayPal is not enabled", captor.getValue().getMessage()); }
@Test public void onActivityResult_sendsAnalyticsEventOnSuccess() { BraintreeFragment fragment = new MockFragmentBuilder() .build(); Intent intent = new Intent() .putExtra(Venmo.EXTRA_PAYMENT_METHOD_NONCE, "123456-12345-12345-a-adfa") .putExtra(Venmo.EXTRA_USERNAME, "username"); Venmo.onActivityResult(fragment, Activity.RESULT_OK, intent); verify(fragment).sendAnalyticsEvent("pay-with-venmo.app-switch.success"); }
@Test public void onActivityResult_postsCancelWhenIntentIsNull() { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.onActivityResult(fragment, Activity.RESULT_OK, null); verify(fragment).postCancelCallback(BraintreeRequestCodes.PAYPAL); }
@Test public void getPaymentMethodNonces_throwsAnError() { BraintreeFragment fragment = new MockFragmentBuilder() .errorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.getPaymentMethodNonces(fragment); ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class); verify(fragment).postCallback(captor.capture()); assertTrue(captor.getValue() instanceof UnexpectedException); }
@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 authorizeAccount_postsExceptionWhenNotInstalled() { Configuration configuration = getConfigurationFromFixture(); BraintreeFragment fragment = new MockFragmentBuilder() .configuration(configuration) .build(); Venmo.authorizeAccount(fragment, false); ArgumentCaptor<AppSwitchNotAvailableException> captor = ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); verify(fragment).postCallback(captor.capture()); assertEquals("Venmo is not installed", captor.getValue().getMessage()); }
@Test public void authorizeAccount_postsExceptionWhenNotEnabled() throws JSONException { Configuration configuration = new TestConfigurationBuilder().buildConfiguration(); BraintreeFragment fragment = new MockFragmentBuilder() .configuration(configuration) .build(); Venmo.authorizeAccount(fragment, false); ArgumentCaptor<AppSwitchNotAvailableException> captor = ArgumentCaptor.forClass(AppSwitchNotAvailableException.class); verify(fragment).postCallback(captor.capture()); assertEquals("Venmo is not enabled", captor.getValue().getMessage()); }
@Test public void requestOneTimePayment_persistsPayPalRequest() { BraintreeFragment braintreeFragment = mMockFragmentBuilder.build(); SharedPreferences prefs = BraintreeSharedPreferences.getSharedPreferences(RuntimeEnvironment.application); PayPal.requestOneTimePayment(braintreeFragment, new PayPalRequest("1").intent(PayPalRequest.INTENT_SALE)); assertNotNull(prefs.getString("com.braintreepayments.api.PayPal.PAYPAL_REQUEST_KEY", null)); }
@Test public void enroll_failsIfUnionPayIsDisabled() throws JSONException { UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder() .cardNumber("some-card-number"); BraintreeFragment fragment = new MockFragmentBuilder() .configuration(Configuration.fromJson(stringFromFixture("configuration/configuration.json"))) .build(); UnionPay.enroll(fragment, unionPayCardBuilder); ArgumentCaptor<ConfigurationException> argumentCaptor = ArgumentCaptor.forClass(ConfigurationException.class); verify(fragment).postCallback(argumentCaptor.capture()); assertEquals("UnionPay is not enabled", argumentCaptor.getValue().getMessage()); }
@Test public void requestBillingAgreement_sendsPayPalCreditOfferedAnalyticsEvent() throws JSONException { BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.requestBillingAgreement(fragment, new PayPalRequest().offerCredit(true)); verify(fragment).sendAnalyticsEvent("paypal.billing-agreement.credit.offered"); }
@Test public void onActivityResult_NotOkAndCheckoutRequestPersisted_sendsAnalyticsEvent() { persistRequest(new CheckoutRequest()); BraintreeFragment fragment = mMockFragmentBuilder.build(); PayPal.onActivityResult(fragment, Activity.RESULT_CANCELED, null); verify(fragment).sendAnalyticsEvent("paypal-single-payment.canceled"); }
@Test public void getCheckoutRequest_buildsWithLiveStageUrl() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_live_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); Request request = PayPal.getCheckoutRequest(fragment, null); assertEquals(EnvironmentManager.LIVE, request.getEnvironment()); }
@Config(sdk = 21) @Test public void deletePaymentMethodNonce_sendAnAnalyticsEventForFailure() throws InvalidArgumentException { Authorization authorization = Authorization.fromString(stringFromFixture("client_token.json")); BraintreeFragment fragment = new MockFragmentBuilder() .authorization(authorization) .graphQLErrorResponse(new UnexpectedException("Error")) .build(); PaymentMethod.deletePaymentMethod(fragment, mCardNonce); verify(fragment).sendAnalyticsEvent("delete-payment-methods.failed"); }
@Test public void getAuthorizationRequest_buildsWithLiveStageUrl() throws JSONException, InvalidArgumentException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_live_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder .authorization(Authorization.fromString(TOKENIZATION_KEY)) .configuration(configuration) .build(); Request request = PayPal.getAuthorizationRequest(fragment); assertEquals(EnvironmentManager.LIVE, request.getEnvironment()); }
@Test public void getAuthorizationRequest_buildsWithOfflineStageUrl() throws JSONException, InvalidArgumentException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder .authorization(Authorization.fromString(TOKENIZATION_KEY)) .configuration(configuration) .build(); Request request = PayPal.getAuthorizationRequest(fragment); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); }
@Test public void getAuthorizationRequest_buildsWithCustomStageUrl() throws JSONException, InvalidArgumentException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_custom_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder .authorization(Authorization.fromString(TOKENIZATION_KEY)) .configuration(configuration) .build(); Request request = PayPal.getAuthorizationRequest(fragment); assertEquals("custom", request.getEnvironment()); }
private BraintreeFragment getSetupFragment() { String configuration = mBaseConfiguration.googlePayment(mBaseConfiguration.googlePayment() .supportedNetworks(new String[]{"visa", "mastercard", "amex", "discover"})) .withAnalytics() .build(); return new MockFragmentBuilder() .configuration(configuration) .build(); }