11import { APP_ID , DUMMY_EXTERNAL_ID } from '__test__/support/constants' ;
22import TestContext from '__test__/support/environment/TestContext' ;
33import { TestEnvironment } from '__test__/support/environment/TestEnvironment' ;
4- import { setupSubModelStore } from '__test__/support/environment/TestEnvironmentHelpers' ;
54import {
6- createUserFn ,
7- setCreateUserResponse ,
8- } from '__test__/support/helpers/requests ' ;
5+ mockUserAgent ,
6+ setupSubModelStore ,
7+ } from '__test__/support/environment/TestEnvironmentHelpers ' ;
98import {
109 getSubscriptionFn ,
1110 MockServiceWorker ,
1211} 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' ;
1315import ContextSW from '../models/ContextSW' ;
1416import { RawPushSubscription } from '../models/RawPushSubscription' ;
17+ import Database from '../services/Database' ;
1518import { IDManager } from './IDManager' ;
1619import {
1720 SubscriptionManager ,
@@ -29,19 +32,24 @@ const getRawSubscription = (): RawPushSubscription => {
2932 rawSubscription . w3cAuth = 'auth' ;
3033 rawSubscription . w3cP256dh = 'p256dh' ;
3134 rawSubscription . w3cEndpoint = new URL ( 'https://example.com/endpoint' ) ;
35+ // @ts -expect-error - legacy property
3236 rawSubscription . safariDeviceToken = 'safariDeviceToken' ;
3337 return rawSubscription ;
3438} ;
3539
40+ const createUserOnServerSpy = vi
41+ . spyOn ( UserDirector , 'createUserOnServer' )
42+ . mockResolvedValue ( ) ;
43+
3644describe ( 'SubscriptionManager' , ( ) => {
3745 beforeEach ( async ( ) => {
3846 vi . resetModules ( ) ;
3947 await TestEnvironment . initialize ( ) ;
48+ await Database . clear ( ) ;
4049 } ) ;
4150
4251 describe ( 'updatePushSubscriptionModelWithRawSubscription' , ( ) => {
4352 test ( 'should create the push subscription model if it does not exist' , async ( ) => {
44- setCreateUserResponse ( ) ;
4553 const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
4654 const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
4755 const rawSubscription = getRawSubscription ( ) ;
@@ -55,17 +63,15 @@ describe('SubscriptionManager', () => {
5563 token : rawSubscription . w3cEndpoint ?. toString ( ) ,
5664 } ) ;
5765
66+ // @ts -expect-error - private method
5867 await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
5968 rawSubscription ,
6069 ) ;
6170
6271 subModels = await OneSignal . coreDirector . subscriptionModelStore . list ( ) ;
6372 expect ( subModels . length ) . toBe ( 1 ) ;
6473
65- const id = subModels [ 0 ] . id ;
66- expect ( IDManager . isLocalId ( id ) ) . toBe ( true ) ;
6774 expect ( subModels [ 0 ] . toJSON ( ) ) . toEqual ( {
68- id,
6975 device_model : '' ,
7076 device_os : 56 ,
7177 enabled : true ,
@@ -77,28 +83,7 @@ describe('SubscriptionManager', () => {
7783 web_p256 : rawSubscription . w3cP256dh ,
7884 } ) ;
7985
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 ( ) ;
10287 } ) ;
10388
10489 test ( 'should create user if push subscription model does not have an id' , async ( ) => {
@@ -110,9 +95,6 @@ describe('SubscriptionManager', () => {
11095 getSubscriptionFn . mockResolvedValue ( {
11196 endpoint : rawSubscription . w3cEndpoint ?. toString ( ) ,
11297 } ) ;
113- setCreateUserResponse ( {
114- externalId : 'some-external-id' ,
115- } ) ;
11698
11799 const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
118100 const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
@@ -128,38 +110,17 @@ describe('SubscriptionManager', () => {
128110 onesignalId : identityModel . onesignalId ,
129111 } ) ;
130112
113+ // @ts -expect-error - private method
131114 await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
132115 rawSubscription ,
133116 ) ;
134117
135118 // should not call generatePushSubscriptionModelSpy
136119 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 ( ) ;
159121 } ) ;
160122
161123 test ( 'should update the push subscription model if it already exists' , async ( ) => {
162- setCreateUserResponse ( ) ;
163124 const context = new ContextSW ( TestContext . getFakeMergedConfig ( ) ) ;
164125 const subscriptionManager = new SubscriptionManager ( context , subConfig ) ;
165126 const rawSubscription = getRawSubscription ( ) ;
@@ -176,6 +137,7 @@ describe('SubscriptionManager', () => {
176137 pushModel . web_auth = 'old-web-auth' ;
177138 pushModel . web_p256 = 'old-web-p256' ;
178139
140+ // @ts -expect-error - private method
179141 await subscriptionManager . _updatePushSubscriptionModelWithRawSubscription (
180142 rawSubscription ,
181143 ) ;
@@ -188,10 +150,60 @@ describe('SubscriptionManager', () => {
188150 expect ( updatedPushModel . web_auth ) . toBe ( rawSubscription . w3cAuth ) ;
189151 expect ( updatedPushModel . web_p256 ) . toBe ( rawSubscription . w3cP256dh ) ;
190152 } ) ;
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+ } ) ;
191194 } ) ;
192195} ) ;
193196
194197Object . defineProperty ( global . navigator , 'serviceWorker' , {
195198 value : new MockServiceWorker ( ) ,
196199 writable : true ,
197200} ) ;
201+
202+ Object . defineProperty ( global , 'PushSubscriptionOptions' , {
203+ value : {
204+ prototype : {
205+ applicationServerKey : 'test' ,
206+ } ,
207+ } ,
208+ writable : true ,
209+ } ) ;
0 commit comments