@@ -40,6 +40,23 @@ const getCallbackUrl = (state: string, scope = REQUIRED_SCOPES.join(" ")) =>
4040 state ,
4141 ) } &code=auth-code&scope=${ encodeURIComponent ( scope ) } `;
4242
43+ const expectGoogleAuthRequestBody = (
44+ request : CapturedAuthRequest | undefined ,
45+ state : string ,
46+ ) => {
47+ expect ( request ?. body ) . toMatchObject ( {
48+ thirdPartyId : "google" ,
49+ clientType : "web" ,
50+ redirectURIInfo : {
51+ redirectURIOnProviderDashboard : expect . stringContaining ( CALLBACK_PATH ) ,
52+ redirectURIQueryParams : {
53+ code : "auth-code" ,
54+ state,
55+ } ,
56+ } ,
57+ } ) ;
58+ } ;
59+
4360const writeGoogleAuthorizationIntent = async ( {
4461 intent,
4562 page,
@@ -67,6 +84,34 @@ const writeGoogleAuthorizationIntent = async ({
6784 ) ;
6885} ;
6986
87+ const setActiveCompassSession = async ( page : Page ) => {
88+ const now = Date . now ( ) ;
89+ const pageOrigin = new URL ( page . url ( ) ) . origin ;
90+ const expires = Math . floor ( now / 1000 ) + 60 * 60 ;
91+ const frontToken = Buffer . from (
92+ JSON . stringify ( {
93+ ate : now + 60 * 60 * 1000 ,
94+ uid : "test-user-id" ,
95+ up : { } ,
96+ } ) ,
97+ ) . toString ( "base64" ) ;
98+
99+ await page . context ( ) . addCookies ( [
100+ {
101+ expires,
102+ name : "st-last-access-token-update" ,
103+ url : pageOrigin ,
104+ value : now . toString ( ) ,
105+ } ,
106+ {
107+ expires,
108+ name : "sFrontToken" ,
109+ url : pageOrigin ,
110+ value : frontToken ,
111+ } ,
112+ ] ) ;
113+ } ;
114+
70115const prepareGoogleAuthCallbackPage = async (
71116 page : Page ,
72117 options : ApiMockOptions = { } ,
@@ -186,17 +231,7 @@ test.describe("Google auth callback", () => {
186231 expect ( apiMocks . loginOrSignup ) . toHaveLength ( 1 ) ;
187232 expect ( apiMocks . connectGoogle ) . toHaveLength ( 0 ) ;
188233 expect ( apiMocks . loginOrSignup [ 0 ] ?. headers . rid ) . toBe ( "thirdparty" ) ;
189- expect ( apiMocks . loginOrSignup [ 0 ] ?. body ) . toMatchObject ( {
190- thirdPartyId : "google" ,
191- clientType : "web" ,
192- redirectURIInfo : {
193- redirectURIOnProviderDashboard : expect . stringContaining ( CALLBACK_PATH ) ,
194- redirectURIQueryParams : {
195- code : "auth-code" ,
196- state,
197- } ,
198- } ,
199- } ) ;
234+ expectGoogleAuthRequestBody ( apiMocks . loginOrSignup [ 0 ] , state ) ;
200235 expect (
201236 await page . evaluate (
202237 ( key ) => sessionStorage . getItem ( key ) ,
@@ -205,7 +240,9 @@ test.describe("Google auth callback", () => {
205240 ) . toBeNull ( ) ;
206241 } ) ;
207242
208- test ( "finishes a saved Google Calendar connect intent" , async ( { page } ) => {
243+ test ( "finishes a saved Google Calendar connect intent when the Compass session is active" , async ( {
244+ page,
245+ } ) => {
209246 const state = "connect-calendar-state" ;
210247 const apiMocks = await prepareGoogleAuthCallbackPage ( page ) ;
211248
@@ -215,23 +252,36 @@ test.describe("Google auth callback", () => {
215252 returnPath : "/week" ,
216253 state,
217254 } ) ;
255+ await setActiveCompassSession ( page ) ;
218256
219257 await page . goto ( getCallbackUrl ( state ) ) ;
220258
221259 await expect ( page ) . toHaveURL ( / \/ w e e k $ / ) ;
222260 expect ( apiMocks . loginOrSignup ) . toHaveLength ( 0 ) ;
223261 expect ( apiMocks . connectGoogle ) . toHaveLength ( 1 ) ;
224- expect ( apiMocks . connectGoogle [ 0 ] ?. body ) . toMatchObject ( {
225- thirdPartyId : "google" ,
226- clientType : "web" ,
227- redirectURIInfo : {
228- redirectURIOnProviderDashboard : expect . stringContaining ( CALLBACK_PATH ) ,
229- redirectURIQueryParams : {
230- code : "auth-code" ,
231- state,
232- } ,
233- } ,
262+ expectGoogleAuthRequestBody ( apiMocks . connectGoogle [ 0 ] , state ) ;
263+ } ) ;
264+
265+ test ( "recovers a saved Google Calendar connect intent through Google sign-in when the Compass session is missing" , async ( {
266+ page,
267+ } ) => {
268+ const state = "connect-calendar-session-missing-state" ;
269+ const apiMocks = await prepareGoogleAuthCallbackPage ( page ) ;
270+
271+ await writeGoogleAuthorizationIntent ( {
272+ intent : "connectCalendar" ,
273+ page,
274+ returnPath : "/week" ,
275+ state,
234276 } ) ;
277+
278+ await page . goto ( getCallbackUrl ( state ) ) ;
279+
280+ await expect ( page ) . toHaveURL ( / \/ w e e k $ / ) ;
281+ expect ( apiMocks . connectGoogle ) . toHaveLength ( 0 ) ;
282+ expect ( apiMocks . loginOrSignup ) . toHaveLength ( 1 ) ;
283+ expect ( apiMocks . loginOrSignup [ 0 ] ?. headers . rid ) . toBe ( "thirdparty" ) ;
284+ expectGoogleAuthRequestBody ( apiMocks . loginOrSignup [ 0 ] , state ) ;
235285 } ) ;
236286
237287 test ( "rejects callbacks that are missing required Google Calendar scopes" , async ( {
0 commit comments