-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathIdentityOperationExecutor.test.ts
More file actions
225 lines (190 loc) · 7.64 KB
/
IdentityOperationExecutor.test.ts
File metadata and controls
225 lines (190 loc) · 7.64 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
import { APP_ID, ONESIGNAL_ID } from '__test__/constants';
import { TestEnvironment } from '__test__/support/environment/TestEnvironment';
import { SomeOperation } from '__test__/support/helpers/executors';
import {
setAddAliasError,
setAddAliasResponse,
setDeleteAliasError,
setDeleteAliasResponse,
} from '__test__/support/helpers/requests';
import { updateIdentityModel } from '__test__/support/helpers/setup';
import { ExecutionResult } from 'src/core/types/operation';
import type { MockInstance } from 'vitest';
import { OPERATION_NAME } from '../constants';
import { RebuildUserService } from '../modelRepo/RebuildUserService';
import { IdentityModelStore } from '../modelStores/IdentityModelStore';
import { PropertiesModelStore } from '../modelStores/PropertiesModelStore';
import { SubscriptionModelStore } from '../modelStores/SubscriptionModelStore';
import { NewRecordsState } from '../operationRepo/NewRecordsState';
import { DeleteAliasOperation } from '../operations/DeleteAliasOperation';
import { SetAliasOperation } from '../operations/SetAliasOperation';
import { IdentityOperationExecutor } from './IdentityOperationExecutor';
const label = 'test-label';
const value = 'test-value';
const setAliasOp = new SetAliasOperation(APP_ID, ONESIGNAL_ID, label, value);
const deleteAliasOp = new DeleteAliasOperation(APP_ID, ONESIGNAL_ID, label);
let identityModelStore: IdentityModelStore;
let newRecordsState: NewRecordsState;
let propertiesModelStore: PropertiesModelStore;
let subscriptionModelStore: SubscriptionModelStore;
let rebuildUserService: RebuildUserService;
let getRebuildOpsSpy: MockInstance;
describe('IdentityOperationExecutor', () => {
const getExecutor = () => {
return new IdentityOperationExecutor(
identityModelStore,
rebuildUserService,
newRecordsState,
);
};
beforeAll(() => {
TestEnvironment.initialize();
});
beforeEach(() => {
setAddAliasResponse();
setDeleteAliasResponse();
identityModelStore = OneSignal._coreDirector._identityModelStore;
newRecordsState = OneSignal._coreDirector._newRecordsState;
propertiesModelStore = OneSignal._coreDirector._propertiesModelStore;
subscriptionModelStore = OneSignal._coreDirector._subscriptionModelStore;
rebuildUserService = new RebuildUserService(
identityModelStore,
propertiesModelStore,
subscriptionModelStore,
);
getRebuildOpsSpy = vi.spyOn(
rebuildUserService,
'_getRebuildOperationsIfCurrentUser',
);
});
test('should return correct operations (names)', async () => {
const executor = getExecutor();
expect(executor._operations).toEqual([
OPERATION_NAME.SET_ALIAS,
OPERATION_NAME.DELETE_ALIAS,
]);
});
test('should validate operations', async () => {
const executor = getExecutor();
const someOp = new SomeOperation();
// with invalid ops
const ops = [setAliasOp, deleteAliasOp, someOp];
const result = executor._execute(ops);
await expect(() => result).rejects.toThrow(
`Unrecognized operation(s)! Attempted operations:\n${JSON.stringify(
ops,
)}`,
);
// with both set and delete alias ops
const ops2 = [setAliasOp, deleteAliasOp];
const result2 = executor._execute(ops2);
await expect(() => result2).rejects.toThrow(
`Can't process SetAliasOperation and DeleteAliasOperation at the same time.`,
);
});
test('can execute set alias op', async () => {
updateIdentityModel('onesignal_id', ONESIGNAL_ID);
const executor = getExecutor();
const ops = [setAliasOp];
await executor._execute(ops);
// should set property for matching onesignalId
expect(identityModelStore.model._getProperty(label)).toBe(value);
});
test('can execute delete alias op', async () => {
updateIdentityModel('onesignal_id', ONESIGNAL_ID);
updateIdentityModel(label, value);
const executor = getExecutor();
const ops = [deleteAliasOp];
await executor._execute(ops);
// should delete property for matching onesignalId
expect(identityModelStore.model._getProperty(label)).toBeUndefined();
});
describe('Errors', () => {
test('should handle setAlias errors', async () => {
const executor = getExecutor();
const ops = [setAliasOp];
// Retryable
setAddAliasError({ status: 429, retryAfter: 10 });
const res = await executor._execute(ops);
expect(res.result).toBe(ExecutionResult.FAIL_RETRY);
expect(res.retryAfterSeconds).toBe(10);
// Invalid
setAddAliasError({ status: 400 });
const res2 = await executor._execute(ops);
expect(res2.result).toBe(ExecutionResult.FAIL_NORETRY);
// Conflict
setAddAliasError({ status: 409, retryAfter: 5 });
const res3 = await executor._execute(ops);
expect(res3.result).toBe(ExecutionResult.FAIL_CONFLICT);
expect(res3.retryAfterSeconds).toBe(5);
// Unauthorized
setAddAliasError({ status: 401, retryAfter: 15 });
const res4 = await executor._execute(ops);
expect(res4.result).toBe(ExecutionResult.FAIL_UNAUTHORIZED);
expect(res4.retryAfterSeconds).toBe(15);
// Missing
setAddAliasError({ status: 410 });
getRebuildOpsSpy.mockReturnValueOnce(null);
const res5 = await executor._execute(ops);
// no rebuild ops
updateIdentityModel('onesignal_id', undefined);
expect(res5.result).toBe(ExecutionResult.FAIL_NORETRY);
expect(res5.retryAfterSeconds).toBeUndefined();
// with rebuild ops
identityModelStore.model._onesignalId = ONESIGNAL_ID;
const res7 = await executor._execute(ops);
expect(res7.result).toBe(ExecutionResult.FAIL_RETRY);
expect(res7.retryAfterSeconds).toBeUndefined();
expect(res7.operations).toMatchObject([
{
_name: 'login-user',
_appId: APP_ID,
_onesignalId: ONESIGNAL_ID,
},
{
_name: 'refresh-user',
_appId: APP_ID,
_onesignalId: ONESIGNAL_ID,
},
]);
// in missing retry window
newRecordsState._add(ONESIGNAL_ID);
setAddAliasError({ status: 404, retryAfter: 20 });
const res6 = await executor._execute(ops);
expect(res6.result).toBe(ExecutionResult.FAIL_RETRY);
expect(res6.retryAfterSeconds).toBe(20);
});
test('should handle deleteAlias errors', async () => {
const executor = getExecutor();
const ops = [deleteAliasOp];
// Retryable
setDeleteAliasError({ status: 429, retryAfter: 10 });
const res = await executor._execute(ops);
expect(res.result).toBe(ExecutionResult.FAIL_RETRY);
expect(res.retryAfterSeconds).toBe(10);
// Invalid
setDeleteAliasError({ status: 400 });
const res2 = await executor._execute(ops);
expect(res2.result).toBe(ExecutionResult.FAIL_NORETRY);
// Conflict
setDeleteAliasError({ status: 409, retryAfter: 5 });
const res3 = await executor._execute(ops);
expect(res3.result).toBe(ExecutionResult.SUCCESS);
// Unauthorized
setDeleteAliasError({ status: 401, retryAfter: 15 });
const res4 = await executor._execute(ops);
expect(res4.result).toBe(ExecutionResult.FAIL_UNAUTHORIZED);
expect(res4.retryAfterSeconds).toBe(15);
// Missing
setDeleteAliasError({ status: 410 });
const res5 = await executor._execute(ops);
expect(res5.result).toBe(ExecutionResult.SUCCESS);
// in missing retry window
newRecordsState._add(ONESIGNAL_ID);
setDeleteAliasError({ status: 404, retryAfter: 20 });
const res6 = await executor._execute(ops);
expect(res6.result).toBe(ExecutionResult.FAIL_RETRY);
expect(res6.retryAfterSeconds).toBe(20);
});
});
});