1
1
import { APP_ID , DUMMY_EXTERNAL_ID } from '__test__/support/constants' ;
2
2
import TestContext from '__test__/support/environment/TestContext' ;
3
3
import { TestEnvironment } from '__test__/support/environment/TestEnvironment' ;
4
- import { setupSubModelStore } from '__test__/support/environment/TestEnvironmentHelpers' ;
5
4
import {
6
- createUserFn ,
7
- setCreateUserResponse ,
8
- } from '__test__/support/helpers/requests ' ;
5
+ mockUserAgent ,
6
+ setupSubModelStore ,
7
+ } from '__test__/support/environment/TestEnvironmentHelpers ' ;
9
8
import {
10
9
getSubscriptionFn ,
11
10
MockServiceWorker ,
12
11
} from '__test__/support/mocks/MockServiceWorker' ;
12
+ import BrowserUserAgent from '__test__/support/models/BrowserUserAgent' ;
13
+ import { SubscriptionType } from 'src/core/types/subscription' ;
14
+ import UserDirector from 'src/onesignal/UserDirector' ;
13
15
import ContextSW from '../models/ContextSW' ;
14
16
import { RawPushSubscription } from '../models/RawPushSubscription' ;
17
+ import Database from '../services/Database' ;
15
18
import { IDManager } from './IDManager' ;
16
19
import {
17
20
SubscriptionManager ,
@@ -29,19 +32,24 @@ const getRawSubscription = (): RawPushSubscription => {
29
32
rawSubscription . w3cAuth = 'auth' ;
30
33
rawSubscription . w3cP256dh = 'p256dh' ;
31
34
rawSubscription . w3cEndpoint = new URL ( 'https://example.com/endpoint' ) ;
35
+ // @ts -expect-error - legacy property
32
36
rawSubscription . safariDeviceToken = 'safariDeviceToken' ;
33
37
return rawSubscription ;
34
38
} ;
35
39
40
+ const createUserOnServerSpy = vi
41
+ . spyOn ( UserDirector , 'createUserOnServer' )
42
+ . mockResolvedValue ( ) ;
43
+
36
44
describe ( 'SubscriptionManager' , ( ) => {
37
45
beforeEach ( async ( ) => {
38
46
vi . resetModules ( ) ;
39
47
await TestEnvironment . initialize ( ) ;
48
+ await Database . clear ( ) ;
40
49
} ) ;
41
50
42
51
describe ( 'updatePushSubscriptionModelWithRawSubscription' , ( ) => {
43
52
test ( 'should create the push subscription model if it does not exist' , async ( ) => {
44
- setCreateUserResponse ( ) ;
45
53
const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
46
54
const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
47
55
const rawSubscription = getRawSubscription ( ) ;
@@ -55,17 +63,15 @@ describe('SubscriptionManager', () => {
55
63
token : rawSubscription . w3cEndpoint ?. toString ( ) ,
56
64
} ) ;
57
65
66
+ // @ts -expect-error - private method
58
67
await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
59
68
rawSubscription ,
60
69
) ;
61
70
62
71
subModels = await OneSignal . coreDirector . subscriptionModelStore . list ( ) ;
63
72
expect ( subModels . length ) . toBe ( 1 ) ;
64
73
65
- const id = subModels [ 0 ] . id ;
66
- expect ( IDManager . isLocalId ( id ) ) . toBe ( true ) ;
67
74
expect ( subModels [ 0 ] . toJSON ( ) ) . toEqual ( {
68
- id,
69
75
device_model : '' ,
70
76
device_os : 56 ,
71
77
enabled : true ,
@@ -77,28 +83,7 @@ describe('SubscriptionManager', () => {
77
83
web_p256 : rawSubscription . w3cP256dh ,
78
84
} ) ;
79
85
80
- await vi . waitUntil ( ( ) => createUserFn . mock . calls . length > 0 ) ;
81
- expect ( createUserFn ) . toHaveBeenCalledWith ( {
82
- identity : { } ,
83
- properties : {
84
- language : 'en' ,
85
- timezone_id : 'America/Los_Angeles' ,
86
- } ,
87
- refresh_device_metadata : true ,
88
- subscriptions : [
89
- {
90
- device_model : '' ,
91
- device_os : 56 ,
92
- enabled : true ,
93
- notification_types : 1 ,
94
- sdk : '1' ,
95
- token : rawSubscription . w3cEndpoint ?. toString ( ) ,
96
- type : 'ChromePush' ,
97
- web_auth : rawSubscription . w3cAuth ,
98
- web_p256 : rawSubscription . w3cP256dh ,
99
- } ,
100
- ] ,
101
- } ) ;
86
+ expect ( createUserOnServerSpy ) . toHaveBeenCalled ( ) ;
102
87
} ) ;
103
88
104
89
test ( 'should create user if push subscription model does not have an id' , async ( ) => {
@@ -110,9 +95,6 @@ describe('SubscriptionManager', () => {
110
95
getSubscriptionFn . mockResolvedValue ( {
111
96
endpoint : rawSubscription . w3cEndpoint ?. toString ( ) ,
112
97
} ) ;
113
- setCreateUserResponse ( {
114
- externalId : 'some-external-id' ,
115
- } ) ;
116
98
117
99
const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
118
100
const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
@@ -128,38 +110,17 @@ describe('SubscriptionManager', () => {
128
110
onesignalId : identityModel . onesignalId ,
129
111
} ) ;
130
112
113
+ // @ts -expect-error - private method
131
114
await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
132
115
rawSubscription ,
133
116
) ;
134
117
135
118
// should not call generatePushSubscriptionModelSpy
136
119
expect ( generatePushSubscriptionModelSpy ) . not . toHaveBeenCalled ( ) ;
137
-
138
- expect ( createUserFn ) . toHaveBeenCalledWith ( {
139
- identity : {
140
- external_id : 'some-external-id' ,
141
- } ,
142
- properties : {
143
- language : 'en' ,
144
- timezone_id : 'America/Los_Angeles' ,
145
- } ,
146
- refresh_device_metadata : true ,
147
- subscriptions : [
148
- {
149
- device_model : '' ,
150
- device_os : 56 ,
151
- enabled : true ,
152
- notification_types : 1 ,
153
- sdk : '1' ,
154
- token : rawSubscription . w3cEndpoint ?. toString ( ) ,
155
- type : 'ChromePush' ,
156
- } ,
157
- ] ,
158
- } ) ;
120
+ expect ( createUserOnServerSpy ) . toHaveBeenCalled ( ) ;
159
121
} ) ;
160
122
161
123
test ( 'should update the push subscription model if it already exists' , async ( ) => {
162
- setCreateUserResponse ( ) ;
163
124
const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
164
125
const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
165
126
const rawSubscription = getRawSubscription ( ) ;
@@ -176,6 +137,7 @@ describe('SubscriptionManager', () => {
176
137
pushModel . web_auth = 'old-web-auth' ;
177
138
pushModel . web_p256 = 'old-web-p256' ;
178
139
140
+ // @ts -expect-error - private method
179
141
await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
180
142
rawSubscription ,
181
143
) ;
@@ -188,10 +150,60 @@ describe('SubscriptionManager', () => {
188
150
expect ( updatedPushModel . web_auth ) . toBe ( rawSubscription . w3cAuth ) ;
189
151
expect ( updatedPushModel . web_p256 ) . toBe ( rawSubscription . w3cP256dh ) ;
190
152
} ) ;
153
+
154
+ test ( 'should port legacy safari push to new format' , async ( ) => {
155
+ const rawSubscription = getRawSubscription ( ) ;
156
+ getSubscriptionFn . mockResolvedValue ( {
157
+ endpoint : rawSubscription . w3cEndpoint ?. toString ( ) ,
158
+ } ) ;
159
+
160
+ const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
161
+ const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
162
+
163
+ // setting up legacy push model
164
+ await OneSignal . database . setTokenAndId ( {
165
+ token : 'old-token' ,
166
+ } ) ;
167
+ const pushModel = await setupSubModelStore ( {
168
+ id : '123' ,
169
+ token : 'old-token' ,
170
+ onesignalId : DUMMY_EXTERNAL_ID ,
171
+ } ) ;
172
+ pushModel . type = SubscriptionType . SafariLegacyPush ;
173
+
174
+ // mock agent to safari with vapid push support
175
+ mockUserAgent ( {
176
+ userAgent : BrowserUserAgent . SafariSupportedMac121 ,
177
+ } ) ;
178
+
179
+ // @ts -expect-error - private method
180
+ await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
181
+ rawSubscription ,
182
+ ) ;
183
+
184
+ // should update push model with new token, type, web_auth, and web_p256
185
+ const updatedPushModel =
186
+ ( await OneSignal . coreDirector . getPushSubscriptionModel ( ) ) ! ;
187
+ expect ( updatedPushModel . type ) . toBe ( SubscriptionType . SafariPush ) ;
188
+ expect ( updatedPushModel . token ) . toBe (
189
+ rawSubscription . w3cEndpoint ! . toString ( ) ,
190
+ ) ;
191
+ expect ( updatedPushModel . web_auth ) . toBe ( rawSubscription . w3cAuth ) ;
192
+ expect ( updatedPushModel . web_p256 ) . toBe ( rawSubscription . w3cP256dh ) ;
193
+ } ) ;
191
194
} ) ;
192
195
} ) ;
193
196
194
197
Object . defineProperty ( global . navigator , 'serviceWorker' , {
195
198
value : new MockServiceWorker ( ) ,
196
199
writable : true ,
197
200
} ) ;
201
+
202
+ Object . defineProperty ( global , 'PushSubscriptionOptions' , {
203
+ value : {
204
+ prototype : {
205
+ applicationServerKey : 'test' ,
206
+ } ,
207
+ } ,
208
+ writable : true ,
209
+ } ) ;
0 commit comments