Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes crash in STPAddCardViewController with some prefilled billing addresses #1004

Merged
merged 4 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Stripe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
04FCFA191BD59A8C00297732 /* STPCategoryLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FCFA171BD59A8C00297732 /* STPCategoryLoader.h */; };
3617A51420FE5BBB001A9E6A /* NSLocale+STPSwizzling.h in Headers */ = {isa = PBXBuildFile; fileRef = 3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */; };
3617A51520FE5BBB001A9E6A /* NSLocale+STPSwizzling.m in Sources */ = {isa = PBXBuildFile; fileRef = 3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */; };
3691EB74211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 3691EB73211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m */; };
8B013C891F1E784A00DD831B /* STPPaymentConfigurationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */; };
8B429AD81EF9D4B400F95F34 /* STPBankAccountParams+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 8B429AD71EF9D4A300F95F34 /* STPBankAccountParams+Private.h */; };
8B429AD91EF9D4B500F95F34 /* STPBankAccountParams+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 8B429AD71EF9D4A300F95F34 /* STPBankAccountParams+Private.h */; };
Expand Down Expand Up @@ -1040,6 +1041,7 @@
11C74B9B164043050071C2CA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSLocale+STPSwizzling.h"; sourceTree = "<group>"; };
3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSLocale+STPSwizzling.m"; sourceTree = "<group>"; };
3691EB73211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = STPShippingAddressViewControllerTest.m; sourceTree = "<group>"; };
4A0D74F918F6106100966D7B /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
7E0B1132203572FB00271AD3 /* fi */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = fi; path = Localizations/fi.lproj/Localizable.strings; sourceTree = "<group>"; };
8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPPaymentConfigurationTest.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1675,6 +1677,7 @@
C1EEDCC91CA2186300A54582 /* STPPhoneNumberValidatorTest.m */,
C1FEE5981CBFF24000A7632B /* STPPostalCodeValidatorTest.m */,
F152321A1EA92F9D00D65C67 /* STPRedirectContextTest.m */,
3691EB73211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m */,
8BD87B8A1EFB136F00269C2B /* STPSourceCardDetailsTest.m */,
8B5B4B431EFDD925005CF475 /* STPSourceOwnerTest.m */,
C1BD9B1E1E390A2700CEE925 /* STPSourceParamsTest.m */,
Expand Down Expand Up @@ -2844,6 +2847,7 @@
C1054F911FE197AE0033C87E /* STPPaymentContextSnapshotTests.m in Sources */,
C127110A1DBA7E490087840D /* STPAddressViewModelTest.m in Sources */,
C17D24EE1E37DBAC005CB188 /* STPSourceTest.m in Sources */,
3691EB74211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m in Sources */,
C1E4F8061EBBEB0F00E611F5 /* STPCustomerContextTest.m in Sources */,
B3302F4C200700AB005DDBE9 /* STPLegalEntityParamsTest.m in Sources */,
F14C872F1D4FCDBA00C7CC6A /* STPPaymentContextApplePayTest.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Stripe/STPAddCardViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ - (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel addedC
if (rowsInSection != NSNotFound && rowsInSection < [self tableView:self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to (cleanly) push this down into the STPAddressViewModel? It looks like it's already trying to track showingPostalCodeCell, and has logic for whether it's being added or removed based on updates to the address.

Or maybe we should move the _addressViewModel.delegate = self line to after we set the pre-filled info, because we don't care about updates to the ViewModel until after we've finished creating the views? That might allow us to avoid this complexity completely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to (cleanly) push this down into the STPAddressViewModel?

I actually prefer the logic to live in the view controller because this situation is a result of layout passes/table view caching/etc. that I think the view controller is closer too than the view model (correct me if I'm wrong in my view model understanding :) ).

Or maybe we should move the _addressViewModel.delegate = self line to after we set the pre-filled info, because we don't care about updates to the ViewModel until after we've finished creating the views? That might allow us to avoid this complexity completely?

I thought about this. I believe that change would also fix the crash, but this solution is more robust

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self updateInputAccessoryVisiblity];
}
[self updateInputAccessoryVisiblity];
}

- (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel removedCellAtIndex:(NSUInteger)index {
Expand Down
73 changes: 73 additions & 0 deletions Tests/Tests/STPShippingAddressViewControllerTest.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// STPShippingAddressViewControllerTest.m
// StripeiOS Tests
//
// Created by Cameron Sabol on 8/7/18.
// Copyright © 2018 Stripe, Inc. All rights reserved.
//

#import <XCTest/XCTest.h>

#import <Stripe/Stripe.h>
#import "NSLocale+STPSwizzling.h"
#import "STPFixtures.h"

@interface STPShippingAddressViewControllerTest : XCTestCase

@end

@implementation STPShippingAddressViewControllerTest

- (void)testPrefilledBillingAddress_removeAddress {
STPPaymentConfiguration *config = [STPFixtures paymentConfiguration];
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress];

STPAddress *address = [STPAddress new];
address.name = @"John Smith Doe";
address.phone = @"8885551212";
address.email = @"[email protected]";
address.line1 = @"55 John St";
address.city = @"Harare";
address.postalCode = @"10002";
address.country = @"ZW";

STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config
theme:[STPTheme defaultTheme]
currency:nil
shippingAddress:address
selectedShippingMethod:nil
prefilledInformation:nil];

[sut loadView];
[sut viewDidLoad];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about adding XCTAssertNoThrow calls around these?

I think that'd catch the NSInternalInconsistencyException that was being triggered. If so, it'd give a better failure message if it regresses, and it helps document what these tests are looking for. As it is, without context this test looks like a no-op.

Same for the matching tests of the other class.

}

- (void)testPrefilledBillingAddress_addAddress {
[NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about adding assertions (or a separate test?) that postalCodeIsRequiredForCountryCode: returns the expected values?

If ZW starts requiring a postal code, or we stop requiring it for US, we start testing a different code path, and the code could break silently.

It also helps document what's being tested, and why those countries were chosen.

STPPaymentConfiguration *config = [STPFixtures paymentConfiguration];
config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress];

STPAddress *address = [STPAddress new];
address.name = @"John Smith Doe";
address.phone = @"8885551212";
address.email = @"[email protected]";
address.line1 = @"55 John St";
address.city = @"New York";
address.state = @"NY";
address.postalCode = @"10002";
address.country = @"US";

STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config
theme:[STPTheme defaultTheme]
currency:nil
shippingAddress:address
selectedShippingMethod:nil
prefilledInformation:nil];

[sut loadView];
[sut viewDidLoad];
[NSLocale stp_resetCurrentLocale];
}


@end