11import {
22 ensureAuthenticatedAdmin ,
3+ ensureAuthenticatedAdminAsApp ,
34 ensureAuthenticatedAppManagementAndBusinessPlatform ,
45 ensureAuthenticatedBusinessPlatform ,
56 ensureAuthenticatedPartners ,
89} from './session.js'
910
1011import { getPartnersToken } from './environment.js'
12+ import { shopifyFetch } from './http.js'
1113import { ApplicationToken } from '../../private/node/session/schema.js'
1214import { ensureAuthenticated , setLastSeenAuthMethod , setLastSeenUserIdAfterAuth } from '../../private/node/session.js'
1315import {
@@ -29,6 +31,7 @@ vi.mock('../../private/node/session.js')
2931vi . mock ( '../../private/node/session/exchange.js' )
3032vi . mock ( '../../private/node/session/store.js' )
3133vi . mock ( './environment.js' )
34+ vi . mock ( './http.js' )
3235
3336describe ( 'ensureAuthenticatedStorefront' , ( ) => {
3437 test ( 'returns only storefront token if success' , async ( ) => {
@@ -271,3 +274,48 @@ describe('ensureAuthenticatedAppManagementAndBusinessPlatform', () => {
271274 expect ( ensureAuthenticated ) . not . toHaveBeenCalled ( )
272275 } )
273276} )
277+
278+ describe ( 'ensureAuthenticatedAdminAsApp' , ( ) => {
279+ test ( 'returns admin token if success' , async ( ) => {
280+ // Given
281+ vi . mocked ( shopifyFetch ) . mockResolvedValueOnce ( {
282+ status : 200 ,
283+ json : async ( ) => ( { access_token : 'app_access_token' } ) ,
284+ } as any )
285+
286+ // When
287+ const got = await ensureAuthenticatedAdminAsApp ( 'mystore.myshopify.com' , 'client123' , 'secret456' )
288+
289+ // Then
290+ expect ( got ) . toEqual ( { token : 'app_access_token' , storeFqdn : 'mystore.myshopify.com' } )
291+ } )
292+
293+ test ( 'throws error if app is not installed' , async ( ) => {
294+ // Given
295+ vi . mocked ( shopifyFetch ) . mockResolvedValueOnce ( {
296+ status : 400 ,
297+ text : async ( ) => 'error: app_not_installed' ,
298+ } as any )
299+
300+ // When
301+ const got = ensureAuthenticatedAdminAsApp ( 'mystore.myshopify.com' , 'client123' , 'secret456' )
302+
303+ // Then
304+ await expect ( got ) . rejects . toThrow ( / A p p i s n o t i n s t a l l e d / )
305+ } )
306+
307+ test ( 'throws error on other 400 errors' , async ( ) => {
308+ // Given
309+ vi . mocked ( shopifyFetch ) . mockResolvedValueOnce ( {
310+ status : 400 ,
311+ statusText : 'Bad Request' ,
312+ text : async ( ) => 'invalid credentials' ,
313+ } as any )
314+
315+ // When
316+ const got = ensureAuthenticatedAdminAsApp ( 'mystore.myshopify.com' , 'client123' , 'secret456' )
317+
318+ // Then
319+ await expect ( got ) . rejects . toThrow ( 'Failed to get access token for app client123 on store mystore.myshopify.com' )
320+ } )
321+ } )
0 commit comments