Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 committed Nov 19, 2024
1 parent 8d0be51 commit a594515
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 19 deletions.
4 changes: 4 additions & 0 deletions WultraMobileTokenSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
DCAB7BCA24580BAC0006989D /* WMTQROperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCAB7BC924580BAC0006989D /* WMTQROperation.swift */; };
DCAC55992CE68C2A0070644A /* ProvisioningUtilsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCAC55982CE68C2A0070644A /* ProvisioningUtilsTests.swift */; };
DCAC559C2CE773E90070644A /* WMTProvisioningUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */; };
DCAC55BC2CEC954C0070644A /* WMTUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCAC55BB2CEC954C0070644A /* WMTUtils.swift */; };
DCC3420424E3DB310045D27D /* WMTPushParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC3420324E3DB310045D27D /* WMTPushParser.swift */; };
DCC5CC9F2449EE21004679AC /* MobileTokenSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = DCC5CC9D2449EE21004679AC /* MobileTokenSDK.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCC5CCAC2449F765004679AC /* WMTOperationsImpl.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCC5CCAB2449F765004679AC /* WMTOperationsImpl.swift */; };
Expand Down Expand Up @@ -130,6 +131,7 @@
DCAB7BC924580BAC0006989D /* WMTQROperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTQROperation.swift; sourceTree = "<group>"; };
DCAC55982CE68C2A0070644A /* ProvisioningUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProvisioningUtilsTests.swift; sourceTree = "<group>"; };
DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTProvisioningUtils.swift; sourceTree = "<group>"; };
DCAC55BB2CEC954C0070644A /* WMTUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTUtils.swift; sourceTree = "<group>"; };
DCC3420324E3DB310045D27D /* WMTPushParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTPushParser.swift; sourceTree = "<group>"; };
DCC5CC9A2449EE21004679AC /* WultraMobileTokenSDK.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = WultraMobileTokenSDK.framework; sourceTree = BUILT_PRODUCTS_DIR; };
DCC5CC9D2449EE21004679AC /* MobileTokenSDK.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MobileTokenSDK.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -367,6 +369,7 @@
DC06D01E25AC74E400F2EA69 /* WMTLock.swift */,
DC9511F826EA02C100FF40AD /* WPNIntegration.swift */,
DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */,
DCAC55BB2CEC954C0070644A /* WMTUtils.swift */,
);
path = Common;
sourceTree = "<group>";
Expand Down Expand Up @@ -617,6 +620,7 @@
DC3D0B392480F886000DC4D9 /* WMTLocalOperation.swift in Sources */,
DCD8B336246C1BAF00385F02 /* WMTRejectionReason.swift in Sources */,
DCC5CCD8244DBBBD004679AC /* WMTAuthorizationData.swift in Sources */,
DCAC55BC2CEC954C0070644A /* WMTUtils.swift in Sources */,
DC488040292282FF00DB844B /* WMTInboxCount.swift in Sources */,
DCA43C6B29927C960059A163 /* WMTOperationAttributeAmountConversion.swift in Sources */,
EA74F7B32C2561BB004340B9 /* WMTResultTexts.swift in Sources */,
Expand Down
33 changes: 33 additions & 0 deletions WultraMobileTokenSDK/Common/WMTUtils.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright 2024 Wultra s.r.o.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions
// and limitations under the License.
//

import Foundation

