Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-vysotskyi-cko committed Jul 10, 2023
1 parent cfe4be0 commit c20fa39
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ final class HttpCustomerTokensRepository: CustomerTokensRepository {
}
let httpRequest = HttpConnectorRequest<Response>.post(
path: "/customers/\(request.customerId)/tokens",
body: request,
includesDeviceMetadata: true,
requiresPrivateKey: true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
import Foundation

@_spi(PO)
public struct POCreateCustomerTokenRequest {
public struct POCreateCustomerTokenRequest: Encodable {

/// Customer id to associate created token with.
public let customerId: String
@POImmutableExcludedCodable
public var customerId: String

/// Flag if you wish to verify the customer token by making zero value transaction. Applicable for cards only.
public let verify: Bool

public init(customerId: String, verify: Bool = false) {
self.customerId = customerId
self._customerId = .init(value: customerId)
self.verify = verify
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ import XCTest
let card = try await cardsService.tokenize(
request: .init(number: "4242424242424242", expMonth: 12, expYear: 40, cvc: "737")
)
let token = try await sut.createCustomerToken(
request: POCreateCustomerTokenRequest(customerId: Constants.customerId)
)
let request = POAssignCustomerTokenRequest(
customerId: Constants.customerId, tokenId: token.id, source: card.id, verify: false
customerId: Constants.customerId,
tokenId: try await createToken(verify: false).id,
source: card.id
)

// When
Expand All @@ -52,8 +51,39 @@ import XCTest
XCTAssertEqual(updatedToken.cardId, card.id)
}

func test_assignCustomerToken_whenVerifyIsSetToTrue_triggers3DS() async throws {
// Given
let card = try await cardsService.tokenize(
request: .init(number: "4000000000000101", expMonth: 12, expYear: 40, cvc: "737")
)
let request = POAssignCustomerTokenRequest(
customerId: Constants.customerId,
tokenId: try await createToken(verify: true).id,
source: card.id,
verify: true,
enableThreeDS2: true
)
let threeDSService = Mock3DSService()
threeDSService.authenticationRequestFromClosure = { _, completion in
completion(.failure(.init(code: .cancelled)))
}

// When
_ = try? await sut.assignCustomerToken(request: request, threeDSService: threeDSService)

// Then
XCTAssertEqual(threeDSService.authenticationRequestCallsCount, 1)
}

// MARK: - Private Properties

private var sut: POCustomerTokensService!
private var cardsService: POCardsService!

// MARK: - Private Methods

private func createToken(verify: Bool) async throws -> POCustomerToken {
let request = POCreateCustomerTokenRequest(customerId: Constants.customerId, verify: verify)
return try await sut.createCustomerToken(request: request)
}
}
6 changes: 3 additions & 3 deletions Tests/ProcessOutTests/Sources/Mocks/3DS/Mock3DSService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ final class Mock3DSService: PO3DSService {
completion: @escaping (Result<PO3DS2AuthenticationRequest, POFailure>) -> Void
) {
authenticationRequestCallsCount += 1
authenticationRequestFromClosure(configuration, completion)
authenticationRequestFromClosure?(configuration, completion)
}

func handle(challenge: PO3DS2Challenge, completion: @escaping (Result<Bool, POFailure>) -> Void) {
handleChallengeCallsCount += 1
handleChallengeFromClosure(challenge, completion)
handleChallengeFromClosure?(challenge, completion)
}

func handle(redirect: PO3DSRedirect, completion: @escaping (Result<String, POFailure>) -> Void) {
handleRedirectCallsCount += 1
handleRedirectFromClosure(redirect, completion)
handleRedirectFromClosure?(redirect, completion)
}
}

Expand Down

0 comments on commit c20fa39

Please sign in to comment.