Skip to content

Commit 80410a9

Browse files
Merge pull request #21 from Iterable/feature/itbl-5833-fingerprinting
[itbl-5833] - Deferred Deeplinking
2 parents 479c42c + 2d7070b commit 80410a9

17 files changed

+653
-128
lines changed

Tests/swift-sdk-objc-tests/IterableAPIInternalTests.m

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,6 @@ - (void)testPushServicePlatformToString {
4444
XCTAssertEqualObjects(@"APNS_SANDBOX", [IterableAPIInternal pushServicePlatformToString:APNS_SANDBOX]);
4545
}
4646

47-
- (void)testDictToJson {
48-
NSDictionary *args = @{
49-
@"email": @"[email protected]",
50-
@"device": @{
51-
@"token": @"foo",
52-
@"platform": @"bar",
53-
@"applicationName": @"baz",
54-
@"dataFields": @{
55-
@"name": @"green",
56-
@"localizedModel": @"eggs",
57-
@"userInterfaceIdiom": @"and",
58-
@"identifierForVendor": @"ham",
59-
@"systemName": @"iterable",
60-
@"systemVersion": @"is",
61-
@"model": @"awesome"
62-
}
63-
}
64-
};
65-
NSString *result = [IterableAPIInternal dictToJson:args];
66-
NSData *data = [result dataUsingEncoding:NSUTF8StringEncoding];
67-
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
68-
XCTAssertEqualObjects(args, json);
69-
70-
NSString *expected = @"{\"email\":\"[email protected]\",\"device\":{\"applicationName\":\"baz\",\"dataFields\":{\"systemName\":\"iterable\",\"model\":\"awesome\",\"localizedModel\":\"eggs\",\"userInterfaceIdiom\":\"and\",\"systemVersion\":\"is\",\"name\":\"green\",\"identifierForVendor\":\"ham\"},\"token\":\"foo\",\"platform\":\"bar\"}}";
71-
72-
id object = [NSJSONSerialization
73-
JSONObjectWithData:[expected dataUsingEncoding:NSUTF8StringEncoding]
74-
options:0
75-
error:nil];
76-
XCTAssertEqualObjects(args, object);
77-
XCTAssertEqualObjects(args, json);
78-
}
79-
8047
- (void)testUserInterfaceIdionEnumToString {
8148
XCTAssertEqualObjects(@"Phone", [IterableAPIInternal userInterfaceIdiomEnumToString:UIUserInterfaceIdiomPhone]);
8249
XCTAssertEqualObjects(@"Pad", [IterableAPIInternal userInterfaceIdiomEnumToString:UIUserInterfaceIdiomPad]);
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// DeferredDeeplinkTests.swift
3+
// swift-sdk-swift-tests
4+
//
5+
// Created by Tapash Majumder on 9/4/18.
6+
// Copyright © 2018 Iterable. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import IterableSDK
11+
12+
class DeferredDeeplinkTests: XCTestCase {
13+
private static let apiKey = "zeeApiKey"
14+
15+
override func setUp() {
16+
super.setUp()
17+
TestUtils.clearUserDefaults()
18+
}
19+
20+
override func tearDown() {
21+
// Put teardown code here. This method is called after the invocation of each test method in the class.
22+
super.tearDown()
23+
}
24+
25+
func testCallCheckForDDL() {
26+
let expectation = XCTestExpectation(description: "callCheckForDDL")
27+
28+
let json: [AnyHashable : Any] = [
29+
"isMatch" : true,
30+
"destinationUrl" : "zeeDestinationUrl",
31+
"campaignId" : "1",
32+
"templateId" : "1",
33+
"messageId" : "1"
34+
]
35+
let networkSession = MockNetworkSession(statusCode: 200, json: json)
36+
37+
let config = IterableConfig()
38+
config.checkForDeferredDeeplink = true
39+
let urlDelegate = MockUrlDelegate(returnValue: true)
40+
urlDelegate.callback = {(url, context) in
41+
expectation.fulfill()
42+
XCTAssertEqual(url.absoluteString, "zeeDestinationUrl")
43+
}
44+
config.urlDelegate = urlDelegate
45+
IterableAPI.initialize(apiKey: DeferredDeeplinkTests.apiKey, config: config, networkSession: networkSession)
46+
47+
wait(for: [expectation], timeout: testExpectationTimeout)
48+
49+
// Test that calling second time does not trigger
50+
let expectation2 = XCTestExpectation(description: "should not callCheckForDDL")
51+
expectation2.isInverted = true
52+
let config2 = IterableConfig()
53+
config2.checkForDeferredDeeplink = true
54+
let urlDelegate2 = MockUrlDelegate(returnValue: true)
55+
urlDelegate2.callback = {(url, context) in
56+
expectation2.fulfill()
57+
}
58+
config.urlDelegate = urlDelegate2
59+
IterableAPI.initialize(apiKey: DeferredDeeplinkTests.apiKey, config: config, networkSession: networkSession)
60+
61+
wait(for: [expectation2], timeout: 1.0)
62+
}
63+
64+
func testDDLNoMatch() {
65+
let expectation = XCTestExpectation(description: "testDDL No Match")
66+
expectation.isInverted = true
67+
68+
let json: [AnyHashable : Any] = [
69+
"isMatch" : false,
70+
]
71+
let networkSession = MockNetworkSession(statusCode: 200, json: json)
72+
73+
let config = IterableConfig()
74+
config.checkForDeferredDeeplink = true
75+
let urlDelegate = MockUrlDelegate(returnValue: true)
76+
urlDelegate.callback = {(url, context) in
77+
expectation.fulfill()
78+
}
79+
config.urlDelegate = urlDelegate
80+
IterableAPI.initialize(apiKey: DeferredDeeplinkTests.apiKey, config: config, networkSession: networkSession)
81+
82+
wait(for: [expectation], timeout: 1.0)
83+
}
84+
85+
func testCheckForDeferredDDLIsSetToFalse() {
86+
let expectation = XCTestExpectation(description: "testDDL No Match")
87+
expectation.isInverted = true
88+
89+
let json: [AnyHashable : Any] = [
90+
"isMatch" : true,
91+
"destinationUrl" : "zeeDestinationUrl",
92+
"campaignId" : "1",
93+
"templateId" : "1",
94+
"messageId" : "1"
95+
]
96+
let networkSession = MockNetworkSession(statusCode: 200, json: json)
97+
98+
let config = IterableConfig()
99+
config.checkForDeferredDeeplink = false
100+
let urlDelegate = MockUrlDelegate(returnValue: true)
101+
urlDelegate.callback = {(url, context) in
102+
expectation.fulfill()
103+
}
104+
config.urlDelegate = urlDelegate
105+
IterableAPI.initialize(apiKey: DeferredDeeplinkTests.apiKey, config: config, networkSession: networkSession)
106+
107+
wait(for: [expectation], timeout: 1.0)
108+
}
109+
}

Tests/swift-sdk-swift-tests/IterableAutoRegistrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class IterableAutoRegistrationTests: XCTestCase {
163163
config.autoPushRegistration = true
164164
let notificationStateProvider = MockNotificationStateProvider(enabled: true, expectation: expectation1)
165165

166-
UserDefaults.standard.set("[email protected]", forKey:ITBConsts.UserDefaults.emailKey)
166+
UserDefaults.standard.set("[email protected]", forKey:ITBL_USER_DEFAULTS_EMAIL_KEY)
167167
IterableAPI.initialize(apiKey: IterableAutoRegistrationTests.apiKey, config:config, networkSession: networkSession, notificationStateProvider: notificationStateProvider)
168168

169169
// only wait for small time, supposed to error out

Tests/swift-sdk-swift-tests/IterableRequestUtilTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,30 @@ class IterableRequestUtilTests: XCTestCase {
2020
super.tearDown()
2121
}
2222

23+
func testDictToJsonData() {
24+
let args: [AnyHashable : Any] = [
25+
"email": "[email protected]",
26+
"device": [
27+
"token": "foo",
28+
"platform": "bar",
29+
"applicationName": "baz",
30+
"dataFields": [
31+
"name": "green",
32+
"localizedModel": "eggs",
33+
"userInterfaceIdiom": "and",
34+
"identifierForVendor": "ham",
35+
"systemName": "iterable",
36+
"systemVersion": "is",
37+
"model": "awesome"
38+
]
39+
]
40+
]
41+
42+
let data = IterableRequestUtil.dictToJsonData(args)!
43+
let jsonObject = try! JSONSerialization.jsonObject(with: data, options: []) as! [AnyHashable : Any]
44+
XCTAssertTrue(NSDictionary(dictionary: args).isEqual(to: jsonObject))
45+
}
46+
2347
func testGetRequest() {
2448
let apiEndPoint = "https://somewhere.com/"
2549
let path = "path"
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//
2+
//
3+
// Created by Tapash Majumder on 8/29/18.
4+
// Copyright © 2018 Iterable. All rights reserved.
5+
//
6+
7+
import XCTest
8+
9+
@testable import IterableSDK
10+
11+
12+
class LocalStorageTests: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
}
18+
19+
override func tearDown() {
20+
// Put teardown code here. This method is called after the invocation of each test method in the class.
21+
super.tearDown()
22+
}
23+
24+
func testUserIdAndEmail() throws {
25+
let mockDateProvider = MockDateProvider()
26+
var localStorage = UserDefaultsLocalStorage(dateProvider: mockDateProvider)
27+
let userId = "zeeUserId"
28+
let email = "[email protected]"
29+
localStorage.userId = userId
30+
localStorage.email = email
31+
32+
XCTAssertEqual(localStorage.userId, userId)
33+
XCTAssertEqual(localStorage.email, email)
34+
}
35+
36+
func testDDLChecked() throws {
37+
let mockDateProvider = MockDateProvider()
38+
var localStorage = UserDefaultsLocalStorage(dateProvider: mockDateProvider)
39+
localStorage.ddlChecked = true
40+
XCTAssertTrue(localStorage.ddlChecked)
41+
42+
localStorage.ddlChecked = false
43+
XCTAssertFalse(localStorage.ddlChecked)
44+
}
45+
46+
func testAttributionInfo() throws {
47+
let mockDateProvider = MockDateProvider()
48+
let localStorage = UserDefaultsLocalStorage(dateProvider: mockDateProvider)
49+
let attributionInfo = IterableAttributionInfo(campaignId: 1, templateId: 2, messageId: "3")
50+
let currentDate = Date()
51+
let expiration = Calendar.current.date(byAdding: Calendar.Component.hour, value: 24, to: currentDate)!
52+
localStorage.save(attributionInfo: attributionInfo, withExpiration: expiration)
53+
// 23 hours, not expired, still present
54+
mockDateProvider.currentDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: 23, to: currentDate)!
55+
let fromLocalStorage:IterableAttributionInfo = localStorage.attributionInfo!
56+
XCTAssert(fromLocalStorage == attributionInfo)
57+
58+
mockDateProvider.currentDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: 25, to: currentDate)!
59+
let fromLocalStorage2:IterableAttributionInfo? = localStorage.attributionInfo
60+
XCTAssertNil(fromLocalStorage2)
61+
}
62+
63+
func testPayload() throws {
64+
let mockDateProvider = MockDateProvider()
65+
let localStorage = UserDefaultsLocalStorage(dateProvider: mockDateProvider)
66+
let payload: [AnyHashable : Any] = [
67+
"email": "[email protected]",
68+
"device": [
69+
"token": "foo",
70+
"platform": "bar",
71+
"applicationName": "baz",
72+
"dataFields": [
73+
"name": "green",
74+
"localizedModel": "eggs",
75+
"userInterfaceIdiom": "and",
76+
"identifierForVendor": "ham",
77+
"systemName": "iterable",
78+
"systemVersion": "is",
79+
"model": "awesome"
80+
]
81+
]
82+
]
83+
let currentDate = Date()
84+
let expiration = Calendar.current.date(byAdding: Calendar.Component.hour, value: 24, to: currentDate)!
85+
localStorage.save(payload: payload, withExpiration: expiration)
86+
// 23 hours, not expired, still present
87+
mockDateProvider.currentDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: 23, to: currentDate)!
88+
let fromLocalStorage:[AnyHashable : Any] = localStorage.payload!
89+
XCTAssertTrue(NSDictionary(dictionary: payload).isEqual(to: fromLocalStorage))
90+
91+
mockDateProvider.currentDate = Calendar.current.date(byAdding: Calendar.Component.hour, value: 25, to: currentDate)!
92+
let fromLocalStorage2:[AnyHashable : Any]? = localStorage.payload
93+
XCTAssertNil(fromLocalStorage2)
94+
}
95+
}

0 commit comments

Comments
 (0)