-
Notifications
You must be signed in to change notification settings - Fork 16
/
CustomerIOImplementationTest.swift
418 lines (313 loc) · 17.2 KB
/
CustomerIOImplementationTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
@testable import CioInternalCommon
@testable import CioTracking
import Foundation
import SharedTests
import XCTest
class CustomerIOImplementationTest: UnitTest {
private var implementation: CustomerIOImplementation!
// When calling CustomerIOInstance functions in the test functions, use this `CustomerIO` instance.
// This is a workaround until this code base contains implementation tests. There have been bugs
// that have gone undiscovered in the code when `CustomerIO` passes a request to `CustomerIOImplementation`.
private var customerIO: CustomerIO!
private let backgroundQueueMock = QueueMock()
private let profileStoreMock = ProfileStoreMock()
private let hooksMock = HooksManagerMock()
private let profileIdentifyHookMock = ProfileIdentifyHookMock()
private let deviceAttributesMock = DeviceAttributesProviderMock()
private let globalDataStoreMock = GlobalDataStoreMock()
private let deviceInfoMock = DeviceInfoMock()
override func setUp() {
super.setUp()
diGraph.override(value: backgroundQueueMock, forType: Queue.self)
diGraph.override(value: profileStoreMock, forType: ProfileStore.self)
diGraph.override(value: hooksMock, forType: HooksManager.self)
diGraph.override(value: deviceAttributesMock, forType: DeviceAttributesProvider.self)
diGraph.override(value: globalDataStoreMock, forType: GlobalDataStore.self)
diGraph.override(value: deviceInfoMock, forType: DeviceInfo.self)
hooksMock.underlyingProfileIdentifyHooks = [profileIdentifyHookMock]
implementation = CustomerIOImplementation(diGraph: diGraph)
customerIO = CustomerIO(implementation: implementation, diGraph: diGraph)
}
// MARK: identify
// testing `identify()` with request body. Will make an integration test for all `identify()` functions
// but copy/paste identify unit tests not needed since only 1 function has logic in it.
//
// NOTE: At this time, the `CustomerIOHttpTest` is that integration test. After refactoring the code
// to make the DI graph work as intended and the http request runner is in the graph we can make
// integration tests with a mocked request runner.
func test_identify_expectSetNewProfileInDeviceStorage() {
let givenIdentifier = String.random
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
XCTAssertNil(profileStoreMock.identifier)
customerIO.identify(identifier: givenIdentifier)
XCTAssertEqual(profileStoreMock.identifier, givenIdentifier)
}
func test_identify_expectAddTaskBackgroundQueue() {
let givenIdentifier = String.random
let givenBody = ["first_name": "Dana"]
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.identify(identifier: givenIdentifier, body: givenBody)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.identifyProfile.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments?.data
.value as? IdentifyProfileQueueTaskData
XCTAssertEqual(actualQueueTaskData?.identifier, givenIdentifier)
XCTAssertEqual(actualQueueTaskData?.attributesJsonString, jsonAdapter.toJsonString(givenBody))
}
func test_identify_givenPreviouslyIdentifiedCustomer_expectRunHooks_expectDeleteDeviceToken() {
let givenIdentifier = String.random
let givenPreviouslyIdentifiedProfile = String.random
let givenDeviceToken = String.random
globalDataStoreMock.underlyingPushDeviceToken = givenDeviceToken
profileStoreMock.identifier = givenPreviouslyIdentifiedProfile
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.identify(identifier: givenIdentifier)
XCTAssertEqual(profileIdentifyHookMock.beforeIdentifiedProfileChangeCallsCount, 1)
XCTAssertEqual(profileIdentifyHookMock.profileIdentifiedCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.deviceTokensDeleted.count, 1)
XCTAssertEqual(backgroundQueueMock.deviceTokensDeleted, [givenDeviceToken])
}
func test_identify_givenProfileAlreadyIdentified_expectDoNotRunHooks_expectDoNotDeleteDeviceToken() {
let givenIdentifier = String.random
let givenPreviouslyIdentifiedProfile = givenIdentifier
profileStoreMock.identifier = givenPreviouslyIdentifiedProfile
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.identify(identifier: givenIdentifier)
XCTAssertFalse(hooksMock.mockCalled)
XCTAssertTrue(backgroundQueueMock.deviceTokensDeleted.isEmpty)
}
func test_identify_givenNoProfilePreviouslyIdentified_expectRunHooks() {
let givenIdentifier = String.random
profileStoreMock.identifier = nil
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.identify(identifier: givenIdentifier)
XCTAssertEqual(hooksMock.profileIdentifyHooksGetCallsCount, 1)
XCTAssertEqual(profileIdentifyHookMock.profileIdentifiedCallsCount, 1)
}
func test_identify_givenEmptyIdentifier_givenNoProfilePreviouslyIdentified_expectRequestIgnored() {
let givenIdentifier = ""
profileStoreMock.identifier = nil
customerIO.identify(identifier: givenIdentifier)
XCTAssertFalse(hooksMock.mockCalled)
XCTAssertNil(profileStoreMock.identifier)
}
func test_identify_givenEmptyIdentifier_givenProfileAlreadyIdentified_expectDoNotRunHooks_expectDoNotDeleteDeviceToken() {
let givenIdentifier = ""
let givenPreviouslyIdentifiedProfile = String.random
profileStoreMock.identifier = givenPreviouslyIdentifiedProfile
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.identify(identifier: givenIdentifier)
XCTAssertFalse(hooksMock.mockCalled)
XCTAssertTrue(backgroundQueueMock.deviceTokensDeleted.isEmpty)
XCTAssertEqual(profileStoreMock.identifier, givenPreviouslyIdentifiedProfile)
}
// MARK: clearIdentify
func test_clearIdentify_givenNoPreviouslyIdentifiedCustomer_expectDoNotRunHooks_expectStorageSetNil() {
profileStoreMock.identifier = nil
customerIO.clearIdentify()
XCTAssertFalse(hooksMock.mockCalled)
XCTAssertNil(profileStoreMock.identifier)
}
func test_clearIdentify_givenPreviouslyIdentifiedCustomer_expectRunHooks_expectStorageSetNil() {
let givenIdentifier = String.random
profileStoreMock.identifier = givenIdentifier
customerIO.clearIdentify()
XCTAssertEqual(hooksMock.profileIdentifyHooksGetCallsCount, 1)
XCTAssertEqual(profileIdentifyHookMock.beforeProfileStoppedBeingIdentifiedCallsCount, 1)
XCTAssertNil(profileStoreMock.identifier)
}
func test_clearIdentify_expectAbleToGetIdentifierFromStorageInHooks() {
let givenIdentifier = String.random
profileStoreMock.identifier = givenIdentifier
let expect = expectation(description: "Expect to call hook")
profileIdentifyHookMock.beforeProfileStoppedBeingIdentifiedClosure = { actualOldIdentifier in
XCTAssertNotNil(self.profileStoreMock.identifier)
XCTAssertEqual(self.profileStoreMock.identifier, actualOldIdentifier)
expect.fulfill()
}
customerIO.clearIdentify()
waitForExpectations()
XCTAssertNil(profileStoreMock.identifier)
}
// MARK: track
func test_track_givenNoProfileIdentified_expectIgnoreRequest() {
profileStoreMock.identifier = nil
customerIO.track(name: String.random)
XCTAssertFalse(backgroundQueueMock.addTaskCalled)
}
func test_track_expectAddTaskToQueue_expectAssociateEventWithCurrentlyIdentifiedProfile() {
let givenIdentifier = String.random
let givenData = ["first_name": "Dana"]
profileStoreMock.identifier = givenIdentifier
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.track(name: String.random, data: givenData)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.trackEvent.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments?.data.value as? TrackEventQueueTaskData
XCTAssertEqual(actualQueueTaskData?.identifier, givenIdentifier)
XCTAssertTrue(actualQueueTaskData!.attributesJsonString.contains(jsonAdapter.toJsonString(givenData)!))
}
// Tests bug found in: https://github.com/customerio/customerio-ios/issues/134#issuecomment-1028090193
// If `{"data": null, ...}`, that's a bug that results in HTTP request returning a 400.
// We want instead: `{"data": {}, ...}`
func test_track_givenDataNil_expectSaveEmptyRequestData() {
let givenIdentifier = String.random
profileStoreMock.identifier = givenIdentifier
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
let data: EmptyRequestBody? = nil
customerIO.track(name: String.random, data: data)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.trackEvent.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments?.data.value as? TrackEventQueueTaskData
XCTAssertEqual(actualQueueTaskData?.identifier, givenIdentifier)
XCTAssertTrue(actualQueueTaskData!.attributesJsonString.contains(#"{"data":{}"#))
XCTAssertFalse(actualQueueTaskData!.attributesJsonString.contains("null"))
}
// MARK: screen
func test_screen_givenNoProfileIdentified_expectIgnoreRequest_expectDoNotCallHooks() {
profileStoreMock.identifier = nil
customerIO.screen(name: String.random)
XCTAssertFalse(backgroundQueueMock.addTaskCalled)
XCTAssertFalse(hooksMock.mockCalled)
}
func test_screen_expectAddTaskToQueue_expectCorrectDataAddedToQueue_expectCallHooks() {
let givenIdentifier = String.random
let givenData = ["first_name": "Dana"]
profileStoreMock.identifier = givenIdentifier
backgroundQueueMock.addTaskReturnValue = (
success: true,
queueStatus: QueueStatus.successAddingSingleTask
)
customerIO.screen(name: String.random, data: givenData)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.trackEvent.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments?.data.value as? TrackEventQueueTaskData
XCTAssertEqual(actualQueueTaskData?.identifier, givenIdentifier)
XCTAssertTrue(actualQueueTaskData!.attributesJsonString.contains(jsonAdapter.toJsonString(givenData)!))
XCTAssertTrue(hooksMock.screenViewHooksCalled)
XCTAssertEqual(hooksMock.screenViewHooksGetCallsCount, 1)
}
// MARK: registerDeviceToken
func test_registerDeviceToken_givenNoCustomerIdentified_expectNoAddingToQueue_expectStoreDeviceToken() {
let givenDeviceToken = String.random
profileStoreMock.identifier = nil
customerIO.registerDeviceToken(givenDeviceToken)
XCTAssertFalse(backgroundQueueMock.mockCalled)
XCTAssertEqual(globalDataStoreMock.pushDeviceToken, givenDeviceToken)
}
func test_registerDeviceToken_givenCustomerIdentified_expectAddTaskToQueue_expectStoreDeviceToken() {
let givenDeviceToken = String.random
let givenIdentifier = String.random
let givenDefaultAttributes = ["foo": "bar"]
profileStoreMock.identifier = givenIdentifier
deviceInfoMock.underlyingOsName = "iOS"
backgroundQueueMock.addTaskReturnValue = (success: true, queueStatus: QueueStatus.successAddingSingleTask)
deviceAttributesMock.getDefaultDeviceAttributesClosure = { onComplete in
onComplete(givenDefaultAttributes)
}
customerIO.registerDeviceToken(givenDeviceToken)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.registerPushToken.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments!.data
.value as? RegisterPushNotificationQueueTaskData
XCTAssertNotNil(actualQueueTaskData)
XCTAssertEqual(actualQueueTaskData?.profileIdentifier, givenIdentifier)
let expectedJsonString = jsonAdapter.toJsonString(RegisterDeviceRequest(
device:
Device(
token: givenDeviceToken,
platform: "iOS",
lastUsed: dateUtilStub
.givenNow,
attributes: StringAnyEncodable(logger: log, givenDefaultAttributes)
)
))
XCTAssertEqual(actualQueueTaskData?.attributesJsonString, expectedJsonString)
XCTAssertEqual(globalDataStoreMock.pushDeviceToken, givenDeviceToken)
}
func test_registerDeviceToken_givenNoOsNameAvailable_expectNoAddingToQueue() {
let givenDeviceToken = String.random
deviceInfoMock.underlyingOsName = nil
profileStoreMock.identifier = String.random
customerIO.registerDeviceToken(givenDeviceToken)
XCTAssertFalse(backgroundQueueMock.mockCalled)
XCTAssertEqual(globalDataStoreMock.pushDeviceToken, givenDeviceToken)
}
// MARK: deleteDeviceToken
func test_deleteDeviceToken_givenNoCustomerIdentified_givenNoExistingPushToken_expectNoAddingTaskToQueue() {
globalDataStoreMock.pushDeviceToken = nil
profileStoreMock.identifier = nil
customerIO.deleteDeviceToken()
XCTAssertFalse(backgroundQueueMock.mockCalled)
XCTAssertNil(globalDataStoreMock.pushDeviceToken)
}
func test_deleteDeviceToken_givenCustomerIdentified_givenNoExistingPushToken_expectNoAddingTaskToQueue() {
globalDataStoreMock.pushDeviceToken = nil
profileStoreMock.identifier = String.random
customerIO.deleteDeviceToken()
XCTAssertFalse(backgroundQueueMock.mockCalled)
XCTAssertNil(globalDataStoreMock.pushDeviceToken)
}
func test_deleteDeviceToken_givenNoCustomerIdentified_givenExistingPushToken_expectNoAddingTaskToQueue() {
globalDataStoreMock.pushDeviceToken = String.random
profileStoreMock.identifier = nil
customerIO.deleteDeviceToken()
XCTAssertFalse(backgroundQueueMock.mockCalled)
XCTAssertNotNil(globalDataStoreMock.pushDeviceToken)
}
func test_deleteDeviceToken_givenCustomerIdentified_givenExistingPushToken_expectAddTaskToQueue() {
let givenDeviceToken = String.random
let givenIdentifier = String.random
globalDataStoreMock.pushDeviceToken = givenDeviceToken
profileStoreMock.identifier = givenIdentifier
backgroundQueueMock.addTaskReturnValue = (success: true, queueStatus: QueueStatus.successAddingSingleTask)
customerIO.deleteDeviceToken()
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.deletePushToken.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments!.data
.value as? DeletePushNotificationQueueTaskData
XCTAssertNotNil(actualQueueTaskData)
XCTAssertEqual(actualQueueTaskData?.profileIdentifier, givenIdentifier)
XCTAssertEqual(actualQueueTaskData?.deviceToken, givenDeviceToken)
XCTAssertNotNil(globalDataStoreMock.pushDeviceToken)
}
// MARK: trackMetric
func test_trackMetric_expectAddTaskToQueue() {
let givenDeliveryId = String.random
let givenEvent = Metric.delivered
let givenDeviceToken = String.random
backgroundQueueMock.addTaskReturnValue = (success: true, queueStatus: QueueStatus.successAddingSingleTask)
customerIO.trackMetric(deliveryID: givenDeliveryId, event: givenEvent, deviceToken: givenDeviceToken)
XCTAssertEqual(backgroundQueueMock.addTaskCallsCount, 1)
XCTAssertEqual(backgroundQueueMock.addTaskReceivedArguments?.type, QueueTaskType.trackPushMetric.rawValue)
let actualQueueTaskData = backgroundQueueMock.addTaskReceivedArguments!.data.value as? MetricRequest
XCTAssertNotNil(actualQueueTaskData)
XCTAssertEqual(actualQueueTaskData?.deliveryId, givenDeliveryId)
XCTAssertEqual(actualQueueTaskData?.event, givenEvent)
XCTAssertEqual(actualQueueTaskData?.deviceToken, givenDeviceToken)
}
}