-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathMockUserRequests.swift
More file actions
222 lines (198 loc) · 8.5 KB
/
Copy pathMockUserRequests.swift
File metadata and controls
222 lines (198 loc) · 8.5 KB
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
import OneSignalCore
import OneSignalCoreMocks
@objc
public class MockUserRequests: NSObject {
public static func testIdentityPayload(onesignalId: String, externalId: String?) -> [String: [String: String]] {
var aliases = [OS_ONESIGNAL_ID: onesignalId]
aliases[OS_EXTERNAL_ID] = externalId // only add if non-nil
return [
"identity": aliases
]
}
public static func testPropertiesPayload(properties: [String: Any]) -> [String: Any] {
return [
"properties": properties
]
}
public static func testDefaultPushSubPayload(id: String) -> [String: Any] {
return [
"id": id,
"app_id": "test-app-id",
"type": "iOSPush",
"token": "",
"enabled": true,
"notification_types": 80,
"session_time": 0,
"session_count": 1,
"sdk": "test_sdk_version",
"device_model": "iPhone14,3",
"device_os": "17.4.1",
"rooted": false,
"test_type": 1,
"app_version": "1.4.4",
"net_type": 0,
"carrier": "",
"web_auth": "",
"web_p256": ""
]
}
public static func testDefaultFullCreateUserResponse(onesignalId: String, externalId: String?, subscriptionId: String?) -> [String: Any] {
let identity = testIdentityPayload(onesignalId: onesignalId, externalId: externalId)
let subscription = testDefaultPushSubPayload(id: subscriptionId ?? testPushSubId)
let properties = [
"language": "en",
"timezone_id": "America/Los_Angeles",
"country": "US",
"first_active": 1714860182,
"last_active": 1714860182,
"ip": "xxx.xx.xxx.xxx"
] as [String: Any]
return [
"subscriptions": [subscription],
"identity": identity["identity"]!,
"properties": properties
]
}
}
// MARK: - Set Up Default Client Responses
extension MockUserRequests {
private static func getOneSignalId(for externalId: String) -> String {
switch externalId {
case userA_EUID:
return userA_OSID
case userB_EUID:
return userB_OSID
default:
return UUID().uuidString
}
}
@objc
public static func setDefaultCreateAnonUserResponses(with client: MockOneSignalClient, onesignalId: String? = nil, subscriptionId: String? = nil) {
let anonCreateResponse = testDefaultFullCreateUserResponse(
onesignalId: onesignalId ?? anonUserOSID,
externalId: nil,
subscriptionId: subscriptionId ?? testPushSubId)
client.setMockResponseForRequest(
request: "<OSRequestCreateUser with external_id: nil>",
response: anonCreateResponse)
}
public static func setDefaultCreateUserResponses(with client: MockOneSignalClient, externalId: String, subscriptionId: String? = nil) {
let osid = getOneSignalId(for: externalId)
let userResponse = testDefaultFullCreateUserResponse(onesignalId: osid, externalId: externalId, subscriptionId: subscriptionId)
client.setMockResponseForRequest(
request: "<OSRequestCreateUser with external_id: \(externalId)>",
response: userResponse
)
client.setMockResponseForRequest(
request: "<OSRequestFetchUser with onesignal_id: \(osid)>",
response: userResponse
)
}
public static func setDefaultIdentifyUserResponses(with client: MockOneSignalClient, externalId: String, conflicted: Bool = false) {
var osid: String
var fetchResponse: [String: [String: String]]
// 1. Set the response for the Identify User request
if conflicted {
osid = getOneSignalId(for: externalId)
fetchResponse = MockUserRequests.testIdentityPayload(onesignalId: osid, externalId: externalId)
client.setMockFailureResponseForRequest(
request: "<OSRequestIdentifyUser with external_id: \(externalId)>",
error: OneSignalClientError(code: 409, message: "not-important", responseHeaders: nil, response: nil, underlyingError: nil)
)
// 2. Set the response for the subsequent Create User request
let userResponse = MockUserRequests.testIdentityPayload(onesignalId: osid, externalId: externalId)
client.setMockResponseForRequest(
request: "<OSRequestCreateUser with external_id: \(externalId)>",
response: userResponse)
// 3. Set the response for the subsequent Fetch User request
client.setMockResponseForRequest(
request: "<OSRequestFetchUser with onesignal_id: \(osid)>",
response: fetchResponse
)
} else {
// The Identify User is successful, the OSID is unchanged
osid = anonUserOSID
fetchResponse = MockUserRequests.testIdentityPayload(onesignalId: osid, externalId: externalId)
client.setMockResponseForRequest(
request: "<OSRequestIdentifyUser with external_id: \(externalId)>",
response: fetchResponse
)
// 2. Set the response for the subsequent Fetch User request
client.setMockResponseForRequest(
request: "<OSRequestFetchUser with onesignalId: \(osid)>",
response: fetchResponse
)
}
}
/**
Returns many user data to mimic pulling remote data. Used to test for hydration.
*/
public static func setDefaultFetchUserResponseForHydration(with client: MockOneSignalClient, externalId: String) {
let osid = getOneSignalId(for: externalId)
var fetchResponse: [String: Any] = [
"identity": ["onesignal_id": osid, "external_id": externalId, "remote_alias": "remote_id"],
"properties": [
"tags": ["remote_tag": "remote_value"],
"language": "remote_language"
],
"subscriptions": [
["type": "Email",
"id": "remote_email_id",
"token": "remote_email@example.com"
]
]
]
client.setMockResponseForRequest(
request: "<OSRequestFetchUser with onesignal_id: \(osid)>",
response: fetchResponse
)
}
public static func setAddTagsResponse(with client: MockOneSignalClient, tags: [String: String]) {
let params: NSDictionary = [
"properties": [
"tags": tags
],
"refresh_device_metadata": false
]
let tagsResponse = MockUserRequests.testPropertiesPayload(properties: ["tags": tags])
client.setMockResponseForRequest(
request: "<OSRequestUpdateProperties with parameters: \(params.toSortedString())>",
response: tagsResponse
)
}
/// Sets the mock response when tags and language are added, which will be sent in one request
public static func setAddTagsAndLanguageResponse(with client: MockOneSignalClient, tags: [String: String], language: String) {
let params: NSDictionary = [
"properties": [
"language": Optional(language), // to match the stringify of the actual request
"tags": tags
],
"refresh_device_metadata": false
]
let tagsResponse = testPropertiesPayload(properties: ["tags": tags])
client.setMockResponseForRequest(
request: "<OSRequestUpdateProperties with parameters: \(params.toSortedString())>",
response: tagsResponse
)
}
public static func setAddAliasesResponse(with client: MockOneSignalClient, aliases: [String: String]) {
client.setMockResponseForRequest(
request: "<OSRequestAddAliases with aliases: \(aliases)>",
response: [:] // The SDK does not use the response in any way
)
}
/** The real response will either contain a subscription payload or be empty (if already exists on user) */
public static func setAddEmailResponse(with client: MockOneSignalClient, email: String) {
let response = [
"subscription": [
"id": "\(email)_id",
"type": "Email",
"token": email
]
]
client.setMockResponseForRequest(
request: "<OSRequestCreateSubscription with token: \(email)>",
response: response
)
}
}