|
| 1 | +// |
| 2 | +// MPKitAdobeTests.swift |
| 3 | +// mParticle-Adobe |
| 4 | +// |
| 5 | +// Created by Denis Chilik on 10/9/25. |
| 6 | +// |
| 7 | +@testable import mParticle_Adobe |
| 8 | +import Foundation |
| 9 | +import XCTest |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +extension URLSession: @retroactive SessionProtocol { |
| 14 | +} |
| 15 | + |
| 16 | +final class MPAdobeTests: XCTestCase { |
| 17 | + var session: SessionProtocolMock! |
| 18 | + |
| 19 | + override func setUp() { |
| 20 | + super.setUp() |
| 21 | + |
| 22 | + session = SessionProtocolMock() |
| 23 | + } |
| 24 | + |
| 25 | + func testSendRequestEncodeAllParametersIntoURL() { |
| 26 | + let sut = MPIAdobe(session: session)! |
| 27 | + sut.sendRequest( |
| 28 | + withMarketingCloudId: "marketingCloudId", |
| 29 | + advertiserId: "advertiserId", |
| 30 | + pushToken: "pushToken", |
| 31 | + organizationId: "organizationId", |
| 32 | + userIdentities: [ |
| 33 | + NSNumber(value: MPUserIdentity.other.rawValue) : "1", |
| 34 | + NSNumber(value: MPUserIdentity.customerId.rawValue) : "2", |
| 35 | + NSNumber(value: MPUserIdentity.facebook.rawValue) : "3", |
| 36 | + NSNumber(value: MPUserIdentity.twitter.rawValue) : "4", |
| 37 | + NSNumber(value: MPUserIdentity.google.rawValue) : "5", |
| 38 | + NSNumber(value: MPUserIdentity.microsoft.rawValue) : "6", |
| 39 | + NSNumber(value: MPUserIdentity.yahoo.rawValue) : "7", |
| 40 | + NSNumber(value: MPUserIdentity.email.rawValue) : "8", |
| 41 | + NSNumber(value: MPUserIdentity.alias.rawValue) : "9", |
| 42 | + NSNumber(value: MPUserIdentity.facebookCustomAudienceId.rawValue) : "10", |
| 43 | + NSNumber(value: MPUserIdentity.other5.rawValue) : "11", |
| 44 | + ], |
| 45 | + audienceManagerServer: "audienceManagerServer" |
| 46 | + ) { _, _, _, _ in } |
| 47 | + |
| 48 | + let expected = "https://audienceManagerServer/id?d_mid=marketingCloudId&d_cid=20915%2501advertiserId&d_cid=20920%2501pushToken&d_cid_ic=google%25015&d_cid_ic=facebook%25013&d_cid_ic=customerid%25012&d_cid_ic=twitter%25014&d_cid_ic=alias%25019&d_cid_ic=microsoft%25016&d_cid_ic=email%25018&d_cid_ic=yahoo%25017&d_cid_ic=other%25011&d_cid_ic=facebookcustomaudienceid%250110&d_orgid=organizationId&d_ptfm=ios&d_ver=2" |
| 49 | + |
| 50 | + guard |
| 51 | + let actualURL = session.dataTaskRequestParam?.url, |
| 52 | + let expectedURL = URL(string: expected), |
| 53 | + let actualComponents = URLComponents(url: actualURL, resolvingAgainstBaseURL: false), |
| 54 | + let expectedComponents = URLComponents(url: expectedURL, resolvingAgainstBaseURL: false) |
| 55 | + else { |
| 56 | + XCTFail("URLs could not be parsed") |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + XCTAssertEqual(actualComponents.scheme, expectedComponents.scheme) |
| 61 | + XCTAssertEqual(actualComponents.host, expectedComponents.host) |
| 62 | + XCTAssertEqual(actualComponents.path, expectedComponents.path) |
| 63 | + |
| 64 | + let actualItems = Set(actualComponents.queryItems ?? []) |
| 65 | + let expectedItems = Set(expectedComponents.queryItems ?? []) |
| 66 | + |
| 67 | + XCTAssertEqual(actualItems, expectedItems) |
| 68 | + } |
| 69 | + |
| 70 | + func testCompletionCallback_success() { |
| 71 | + let sut = MPIAdobe(session: session)! |
| 72 | + sut.sendRequest( |
| 73 | + withMarketingCloudId: "", |
| 74 | + advertiserId: "", |
| 75 | + pushToken: "", |
| 76 | + organizationId: "", |
| 77 | + userIdentities: [:], |
| 78 | + audienceManagerServer: "" |
| 79 | + ) { marketingCloudId, locationHint, blob, error in |
| 80 | + XCTAssertNil(error) |
| 81 | + XCTAssertEqual(marketingCloudId, "mock_mid") |
| 82 | + XCTAssertEqual(locationHint, "mock_region") |
| 83 | + XCTAssertEqual(blob, "mock_blob") |
| 84 | + } |
| 85 | + |
| 86 | + let json: [String: Any] = [ |
| 87 | + "d_mid": "mock_mid", |
| 88 | + "d_blob": "mock_blob", |
| 89 | + "dcs_region": "mock_region" |
| 90 | + ] |
| 91 | + |
| 92 | + let data = try! JSONSerialization.data(withJSONObject: json, options: []) |
| 93 | + |
| 94 | + session.dataTaskCompletionHandlerParam?(data, URLResponse(), nil) |
| 95 | + } |
| 96 | + |
| 97 | + func testCompletionCallback_success_empty_json() { |
| 98 | + let sut = MPIAdobe(session: session)! |
| 99 | + sut.sendRequest( |
| 100 | + withMarketingCloudId: "", |
| 101 | + advertiserId: "", |
| 102 | + pushToken: "", |
| 103 | + organizationId: "", |
| 104 | + userIdentities: [:], |
| 105 | + audienceManagerServer: "" |
| 106 | + ) { marketingCloudId, locationHint, blob, error in |
| 107 | + XCTAssertNil(error) |
| 108 | + XCTAssertNil(marketingCloudId) |
| 109 | + XCTAssertNil(locationHint) |
| 110 | + XCTAssertNil(blob) |
| 111 | + } |
| 112 | + |
| 113 | + let json: [String: Any] = [:] |
| 114 | + |
| 115 | + let data = try! JSONSerialization.data(withJSONObject: json, options: []) |
| 116 | + |
| 117 | + session.dataTaskCompletionHandlerParam?(data, URLResponse(), nil) |
| 118 | + } |
| 119 | + |
| 120 | + func testCompletionCallback_success_parametersNotStrings() { |
| 121 | + let sut = MPIAdobe(session: session)! |
| 122 | + sut.sendRequest( |
| 123 | + withMarketingCloudId: "", |
| 124 | + advertiserId: "", |
| 125 | + pushToken: "", |
| 126 | + organizationId: "", |
| 127 | + userIdentities: [:], |
| 128 | + audienceManagerServer: "" |
| 129 | + ) { marketingCloudId, locationHint, blob, error in |
| 130 | + XCTAssertNil(error) |
| 131 | + XCTAssertNil(marketingCloudId) |
| 132 | + XCTAssertNil(locationHint) |
| 133 | + XCTAssertNil(blob) |
| 134 | + } |
| 135 | + |
| 136 | + let json: [String: Any] = [ |
| 137 | + "d_mid": 1, |
| 138 | + "d_blob": 2, |
| 139 | + "dcs_region": 3 |
| 140 | + ] |
| 141 | + |
| 142 | + let data = try! JSONSerialization.data(withJSONObject: json, options: []) |
| 143 | + session.dataTaskCompletionHandlerParam?(data, URLResponse(), nil) |
| 144 | + } |
| 145 | + |
| 146 | + func testCompletionCallback_success_errorFromBackend() { |
| 147 | + let sut = MPIAdobe(session: session)! |
| 148 | + sut.sendRequest( |
| 149 | + withMarketingCloudId: "", |
| 150 | + advertiserId: "", |
| 151 | + pushToken: "", |
| 152 | + organizationId: "", |
| 153 | + userIdentities: [:], |
| 154 | + audienceManagerServer: "" |
| 155 | + ) { marketingCloudId, locationHint, blob, error in |
| 156 | + XCTAssertNil(marketingCloudId) |
| 157 | + XCTAssertNil(locationHint) |
| 158 | + XCTAssertNil(blob) |
| 159 | + XCTAssertNotNil(error) |
| 160 | + } |
| 161 | + |
| 162 | + let json: [String: Any] = [ |
| 163 | + "error_msg": [ |
| 164 | + "some_key": "Invalid request parameters" |
| 165 | + ] |
| 166 | + ] |
| 167 | + |
| 168 | + let data = try! JSONSerialization.data(withJSONObject: json, options: []) |
| 169 | + session.dataTaskCompletionHandlerParam?(data, URLResponse(), nil) |
| 170 | + } |
| 171 | + |
| 172 | + func testCompletionCallback_success_errorErrorMsgContains_shouldNotCrash() { |
| 173 | + let sut = MPIAdobe(session: session)! |
| 174 | + sut.sendRequest( |
| 175 | + withMarketingCloudId: "", |
| 176 | + advertiserId: "", |
| 177 | + pushToken: "", |
| 178 | + organizationId: "", |
| 179 | + userIdentities: [:], |
| 180 | + audienceManagerServer: "" |
| 181 | + ) { marketingCloudId, locationHint, blob, error in |
| 182 | + XCTAssertNil(marketingCloudId) |
| 183 | + XCTAssertNil(locationHint) |
| 184 | + XCTAssertNil(blob) |
| 185 | + XCTAssertNotNil(error) |
| 186 | + } |
| 187 | + |
| 188 | + let json: [String: Any] = [ |
| 189 | + "error_msg": "Invalid request parameters" |
| 190 | + ] |
| 191 | + |
| 192 | + let data = try! JSONSerialization.data(withJSONObject: json, options: []) |
| 193 | + session.dataTaskCompletionHandlerParam?(data, URLResponse(), nil) |
| 194 | + } |
| 195 | +} |
0 commit comments