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

Add tests for changes from #937: ignoring 3rd party load failures while redirecting #945

Merged
merged 2 commits into from
May 24, 2018
Merged
Changes from all 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
97 changes: 94 additions & 3 deletions Tests/Tests/STPRedirectContextTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ - (void)testSafariViewControllerRedirectFlow_foregroundNotification {
OCMVerify([mockVC presentViewController:[OCMArg checkWithBlock:checker]
animated:YES
completion:[OCMArg any]]);
[self unsubscribeContext:context];
}


Expand Down Expand Up @@ -223,10 +224,55 @@ - (void)testSafariViewControllerRedirectFlow_didFinish {

/**
After starting a SafariViewController redirect flow,
when SafariViewController fails to load, RedirectContext's completion block
and dismiss method should be called.
when SafariViewController fails to load the initial page (on iOS < 11.0),
RedirectContext's completion block should not be called (SFVC keeps loading)
*/
- (void)testSafariViewControllerRedirectFlow_failedToLoad {
- (void)testSafariViewControllerRedirectFlow_failedInitialLoad_preiOS11 {
if (@available(iOS 11, *)) {
// See testSafariViewControllerRedirectFlow_failedInitialLoad_iOS11Plus
// and testSafariViewControllerRedirectFlow_failedInitialLoadAfterRedirect_iOS11Plus
return; // Skipping
}

id mockVC = OCMClassMock([UIViewController class]);
STPSource *source = [STPFixtures iDEALSource];
STPRedirectContext *context = [[STPRedirectContext alloc] initWithSource:source completion:^(__unused NSString *sourceID, __unused NSString *clientSecret, __unused NSError *error) {
XCTFail(@"completion called");
}];
id sut = OCMPartialMock(context);

OCMReject([sut unsubscribeFromNotifications]);
OCMReject([sut dismissPresentedViewController]);

[sut startSafariViewControllerRedirectFlowFromViewController:mockVC];

BOOL(^checker)(id) = ^BOOL(id vc) {
if ([vc isKindOfClass:[SFSafariViewController class]]) {
SFSafariViewController *sfvc = (SFSafariViewController *)vc;
// Tell the delegate that the initial load failed. on iOS 10, this is a no-op
[sfvc.delegate safariViewController:sfvc didCompleteInitialLoad:NO];
return YES;
}
return NO;
};
OCMVerify([mockVC presentViewController:[OCMArg checkWithBlock:checker]
animated:YES
completion:[OCMArg any]]);
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to call [self unsubscribeContext:context] here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch. I found another (pre-existing) test case that wasn't unsubscribing, and have updated them both.

[self unsubscribeContext:context];
}

/**
After starting a SafariViewController redirect flow,
when SafariViewController fails to load the initial page (on iOS 11+ & without redirects),
RedirectContext's completion block and dismiss method should be called.
*/
- (void)testSafariViewControllerRedirectFlow_failedInitialLoad_iOS11Plus API_AVAILABLE(ios(11)) {
if (@available(iOS 11, *)) {}
else {
// see testSafariViewControllerRedirectFlow_failedInitialLoad_preiOS11
return; // Skipping
}

id mockVC = OCMClassMock([UIViewController class]);
STPSource *source = [STPFixtures iDEALSource];
XCTestExpectation *exp = [self expectationWithDescription:@"completion"];
Expand Down Expand Up @@ -258,6 +304,51 @@ - (void)testSafariViewControllerRedirectFlow_failedToLoad {
[self waitForExpectationsWithTimeout:2 handler:nil];
}

/**
After starting a SafariViewController redirect flow,
when SafariViewController fails to load the initial page (on iOS 11+ after redirecting to non-Stripe page),
RedirectContext's completion block should not be called (SFVC keeps loading)
*/

- (void)testSafariViewControllerRedirectFlow_failedInitialLoadAfterRedirect_iOS11Plus API_AVAILABLE(ios(11)) {
if (@available(iOS 11, *)) {}
else {
// see testSafariViewControllerRedirectFlow_failedInitialLoad_preiOS11
return; // Skipping
}

id mockVC = OCMClassMock([UIViewController class]);
STPSource *source = [STPFixtures iDEALSource];
STPRedirectContext *context = [[STPRedirectContext alloc] initWithSource:source completion:^(__unused NSString *sourceID, __unused NSString *clientSecret, __unused NSError *error) {
XCTFail(@"completion called");
}];
id sut = OCMPartialMock(context);

OCMReject([sut unsubscribeFromNotifications]);
OCMReject([sut dismissPresentedViewController]);

[sut startSafariViewControllerRedirectFlowFromViewController:mockVC];

BOOL(^checker)(id) = ^BOOL(id vc) {
if ([vc isKindOfClass:[SFSafariViewController class]]) {
SFSafariViewController *sfvc = (SFSafariViewController *)vc;
// before initial load is done, SFVC was redirected to a non-stripe.com domain
[sfvc.delegate safariViewController:sfvc
initialLoadDidRedirectToURL:[NSURL URLWithString:@"https://girogate.de"]];
// Tell the delegate that the initial load failed.
// on iOS 11, with the redirect, this is a no-op
[sfvc.delegate safariViewController:sfvc didCompleteInitialLoad:NO];
return YES;
}
return NO;
};
OCMVerify([mockVC presentViewController:[OCMArg checkWithBlock:checker]
animated:YES
completion:[OCMArg any]]);

[self unsubscribeContext:context];
}

/**
After starting a SafariViewController redirect flow,
when the RedirectContext is cancelled, its dismiss method should be called.
Expand Down