Skip to content

Commit

Permalink
Implement AcquiringRequestBuilderTests
Browse files Browse the repository at this point in the history
  • Loading branch information
nik3212 committed Jul 25, 2023
1 parent 2fdc75b commit 008bc34
Show file tree
Hide file tree
Showing 13 changed files with 487 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class AcquiringRequestBuilder: IAcquiringRequestBuilder {
private let baseURLProvider: IURLProvider
private let publicKeyProvider: IPublicKeyProvider
private let terminalKeyProvider: IStringProvider
private let cardDataFormatter: CardDataFormatter
private let cardDataFormatter: ICardDataFormatter
private let rsaEncryptor: IRSAEncryptor
private let ipAddressProvider: IIPAddressProvider
private let environmentParametersProvider: IEnvironmentParametersProvider
Expand All @@ -40,7 +40,7 @@ final class AcquiringRequestBuilder: IAcquiringRequestBuilder {
baseURLProvider: IURLProvider,
publicKeyProvider: IPublicKeyProvider,
terminalKeyProvider: IStringProvider,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
rsaEncryptor: IRSAEncryptor,
ipAddressProvider: IIPAddressProvider,
environmentParametersProvider: IEnvironmentParametersProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct AttachCardRequest: AcquiringRequest {
init(
data: AttachCardData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
publicKey: SecKey,
baseURL: URL
) {
Expand All @@ -50,7 +50,7 @@ private extension HTTPParameters {
static func parameters(
requestData: AttachCardData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
publicKey: SecKey
) -> HTTPParameters {
var parameters: HTTPParameters = [Constants.Keys.requestKey: requestData.requestKey]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct Check3DSVersionRequest: AcquiringRequest {
init(
check3DSRequestData: Check3DSVersionData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
publicKey: SecKey,
baseURL: URL
) {
Expand All @@ -50,7 +50,7 @@ private extension HTTPParameters {
static func create(
requestData: Check3DSVersionData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
publicKey: SecKey
) -> HTTPParameters {
var parameters: HTTPParameters = [Constants.Keys.paymentId: requestData.paymentId]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct FinishAuthorizeRequest: AcquiringRequest {
init(
requestData: FinishAuthorizeData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
ipAddressProvider: IIPAddressProvider,
environmentParametersProvider: IEnvironmentParametersProvider,
publicKey: SecKey,
Expand All @@ -54,7 +54,7 @@ private extension HTTPParameters {
static func parameters(
requestData: FinishAuthorizeData,
encryptor: IRSAEncryptor,
cardDataFormatter: CardDataFormatter,
cardDataFormatter: ICardDataFormatter,
ipAddressProvider: IIPAddressProvider,
environmentParametersProvider: IEnvironmentParametersProvider,
publicKey: SecKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import Foundation

struct CardDataFormatter {
protocol ICardDataFormatter {
func formatCardData(cardNumber: String, expDate: String, cvv: String) -> String
func formatCardData(cardId: String, cvv: String?) -> String
}

struct CardDataFormatter: ICardDataFormatter {
func formatCardData(cardNumber: String, expDate: String, cvv: String) -> String {
return "\(Constants.Keys.cardNumber)=\(cardNumber);\(Constants.Keys.cardExpDate)=\(expDate);\(Constants.Keys.cardCVV)=\(cvv)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// FinishAuthorizeData+Fake.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

extension FinishAuthorizeData {
static func fake() -> FinishAuthorizeData {
FinishAuthorizeData(paymentId: "id", paymentSource: .savedCard(cardId: "123", cvv: "213"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PaymentInitData+Fake.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

extension PaymentInitData {
static func fake() -> PaymentInitData {
PaymentInitData(amount: Int64(5000), orderId: "order_id", customerKey: "key")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// CardDataFormatterMock.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

final class CardDataFormatterMock: ICardDataFormatter {

// MARK: - formatCardDataCardNumberExpDate

typealias FormatCardDataCardNumberExpDateArguments = (cardNumber: String, expDate: String, cvv: String)

var formatCardDataCardNumberExpDateCallsCount = 0
var formatCardDataCardNumberExpDateReceivedArguments: FormatCardDataCardNumberExpDateArguments?
var formatCardDataCardNumberExpDateReceivedInvocations: [FormatCardDataCardNumberExpDateArguments?] = []
var formatCardDataCardNumberExpDateReturnValue: String!

func formatCardData(cardNumber: String, expDate: String, cvv: String) -> String {
formatCardDataCardNumberExpDateCallsCount += 1
let arguments = (cardNumber, expDate, cvv)
formatCardDataCardNumberExpDateReceivedArguments = arguments
formatCardDataCardNumberExpDateReceivedInvocations.append(arguments)
return formatCardDataCardNumberExpDateReturnValue
}

// MARK: - formatCardDataCardIdCvv

typealias FormatCardDataCardIdCvvArguments = (cardId: String, cvv: String?)

var formatCardDataCardIdCvvCallsCount = 0
var formatCardDataCardIdCvvReceivedArguments: FormatCardDataCardIdCvvArguments?
var formatCardDataCardIdCvvReceivedInvocations: [FormatCardDataCardIdCvvArguments?] = []
var formatCardDataCardIdCvvReturnValue: String!

func formatCardData(cardId: String, cvv: String?) -> String {
formatCardDataCardIdCvvCallsCount += 1
let arguments = (cardId, cvv)
formatCardDataCardIdCvvReceivedArguments = arguments
formatCardDataCardIdCvvReceivedInvocations.append(arguments)
return formatCardDataCardIdCvvReturnValue
}
}

// MARK: - Resets

extension CardDataFormatterMock {
func fullReset() {
formatCardDataCardNumberExpDateCallsCount = 0
formatCardDataCardNumberExpDateReceivedArguments = nil
formatCardDataCardNumberExpDateReceivedInvocations = []

formatCardDataCardIdCvvCallsCount = 0
formatCardDataCardIdCvvReceivedArguments = nil
formatCardDataCardIdCvvReceivedInvocations = []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

final class EnvironmentParametersProviderMock: IEnvironmentParametersProvider {
var environmentParameters: [String: String] = [:]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// IPAddressProviderMock.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

public final class IPAddressProviderMock: IIPAddressProvider {
public init() {}
public var ipAddress: IPAddress?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// PublicKeyProviderMock.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

final class PublicKeyProviderMock: IPublicKeyProvider {

var publicKey: SecKey {
get { return underlyingPublicKey }
set(value) { underlyingPublicKey = value }
}

var underlyingPublicKey: SecKey!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// RSAEncryptorMock.swift
// TinkoffASDKCore-Unit-Tests
//
// Created by Никита Васильев on 21.07.2023.
//

import Foundation
@testable import TinkoffASDKCore

final class RSAEncryptorMock: IRSAEncryptor {

// MARK: - createPublicSecKey

typealias CreatePublicSecKeyArguments = String

var createPublicSecKeyCallsCount = 0
var createPublicSecKeyReceivedArguments: CreatePublicSecKeyArguments?
var createPublicSecKeyReceivedInvocations: [CreatePublicSecKeyArguments?] = []
var createPublicSecKeyReturnValue: SecKey?

func createPublicSecKey(publicKey: String) -> SecKey? {
createPublicSecKeyCallsCount += 1
let arguments = publicKey
createPublicSecKeyReceivedArguments = arguments
createPublicSecKeyReceivedInvocations.append(arguments)
return createPublicSecKeyReturnValue
}

// MARK: - encrypt

typealias EncryptArguments = (string: String, publicKey: SecKey)

var encryptCallsCount = 0
var encryptReceivedArguments: EncryptArguments?
var encryptReceivedInvocations: [EncryptArguments?] = []
var encryptReturnValue: String?

func encrypt(string: String, publicKey: SecKey) -> String? {
encryptCallsCount += 1
let arguments = (string, publicKey)
encryptReceivedArguments = arguments
encryptReceivedInvocations.append(arguments)
return encryptReturnValue
}
}

// MARK: - Resets

extension RSAEncryptorMock {
func fullReset() {
createPublicSecKeyCallsCount = 0
createPublicSecKeyReceivedArguments = nil
createPublicSecKeyReceivedInvocations = []

encryptCallsCount = 0
encryptReceivedArguments = nil
encryptReceivedInvocations = []
}
}
Loading

0 comments on commit 008bc34

Please sign in to comment.