Skip to content

Commit

Permalink
fix(GiniBankSDK): Possible solution to fix tests crashing on ginibank…
Browse files Browse the repository at this point in the history
…api keychain access
  • Loading branch information
igor-gini committed Jan 14, 2025
1 parent 645d99e commit dbb8259
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import Foundation

// should only be used in tests
var _GINIBANKAPILIBRARY_DISABLE_KEYCHAIN_PRECONDITION_FAILURE: Bool = false

/// The Gini Bank API Library
public final class GiniBankAPI {

Expand Down Expand Up @@ -145,8 +148,10 @@ extension GiniBankAPI {
value: client.domain,
service: .auth))
} catch {
preconditionFailure("There was an error using the Keychain. " +
"Check that the Keychain capability is enabled in your project")
if !_GINIBANKAPILIBRARY_DISABLE_KEYCHAIN_PRECONDITION_FAILURE {
preconditionFailure("There was an error using the Keychain. " +
"Check that the Keychain capability is enabled in your project")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ private class MockTokenSource: AlternativeTokenSource {
}

private class MockCaptureResultsDelegate: GiniCaptureResultsDelegate {
private(set) var cancelCalled: Bool = false
private(set) var closeCalled: Bool = false
func giniCaptureAnalysisDidFinishWith(result: AnalysisResult) {
}

func giniCaptureDidCancelAnalysis() {
cancelCalled = true
func giniCaptureDidCloseAnalysis() {
closeCalled = true
}

func giniCaptureDidEnterManually() {
Expand Down Expand Up @@ -58,6 +58,7 @@ final class NetworkingScreenApiCoordinatorTests: XCTestCase {
private var trackingDelegate: MockTrackingDelegate!

override func setUp() {
_GINIBANKAPILIBRARY_DISABLE_KEYCHAIN_PRECONDITION_FAILURE = true
tokenSource = makeTokenSource()
resultsDelegate = MockCaptureResultsDelegate()
configuration = GiniBankConfiguration()
Expand All @@ -66,65 +67,65 @@ final class NetworkingScreenApiCoordinatorTests: XCTestCase {
}

func testSDKClose() throws {
// let (coordinator, _) = try makeCoordinatorAndService(fromViewController: true) // so the sdk would start
//
// XCTAssertEqual(GiniBankNetworkingScreenApiCoordinator.currentCoordinator, coordinator, "The coordinator should be the same")
//
// GiniBank.cancelCurrentSDK()
// XCTAssertNil(GiniBankNetworkingScreenApiCoordinator.currentCoordinator)
// XCTAssertTrue(resultsDelegate.cancelCalled, "Should've called delegate for cancelling")
let (coordinator, _) = try makeCoordinatorAndService(fromViewController: true) // so the sdk would start

XCTAssertEqual(GiniBankNetworkingScreenApiCoordinator.currentCoordinator, coordinator, "The coordinator should be the same")

GiniBank.closeCurrentSDK()
XCTAssertNil(GiniBankNetworkingScreenApiCoordinator.currentCoordinator)
XCTAssertTrue(resultsDelegate.closeCalled, "Should've called delegate for cancelling")
}

func testInitWithAlternativeTokenSource() throws {
// let (coordinator, service) = try makeCoordinatorAndService()
//
// // check domain
// XCTAssertEqual(service.apiDomain.domainString, "pay-api.gini.net", "Service api domain should match our default")
//
// // check token
// let receivedToken = try XCTUnwrap(
// login(service: service),
// "Should log in successfully"
// )
// XCTAssertEqual(receivedToken, tokenSource.token, "Received token should match the expected token")
//
// // check for delegates/configs
// XCTAssertNotNil(
// coordinator.resultsDelegate as? MockCaptureResultsDelegate,
// "Coordinator should have correct results delegate instance"
// )
// XCTAssertEqual(coordinator.giniBankConfiguration, configuration, "Coordinator should have correct configuration instance")
// XCTAssertNotNil(
// coordinator.trackingDelegate as? MockTrackingDelegate,
// "Coordinator should have correct tracking delegate instance"
// )
// XCTAssertEqual(coordinator.documentService.metadata?.headers, metadata.headers, "Metadata headers should match")
let (coordinator, service) = try makeCoordinatorAndService()

// check domain
XCTAssertEqual(service.apiDomain.domainString, "pay-api.gini.net", "Service api domain should match our default")

// check token
let receivedToken = try XCTUnwrap(
login(service: service),
"Should log in successfully"
)
XCTAssertEqual(receivedToken, tokenSource.token, "Received token should match the expected token")

// check for delegates/configs
XCTAssertNotNil(
coordinator.resultsDelegate as? MockCaptureResultsDelegate,
"Coordinator should have correct results delegate instance"
)
XCTAssertEqual(coordinator.giniBankConfiguration, configuration, "Coordinator should have correct configuration instance")
XCTAssertNotNil(
coordinator.trackingDelegate as? MockTrackingDelegate,
"Coordinator should have correct tracking delegate instance"
)
XCTAssertEqual(coordinator.documentService.metadata?.headers, metadata.headers, "Metadata headers should match")
}

func testViewControllerWithAlternativeTokenSource() throws {
// let (coordinator, service) = try makeCoordinatorAndService(fromViewController: true)
//
// // check domain
// XCTAssertEqual(service.apiDomain.domainString, "pay-api.gini.net", "Service api domain should match our default")
//
// // check token
// let receivedToken = try XCTUnwrap(
// login(service: service),
// "Should log in successfully"
// )
// XCTAssertEqual(receivedToken, tokenSource.token, "Received token should match the expected token")
//
// // check for delegates/configs
// XCTAssertNotNil(
// coordinator.resultsDelegate as? MockCaptureResultsDelegate,
// "Coordinator should have correct results delegate instance"
// )
// XCTAssertEqual(coordinator.giniBankConfiguration, configuration, "Coordinator should have correct configuration instance")
// XCTAssertNotNil(
// coordinator.trackingDelegate as? MockTrackingDelegate,
// "Coordinator should have correct tracking delegate instance"
// )
// XCTAssertEqual(coordinator.documentService.metadata?.headers, metadata.headers, "Metadata headers should match")
let (coordinator, service) = try makeCoordinatorAndService(fromViewController: true)

// check domain
XCTAssertEqual(service.apiDomain.domainString, "pay-api.gini.net", "Service api domain should match our default")

// check token
let receivedToken = try XCTUnwrap(
login(service: service),
"Should log in successfully"
)
XCTAssertEqual(receivedToken, tokenSource.token, "Received token should match the expected token")

// check for delegates/configs
XCTAssertNotNil(
coordinator.resultsDelegate as? MockCaptureResultsDelegate,
"Coordinator should have correct results delegate instance"
)
XCTAssertEqual(coordinator.giniBankConfiguration, configuration, "Coordinator should have correct configuration instance")
XCTAssertNotNil(
coordinator.trackingDelegate as? MockTrackingDelegate,
"Coordinator should have correct tracking delegate instance"
)
XCTAssertEqual(coordinator.documentService.metadata?.headers, metadata.headers, "Metadata headers should match")
}
}

Expand Down

0 comments on commit dbb8259

Please sign in to comment.