Skip to content

Commit 9dfe97c

Browse files
authored
Merge pull request #7258 from Shopify/04-13-fix_store_auth_scope_parsing_for_space-separated_input
Fix store auth scope parsing for space-separated input
2 parents 6fc572d + ad4b9fa commit 9dfe97c

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

packages/cli/src/cli/services/store/auth/index.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,42 @@ describe('store auth service', () => {
306306
expect(setStoredStoreAppSession).not.toHaveBeenCalled()
307307
})
308308

309+
test('authenticateStoreWithApp succeeds when scopes input is space-separated', async () => {
310+
const waitForStoreAuthCodeMock = vi.fn().mockImplementation(async (options) => {
311+
await options.onListening?.()
312+
return 'abc123'
313+
})
314+
315+
const result = await authenticateStoreWithApp(
316+
{
317+
store: 'shop.myshopify.com',
318+
scopes: 'read_products read_inventory',
319+
},
320+
{
321+
openURL: vi.fn().mockResolvedValue(true),
322+
waitForStoreAuthCode: waitForStoreAuthCodeMock,
323+
exchangeStoreAuthCodeForToken: vi.fn().mockResolvedValue({
324+
access_token: 'token',
325+
scope: 'read_products,read_inventory',
326+
expires_in: 86400,
327+
associated_user: {id: 42, email: 'test@example.com'},
328+
}),
329+
presenter: {
330+
openingBrowser: vi.fn(),
331+
manualAuthUrl: vi.fn(),
332+
success: vi.fn(),
333+
},
334+
},
335+
)
336+
337+
expect(result.scopes).toEqual(['read_products', 'read_inventory'])
338+
expect(setStoredStoreAppSession).toHaveBeenCalledWith(
339+
expect.objectContaining({
340+
scopes: ['read_products', 'read_inventory'],
341+
}),
342+
)
343+
})
344+
309345
test('authenticateStoreWithApp accepts compressed write scopes that imply requested read scopes', async () => {
310346
const waitForStoreAuthCodeMock = vi.fn().mockImplementation(async (options) => {
311347
await options.onListening?.()

packages/cli/src/cli/services/store/auth/scopes.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ describe('store auth scope helpers', () => {
1717
expect(mergeRequestedAndStoredScopes(['read_products'], ['read_orders'])).toEqual(['read_orders', 'read_products'])
1818
})
1919

20+
test('parseStoreAuthScopes splits space-separated scopes', () => {
21+
expect(parseStoreAuthScopes('read_products read_inventory')).toEqual(['read_products', 'read_inventory'])
22+
})
23+
24+
test('parseStoreAuthScopes splits mixed comma-and-space delimiters', () => {
25+
expect(parseStoreAuthScopes('read_products, read_inventory,write_orders')).toEqual([
26+
'read_products',
27+
'read_inventory',
28+
'write_orders',
29+
])
30+
})
31+
32+
test('resolveGrantedScopes succeeds when granted scopes are space-separated', () => {
33+
expect(
34+
resolveGrantedScopes(
35+
{
36+
access_token: 'token',
37+
scope: 'read_products read_inventory',
38+
},
39+
['read_products', 'read_inventory'],
40+
),
41+
).toEqual(['read_products', 'read_inventory'])
42+
})
43+
2044
test('resolveGrantedScopes accepts compressed write scopes that imply requested reads', () => {
2145
expect(
2246
resolveGrantedScopes(

packages/cli/src/cli/services/store/auth/scopes.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import {outputContent, outputDebug} from '@shopify/cli-kit/node/output'
33
import type {StoreTokenResponse} from './token-client.js'
44

55
export function parseStoreAuthScopes(input: string): string[] {
6-
const scopes = input
7-
.split(',')
8-
.map((scope) => scope.trim())
9-
.filter(Boolean)
6+
const scopes = input.split(/[ ,]+/).filter(Boolean)
107

118
if (scopes.length === 0) {
129
throw new AbortError('At least one scope is required.', 'Pass --scopes as a comma-separated list.')

0 commit comments

Comments
 (0)