internal extension Data {

static let toHexTable: [Character] = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" ]

func toHex() -> String {
var result = ""
result.reserveCapacity(count * 2)
for byte in self {
let byteAsUInt = Int(byte)
result.append(Self.toHexTable[byteAsUInt >> 4])
result.append(Self.toHexTable[byteAsUInt & 15])
}
return result
}
}
26 changes: 8 additions & 18 deletions WultraMobileTokenSDK/Push/Service/WMTPushImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class WMTPushImpl: WMTPush, WMTService {
// ios for backwards compatibility
return registerPush(
platform: .ios,
token: HexadecimalString.encodeData(token),
token: token.toHex(),
environment: getPushEnvironment(environment: .automatic),
completion: completion
)
Expand All @@ -75,17 +75,15 @@ class WMTPushImpl: WMTPush, WMTService {
func register(to platform: WMTPushPlatform, completion: @escaping (Result<Void, WMTError>) -> Void) -> Operation? {

let payloadPlatform: WMTPushRegistrationPlatform
let payloadToken: String
let payloadToken = platform.token
let payloadEnvironment: WMTPushRegistrationEnvironment?

switch platform {
case .apns(let data, let environment):
case .apns(_, let environment):
payloadPlatform = .apns
payloadToken = HexadecimalString.encodeData(data)
payloadEnvironment = getPushEnvironment(environment: environment)
case .fcm(let token):
payloadPlatform = .fcm
payloadToken = token
payloadEnvironment = nil // no env for FCM
}

Expand Down Expand Up @@ -131,19 +129,11 @@ class WMTPushImpl: WMTPush, WMTService {
}
}

private class HexadecimalString {

static let toHexTable: [Character] = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" ]

static func encodeData(_ data: Data) -> String {
var result = ""
result.reserveCapacity(data.count * 2)
for byte in data {
let byteAsUInt = Int(byte)
result.append(toHexTable[byteAsUInt >> 4])
result.append(toHexTable[byteAsUInt & 15])
extension WMTPushPlatform {
var token: String {
return switch self {
case .apns(token: let token, environment: _): token.toHex()
case .fcm(token: let token): token
}
return result
}

}
26 changes: 25 additions & 1 deletion WultraMobileTokenSDKTests/NetworkingObjectsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,19 @@ class NetworkingObjectsTests: XCTestCase {
r.testSerialization(expectation: expectation)
}

func testTokenRequestApns() {
func testTokenRequestApnsDevelopment() {
let expectation = """
{"requestObject":{"platform":"apns","token":"5FBC85D026945C48A17FE1327C68C77F7793FEBFE23FF5850224BEE4215C5525","environment":"development"}}
"""
let r = WMTPushEndpoints.RegisterDevice.EndpointType.RequestData(WMTPushRegistrationData(platform: .apns, token: "5FBC85D026945C48A17FE1327C68C77F7793FEBFE23FF5850224BEE4215C5525", environment: .development))

r.testSerialization(expectation: expectation)
}

func testTokenRequestApnsProduction() {
let expectation = """
{"requestObject":{"platform":"apns","token":"5FBC85D026945C48A17FE1327C68C77F7793FEBFE23FF5850224BEE4215C5525","environment":"production"}}
"""
let r = WMTPushEndpoints.RegisterDevice.EndpointType.RequestData(WMTPushRegistrationData(platform: .apns, token: "5FBC85D026945C48A17FE1327C68C77F7793FEBFE23FF5850224BEE4215C5525", environment: .production))

r.testSerialization(expectation: expectation)
Expand All @@ -70,6 +79,21 @@ class NetworkingObjectsTests: XCTestCase {
r.testSerialization(expectation: expectation)
}

func testApnsTokenSerialization() {
let apnsData = "testData".data(using: .utf8)!
let expectedApnsHex = "7465737444617461"
let apns = WMTPushPlatform.apns(token: apnsData, environment: .automatic)
// data should be transformed to hexformat
XCTAssertEqual(expectedApnsHex, apns.token)
}

func testFcmTokenSerialization() {
let fcmData = "testToken"
let fcm = WMTPushPlatform.fcm(token: fcmData)
// FCM token should be the same
XCTAssertEqual(fcmData, fcm.token)
}

func testOperationsResponse() {
let response = """
{"status":"OK","currentTimestamp":"2023-02-10T12:30:42+0000","responseObject":[{"id":"930febe7-f350-419a-8bc0-c8883e7f71e3","name":"authorize_payment","data":"A1*A100CZK*Q238400856/0300**D20170629*NUtility Bill Payment - 05/2017","status":"PENDING","operationCreated":"2018-08-08T12:30:42+0000","operationExpires":"2018-08-08T12:35:43+0000","allowedSignatureType":{"type":"2FA","variants":["possession_knowledge", "possession_biometry"]},"formData":{"title":"Potvrzení platby","message":"Dobrý den,prosíme o potvrzení následující platby:","attributes":[{"type":"AMOUNT","id":"operation.amount","label":"Částka","amount":965165234082.23,"currency":"CZK", "valueFormatted": "965165234082.23 CZK"},{"type":"KEY_VALUE","id":"operation.account","label":"Na účet","value":"238400856/0300"},{"type":"KEY_VALUE","id":"operation.dueDate","label":"Datum splatnosti","value":"29.6.2017"},{"type":"NOTE","id":"operation.note","label":"Poznámka","note":"Utility Bill Payment - 05/2017"},{"type":"PARTY_INFO","id":"operation.partyInfo","label":"Application","partyInfo":{"logoUrl":"http://whywander.com/wp-content/uploads/2017/05/prague_hero-100x100.jpg","name":"Tesco","description":"Objevte více příběhů psaných s chutí","websiteUrl":"https://itesco.cz/hello/vse-o-jidle/pribehy-psane-s-chuti/clanek/tomovy-burgery-pro-zapalene-fanousky/15012"}},{ "type": "AMOUNT_CONVERSION", "id": "operation.conversion", "label": "Conversion", "dynamic": true, "sourceAmount": 1.26, "sourceCurrency": "ETC", "sourceAmountFormatted": "1.26", "sourceCurrencyFormatted": "ETC", "sourceValueFormatted": "1.26 ETC", "targetAmount": 1710.98, "targetCurrency": "USD", "targetAmountFormatted": "1,710.98", "targetCurrencyFormatted": "USD", "targetValueFormatted": "1,710.98 USD"},{ "type": "IMAGE", "id": "operation.image", "label": "Image", "thumbnailUrl": "https://example.com/123_thumb.jpeg", "originalUrl": "https://example.com/123.jpeg" },{ "type": "IMAGE", "id": "operation.image", "label": "Image", "thumbnailUrl": "https://example.com/123_thumb.jpeg" }]}},{"id":"930febe7-f350-419a-8bc0-c8883e7f71e3","name":"authorize_payment","data":"A1*A100CZK*Q238400856/0300**D20170629*NUtility Bill Payment - 05/2017","status":"PENDING","operationCreated":"2018-08-08T12:30:42+0000","operationExpires":"2018-08-08T12:35:43+0000","allowedSignatureType":{"type":"1FA","variants":["possession_knowledge"]},"formData":{"title":"Potvrzení platby","message":"Dobrý den,prosíme o potvrzení následující platby:","attributes":[{"type":"AMOUNT","id":"operation.amount","label":"Částka","amount":100,"currency":"CZK"},{"type":"KEY_VALUE","id":"operation.account","label":"Na účet","value":"238400856/0300"},{"type":"KEY_VALUE","id":"operation.dueDate","label":"Datum splatnosti","value":"29.6.2017"},{"type":"NOTE","id":"operation.note","label":"Poznámka","note":"Utility Bill Payment - 05/2017"}]}}]}
Expand Down

0 comments on commit a594515

Please sign in to comment.