Skip to content

Commit

Permalink
Call Cardinal Cleanup from ThreeDSecureActivity (#1048)
Browse files Browse the repository at this point in the history
* Call cleanup from ThreeDSecureActivity.

* Add unit test for cardinal cleanup from ThreeDSecureActivity.

* Update CHANGELOG.
  • Loading branch information
sshropshire authored Jun 27, 2024
1 parent 6bd00eb commit a3b9035
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* BraintreeCore
* Use TLS 1.3 for all HTTP requests, when available
* Refactor TLSCertificatePinning `certInputStream` property to initialize a `ByteArrayInputStream` once instead of every time the property is accessed.
* ThreeDSecure
* Move Cardinal cleanup from SDK internals into `ThreeDSecureActivity`.

## 4.47.0 (2024-06-06)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void continueLookup(ThreeDSecureResult threeDSecureResult, CardinalChallengeObse
} catch (RuntimeException e) {
throw new BraintreeException("Cardinal SDK cca_continue Error.", e);
}

Cardinal.getInstance().cleanup();
}

private void configureCardinal(Context context, Configuration configuration, ThreeDSecureRequest request) throws BraintreeException {
Expand Down Expand Up @@ -116,4 +114,8 @@ private void configureCardinal(Context context, Configuration configuration, Thr
String getConsumerSessionId() {
return consumerSessionId;
}

void cleanup() {
Cardinal.getInstance().cleanup();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ThreeDSecureActivity extends AppCompatActivity implements CardinalV
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
challengeObserver = new CardinalChallengeObserver(
this, (context, validateResponse, s) -> handleValidated(validateResponse, s));
this, (context, validateResponse, s) -> handleValidated(cardinalClient, validateResponse, s));

/*
Here, we schedule the 3DS auth challenge launch to run immediately after onCreate() is
Expand Down Expand Up @@ -84,10 +84,13 @@ private void finishWithError(String errorMessage) {
// TODO: NEXT_MAJOR_VERSION remove implementation of CardinalValidateReceiver
@Override
public void onValidated(Context context, ValidateResponse validateResponse, String jwt) {
handleValidated(validateResponse, jwt);
handleValidated(cardinalClient, validateResponse, jwt);
}

private void handleValidated(ValidateResponse validateResponse, String jwt) {
@VisibleForTesting
void handleValidated(CardinalClient cardinalClient, ValidateResponse validateResponse, String jwt) {
cardinalClient.cleanup();

Intent result = new Intent();
result.putExtra(EXTRA_JWT, jwt);
result.putExtra(EXTRA_THREE_D_SECURE_RESULT, (ThreeDSecureResult) getIntent().getExtras()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ class CardinalClientUnitTest {
cardinalChallengeObserver
)
}

verify { cardinalInstance.cleanup() }
}

@Test
Expand All @@ -270,4 +268,13 @@ class CardinalClientUnitTest {
assertSame(runtimeException, e.cause)
}
}

@Test
fun cleanup_cleansUpCardinalInstance() {
every { Cardinal.getInstance() } returns cardinalInstance

val sut = CardinalClient()
sut.cleanup()
verify { cardinalInstance.cleanup() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void onCreate_withoutExtras_finishesWithError() throws BraintreeException
}

@Test
public void onValidated_returnsValidationResults() throws JSONException {
public void handleValidated_returnsValidationResults() throws JSONException {
ThreeDSecureResult threeDSecureResult =
ThreeDSecureResult.fromJson(Fixtures.THREE_D_SECURE_LOOKUP_RESPONSE);

Expand All @@ -121,11 +121,14 @@ public void onValidated_returnsValidationResults() throws JSONException {

ValidateResponse cardinalValidateResponse = mock(ValidateResponse.class);
when(cardinalValidateResponse.getActionCode()).thenReturn(CardinalActionCode.SUCCESS);
sut.onValidated(null, cardinalValidateResponse, "jwt");

CardinalClient cardinalClient = mock(CardinalClient.class);
sut.handleValidated(cardinalClient, cardinalValidateResponse, "jwt");
verify(sut).finish();

ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(sut).setResult(eq(RESULT_OK), captor.capture());
verify(cardinalClient).cleanup();

Intent intentForResult = captor.getValue();
ValidateResponse activityResult = (ValidateResponse) (intentForResult.getSerializableExtra(ThreeDSecureActivity.EXTRA_VALIDATION_RESPONSE));
Expand Down

0 comments on commit a3b9035

Please sign in to comment.