Skip to content

Commit

Permalink
[Identity] Fix test mode return value (stripe#2988)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccen-stripe authored Oct 16, 2023
1 parent ceedf66 commit 40796f9
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ protocol VerificationSheetControllerProtocol: AnyObject {
)

func verifyAndTransition(
simulateDelay: Bool
simulateDelay: Bool,
completion: @escaping () -> Void
)

func unverifyAndTransition(
simulateDelay: Bool
simulateDelay: Bool,
completion: @escaping () -> Void
)

/// Request a new phoneOtp, transition to error view controller if request failed, callback on successCallback otherwise.
Expand Down Expand Up @@ -357,22 +359,28 @@ final class VerificationSheetController: VerificationSheetControllerProtocol {
}

func verifyAndTransition(
simulateDelay: Bool
simulateDelay: Bool,
completion: @escaping () -> Void
) {
apiClient.verifyTestVerificationSession(
simulateDelay: simulateDelay
).observe(on: .main) { [weak self] result in
self?.overrideTestModeReturnValue(result: .flowCompleted)
self?.transitionWithVerificaionPageDataResult(result)
completion()
}
}

func unverifyAndTransition(
simulateDelay: Bool
simulateDelay: Bool,
completion: @escaping () -> Void
) {
apiClient.unverifyTestVerificationSession(
simulateDelay: simulateDelay
).observe(on: .main) { [weak self] result in
self?.overrideTestModeReturnValue(result: .flowCompleted)
self?.transitionWithVerificaionPageDataResult(result)
completion()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ final class DebugViewController: IdentityFlowViewController {
case .submit(let completeOption):
switch completeOption {
case .success:
self.sheetController?.verifyAndTransition(simulateDelay: false)
self.sheetController?.verifyAndTransition(simulateDelay: false, completion: {})
case .failure:
self.sheetController?.unverifyAndTransition(simulateDelay: false)
self.sheetController?.unverifyAndTransition(simulateDelay: false, completion: {})
case .successAsync:
self.sheetController?.verifyAndTransition(simulateDelay: true)
self.sheetController?.verifyAndTransition(simulateDelay: true, completion: {})
case .failureAsync:
self.sheetController?.unverifyAndTransition(simulateDelay: true)
self.sheetController?.unverifyAndTransition(simulateDelay: true, completion: {})
}
case .cancelled:
finishWithResult(result: .flowCanceled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ final class VerificationSheetControllerMock: VerificationSheetControllerProtocol

}

func verifyAndTransition(simulateDelay: Bool) {
func verifyAndTransition(simulateDelay: Bool, completion: @escaping () -> Void) {
testModeReturnResult = .flowCompleted
completeOption = simulateDelay ? .successAsync : .success
}

func unverifyAndTransition(simulateDelay: Bool) {
func unverifyAndTransition(simulateDelay: Bool, completion: @escaping () -> Void) {
testModeReturnResult = .flowCompleted
completeOption = simulateDelay ? .failureAsync : .failure
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,57 +631,93 @@ final class VerificationSheetControllerTest: XCTestCase {
// Mock initial VerificationPage request successful
controller.verificationPageResponse = .success(try VerificationPageMock.response200.make())

controller.verifyAndTransition(simulateDelay: false)
exp = XCTestExpectation(description: "transition finished")
controller.verifyAndTransition(simulateDelay: false) {
self.exp.fulfill()
}

XCTAssertEqual(mockAPIClient.verifyUnverifyRequest.requestHistory.count, 1)

XCTAssertEqual(
mockAPIClient.verifyUnverifyRequest.requestHistory.first,
["simulateDelay": false]
)

mockAPIClient.verifyUnverifyRequest.respondToRequests(with: .success(try VerificationPageDataMock.response200.make()))

wait(for: [exp], timeout: 1)

XCTAssertEqual(controller.testModeReturnValue, IdentityVerificationSheet.VerificationFlowResult.flowCompleted)
}

func testVerifyAndTransitionWithDelay() throws {
// Mock initial VerificationPage request successful
controller.verificationPageResponse = .success(try VerificationPageMock.response200.make())

controller.verifyAndTransition(simulateDelay: true)
exp = XCTestExpectation(description: "transition finished")
controller.verifyAndTransition(simulateDelay: true) {
self.exp.fulfill()
}

XCTAssertEqual(mockAPIClient.verifyUnverifyRequest.requestHistory.count, 1)

XCTAssertEqual(
mockAPIClient.verifyUnverifyRequest.requestHistory.first,
["simulateDelay": true]
)

mockAPIClient.verifyUnverifyRequest.respondToRequests(with: .success(try VerificationPageDataMock.response200.make()))

wait(for: [exp], timeout: 1)

XCTAssertEqual(controller.testModeReturnValue, IdentityVerificationSheet.VerificationFlowResult.flowCompleted)
}

func testUnverifyAndTransitionWithoutDelay() throws {
// Mock initial VerificationPage request successful
controller.verificationPageResponse = .success(try VerificationPageMock.response200.make())

controller.unverifyAndTransition(simulateDelay: false)
exp = XCTestExpectation(description: "transition finished")
controller.unverifyAndTransition(simulateDelay: false) {
self.exp.fulfill()
}

XCTAssertEqual(mockAPIClient.verifyUnverifyRequest.requestHistory.count, 1)

XCTAssertEqual(
mockAPIClient.verifyUnverifyRequest.requestHistory.first,
["simulateDelay": false]
)

mockAPIClient.verifyUnverifyRequest.respondToRequests(with: .success(try VerificationPageDataMock.response200.make()))

wait(for: [exp], timeout: 1)

XCTAssertEqual(controller.testModeReturnValue, IdentityVerificationSheet.VerificationFlowResult.flowCompleted)
}

func testUnverifyAndTransitionWithDelay() throws {
// Mock initial VerificationPage request successful
controller.verificationPageResponse = .success(try VerificationPageMock.response200.make())

// Verify
controller.unverifyAndTransition(simulateDelay: true)
exp = XCTestExpectation(description: "transition finished")
controller.unverifyAndTransition(simulateDelay: true) {
self.exp.fulfill()
}

XCTAssertEqual(mockAPIClient.verifyUnverifyRequest.requestHistory.count, 1)

XCTAssertEqual(
mockAPIClient.verifyUnverifyRequest.requestHistory.first,
["simulateDelay": true]
)

mockAPIClient.verifyUnverifyRequest.respondToRequests(with: .success(try VerificationPageDataMock.response200.make()))

wait(for: [exp], timeout: 1)

XCTAssertEqual(controller.testModeReturnValue, IdentityVerificationSheet.VerificationFlowResult.flowCompleted)
}

func testGeneratePhoneOtp() throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,29 @@ final class DebugViewControllerTest: XCTestCase {

func testClickSubmitWithSuccess() {
vc.didTapButton(.submit(completeOption: .success))

XCTAssertEqual(mockSheetController.testModeReturnResult, .flowCompleted)
XCTAssertEqual(mockSheetController.completeOption, .success)
}

func testClickSubmitWithSuccessAsync() {
vc.didTapButton(.submit(completeOption: .successAsync))

XCTAssertEqual(mockSheetController.testModeReturnResult, .flowCompleted)
XCTAssertEqual(mockSheetController.completeOption, .successAsync)
}

func testClickSubmitWithFailure() {
vc.didTapButton(.submit(completeOption: .failure))

XCTAssertEqual(mockSheetController.testModeReturnResult, .flowCompleted)
XCTAssertEqual(mockSheetController.completeOption, .failure)
}

func testClickSubmitWithFailureAsync() {
vc.didTapButton(.submit(completeOption: .failureAsync))

XCTAssertEqual(mockSheetController.testModeReturnResult, .flowCompleted)
XCTAssertEqual(mockSheetController.completeOption, .failureAsync)
}
}

0 comments on commit 40796f9

Please sign in to comment.