Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/theme/src/cli/utilities/theme-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {resolvePath} from '@shopify/cli-kit/node/path'
import {renderConcurrent, renderConfirmationPrompt, renderError, renderWarning} from '@shopify/cli-kit/node/ui'
import {addPublicMetadata, addSensitiveMetadata} from '@shopify/cli-kit/node/metadata'
import {hashString} from '@shopify/cli-kit/node/crypto'
import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output'

import type {Writable} from 'stream'

Expand Down Expand Up @@ -206,6 +207,7 @@ describe('ThemeCommand', () => {
vi.mocked(getCurrentStoredStoreAppSession).mockReturnValue(undefined)
vi.mocked(listCurrentStoredStoreAppSessions).mockReturnValue([])
vi.mocked(fileExistsSync).mockReturnValue(true)
mockAndCaptureOutput().clear()
})

describe('run', () => {
Expand Down Expand Up @@ -279,6 +281,8 @@ describe('ThemeCommand', () => {
acquiredAt: '2026-06-08T11:00:00.000Z',
})

const outputMock = mockAndCaptureOutput()

await CommandConfig.load()
const command = new TestScopedThemeCommand([], CommandConfig)

Expand All @@ -289,6 +293,9 @@ describe('ThemeCommand', () => {
expect(command.commandCalls[0]).toMatchObject({
session: {token: 'shpat_preview_token', storeFqdn: 'test-store.myshopify.com'},
})
expect(outputMock.debug()).toContain(
'Using stored store auth session for test-store.myshopify.com (scopes: read_themes).',
)
})

test('uses the password flag instead of a matching store auth cache session', async () => {
Expand Down Expand Up @@ -391,6 +398,7 @@ describe('ThemeCommand', () => {
scopes: ['read_products'],
acquiredAt: '2026-06-08T11:00:00.000Z',
})
const outputMock = mockAndCaptureOutput()

await CommandConfig.load()
const command = new TestScopedThemeCommand([], CommandConfig)
Expand All @@ -400,6 +408,9 @@ describe('ThemeCommand', () => {
expect(getCurrentStoredStoreAppSession).toHaveBeenCalledWith('test-store.myshopify.com')
expect(ensureAuthenticatedThemes).toHaveBeenCalledWith('test-store.myshopify.com', undefined)
expect(command.commandCalls[0]).toMatchObject({session: mockSession})
expect(outputMock.debug()).toContain(
'Ignoring stored store auth session for test-store.myshopify.com: it is missing required scopes (has: read_products; needs: read_themes).',
)
})

test('falls back to theme authentication when the stored session is expired', async () => {
Expand All @@ -412,6 +423,7 @@ describe('ThemeCommand', () => {
acquiredAt: '2026-06-08T11:00:00.000Z',
expiresAt: new Date(Date.now() - 60 * 1000).toISOString(),
})
const outputMock = mockAndCaptureOutput()

await CommandConfig.load()
const command = new TestScopedThemeCommand([], CommandConfig)
Expand All @@ -420,6 +432,9 @@ describe('ThemeCommand', () => {

expect(ensureAuthenticatedThemes).toHaveBeenCalledWith('test-store.myshopify.com', undefined)
expect(command.commandCalls[0]).toMatchObject({session: mockSession})
expect(outputMock.debug()).toContain(
'Ignoring stored store auth session for test-store.myshopify.com: it expired at',
)
})

test('uses a stored session whose expiry is far enough in the future', async () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/theme/src/cli/utilities/theme-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {AbortController} from '@shopify/cli-kit/node/abort'
import {AbortError} from '@shopify/cli-kit/node/error'
import {recordEvent, compileData} from '@shopify/cli-kit/node/analytics'
import {addPublicMetadata, addSensitiveMetadata} from '@shopify/cli-kit/node/metadata'
import {outputDebug} from '@shopify/cli-kit/node/output'
import {cwd, joinPath, resolvePath} from '@shopify/cli-kit/node/path'
import {fileExistsSync} from '@shopify/cli-kit/node/fs'
import {normalizeStoreFqdn} from '@shopify/cli-kit/node/context/fqdn'
Expand Down Expand Up @@ -423,13 +424,23 @@ export default abstract class ThemeCommand extends Command {
requiredScopes: string[],
): AdminSession | undefined {
if (isSessionExpired(storedSession)) {
outputDebug(
`Ignoring stored store auth session for ${storeFqdn}: it expired at ${storedSession.expiresAt ?? 'unknown'}.`,
)
return undefined
}

if (!this.hasRequiredStoreAuthScopes(storedSession.scopes, requiredScopes)) {
outputDebug(
`Ignoring stored store auth session for ${storeFqdn}: it is missing required scopes (has: ${storedSession.scopes.join(
', ',
)}; needs: ${requiredScopes.join(', ')}).`,
)
return undefined
}

outputDebug(`Using stored store auth session for ${storeFqdn} (scopes: ${storedSession.scopes.join(', ')}).`)

setLastSeenUserId(storedSession.userId)

return {
Expand Down
Loading