Skip to content

Commit b91d58e

Browse files
Merge branch 'feature/itbl_track_anon_user' into bugfix/MOB-9306-Add-test-to-validate-object-created-by-custom-event-api-calls
2 parents 526169a + 900011b commit b91d58e

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

swift-sdk.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
00CB31B621096129004ACDEC /* TestUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00CB31B4210960C4004ACDEC /* TestUtils.swift */; };
1414
181063DB2C9841460078E0ED /* CustomEventUserUpdateTestCaseTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181063DA2C9841460078E0ED /* CustomEventUserUpdateTestCaseTests.swift */; };
1515
181063DD2C994FA40078E0ED /* ValidateCustomEventUserUpdateAPITest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181063DC2C994FA40078E0ED /* ValidateCustomEventUserUpdateAPITest.swift */; };
16+
181063DF2C9D51000078E0ED /* ValidateStoredEventCheckUnknownToKnownUserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 181063DE2C9D51000078E0ED /* ValidateStoredEventCheckUnknownToKnownUserTest.swift */; };
1617
182A2A152C661C9A002FF058 /* DataTypeComparatorSearchQueryCriteria.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182A2A142C661C9A002FF058 /* DataTypeComparatorSearchQueryCriteria.swift */; };
1718
1881A21B2C7602F80020C64D /* ComparatorDataTypeWithArrayInput.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1881A21A2C7602F80020C64D /* ComparatorDataTypeWithArrayInput.swift */; };
1819
18A3520A2C7DC51C007FED53 /* NestedFieldSupportForArrayData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18A352092C7DC51C007FED53 /* NestedFieldSupportForArrayData.swift */; };
@@ -568,6 +569,7 @@
568569
00CB31B4210960C4004ACDEC /* TestUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
569570
181063DA2C9841460078E0ED /* CustomEventUserUpdateTestCaseTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomEventUserUpdateTestCaseTests.swift; sourceTree = "<group>"; };
570571
181063DC2C994FA40078E0ED /* ValidateCustomEventUserUpdateAPITest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidateCustomEventUserUpdateAPITest.swift; sourceTree = "<group>"; };
572+
181063DE2C9D51000078E0ED /* ValidateStoredEventCheckUnknownToKnownUserTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ValidateStoredEventCheckUnknownToKnownUserTest.swift; sourceTree = "<group>"; };
571573
182A2A142C661C9A002FF058 /* DataTypeComparatorSearchQueryCriteria.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataTypeComparatorSearchQueryCriteria.swift; sourceTree = "<group>"; };
572574
1881A21A2C7602F80020C64D /* ComparatorDataTypeWithArrayInput.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComparatorDataTypeWithArrayInput.swift; sourceTree = "<group>"; };
573575
18A352092C7DC51C007FED53 /* NestedFieldSupportForArrayData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NestedFieldSupportForArrayData.swift; sourceTree = "<group>"; };
@@ -1692,6 +1694,7 @@
16921694
18A3520B2C85BAF0007FED53 /* IsOneOfInNotOneOfCriteareaTest.swift */,
16931695
181063DA2C9841460078E0ED /* CustomEventUserUpdateTestCaseTests.swift */,
16941696
181063DC2C994FA40078E0ED /* ValidateCustomEventUserUpdateAPITest.swift */,
1697+
181063DE2C9D51000078E0ED /* ValidateStoredEventCheckUnknownToKnownUserTest.swift */,
16951698
);
16961699
name = "anonymous-tracking-tests";
16971700
sourceTree = "<group>";
@@ -2351,6 +2354,7 @@
23512354
55B9F15124B3D33700E8198A /* AuthTests.swift in Sources */,
23522355
55B06F3829D5102800C3B1BC /* BlankApiClient.swift in Sources */,
23532356
5588DFE928C046D7000697D7 /* MockInboxState.swift in Sources */,
2357+
181063DF2C9D51000078E0ED /* ValidateStoredEventCheckUnknownToKnownUserTest.swift in Sources */,
23542358
ACED4C01213F50B30055A497 /* LoggingTests.swift in Sources */,
23552359
AC52C5B8272A8B32000DCDCF /* KeychainWrapperTests.swift in Sources */,
23562360
ACC3FD9E2536D7A30004A2E0 /* InAppFilePersistenceTests.swift in Sources */,

swift-sdk/Internal/AuthManager.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ class AuthManager: IterableAuthManagerProtocol {
9090

9191
clearRefreshTimer()
9292

93-
localStorage.anonymousUserEvents = nil
94-
localStorage.anonymousSessions = nil
93+
if localStorage.email != nil || localStorage.userId != nil || localStorage.userIdAnnon != nil {
94+
localStorage.anonymousUserEvents = nil
95+
localStorage.anonymousSessions = nil
96+
}
9597

9698
isLastAuthTokenValid = false
9799
}

swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
134134

135135
ITBInfo()
136136

137-
let shouldMerge = merge && localStorage.userIdAnnon != nil
138-
137+
//let shouldMerge = merge && localStorage.userIdAnnon != nil
138+
139139
if(config.enableAnonTracking) {
140140
if(email != nil) {
141-
attemptAndProcessMerge(shouldMerge: shouldMerge, destinationUser: email, isEmail: true, failureHandler: failureHandler)
141+
attemptAndProcessMerge(shouldMerge: merge, destinationUser: email, isEmail: true, failureHandler: failureHandler)
142142
}
143143
self.localStorage.userIdAnnon = nil
144144
}
@@ -167,11 +167,11 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
167167
func setUserId(_ userId: String?, authToken: String? = nil, merge: Bool = true, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil, isAnon: Bool = false) {
168168
ITBInfo()
169169

170-
let shouldMerge = merge && localStorage.userIdAnnon != nil
171-
170+
//let shouldMerge = && localStorage.userIdAnnon != nil
171+
172172
if(config.enableAnonTracking) {
173173
if(userId != nil && userId != localStorage.userIdAnnon) {
174-
attemptAndProcessMerge(shouldMerge: shouldMerge, destinationUser: userId, isEmail: false, failureHandler: failureHandler)
174+
attemptAndProcessMerge(shouldMerge: merge, destinationUser: userId, isEmail: false, failureHandler: failureHandler)
175175
}
176176

177177
if(!isAnon) {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// ValidateStoredEventCheckUnknownToKnownUserTest.swift
3+
// unit-tests
4+
//
5+
// Created by Apple on 20/09/24.
6+
// Copyright © 2024 Iterable. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import IterableSDK
11+
12+
final class ValidateStoredEventCheckUnknownToKnownUserTest: XCTestCase, AuthProvider {
13+
private static let apiKey = "zeeApiKey"
14+
private let authToken = "asdf"
15+
private let dateProvider = MockDateProvider()
16+
let mockSession = MockNetworkSession(statusCode: 200)
17+
let localStorage = MockLocalStorage()
18+
19+
var auth: Auth {
20+
Auth(userId: nil, email: nil, authToken: authToken, userIdAnon: nil)
21+
}
22+
23+
override func setUp() {
24+
super.setUp()
25+
}
26+
27+
func data(from jsonString: String) -> Data? {
28+
return jsonString.data(using: .utf8)
29+
}
30+
31+
override func tearDown() {
32+
// Clean up after each test
33+
super.tearDown()
34+
}
35+
36+
// Helper function to wait for a specified duration
37+
private func waitForDuration(seconds: TimeInterval) {
38+
let waitExpectation = expectation(description: "Waiting for \(seconds) seconds")
39+
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
40+
waitExpectation.fulfill()
41+
}
42+
wait(for: [waitExpectation], timeout: seconds + 1)
43+
}
44+
45+
func testCriteriaCustomEventCheck() { // criteria not met with merge false with setUserId
46+
let config = IterableConfig()
47+
config.enableAnonTracking = true
48+
IterableAPI.initializeForTesting(apiKey: ValidateStoredEventCheckUnknownToKnownUserTest.apiKey,
49+
config: config,
50+
networkSession: mockSession,
51+
localStorage: localStorage)
52+
53+
IterableAPI.track(event: "animal-found", dataFields: ["type": "cat", "count": 16, "vaccinated": true])
54+
IterableAPI.track(purchase: 10.0, items: [CommerceItem(id: "mocha", name: "Mocha", price: 10.0, quantity: 17, dataFields: nil)])
55+
IterableAPI.updateCart(items: [CommerceItem(id: "fdsafds", name: "sneakers", price: 4, quantity: 3, dataFields: ["timestemp_createdAt": Int(Date().timeIntervalSince1970)])])
56+
IterableAPI.track(event: "button-clicked", dataFields: ["lastPageViewed":"signup page", "timestemp_createdAt": Int(Date().timeIntervalSince1970)])
57+
waitForDuration(seconds: 3)
58+
59+
IterableAPI.setUserId("testuser123")
60+
61+
if let events = self.localStorage.anonymousUserEvents {
62+
XCTAssertFalse(events.isEmpty, "Expected events to be logged")
63+
} else {
64+
XCTFail("Expected events to be logged but found nil")
65+
}
66+
67+
self.waitForDuration(seconds: 3)
68+
69+
//Sync Completed
70+
if self.localStorage.anonymousUserEvents != nil {
71+
XCTFail("Expected local stored Event nil but found")
72+
} else {
73+
XCTAssertNil(self.localStorage.anonymousUserEvents, "Event found nil as event Sync Completed")
74+
}
75+
}
76+
77+
78+
}

0 commit comments

Comments
 (0)