Skip to content

Commit

Permalink
Fix BTCard to pass through options
Browse files Browse the repository at this point in the history
- Fix bug where validate was not being passed through
  when initialized with a parameters dictionary, which
  broke card vaulting in Drop-in.
- Change behavior of BTCard so that after initializing
  with a parameters dictionary, the property getters
  reflect the given values.
- The "options" parameter is no longer overwritten
  and the "validate" value is set from the
  shouldValidate property.
  • Loading branch information
intelliot authored and Richard Shin committed Nov 17, 2015
1 parent ecff079 commit 18b67d3
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Braintree.xcodeproj/xcshareddata/xcschemes/Demo.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
28 changes: 20 additions & 8 deletions BraintreeCard/BTCard.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ - (instancetype)init {
- (nonnull instancetype)initWithParameters:(NSDictionary *)parameters {
if (self = [super init]) {
_mutableParameters = [parameters mutableCopy];
_number = parameters[@"number"];
NSArray *components = [parameters[@"expiration_date"] componentsSeparatedByString:@"/"];
if (components.count == 2) {
_expirationMonth = components[0];
_expirationYear = components[1];
}
_postalCode = parameters[@"billing_address"][@"postal_code"];
_cvv = parameters[@"cvv"];
_shouldValidate = [parameters[@"options"][@"validate"] boolValue];
}
return self;
}
Expand Down Expand Up @@ -46,16 +55,19 @@ - (NSDictionary *)parameters {
p[@"cvv"] = self.cvv;
}
if (self.postalCode) {
if (![p[@"billing_address"] isKindOfClass:[NSDictionary class]]) {
p[@"billing_address"] = @{ @"postal_code": self.postalCode };
} else if ([p[@"billing_address"] isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *billingAddress = [p[@"billing_address"] mutableCopy];
billingAddress[@"postal_code"] = self.postalCode;
p[@"billing_address"] = [billingAddress copy];
NSMutableDictionary *billingAddressDictionary = [NSMutableDictionary new];
if ([p[@"billing_address"] isKindOfClass:[NSDictionary class]]) {
[billingAddressDictionary addEntriesFromDictionary:p[@"billing_address"]];
}
billingAddressDictionary[@"postal_code"] = self.postalCode;
p[@"billing_address"] = [billingAddressDictionary copy];
}
p[@"options"] = @{ @"validate": @(self.shouldValidate) };

NSMutableDictionary *optionsDictionary = [NSMutableDictionary new];
if ([p[@"options"] isKindOfClass:[NSDictionary class]]) {
[optionsDictionary addEntriesFromDictionary:p[@"options"]];
}
optionsDictionary[@"validate"] = @(self.shouldValidate);
p[@"options"] = [optionsDictionary copy];
return [p copy];
}

Expand Down
74 changes: 72 additions & 2 deletions UnitTests/BTCardTokenizationRequest_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,83 @@ class BTCard_Tests: XCTestCase {
XCTAssertEqual(card.cvv!, "123")
}

func testInitialization_fromParameters() {
let card = BTCard(parameters: ["cvv": "123", "billing_address": ["postal_code": "94949"] ])
func testInitWithParameters_withAllValuesPresent_setsAllProperties() {
let card = BTCard(parameters: [
"number": "4111111111111111",
"expiration_date": "12/20",
"cvv": "123",
"billing_address": ["postal_code": "94949"],
"options": ["validate": true],
])

XCTAssertEqual(card.number, "4111111111111111")
XCTAssertEqual(card.expirationMonth, "12")
XCTAssertEqual(card.expirationYear, "20")
XCTAssertEqual(card.postalCode, "94949")
XCTAssertEqual(card.cvv, "123")
XCTAssertTrue(card.shouldValidate)
}

func testInitWithParameters_withEmptyParameters_setsPropertiesToExpectedValues() {
let card = BTCard(parameters: [:])

XCTAssertNil(card.number)
XCTAssertNil(card.expirationMonth)
XCTAssertNil(card.expirationYear)
XCTAssertNil(card.postalCode)
XCTAssertNil(card.cvv)
XCTAssertFalse(card.shouldValidate)
}

func testInitWithParameters_withCVVAndPostalCode_setsPropertiesToExpectedValues() {
let card = BTCard(parameters: [
"cvv": "123",
"billing_address": ["postal_code": "94949"],
])

XCTAssertNil(card.number)
XCTAssertNil(card.expirationMonth)
XCTAssertNil(card.expirationYear)
XCTAssertEqual(card.postalCode, "94949")
XCTAssertEqual(card.cvv, "123")
XCTAssertFalse(card.shouldValidate)
}

func testParameters_whenInitializedWithInitWithParameters_returnsExpectedValues() {
let card = BTCard(parameters: [
"cvv": "123",
"billing_address": ["postal_code": "94949"],
])

XCTAssertEqual(card.parameters() as! Dictionary<String, NSObject>, [
"cvv": "123",
"billing_address": ["postal_code": "94949"],
"options": ["validate": false],
])
}

func testParameters_whenInitializedWithCustomParameters_returnsExpectedValues() {
let card = BTCard(parameters: [
"cvv": "123",
"billing_address": ["postal_code": "94949"],
"options": ["foo": "bar"],
])

XCTAssertEqual(card.parameters() as! Dictionary<String, NSObject>, [
"cvv": "123",
"billing_address": ["postal_code": "94949"],
"options": [
"foo": "bar",
"validate": false,
],
])
}

func testParameters_whenShouldValidateIsSetToNewValue_returnsExpectedValues() {
let card = BTCard(parameters: ["options": ["validate": false]])
card.shouldValidate = true
XCTAssertEqual(card.parameters() as! Dictionary<String, NSObject>, [
"options": [ "validate": true ],
])
}
}
20 changes: 13 additions & 7 deletions UnitTests/BTCard_Internal_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ @implementation BTCard_Internal_Tests

- (void)testParameters_standardProperties {
BTCard *card = [[BTCard alloc] initWithNumber:@"4111111111111111"
expirationMonth:@"12"
expirationYear:@"2038"
cvv:@"123"];
expirationMonth:@"12"
expirationYear:@"2038"
cvv:@"123"];
BTJSON *parameters = [[BTJSON alloc] initWithValue:card.parameters];
XCTAssertEqualObjects(parameters[@"number"].asString, @"4111111111111111");
XCTAssertEqualObjects(parameters[@"expiration_date"].asString, @"12/2038");
Expand All @@ -27,14 +27,20 @@ - (void)testParameters_whenShouldValidateIsTrue_encodesParametersCorrectly {
XCTAssertTrue(parameters[@"options"][@"validate"].isTrue);
}

- (void)testParameters_whenShouldValidateIsTrueInParameters_encodesParametersCorrectly {
BTCard *card = [[BTCard alloc] initWithParameters:@{@"options": @{@"validate": @YES}}];
BTJSON *parameters = [[BTJSON alloc] initWithValue:card.parameters];
XCTAssertTrue(parameters[@"options"][@"validate"].isTrue);
}

- (void)testParameters_encodesAllParametersIncludingAdditionalParameters {
BTCard *card =
[[BTCard alloc] initWithParameters:@{
@"billing_address": @{
@"street_address": @"724 Evergreen Terrace" }
}];
@"billing_address": @{
@"street_address": @"724 Evergreen Terrace" }
}];

card.number =@"4111111111111111";
card.number = @"4111111111111111";
card.expirationMonth = @"12";
card.expirationYear = @"2038";
card.postalCode = @"40404";
Expand Down
1 change: 1 addition & 0 deletions UnitTests/UnitTests-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// Internal headers for testing
#import "BTAPIClient_Internal.h"
#import "BTApplePayClient_Internal.h"
#import "BTCard_Internal.h"
#import "BTCardClient_Internal.h"
#import "BTPayPalDriver_Internal.h"
#import "BTThreeDSecureDriver_Internal.h"
Expand Down

0 comments on commit 18b67d3

Please sign in to comment.