From 667b28acae12932c8f8e71a6c5e5677c98ff9c22 Mon Sep 17 00:00:00 2001 From: Diogo Moreira Date: Tue, 20 Feb 2018 15:17:07 +0000 Subject: [PATCH] Add support for transaction commit request security code --- .../integration/model/TransactionTest.java | 43 +++++++++++++++++++ .../service/UserCardServiceTest.java | 2 +- .../uphold_android_sdk/model/Transaction.java | 2 +- .../transaction/TransactionCommitRequest.java | 27 ++++++++++++ .../TransactionCommitRequestTest.java | 10 +++++ 5 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java index 9feb229..7b9505e 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/model/TransactionTest.java @@ -455,6 +455,49 @@ public Promise call(UpholdRestAdapter adapter) { Assert.assertEquals(bodyOutput.toString(), "{\"beneficiary\":{\"address\":{\"city\":\"faz\",\"country\":\"fez\",\"line1\":\"fiz\",\"line2\":\"foz\",\"state\":\"fuz\",\"zipCode\":\"foobiz\"},\"name\":\"buz\",\"relationship\":\"fez\"},\"message\":\"foobar\"}"); } + @Test + public void commitWithMessageAndSecurityCodeShouldReturnTheTransaction() throws Exception { + ByteArrayOutputStream bodyOutput = new ByteArrayOutputStream(); + String responseString = "{ \"id\": \"foobar\" }"; + + MockRestAdapter adapter = new MockRestAdapter<>(responseString, null); + + adapter.request(new RepromiseFunction() { + @Override + public Promise call(UpholdRestAdapter adapter) { + Transaction transaction = Fixtures.loadTransaction(new HashMap() {{ + put("transactionStatus", "pending"); + put("originCardId", "foo"); + put("transactionId", "bar"); + }}); + + transaction.setUpholdRestAdapter(adapter); + + return transaction.commit(new TransactionCommitRequest("foobar", "foo")); + } + }); + + Header otpHeader = null; + Request request = adapter.getRequest(); + Transaction transaction = adapter.getResult(); + + for (Header header : request.getHeaders()) { + if ("OTP-Token".equalsIgnoreCase(header.getName())) { + otpHeader = header; + + break; + } + } + + request.getBody().writeTo(bodyOutput); + + Assert.assertEquals(request.getUrl(), String.format("%s/v0/me/cards/foo/transactions/bar/commit", BuildConfig.API_SERVER_URL)); + Assert.assertEquals(request.getMethod(), "POST"); + Assert.assertNull(otpHeader); + Assert.assertEquals(transaction.getId(), "foobar"); + Assert.assertEquals(bodyOutput.toString(), "{\"message\":\"foobar\",\"securityCode\":\"foo\"}"); + } + @Test public void commitWithOTPShouldReturnTheTransaction() throws Exception { ByteArrayOutputStream bodyOutput = new ByteArrayOutputStream(); diff --git a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java index e8a6d35..b2e6b22 100644 --- a/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java +++ b/src/app/src/androidTest/java/com/uphold/uphold_android_sdk/test/integration/service/UserCardServiceTest.java @@ -74,7 +74,7 @@ public Promise call(UpholdRestAdapter upholdRestAdapter) { UserCardService userCardService = adapter.getRestAdapter().create(UserCardService.class); RetrofitPromise promise = new RetrofitPromise<>(); - userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message"), "otp", promise); + userCardService.confirmTransaction("foo", "bar", new TransactionCommitRequest("message", "securityCode"), "otp", promise); return promise; } diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java index 594971c..9ea1bce 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/Transaction.java @@ -207,7 +207,7 @@ public Promise commit(String otp, TransactionCommitRequest transact } if (transactionCommitRequest == null) { - transactionCommitRequest = new TransactionCommitRequest(null, null); + transactionCommitRequest = new TransactionCommitRequest((String) null); } userCardService.confirmTransaction(getCardIdPath(), this.getId(), transactionCommitRequest, otp, promise); diff --git a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java index a9dfd96..859e243 100644 --- a/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java +++ b/src/app/src/main/java/com/uphold/uphold_android_sdk/model/transaction/TransactionCommitRequest.java @@ -10,6 +10,7 @@ public class TransactionCommitRequest implements Serializable { private final Beneficiary beneficiary; private final String message; + private final String securityCode; /** * Constructor. @@ -20,6 +21,7 @@ public class TransactionCommitRequest implements Serializable { public TransactionCommitRequest(Beneficiary beneficiary) { this.beneficiary = beneficiary; this.message = null; + this.securityCode = null; } /** @@ -31,6 +33,7 @@ public TransactionCommitRequest(Beneficiary beneficiary) { public TransactionCommitRequest(String message) { this.beneficiary = null; this.message = message; + this.securityCode = null; } /** @@ -43,6 +46,20 @@ public TransactionCommitRequest(String message) { public TransactionCommitRequest(Beneficiary beneficiary, String message) { this.beneficiary = beneficiary; this.message = message; + this.securityCode = null; + } + + /** + * Constructor. + * + * @param message The transaction message. + * @param securityCode The transaction security code. + */ + + public TransactionCommitRequest(String message, String securityCode) { + this.beneficiary = null; + this.message = message; + this.securityCode = securityCode; } /** @@ -65,4 +82,14 @@ public String getMessage() { return message; } + /** + * Gets the transaction security code. + * + * @return the transaction security code. + */ + + public String getSecurityCode() { + return securityCode; + } + } diff --git a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java index 31a0bb9..aa22990 100644 --- a/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java +++ b/src/app/src/test/java/com/uphold/uphold_android_sdk/test/unit/model/transaction/TransactionCommitRequestTest.java @@ -64,4 +64,14 @@ public void shouldBeSerializableTransactionCommitRequestWithBeneficiaryAndMessag Assert.assertEquals(transactionCommitRequest.getBeneficiary().getRelationship(), deserializedTransactionCommitRequestTest.getBeneficiary().getRelationship()); } + @Test + public void shouldBeSerializableTransactionCommitRequestWithMessageAndSecurityCode() { + TransactionCommitRequest transactionCommitRequest = new TransactionCommitRequest("foo", "bar"); + byte[] serializedTransactionCommitRequestTest = SerializationUtils.serialize(transactionCommitRequest); + TransactionCommitRequest deserializedTransactionCommitRequestTest = SerializationUtils.deserialize(serializedTransactionCommitRequestTest); + + Assert.assertEquals(transactionCommitRequest.getMessage(), deserializedTransactionCommitRequestTest.getMessage()); + Assert.assertEquals(transactionCommitRequest.getSecurityCode(), deserializedTransactionCommitRequestTest.getSecurityCode()); + } + }