|
| 1 | +import {STORE_AUTH_APP_CLIENT_ID, storeAuthSessionKey} from './config.js' |
| 2 | +import {setStoredStoreAppSession, type StoredStoreAppSession} from './session-store.js' |
| 3 | +import {listStoredStoreAuthSummaries} from './stored-auth.js' |
| 4 | +import {inTemporaryDirectory} from '@shopify/cli-kit/node/fs' |
| 5 | +import {LocalStorage} from '@shopify/cli-kit/node/local-storage' |
| 6 | +import {describe, expect, test} from 'vitest' |
| 7 | + |
| 8 | +function buildSession(overrides: Partial<StoredStoreAppSession> = {}): StoredStoreAppSession { |
| 9 | + return { |
| 10 | + store: 'shop.myshopify.com', |
| 11 | + clientId: STORE_AUTH_APP_CLIENT_ID, |
| 12 | + userId: '42', |
| 13 | + accessToken: 'token-1', |
| 14 | + refreshToken: 'refresh-token-1', |
| 15 | + scopes: ['read_products'], |
| 16 | + acquiredAt: '2026-03-27T00:00:00.000Z', |
| 17 | + ...overrides, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +describe('listStoredStoreAuthSummaries', () => { |
| 22 | + test('returns an empty array when no store auth is persisted', async () => { |
| 23 | + await inTemporaryDirectory((cwd) => { |
| 24 | + const storage = new LocalStorage<Record<string, unknown>>({cwd}) |
| 25 | + |
| 26 | + expect(listStoredStoreAuthSummaries(storage as any)).toEqual([]) |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + test('returns one summary per store sorted by store using the current user session', async () => { |
| 31 | + await inTemporaryDirectory((cwd) => { |
| 32 | + const storage = new LocalStorage<Record<string, unknown>>({cwd}) |
| 33 | + |
| 34 | + setStoredStoreAppSession(buildSession({store: 'b-shop.myshopify.com'}), storage as any) |
| 35 | + setStoredStoreAppSession(buildSession({store: 'a-shop.myshopify.com', userId: '41', accessToken: 'token-41'}), storage as any) |
| 36 | + setStoredStoreAppSession(buildSession({store: 'a-shop.myshopify.com', userId: '84', accessToken: 'token-84'}), storage as any) |
| 37 | + |
| 38 | + expect(listStoredStoreAuthSummaries(storage as any)).toEqual([ |
| 39 | + { |
| 40 | + store: 'a-shop.myshopify.com', |
| 41 | + userId: '84', |
| 42 | + scopes: ['read_products'], |
| 43 | + acquiredAt: '2026-03-27T00:00:00.000Z', |
| 44 | + }, |
| 45 | + { |
| 46 | + store: 'b-shop.myshopify.com', |
| 47 | + userId: '42', |
| 48 | + scopes: ['read_products'], |
| 49 | + acquiredAt: '2026-03-27T00:00:00.000Z', |
| 50 | + }, |
| 51 | + ]) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + test('projects associated user metadata without exposing tokens', async () => { |
| 56 | + await inTemporaryDirectory((cwd) => { |
| 57 | + const storage = new LocalStorage<Record<string, unknown>>({cwd}) |
| 58 | + |
| 59 | + setStoredStoreAppSession( |
| 60 | + buildSession({ |
| 61 | + expiresAt: '2026-03-28T00:00:00.000Z', |
| 62 | + refreshTokenExpiresAt: '2026-04-28T00:00:00.000Z', |
| 63 | + associatedUser: { |
| 64 | + id: 42, |
| 65 | + email: 'merchant@example.com', |
| 66 | + firstName: 'Merchant', |
| 67 | + lastName: 'User', |
| 68 | + accountOwner: true, |
| 69 | + }, |
| 70 | + }), |
| 71 | + storage as any, |
| 72 | + ) |
| 73 | + |
| 74 | + const [summary] = listStoredStoreAuthSummaries(storage as any) |
| 75 | + |
| 76 | + expect(summary).toEqual({ |
| 77 | + store: 'shop.myshopify.com', |
| 78 | + userId: '42', |
| 79 | + scopes: ['read_products'], |
| 80 | + acquiredAt: '2026-03-27T00:00:00.000Z', |
| 81 | + expiresAt: '2026-03-28T00:00:00.000Z', |
| 82 | + refreshTokenExpiresAt: '2026-04-28T00:00:00.000Z', |
| 83 | + associatedUser: { |
| 84 | + id: 42, |
| 85 | + email: 'merchant@example.com', |
| 86 | + firstName: 'Merchant', |
| 87 | + lastName: 'User', |
| 88 | + accountOwner: true, |
| 89 | + }, |
| 90 | + }) |
| 91 | + expect(summary).not.toHaveProperty('accessToken') |
| 92 | + expect(summary).not.toHaveProperty('refreshToken') |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + test('skips malformed persisted buckets while listing summaries', async () => { |
| 97 | + await inTemporaryDirectory((cwd) => { |
| 98 | + const storage = new LocalStorage<Record<string, unknown>>({cwd}) |
| 99 | + storage.set(storeAuthSessionKey('broken-shop.myshopify.com'), { |
| 100 | + currentUserId: '42', |
| 101 | + sessionsByUserId: { |
| 102 | + '42': {userId: '42'}, |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + expect(listStoredStoreAuthSummaries(storage as any)).toEqual([]) |
| 107 | + expect(storage.get(storeAuthSessionKey('broken-shop.myshopify.com'))).toBeUndefined() |
| 108 | + }) |
| 109 | + }) |
| 110 | +}) |
0 commit comments