From 134a414471d456970c54b135871e3f31c7c87562 Mon Sep 17 00:00:00 2001 From: Cameron Sabol Date: Tue, 17 Jul 2018 13:10:36 -0700 Subject: [PATCH 1/4] Fixes STPAddCardViewController STPAddressViewModelDelegate methods to check for valid table view inserts and removes. In cases where a zip code is required and the user's locale doesn't support zip codes but their billing address includes one (or vice-versa), this was causing a crash because we were trying to add or remove cells that were already there or were never there respectively. --- Stripe.xcodeproj/project.pbxproj | 18 +++++-- Stripe/STPAddCardViewController.m | 24 +++++---- Tests/Tests/NSLocale+STPSwizzling.h | 16 ++++++ Tests/Tests/NSLocale+STPSwizzling.m | 59 ++++++++++++++++++++++ Tests/Tests/STPAddCardViewControllerTest.m | 48 ++++++++++++++++++ 5 files changed, 151 insertions(+), 14 deletions(-) create mode 100644 Tests/Tests/NSLocale+STPSwizzling.h create mode 100644 Tests/Tests/NSLocale+STPSwizzling.m diff --git a/Stripe.xcodeproj/project.pbxproj b/Stripe.xcodeproj/project.pbxproj index c056e4bb5c9..18297e3667a 100644 --- a/Stripe.xcodeproj/project.pbxproj +++ b/Stripe.xcodeproj/project.pbxproj @@ -324,6 +324,8 @@ 04F94DD31D22A23F004FC826 /* NSBundle+Stripe_AppName.h in Headers */ = {isa = PBXBuildFile; fileRef = 049A3F971CC76A2400F57DE7 /* NSBundle+Stripe_AppName.h */; }; 04F94DD41D22A242004FC826 /* NSBundle+Stripe_AppName.m in Sources */ = {isa = PBXBuildFile; fileRef = 049A3F981CC76A2400F57DE7 /* NSBundle+Stripe_AppName.m */; }; 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 */; }; 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 */; }; @@ -1036,6 +1038,8 @@ 04F94D6F1D21CB20004FC826 /* StripeiOSResources.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = StripeiOSResources.xcconfig; sourceTree = ""; }; 04FCFA171BD59A8C00297732 /* STPCategoryLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STPCategoryLoader.h; sourceTree = ""; }; 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 = ""; }; + 3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSLocale+STPSwizzling.m"; sourceTree = ""; }; 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 = ""; }; 8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPPaymentConfigurationTest.m; sourceTree = ""; }; @@ -1500,12 +1504,14 @@ 04CDB5281A5F3A9300B854EE /* StripeTests */ = { isa = PBXGroup; children = ( - C18867D61E8B069E00A77634 /* Snapshot */, - C18867D71E8B07F600A77634 /* Functional */, - C18867D81E8B093300A77634 /* Unit */, - C1D23FB71D37FE0F002FD83C /* JSON */, F1B8534D1FDF544B0065A49E /* FBSnapshotTestCase+STPViewControllerLoading.h */, F1B8534E1FDF544B0065A49E /* FBSnapshotTestCase+STPViewControllerLoading.m */, + C18867D71E8B07F600A77634 /* Functional */, + C1D23FB71D37FE0F002FD83C /* JSON */, + 3617A51220FE5BBB001A9E6A /* NSLocale+STPSwizzling.h */, + 3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */, + C18867D61E8B069E00A77634 /* Snapshot */, + C1CFCB781ED5F85A00BE45DF /* stp_test_upload_image.jpeg */, C18867D91E8B0C4100A77634 /* STPFixtures.h */, C18867DA1E8B0C4100A77634 /* STPFixtures.m */, F1D96F981DC7DCDE00477E64 /* STPLocalizationUtils+STPTestAdditions.h */, @@ -1514,7 +1520,7 @@ C1CFCB6A1ED5E0F400BE45DF /* STPMocks.m */, C1D23FAF1D37FC90002FD83C /* STPTestUtils.h */, C1D23FB01D37FC90002FD83C /* STPTestUtils.m */, - C1CFCB781ED5F85A00BE45DF /* stp_test_upload_image.jpeg */, + C18867D81E8B093300A77634 /* Unit */, ); name = StripeTests; path = Tests/Tests; @@ -2130,6 +2136,7 @@ F148ABFA1D5E88C40014FD92 /* STPTestUtils.h in Headers */, C1CFCB6D1ED5E0F800BE45DF /* STPMocks.h in Headers */, C18867DB1E8B0C4100A77634 /* STPFixtures.h in Headers */, + 3617A51420FE5BBB001A9E6A /* NSLocale+STPSwizzling.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -2833,6 +2840,7 @@ F148ABFB1D5E88C70014FD92 /* STPTestUtils.m in Sources */, 8BB97F081F26645B0095122A /* NSDictionary+StripeTest.m in Sources */, 045A62AB1B8E7259000165CE /* STPPaymentCardTextFieldTest.m in Sources */, + 3617A51520FE5BBB001A9E6A /* NSLocale+STPSwizzling.m in Sources */, C1054F911FE197AE0033C87E /* STPPaymentContextSnapshotTests.m in Sources */, C127110A1DBA7E490087840D /* STPAddressViewModelTest.m in Sources */, C17D24EE1E37DBAC005CB188 /* STPSourceTest.m in Sources */, diff --git a/Stripe/STPAddCardViewController.m b/Stripe/STPAddCardViewController.m index aa44ff9ee7d..0407836d772 100644 --- a/Stripe/STPAddCardViewController.m +++ b/Stripe/STPAddCardViewController.m @@ -115,10 +115,6 @@ - (void)createAndSetupViews { STPPaymentCardTextFieldCell *paymentCell = [[STPPaymentCardTextFieldCell alloc] init]; paymentCell.paymentField.delegate = self; self.paymentCell = paymentCell; - - if (self.prefilledInformation.billingAddress != nil) { - self.addressViewModel.address = self.prefilledInformation.billingAddress; - } self.activityIndicator = [[STPPaymentActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20.0f, 20.0f)]; @@ -128,6 +124,10 @@ - (void)createAndSetupViews { self.tableView.dataSource = self; self.tableView.delegate = self; + if (self.prefilledInformation.billingAddress != nil) { + self.addressViewModel.address = self.prefilledInformation.billingAddress; + } + STPSectionHeaderView *addressHeaderView = [STPSectionHeaderView new]; addressHeaderView.theme = self.theme; addressHeaderView.title = STPLocalizedString(@"Billing Address", @"Title for billing address entry section"); @@ -398,14 +398,20 @@ - (void)paymentCardTextFieldDidEndEditingCVC:(__unused STPPaymentCardTextField * #pragma mark - STPAddressViewModelDelegate - (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel addedCellAtIndex:(NSUInteger)index { - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; - [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; - [self updateInputAccessoryVisiblity]; + NSInteger rowsInSection = [self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]; + if (rowsInSection != NSNotFound && rowsInSection < [self tableView:self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]) { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; + [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + [self updateInputAccessoryVisiblity]; + } } - (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel removedCellAtIndex:(NSUInteger)index { - NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; - [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + NSInteger rowsInSection = [self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]; + if (rowsInSection != NSNotFound && index < (NSUInteger)rowsInSection) { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:STPPaymentCardBillingAddressSection]; + [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; + } [self updateInputAccessoryVisiblity]; } diff --git a/Tests/Tests/NSLocale+STPSwizzling.h b/Tests/Tests/NSLocale+STPSwizzling.h new file mode 100644 index 00000000000..aa3904654f2 --- /dev/null +++ b/Tests/Tests/NSLocale+STPSwizzling.h @@ -0,0 +1,16 @@ +// +// NSLocale+STPSwizzling.h +// StripeiOS Tests +// +// Created by Cameron Sabol on 7/17/18. +// Copyright © 2018 Stripe, Inc. All rights reserved. +// + +#import + +@interface NSLocale (STPSwizzling) + ++ (void)stp_setCurrentLocale:(NSLocale *)locale; ++ (void)stp_resetCurrentLocale; + +@end diff --git a/Tests/Tests/NSLocale+STPSwizzling.m b/Tests/Tests/NSLocale+STPSwizzling.m new file mode 100644 index 00000000000..7386308cd99 --- /dev/null +++ b/Tests/Tests/NSLocale+STPSwizzling.m @@ -0,0 +1,59 @@ +// +// NSLocale+STPSwizzling.m +// StripeiOS Tests +// +// Created by Cameron Sabol on 7/17/18. +// Copyright © 2018 Stripe, Inc. All rights reserved. +// + +#import "NSLocale+STPSwizzling.h" + +#import + +@interface NSObject (STPSwizzling) + ++ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement; + +@end + +@implementation NSObject (STPSwizzling) + ++ (void)stp_swizzleClassMethod:(SEL)original withReplacement:(SEL)replacement +{ + method_exchangeImplementations(class_getClassMethod(self, original), class_getClassMethod(self, replacement)); +} + +@end + +@implementation NSLocale (STPSwizzling) + +static NSLocale *_stpLocaleOverride = nil; + ++ (void)stp_setCurrentLocale:(NSLocale *)locale +{ + if (_stpLocaleOverride == nil & locale != nil) { + [self stp_swizzleClassMethod:@selector(currentLocale) withReplacement:@selector(stp_currentLocale)]; + [self stp_swizzleClassMethod:@selector(autoupdatingCurrentLocale) withReplacement:@selector(stp_autoUpdatingCurrentLocale)]; + [self stp_swizzleClassMethod:@selector(systemLocale) withReplacement:@selector(stp_systemLocale)]; + } + _stpLocaleOverride = locale; +} + ++ (void)stp_resetCurrentLocale +{ + [self stp_setCurrentLocale:nil]; +} + ++ (instancetype)stp_currentLocale { + return _stpLocaleOverride ?: [self stp_currentLocale]; +} + ++ (instancetype)stp_autoUpdatingCurrentLocale { + return _stpLocaleOverride ?: [self stp_autoUpdatingCurrentLocale]; +} + ++ (instancetype)stp_systemLocale { + return _stpLocaleOverride ?: [self stp_systemLocale]; +} + +@end diff --git a/Tests/Tests/STPAddCardViewControllerTest.m b/Tests/Tests/STPAddCardViewControllerTest.m index f65728aa770..269c2a83c33 100644 --- a/Tests/Tests/STPAddCardViewControllerTest.m +++ b/Tests/Tests/STPAddCardViewControllerTest.m @@ -10,6 +10,7 @@ #import #import #import "NSError+Stripe.h" +#import "NSLocale+STPSwizzling.h" #import "STPCard.h" #import "STPFixtures.h" #import "STPPaymentCardTextFieldCell.h" @@ -41,6 +42,53 @@ - (STPAddCardViewController *)buildAddCardViewController { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" +- (void)testPrefilledBillingAddress_removeAddress { + STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; + config.requiredBillingAddressFields = STPBillingAddressFieldsZip; + STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config + theme:[STPTheme defaultTheme]]; + STPAddress *address = [STPAddress new]; + address.name = @"John Smith Doe"; + address.phone = @"8885551212"; + address.email = @"foo@example.com"; + address.line1 = @"55 John St"; + address.city = @"Harare"; + address.postalCode = @"10002"; + address.country = @"ZW"; + + STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; + prefilledInfo.billingAddress = address; + sut.prefilledInformation = prefilledInfo; + + [sut loadView]; + [sut viewDidLoad]; +} + +- (void)testPrefilledBillingAddress_addAddress { + [NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; + STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; + config.requiredBillingAddressFields = STPBillingAddressFieldsZip; + STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config + theme:[STPTheme defaultTheme]]; + STPAddress *address = [STPAddress new]; + address.name = @"John Smith Doe"; + address.phone = @"8885551212"; + address.email = @"foo@example.com"; + address.line1 = @"55 John St"; + address.city = @"New York"; + address.state = @"NY"; + address.postalCode = @"10002"; + address.country = @"US"; + + STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; + prefilledInfo.billingAddress = address; + sut.prefilledInformation = prefilledInfo; + + [sut loadView]; + [sut viewDidLoad]; + [NSLocale stp_resetCurrentLocale]; +} + - (void)testNextWithCreateTokenError { STPAddCardViewController *sut = [self buildAddCardViewController]; STPCardParams *expectedCardParams = [STPFixtures cardParams]; From fe5b1aaa1b4f52ef080939ec1411a5180bef1333 Mon Sep 17 00:00:00 2001 From: Cameron Sabol Date: Mon, 6 Aug 2018 14:04:40 -0700 Subject: [PATCH 2/4] Consistent accessory update calls --- Stripe/STPAddCardViewController.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Stripe/STPAddCardViewController.m b/Stripe/STPAddCardViewController.m index 0407836d772..94e20054a55 100644 --- a/Stripe/STPAddCardViewController.m +++ b/Stripe/STPAddCardViewController.m @@ -402,8 +402,8 @@ - (void)addressViewModel:(__unused STPAddressViewModel *)addressViewModel addedC if (rowsInSection != NSNotFound && rowsInSection < [self tableView:self.tableView numberOfRowsInSection:STPPaymentCardBillingAddressSection]) { 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 { From 70e2b835690cf89e7aa7cf5d860eb7de0a308ecd Mon Sep 17 00:00:00 2001 From: Cameron Sabol Date: Tue, 7 Aug 2018 15:34:47 -0700 Subject: [PATCH 3/4] Adds test to catch zip code switching regressions in STPShippingAddressViewController --- Stripe.xcodeproj/project.pbxproj | 4 + .../STPShippingAddressViewControllerTest.m | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 Tests/Tests/STPShippingAddressViewControllerTest.m diff --git a/Stripe.xcodeproj/project.pbxproj b/Stripe.xcodeproj/project.pbxproj index 18297e3667a..2af17ed770f 100644 --- a/Stripe.xcodeproj/project.pbxproj +++ b/Stripe.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 = ""; }; 3617A51320FE5BBB001A9E6A /* NSLocale+STPSwizzling.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSLocale+STPSwizzling.m"; sourceTree = ""; }; + 3691EB73211A4F31008C49E1 /* STPShippingAddressViewControllerTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = STPShippingAddressViewControllerTest.m; sourceTree = ""; }; 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 = ""; }; 8B013C881F1E784A00DD831B /* STPPaymentConfigurationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPPaymentConfigurationTest.m; sourceTree = ""; }; @@ -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 */, @@ -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 */, diff --git a/Tests/Tests/STPShippingAddressViewControllerTest.m b/Tests/Tests/STPShippingAddressViewControllerTest.m new file mode 100644 index 00000000000..5b9fdbabf1e --- /dev/null +++ b/Tests/Tests/STPShippingAddressViewControllerTest.m @@ -0,0 +1,73 @@ +// +// STPShippingAddressViewControllerTest.m +// StripeiOS Tests +// +// Created by Cameron Sabol on 8/7/18. +// Copyright © 2018 Stripe, Inc. All rights reserved. +// + +#import + +#import +#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 = @"foo@example.com"; + 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]; +} + +- (void)testPrefilledBillingAddress_addAddress { + [NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; + STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; + config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; + + STPAddress *address = [STPAddress new]; + address.name = @"John Smith Doe"; + address.phone = @"8885551212"; + address.email = @"foo@example.com"; + 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 From 9e214319b87c8bc3041462d2253056f86d56a5d5 Mon Sep 17 00:00:00 2001 From: Cameron Sabol Date: Thu, 9 Aug 2018 10:10:06 -0700 Subject: [PATCH 4/4] Cleanup and adds sanity checks to new unit tests --- Tests/Tests/STPAddCardViewControllerTest.m | 26 ++++++++++++------- .../STPShippingAddressViewControllerTest.m | 18 +++++++++---- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Tests/Tests/STPAddCardViewControllerTest.m b/Tests/Tests/STPAddCardViewControllerTest.m index 269c2a83c33..bb345cc5a19 100644 --- a/Tests/Tests/STPAddCardViewControllerTest.m +++ b/Tests/Tests/STPAddCardViewControllerTest.m @@ -14,6 +14,7 @@ #import "STPCard.h" #import "STPFixtures.h" #import "STPPaymentCardTextFieldCell.h" +#import "STPPostalCodeValidator.h" @interface STPAddCardViewController (Testing) @property (nonatomic) STPPaymentCardTextFieldCell *paymentCell; @@ -39,9 +40,6 @@ - (STPAddCardViewController *)buildAddCardViewController { return vc; } -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Warc-performSelector-leaks" - - (void)testPrefilledBillingAddress_removeAddress { STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; config.requiredBillingAddressFields = STPBillingAddressFieldsZip; @@ -54,18 +52,24 @@ - (void)testPrefilledBillingAddress_removeAddress { address.line1 = @"55 John St"; address.city = @"Harare"; address.postalCode = @"10002"; - address.country = @"ZW"; + address.country = @"ZW"; // Zimbabwe does not require zip codes, while the default locale for tests (US) does + // Sanity checks + XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); + XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); STPUserInformation *prefilledInfo = [[STPUserInformation alloc] init]; prefilledInfo.billingAddress = address; sut.prefilledInformation = prefilledInfo; - [sut loadView]; - [sut viewDidLoad]; + XCTAssertNoThrow([sut loadView]); + XCTAssertNoThrow([sut viewDidLoad]); } - (void)testPrefilledBillingAddress_addAddress { - [NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; + [NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; // Zimbabwe does not require zip codes, while the default locale for tests (US) does + // Sanity checks + XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); + XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; config.requiredBillingAddressFields = STPBillingAddressFieldsZip; STPAddCardViewController *sut = [[STPAddCardViewController alloc] initWithConfiguration:config @@ -84,11 +88,15 @@ - (void)testPrefilledBillingAddress_addAddress { prefilledInfo.billingAddress = address; sut.prefilledInformation = prefilledInfo; - [sut loadView]; - [sut viewDidLoad]; + XCTAssertNoThrow([sut loadView]); + XCTAssertNoThrow([sut viewDidLoad]); [NSLocale stp_resetCurrentLocale]; } + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-performSelector-leaks" + - (void)testNextWithCreateTokenError { STPAddCardViewController *sut = [self buildAddCardViewController]; STPCardParams *expectedCardParams = [STPFixtures cardParams]; diff --git a/Tests/Tests/STPShippingAddressViewControllerTest.m b/Tests/Tests/STPShippingAddressViewControllerTest.m index 5b9fdbabf1e..f2fbacb9357 100644 --- a/Tests/Tests/STPShippingAddressViewControllerTest.m +++ b/Tests/Tests/STPShippingAddressViewControllerTest.m @@ -11,6 +11,7 @@ #import #import "NSLocale+STPSwizzling.h" #import "STPFixtures.h" +#import "STPPostalCodeValidator.h" @interface STPShippingAddressViewControllerTest : XCTestCase @@ -29,7 +30,10 @@ - (void)testPrefilledBillingAddress_removeAddress { address.line1 = @"55 John St"; address.city = @"Harare"; address.postalCode = @"10002"; - address.country = @"ZW"; + address.country = @"ZW"; // Zimbabwe does not require zip codes, while the default locale for tests (US) does + // Sanity checks + XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); + XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); STPShippingAddressViewController *sut = [[STPShippingAddressViewController alloc] initWithConfiguration:config theme:[STPTheme defaultTheme] @@ -38,12 +42,16 @@ - (void)testPrefilledBillingAddress_removeAddress { selectedShippingMethod:nil prefilledInformation:nil]; - [sut loadView]; - [sut viewDidLoad]; + XCTAssertNoThrow([sut loadView]); + XCTAssertNoThrow([sut viewDidLoad]); } - (void)testPrefilledBillingAddress_addAddress { [NSLocale stp_setCurrentLocale:[NSLocale localeWithLocaleIdentifier:@"en_ZW"]]; + // Zimbabwe does not require zip codes, while the default locale for tests (US) does + // Sanity checks + XCTAssertFalse([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"ZW"]); + XCTAssertTrue([STPPostalCodeValidator postalCodeIsRequiredForCountryCode:@"US"]); STPPaymentConfiguration *config = [STPFixtures paymentConfiguration]; config.requiredShippingAddressFields = [NSSet setWithObject:STPContactFieldPostalAddress]; @@ -64,8 +72,8 @@ - (void)testPrefilledBillingAddress_addAddress { selectedShippingMethod:nil prefilledInformation:nil]; - [sut loadView]; - [sut viewDidLoad]; + XCTAssertNoThrow([sut loadView]); + XCTAssertNoThrow([sut viewDidLoad]); [NSLocale stp_resetCurrentLocale]; }