@@ -50,35 +50,13 @@ const validIdentityToken: IdentityToken = {
5050}
5151
5252const validTokens : OAuthSession = {
53- admin : { token : 'admin_token ' , storeFqdn : 'mystore.myshopify.com' } ,
54- storefront : 'storefront_token ' ,
55- partners : 'partners_token ' ,
53+ admin : { token : 'access_token ' , storeFqdn : 'mystore.myshopify.com' } ,
54+ storefront : 'access_token ' ,
55+ partners : 'access_token ' ,
5656 userId,
5757}
5858
59- const appTokens : Record < string , ApplicationToken > = {
60- // Admin APIs includes domain in the key
61- 'mystore.myshopify.com-admin' : {
62- accessToken : 'admin_token' ,
63- expiresAt : futureDate ,
64- scopes : [ 'scope' , 'scope2' ] ,
65- } ,
66- 'storefront-renderer' : {
67- accessToken : 'storefront_token' ,
68- expiresAt : futureDate ,
69- scopes : [ 'scope1' ] ,
70- } ,
71- partners : {
72- accessToken : 'partners_token' ,
73- expiresAt : futureDate ,
74- scopes : [ 'scope2' ] ,
75- } ,
76- 'business-platform' : {
77- accessToken : 'business_platform_token' ,
78- expiresAt : futureDate ,
79- scopes : [ 'scope3' ] ,
80- } ,
81- }
59+ const appTokens : Record < string , ApplicationToken > = { }
8260
8361const partnersToken : ApplicationToken = {
8462 accessToken : 'custom_partners_token' ,
@@ -162,7 +140,7 @@ describe('ensureAuthenticated when previous session is invalid', () => {
162140 const got = await ensureAuthenticated ( defaultApplications )
163141
164142 // Then
165- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
143+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
166144 expect ( refreshAccessToken ) . not . toBeCalled ( )
167145 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
168146 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
@@ -211,7 +189,7 @@ The CLI is currently unable to prompt for reauthentication.`,
211189 ...validIdentityToken ,
212190 alias : 'user@example.com' ,
213191 } ,
214- applications : appTokens ,
192+ applications : { } ,
215193 } ,
216194 } ,
217195 }
@@ -220,7 +198,7 @@ The CLI is currently unable to prompt for reauthentication.`,
220198 const got = await ensureAuthenticated ( defaultApplications )
221199
222200 // Then
223- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
201+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
224202 expect ( refreshAccessToken ) . not . toBeCalled ( )
225203 expect ( storeSessions ) . toBeCalledWith ( expectedSessions )
226204 expect ( got ) . toEqual ( validTokens )
@@ -239,7 +217,7 @@ The CLI is currently unable to prompt for reauthentication.`,
239217 const got = await ensureAuthenticated ( defaultApplications )
240218
241219 // Then
242- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
220+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
243221 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
244222 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
245223
@@ -250,26 +228,20 @@ The CLI is currently unable to prompt for reauthentication.`,
250228 expect ( got ) . toEqual ( validTokens )
251229 } )
252230
253- test ( 'falls back to userId when no business platform token available ' , async ( ) => {
231+ test ( 'uses identity token to fetch email during full auth flow ' , async ( ) => {
254232 // Given
255233 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'needs_full_auth' )
256234 vi . mocked ( fetchSessions ) . mockResolvedValue ( undefined )
257- const appTokensWithoutBusinessPlatform = {
258- 'mystore.myshopify.com-admin' : appTokens [ 'mystore.myshopify.com-admin' ] ! ,
259- 'storefront-renderer' : appTokens [ 'storefront-renderer' ] ! ,
260- partners : appTokens . partners ! ,
261- }
262- vi . mocked ( exchangeAccessForApplicationTokens ) . mockResolvedValueOnce ( appTokensWithoutBusinessPlatform )
263235
264236 // When
265237 const got = await ensureAuthenticated ( defaultApplications )
266238
267239 // Then
268- expect ( businessPlatformRequest ) . not . toHaveBeenCalled ( )
240+ expect ( businessPlatformRequest ) . toHaveBeenCalledWith ( expect . any ( String ) , 'access_token' )
269241
270- // Verify the session was stored with userId as alias (fallback)
242+ // Verify the session was stored with email as alias
271243 const storedSession = vi . mocked ( storeSessions ) . mock . calls [ 0 ] ! [ 0 ]
272- expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( userId )
244+ expect ( storedSession [ fqdn ] ! [ userId ] ! . identity . alias ) . toBe ( 'user@example.com' )
273245 } )
274246
275247 test ( 'executes complete auth flow if requesting additional scopes' , async ( ) => {
@@ -281,7 +253,7 @@ The CLI is currently unable to prompt for reauthentication.`,
281253 const got = await ensureAuthenticated ( defaultApplications )
282254
283255 // Then
284- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
256+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
285257 expect ( refreshAccessToken ) . not . toBeCalled ( )
286258 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
287259 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
@@ -320,7 +292,12 @@ describe('when existing session is valid', () => {
320292 vi . mocked ( validateSession ) . mockResolvedValueOnce ( 'ok' )
321293 vi . mocked ( fetchSessions ) . mockResolvedValue ( validSessions )
322294 vi . mocked ( getPartnersToken ) . mockReturnValue ( 'custom_cli_token' )
323- const expected = { ...validTokens , partners : 'custom_partners_token' }
295+ const expected = {
296+ admin : { token : 'access_token' , storeFqdn : 'mystore.myshopify.com' } ,
297+ storefront : 'access_token' ,
298+ partners : 'custom_partners_token' ,
299+ userId,
300+ }
324301
325302 // When
326303 const got = await ensureAuthenticated ( defaultApplications )
@@ -344,8 +321,16 @@ describe('when existing session is valid', () => {
344321
345322 // Then
346323 expect ( refreshAccessToken ) . toBeCalled ( )
347- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
348- expect ( storeSessions ) . toBeCalledWith ( validSessions )
324+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
325+ const expectedSessions = {
326+ [ fqdn ] : {
327+ [ userId ] : {
328+ identity : validIdentityToken ,
329+ applications : { } ,
330+ } ,
331+ } ,
332+ }
333+ expect ( storeSessions ) . toBeCalledWith ( expectedSessions )
349334 expect ( got ) . toEqual ( validTokens )
350335 await expect ( getLastSeenUserIdAfterAuth ( ) ) . resolves . toBe ( '1234-5678' )
351336 await expect ( getLastSeenAuthMethod ( ) ) . resolves . toEqual ( 'device_auth' )
@@ -364,8 +349,16 @@ describe('when existing session is expired', () => {
364349
365350 // Then
366351 expect ( refreshAccessToken ) . toBeCalled ( )
367- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
368- expect ( storeSessions ) . toBeCalledWith ( validSessions )
352+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
353+ const expectedSessions = {
354+ [ fqdn ] : {
355+ [ userId ] : {
356+ identity : validIdentityToken ,
357+ applications : { } ,
358+ } ,
359+ } ,
360+ }
361+ expect ( storeSessions ) . toBeCalledWith ( expectedSessions )
369362 expect ( got ) . toEqual ( validTokens )
370363 await expect ( getLastSeenUserIdAfterAuth ( ) ) . resolves . toBe ( '1234-5678' )
371364 await expect ( getLastSeenAuthMethod ( ) ) . resolves . toEqual ( 'device_auth' )
@@ -385,7 +378,7 @@ describe('when existing session is expired', () => {
385378
386379 // Then
387380 expect ( refreshAccessToken ) . toBeCalled ( )
388- expect ( exchangeAccessForApplicationTokens ) . toBeCalled ( )
381+ expect ( exchangeAccessForApplicationTokens ) . not . toBeCalled ( )
389382 expect ( businessPlatformRequest ) . toHaveBeenCalled ( )
390383 expect ( storeSessions ) . toHaveBeenCalledOnce ( )
391384
@@ -644,7 +637,15 @@ describe('ensureAuthenticated email fetch functionality', () => {
644637 const got = await ensureAuthenticated ( defaultApplications )
645638
646639 // Then
647- expect ( storeSessions ) . toBeCalledWith ( validSessions )
640+ const expectedSessions = {
641+ [ fqdn ] : {
642+ [ userId ] : {
643+ identity : validIdentityToken ,
644+ applications : { } ,
645+ } ,
646+ } ,
647+ }
648+ expect ( storeSessions ) . toBeCalledWith ( expectedSessions )
648649 expect ( got ) . toEqual ( validTokens )
649650 } )
650651
@@ -659,7 +660,15 @@ describe('ensureAuthenticated email fetch functionality', () => {
659660 // Then
660661 // The email fetch is not called during refresh - the session keeps its existing alias
661662 expect ( businessPlatformRequest ) . not . toHaveBeenCalled ( )
662- expect ( storeSessions ) . toBeCalledWith ( validSessions )
663+ const expectedSessions = {
664+ [ fqdn ] : {
665+ [ userId ] : {
666+ identity : validIdentityToken ,
667+ applications : { } ,
668+ } ,
669+ } ,
670+ }
671+ expect ( storeSessions ) . toBeCalledWith ( expectedSessions )
663672 expect ( got ) . toEqual ( validTokens )
664673 } )
665674
0 commit comments