-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathMockUserRequests.swift
More file actions
277 lines (242 loc) · 11.5 KB
/
Copy pathMockUserRequests.swift
File metadata and controls
277 lines (242 loc) · 11.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
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
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
]
}
public static func testUnauthorizedailureError() -> OneSignalClientError {
let userInfo = ["returned": [
"errors": [["title": "token has invalid claims: token is expired", "code": "auth-0"]],
"httpStatusCode": 401
]]
let nsError = NSError(domain: "not-important", code: 401, userInfo: userInfo)
let error = OneSignalClientError(code: 401, message: "not-important", responseHeaders: nil, response: nil, underlyingError: nsError)
return error!
}
}
// 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) {
let anonCreateResponse = testDefaultFullCreateUserResponse(onesignalId: anonUserOSID, externalId: nil, 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 setUnauthorizedCreateUserFailureResponses(with client: MockOneSignalClient, externalId: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "<OSRequestCreateUser with external_id: \(externalId)>", error: error)
}
public static func setUnauthorizedFetchUserFailureResponses(with client: MockOneSignalClient, onesignalId: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "<OSRequestFetchUser with onesignal_id: \(onesignalId)>", error: error)
}
public static func setUnauthorizedUpdatePropertiesFailureResponses(with client: MockOneSignalClient, tags: [String: String]) {
let error = testUnauthorizedailureError()
let params: NSDictionary = [
"properties": [
"tags": tags
],
"refresh_device_metadata": false
]
client.setMockFailureResponseForRequest(request: "<OSRequestUpdateProperties with parameters: \(params.toSortedString())>", error: error)
}
public static func setUnauthorizedAddEmailFailureResponse(with client: MockOneSignalClient, email: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "<OSRequestCreateSubscription with token: \(email)>", error: error)
}
public static func setUnauthorizedRemoveEmailFailureResponse(with client: MockOneSignalClient, email: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "<OSRequestDeleteSubscription with subscriptionModel: \(email)>", error: error)
}
public static func setUnauthorizedUpdateSubscriptionFailureResponse(with client: MockOneSignalClient, token: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "OSRequestUpdateSubscription with subscriptionObject: [\"token\": \"\(token)\"]", error: error)
}
public static func setUnauthorizedAddAliasFailureResponse(with client: MockOneSignalClient, aliases: [String: String]) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "<OSRequestAddAliases with aliases: \(aliases)>", error: error)
}
public static func setUnauthorizedRemoveAliasFailureResponse(with client: MockOneSignalClient, aliasLabel: String) {
let error = testUnauthorizedailureError()
client.setMockFailureResponseForRequest(request: "OSRequestRemoveAlias with aliasLabel: \(aliasLabel)", error: error)
}
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
)
}
}