Skip to content

Commit

Permalink
Merge branch 'main' into paypal-app-switch-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
richherrera committed Sep 24, 2024
2 parents 383917b + ef22d3f commit 2e4b21a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.braintreepayments.api.threedsecure
import com.braintreepayments.api.core.ApiClient
import com.braintreepayments.api.core.ApiClient.Companion.versionedPath
import com.braintreepayments.api.core.BraintreeClient
import com.braintreepayments.api.core.BraintreeException
import com.braintreepayments.api.threedsecure.ThreeDSecureParams.Companion.fromJson
import org.json.JSONException
import org.json.JSONObject
Expand Down Expand Up @@ -36,12 +37,16 @@ internal class ThreeDSecureAPI(
}

fun authenticateCardinalJWT(
threeDSecureParams: ThreeDSecureParams,
cardinalJWT: String,
threeDSecureParams: ThreeDSecureParams?,
cardinalJWT: String?,
callback: ThreeDSecureResultCallback
) {
val lookupCardNonce = threeDSecureParams.threeDSecureNonce
val lookupNonce = threeDSecureParams.threeDSecureNonce?.string
if (threeDSecureParams == null || cardinalJWT == null) {
callback.onThreeDSecureResult(null, BraintreeException("threeDSecureParams or jwt is null"))
}

val lookupCardNonce = threeDSecureParams?.threeDSecureNonce
val lookupNonce = threeDSecureParams?.threeDSecureNonce?.string

val body = JSONObject()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,18 +320,6 @@ class ThreeDSecureClient internal constructor(
val validateResponse = paymentAuthResult.validateResponse
val jwt = paymentAuthResult.jwt

if (threeDSecureParams == null || jwt == null) {
braintreeClient.sendAnalyticsEvent(ThreeDSecureAnalytics.JWT_AUTH_FAILED)
callbackTokenizeFailure(
callback,
ThreeDSecureResult.Failure(
error = BraintreeException("threeDSecureParams or jwt is null"),
nonce = null
)
)
return
}

when (validateResponse?.actionCode) {
CardinalActionCode.FAILURE,
CardinalActionCode.NOACTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
Expand All @@ -15,6 +14,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.braintreepayments.api.core.BraintreeException;
import com.braintreepayments.api.testutils.Fixtures;
import com.braintreepayments.api.core.BraintreeClient;
import com.braintreepayments.api.sharedutils.HttpResponseCallback;
Expand Down Expand Up @@ -309,4 +309,39 @@ public void authenticateCardinalJWT_whenPostError_returnsException() throws JSON

verify(threeDSecureResultCallback).onThreeDSecureResult(null, exception);
}

@Test
public void authenticateCardinalJWT_whenJWTNull_returnsException()
throws JSONException {
BraintreeClient braintreeClient = new MockBraintreeClientBuilder()
.build();

ThreeDSecureResultCallback threeDSecureResultCallback =
mock(ThreeDSecureResultCallback.class);
ThreeDSecureParams threeDSecureParams =
ThreeDSecureParams.fromJson(Fixtures.THREE_D_SECURE_V2_LOOKUP_RESPONSE);

ThreeDSecureAPI sut = new ThreeDSecureAPI(braintreeClient);
sut.authenticateCardinalJWT(threeDSecureParams, null, threeDSecureResultCallback);

ArgumentCaptor<BraintreeException> captor = ArgumentCaptor.forClass(BraintreeException.class);
verify(threeDSecureResultCallback).onThreeDSecureResult(isNull(), captor.capture());
assertEquals(captor.getValue().getMessage(), "threeDSecureParams or jwt is null");
}

@Test
public void authenticateCardinalJWT_whenThreeDSecureParamsNull_returnsException() {
BraintreeClient braintreeClient = new MockBraintreeClientBuilder()
.build();

ThreeDSecureResultCallback threeDSecureResultCallback =
mock(ThreeDSecureResultCallback.class);

ThreeDSecureAPI sut = new ThreeDSecureAPI(braintreeClient);
sut.authenticateCardinalJWT(null, "jwt", threeDSecureResultCallback);

ArgumentCaptor<BraintreeException> captor = ArgumentCaptor.forClass(BraintreeException.class);
verify(threeDSecureResultCallback).onThreeDSecureResult(isNull(), captor.capture());
assertEquals(captor.getValue().getMessage(), "threeDSecureParams or jwt is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -789,75 +789,5 @@ public void tokenize_whenValidateResponseSuccess_onAuthenticateCardinalJWTError_
verify(braintreeClient).sendAnalyticsEvent(ThreeDSecureAnalytics.VERIFY_FAILED, new AnalyticsEventParams());
}

@Test
public void tokenize_null_threeDSecureParams_returns_error()
throws BraintreeException {
CardinalClient cardinalClient = new MockCardinalClientBuilder().build();
BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build();

ValidateResponse validateResponse = mock(ValidateResponse.class);
when(validateResponse.getActionCode()).thenReturn(CardinalActionCode.SUCCESS);

doAnswer((Answer<Void>) invocation -> {
ThreeDSecureResultCallback callback =
(ThreeDSecureResultCallback) invocation.getArguments()[2];
callback.onThreeDSecureResult(threeDSecureParams, null);
return null;
}).when(threeDSecureAPI).authenticateCardinalJWT(any(ThreeDSecureParams.class), anyString(),
any(ThreeDSecureResultCallback.class));

ThreeDSecureClient sut =
new ThreeDSecureClient(braintreeClient, cardinalClient,
threeDSecureAPI);

ThreeDSecurePaymentAuthResult paymentAuthResult =
new ThreeDSecurePaymentAuthResult("jwt", validateResponse, null, null);
sut.tokenize(paymentAuthResult, threeDSecureTokenizeCallback);

ArgumentCaptor<ThreeDSecureResult> captor = ArgumentCaptor.forClass(ThreeDSecureResult.class);
verify(threeDSecureTokenizeCallback).onThreeDSecureResult(captor.capture());
ThreeDSecureResult result = captor.getValue();
assertTrue(result instanceof ThreeDSecureResult.Failure);
assertEquals(((ThreeDSecureResult.Failure) result).getError().getMessage(), "threeDSecureParams or jwt is null");

verify(braintreeClient).sendAnalyticsEvent(ThreeDSecureAnalytics.JWT_AUTH_FAILED, new AnalyticsEventParams());
verify(braintreeClient).sendAnalyticsEvent(ThreeDSecureAnalytics.VERIFY_FAILED, new AnalyticsEventParams());
}

@Test
public void tokenize_null_jwt_returns_error()
throws BraintreeException {
CardinalClient cardinalClient = new MockCardinalClientBuilder().build();
BraintreeClient braintreeClient = new MockBraintreeClientBuilder().build();

ValidateResponse validateResponse = mock(ValidateResponse.class);
when(validateResponse.getActionCode()).thenReturn(CardinalActionCode.SUCCESS);

doAnswer((Answer<Void>) invocation -> {
ThreeDSecureResultCallback callback =
(ThreeDSecureResultCallback) invocation.getArguments()[2];
callback.onThreeDSecureResult(threeDSecureParams, null);
return null;
}).when(threeDSecureAPI).authenticateCardinalJWT(any(ThreeDSecureParams.class), anyString(),
any(ThreeDSecureResultCallback.class));

ThreeDSecureClient sut =
new ThreeDSecureClient(braintreeClient, cardinalClient,
threeDSecureAPI);

ThreeDSecurePaymentAuthResult paymentAuthResult =
new ThreeDSecurePaymentAuthResult(null, validateResponse, threeDSecureParams, null);
sut.tokenize(paymentAuthResult, threeDSecureTokenizeCallback);

ArgumentCaptor<ThreeDSecureResult> captor = ArgumentCaptor.forClass(ThreeDSecureResult.class);
verify(threeDSecureTokenizeCallback).onThreeDSecureResult(captor.capture());
ThreeDSecureResult result = captor.getValue();
assertTrue(result instanceof ThreeDSecureResult.Failure);
assertEquals(((ThreeDSecureResult.Failure) result).getError().getMessage(), "threeDSecureParams or jwt is null");

verify(braintreeClient).sendAnalyticsEvent(ThreeDSecureAnalytics.JWT_AUTH_FAILED, new AnalyticsEventParams());
verify(braintreeClient).sendAnalyticsEvent(ThreeDSecureAnalytics.VERIFY_FAILED, new AnalyticsEventParams());
}

// endregion
}

0 comments on commit 2e4b21a

Please sign in to comment.