|
1 | 1 | import {authenticateStoreWithApp} from './index.js' |
2 | | -import {setStoredStoreAppSession} from './session-store.js' |
| 2 | +import {getCurrentStoredStoreAppSession, setStoredStoreAppSession} from './session-store.js' |
3 | 3 | import {STORE_AUTH_APP_CLIENT_ID} from './config.js' |
4 | 4 | import {recordStoreFqdnMetadata} from '../attribution.js' |
5 | 5 | import {setLastSeenUserId} from '@shopify/cli-kit/node/session' |
6 | | -import {describe, expect, test, vi} from 'vitest' |
| 6 | +import {randomUUID} from '@shopify/cli-kit/node/crypto' |
| 7 | +import {terminalSupportsPrompting} from '@shopify/cli-kit/node/system' |
| 8 | +import {beforeEach, describe, expect, test, vi} from 'vitest' |
7 | 9 |
|
8 | 10 | vi.mock('./session-store.js') |
9 | 11 | vi.mock('../attribution.js') |
10 | 12 | vi.mock('@shopify/cli-kit/node/session') |
11 | | -vi.mock('@shopify/cli-kit/node/system', () => ({openURL: vi.fn().mockResolvedValue(true)})) |
| 13 | +vi.mock('@shopify/cli-kit/node/system', () => ({ |
| 14 | + openURL: vi.fn().mockResolvedValue(true), |
| 15 | + terminalSupportsPrompting: vi.fn().mockReturnValue(true), |
| 16 | +})) |
12 | 17 | vi.mock('@shopify/cli-kit/node/crypto', () => ({randomUUID: vi.fn().mockReturnValue('state-123')})) |
13 | 18 |
|
14 | 19 | describe('store auth service', () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.mocked(randomUUID).mockReturnValue('state-123') |
| 22 | + vi.mocked(terminalSupportsPrompting).mockReturnValue(true) |
| 23 | + }) |
| 24 | + |
15 | 25 | test('authenticateStoreWithApp opens the browser, stores the session, and returns auth result', async () => { |
16 | 26 | const openURL = vi.fn().mockResolvedValue(true) |
17 | 27 | const presenter = { |
@@ -77,6 +87,92 @@ describe('store auth service', () => { |
77 | 87 | }) |
78 | 88 | }) |
79 | 89 |
|
| 90 | + test('authenticateStoreWithApp keeps waiting for auth when the terminal cannot prompt', async () => { |
| 91 | + const openURL = vi.fn().mockResolvedValue(false) |
| 92 | + const presenter = { |
| 93 | + openingBrowser: vi.fn(), |
| 94 | + manualAuthUrl: vi.fn(), |
| 95 | + success: vi.fn(), |
| 96 | + } |
| 97 | + const waitForStoreAuthCodeMock = vi.fn().mockImplementation(async (options) => { |
| 98 | + await options.onListening?.() |
| 99 | + return 'abc123' |
| 100 | + }) |
| 101 | + |
| 102 | + const result = await authenticateStoreWithApp( |
| 103 | + { |
| 104 | + store: 'shop.myshopify.com', |
| 105 | + scopes: 'read_products', |
| 106 | + }, |
| 107 | + { |
| 108 | + openURL, |
| 109 | + presenter, |
| 110 | + terminalSupportsPrompting: vi.fn().mockReturnValue(false), |
| 111 | + waitForStoreAuthCode: waitForStoreAuthCodeMock, |
| 112 | + exchangeStoreAuthCodeForToken: vi.fn().mockResolvedValue({ |
| 113 | + access_token: 'token', |
| 114 | + scope: 'read_products', |
| 115 | + expires_in: 86400, |
| 116 | + associated_user: {id: 42, email: 'test@example.com'}, |
| 117 | + }), |
| 118 | + }, |
| 119 | + ) |
| 120 | + |
| 121 | + expect(result).toEqual( |
| 122 | + expect.objectContaining({ |
| 123 | + store: 'shop.myshopify.com', |
| 124 | + userId: '42', |
| 125 | + scopes: ['read_products'], |
| 126 | + }), |
| 127 | + ) |
| 128 | + expect(presenter.openingBrowser).toHaveBeenCalledOnce() |
| 129 | + expect(presenter.manualAuthUrl).toHaveBeenCalledWith( |
| 130 | + expect.stringContaining('https://shop.myshopify.com/admin/oauth/authorize?'), |
| 131 | + ) |
| 132 | + expect(presenter.success).toHaveBeenCalledWith(result) |
| 133 | + }) |
| 134 | + |
| 135 | + test('authenticateStoreWithApp returns existing session without auth when non-TTY scopes are already granted', async () => { |
| 136 | + const presenter = { |
| 137 | + openingBrowser: vi.fn(), |
| 138 | + manualAuthUrl: vi.fn(), |
| 139 | + success: vi.fn(), |
| 140 | + } |
| 141 | + vi.mocked(getCurrentStoredStoreAppSession).mockReturnValue({ |
| 142 | + store: 'shop.myshopify.com', |
| 143 | + clientId: STORE_AUTH_APP_CLIENT_ID, |
| 144 | + userId: '42', |
| 145 | + accessToken: 'token', |
| 146 | + scopes: ['read_products'], |
| 147 | + acquiredAt: '2026-03-27T00:00:00.000Z', |
| 148 | + associatedUser: {id: 42, email: 'test@example.com'}, |
| 149 | + }) |
| 150 | + |
| 151 | + const result = await authenticateStoreWithApp( |
| 152 | + { |
| 153 | + store: 'shop.myshopify.com', |
| 154 | + scopes: 'read_products', |
| 155 | + }, |
| 156 | + { |
| 157 | + presenter, |
| 158 | + resolveExistingScopes: vi.fn().mockResolvedValue({scopes: ['read_products'], authoritative: true}), |
| 159 | + terminalSupportsPrompting: vi.fn().mockReturnValue(false), |
| 160 | + waitForStoreAuthCode: vi.fn(), |
| 161 | + exchangeStoreAuthCodeForToken: vi.fn(), |
| 162 | + }, |
| 163 | + ) |
| 164 | + |
| 165 | + expect(result).toEqual( |
| 166 | + expect.objectContaining({ |
| 167 | + store: 'shop.myshopify.com', |
| 168 | + userId: '42', |
| 169 | + scopes: ['read_products'], |
| 170 | + associatedUser: expect.objectContaining({email: 'test@example.com'}), |
| 171 | + }), |
| 172 | + ) |
| 173 | + expect(presenter.success).toHaveBeenCalledWith(result) |
| 174 | + }) |
| 175 | + |
80 | 176 | test('authenticateStoreWithApp uses remote scopes by default when available', async () => { |
81 | 177 | const openURL = vi.fn().mockResolvedValue(true) |
82 | 178 | const presenter = { |
|
0 commit comments