From a59451504817b25c20ff69282ff89932ae367b6f Mon Sep 17 00:00:00 2001 From: Jan Kobersky Date: Tue, 19 Nov 2024 10:54:09 +0100 Subject: [PATCH] Improved tests --- .../project.pbxproj | 4 +++ WultraMobileTokenSDK/Common/WMTUtils.swift | 33 +++++++++++++++++++ .../Push/Service/WMTPushImpl.swift | 26 +++++---------- .../NetworkingObjectsTests.swift | 26 ++++++++++++++- 4 files changed, 70 insertions(+), 19 deletions(-) create mode 100644 WultraMobileTokenSDK/Common/WMTUtils.swift diff --git a/WultraMobileTokenSDK.xcodeproj/project.pbxproj b/WultraMobileTokenSDK.xcodeproj/project.pbxproj index 3d65836..f77c207 100644 --- a/WultraMobileTokenSDK.xcodeproj/project.pbxproj +++ b/WultraMobileTokenSDK.xcodeproj/project.pbxproj @@ -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 */; }; @@ -130,6 +131,7 @@ DCAB7BC924580BAC0006989D /* WMTQROperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTQROperation.swift; sourceTree = ""; }; DCAC55982CE68C2A0070644A /* ProvisioningUtilsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProvisioningUtilsTests.swift; sourceTree = ""; }; DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTProvisioningUtils.swift; sourceTree = ""; }; + DCAC55BB2CEC954C0070644A /* WMTUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTUtils.swift; sourceTree = ""; }; DCC3420324E3DB310045D27D /* WMTPushParser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WMTPushParser.swift; sourceTree = ""; }; 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 = ""; }; @@ -367,6 +369,7 @@ DC06D01E25AC74E400F2EA69 /* WMTLock.swift */, DC9511F826EA02C100FF40AD /* WPNIntegration.swift */, DCAC559B2CE773E90070644A /* WMTProvisioningUtils.swift */, + DCAC55BB2CEC954C0070644A /* WMTUtils.swift */, ); path = Common; sourceTree = ""; @@ -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 */, diff --git a/WultraMobileTokenSDK/Common/WMTUtils.swift b/WultraMobileTokenSDK/Common/WMTUtils.swift new file mode 100644 index 0000000..e77b8f8 --- /dev/null +++ b/WultraMobileTokenSDK/Common/WMTUtils.swift @@ -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 + } +} diff --git a/WultraMobileTokenSDK/Push/Service/WMTPushImpl.swift b/WultraMobileTokenSDK/Push/Service/WMTPushImpl.swift index 6b14af4..e50f475 100644 --- a/WultraMobileTokenSDK/Push/Service/WMTPushImpl.swift +++ b/WultraMobileTokenSDK/Push/Service/WMTPushImpl.swift @@ -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 ) @@ -75,17 +75,15 @@ class WMTPushImpl: WMTPush, WMTService { func register(to platform: WMTPushPlatform, completion: @escaping (Result) -> 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 } @@ -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 } - } diff --git a/WultraMobileTokenSDKTests/NetworkingObjectsTests.swift b/WultraMobileTokenSDKTests/NetworkingObjectsTests.swift index 8bb8322..e72a0ea 100644 --- a/WultraMobileTokenSDKTests/NetworkingObjectsTests.swift +++ b/WultraMobileTokenSDKTests/NetworkingObjectsTests.swift @@ -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) @@ -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"}]}}]}