Skip to content

Commit b89b243

Browse files
committed
[tests] Add test for switching users with adding tags
* Starts with anon user, log into 2 users via Identify User and Create User * Add tags to each user
1 parent bde68d1 commit b89b243

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

iOS_SDK/OneSignalSDK/OneSignalUserTests/OneSignalUserTests.swift

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class OneSignalUserTests: XCTestCase {
4343
}
4444

4545
override func tearDownWithError() throws {
46-
// TODO: Need to clear all data between tests for user manager, models, etc.
46+
// TODO: Need to clear all data between tests for client, user manager, models, etc.
4747
OneSignalCoreMocks.clearUserDefaults()
4848
OneSignalUserMocks.reset()
4949
}
@@ -137,4 +137,83 @@ final class OneSignalUserTests: XCTestCase {
137137
identityModel.clearData()
138138
}
139139
}
140+
141+
func testSwitchUser_sendsCorrectTags() throws {
142+
/* Setup */
143+
144+
let client = MockOneSignalClient()
145+
146+
// 1. Set up mock responses for the anonymous user
147+
let anonCreateResponse = MockUserRequests.testIdentityPayload(onesignalId: anonUserOSID, externalId: nil)
148+
149+
client.setMockResponseForRequest(
150+
request: "<OSRequestCreateUser with externalId: nil>",
151+
response: anonCreateResponse)
152+
153+
// 2. Set up mock responses for User A
154+
let tagsUserA = ["tag_a": "value_a"]
155+
let createUserA = MockUserRequests.testIdentityPayload(onesignalId: userA_OSID, externalId: userA_EUID)
156+
let tagsResponseUserA = MockUserRequests.testPropertiesPayload(properties: ["tags": tagsUserA])
157+
158+
client.setMockResponseForRequest(
159+
request: "<OSRequestIdentifyUser with aliasLabel: external_id aliasId: \(userA_EUID)>",
160+
response: createUserA
161+
)
162+
client.setMockResponseForRequest(
163+
request: "<OSRequestFetchUser with aliasLabel: external_id aliasId: \(userA_EUID)>",
164+
response: createUserA
165+
)
166+
client.setMockResponseForRequest(
167+
request: "<OSRequestUpdateProperties with properties: [\"tags\": \(tagsUserA)] deltas: nil refreshDeviceMetadata: false>",
168+
response: tagsResponseUserA
169+
)
170+
171+
// 3. Set up mock responses for User B
172+
let tagsUserB = ["tag_b": "value_b"]
173+
let createUserB = MockUserRequests.testIdentityPayload(onesignalId: userB_OSID, externalId: userB_EUID)
174+
let tagsResponseUserB = MockUserRequests.testPropertiesPayload(properties: ["tags": tagsUserB])
175+
176+
client.setMockResponseForRequest(
177+
request: "<OSRequestCreateUser with externalId: \(userB_EUID)>",
178+
response: createUserB
179+
)
180+
client.setMockResponseForRequest(
181+
request: "<OSRequestFetchUser with aliasLabel: external_id aliasId: \(userB_EUID)>",
182+
response: createUserB
183+
)
184+
client.setMockResponseForRequest(
185+
request: "<OSRequestUpdateProperties with properties: [\"tags\": \(tagsUserB)] deltas: nil refreshDeviceMetadata: false>",
186+
response: tagsResponseUserB)
187+
188+
OneSignalCoreImpl.setSharedClient(client)
189+
190+
/* When */
191+
192+
// 1. Login to user A and add tag
193+
OneSignalUserManagerImpl.sharedInstance.login(externalId: userA_EUID, token: nil)
194+
OneSignalUserManagerImpl.sharedInstance.addTag(key: "tag_a", value: "value_a")
195+
196+
// 2. Login to user B and add tag
197+
OneSignalUserManagerImpl.sharedInstance.login(externalId: userB_EUID, token: nil)
198+
OneSignalUserManagerImpl.sharedInstance.addTag(key: "tag_b", value: "value_b")
199+
200+
// 3. Run background threads
201+
OneSignalCoreMocks.waitForBackgroundThreads(seconds: 0.5)
202+
203+
/* Then */
204+
205+
// Assert that every request SDK makes has a response set, and is handled
206+
XCTAssertTrue(client.allRequestsHandled)
207+
208+
// Assert there is only one request containing these tags and they are sent to userA
209+
XCTAssertTrue(client.onlyOneRequest(
210+
contains: "apps/test-app-id/users/by/onesignal_id/\(userA_OSID)",
211+
contains: ["properties": ["tags": tagsUserA]])
212+
)
213+
// Assert there is only one request containing these tags and they are sent to userB
214+
XCTAssertTrue(client.onlyOneRequest(
215+
contains: "apps/test-app-id/users/by/onesignal_id/\(userB_OSID)",
216+
contains: ["properties": ["tags": tagsUserB]])
217+
)
218+
}
140219
}

0 commit comments

Comments
 (0)