|
| 1 | +import { |
| 2 | + isOrganizationAccessError, |
| 3 | + OrganizationStoreAccessError, |
| 4 | + organizationStoresRequestDoc, |
| 5 | +} from './business-platform.js' |
| 6 | +import {describe, expect, test, vi, beforeEach} from 'vitest' |
| 7 | +import {businessPlatformOrganizationsRequestDoc} from '@shopify/cli-kit/node/api/business-platform' |
| 8 | +import {getAppAutomationToken} from '@shopify/cli-kit/node/environment' |
| 9 | +import {isServiceAccountTokenSet} from '@shopify/cli-kit/node/session' |
| 10 | +import {AbortError} from '@shopify/cli-kit/node/error' |
| 11 | + |
| 12 | +vi.mock('@shopify/cli-kit/node/environment') |
| 13 | +vi.mock('@shopify/cli-kit/node/api/business-platform') |
| 14 | +vi.mock('@shopify/cli-kit/node/session') |
| 15 | + |
| 16 | +const organization = {id: '299069', businessName: 'A Better Looking Shop'} |
| 17 | + |
| 18 | +// The shape cli-kit gives a handled 4xx response: a `GraphQLClientError`, which carries `statusCode`. |
| 19 | +function handledClientError(statusCode: number): Error { |
| 20 | + return Object.assign(new Error(`The Business Platform GraphQL API responded with the HTTP status ${statusCode}.`), { |
| 21 | + statusCode, |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +// The shape graphql-request throws when the caller handles errors itself. |
| 26 | +function rawClientError(status: number): Error { |
| 27 | + return Object.assign(new Error('GraphQL Error (Code: 401)'), {response: {status, errors: undefined}, request: {}}) |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + vi.mocked(isServiceAccountTokenSet).mockReturnValue(false) |
| 32 | + vi.mocked(getAppAutomationToken).mockReturnValue(undefined) |
| 33 | +}) |
| 34 | + |
| 35 | +describe('isOrganizationAccessError', () => { |
| 36 | + test('recognises a handled 401 and 403', () => { |
| 37 | + expect(isOrganizationAccessError(handledClientError(401))).toBe(true) |
| 38 | + expect(isOrganizationAccessError(handledClientError(403))).toBe(true) |
| 39 | + }) |
| 40 | + |
| 41 | + test('recognises a raw graphql-request 401', () => { |
| 42 | + expect(isOrganizationAccessError(rawClientError(401))).toBe(true) |
| 43 | + }) |
| 44 | + |
| 45 | + test('ignores other statuses and other errors', () => { |
| 46 | + expect(isOrganizationAccessError(handledClientError(404))).toBe(false) |
| 47 | + expect(isOrganizationAccessError(handledClientError(500))).toBe(false) |
| 48 | + expect(isOrganizationAccessError(new Error('boom'))).toBe(false) |
| 49 | + expect(isOrganizationAccessError(undefined)).toBe(false) |
| 50 | + }) |
| 51 | +}) |
| 52 | + |
| 53 | +describe('OrganizationStoreAccessError', () => { |
| 54 | + test('tells a service account which access it lacks and where to grant it', () => { |
| 55 | + vi.mocked(isServiceAccountTokenSet).mockReturnValue(true) |
| 56 | + |
| 57 | + const error = new OrganizationStoreAccessError(organization) |
| 58 | + |
| 59 | + expect(error).toBeInstanceOf(AbortError) |
| 60 | + expect(error.message).toBe("This service account can't manage dev stores in A Better Looking Shop.") |
| 61 | + // The guidance lives in nextSteps, the field the store commands' --json output carries. |
| 62 | + expect(error.tryMessage).toBeNull() |
| 63 | + expect(error.nextSteps).toEqual([ |
| 64 | + 'Give the service account Dev store management access in the Shopify admin under Settings > Users > Security.', |
| 65 | + 'Or use a token from a service account that has that access.', |
| 66 | + ]) |
| 67 | + }) |
| 68 | + |
| 69 | + test('points a legacy automation token at service accounts', () => { |
| 70 | + vi.mocked(getAppAutomationToken).mockReturnValue('legacy-token') |
| 71 | + |
| 72 | + const error = new OrganizationStoreAccessError(organization) |
| 73 | + |
| 74 | + expect(error.message).toBe("This automation token can't manage dev stores in A Better Looking Shop.") |
| 75 | + expect(error.nextSteps?.join(' ')).toContain('SHOPIFY_SERVICE_ACCOUNT_TOKEN') |
| 76 | + }) |
| 77 | + |
| 78 | + test('talks about permission for a logged-in user', () => { |
| 79 | + const error = new OrganizationStoreAccessError(organization) |
| 80 | + |
| 81 | + expect(error.message).toBe("You don't have permission to manage dev stores in A Better Looking Shop.") |
| 82 | + expect(error.nextSteps).toEqual([ |
| 83 | + "Check that you're logged in to the right account.", |
| 84 | + 'Or ask an organization admin for access.', |
| 85 | + ]) |
| 86 | + }) |
| 87 | + |
| 88 | + test('falls back to the organization ID when the name is unknown', () => { |
| 89 | + const error = new OrganizationStoreAccessError({id: '299069'}) |
| 90 | + |
| 91 | + expect(error.message).toBe("You don't have permission to manage dev stores in organization 299069.") |
| 92 | + }) |
| 93 | +}) |
| 94 | + |
| 95 | +describe('organizationStoresRequestDoc', () => { |
| 96 | + const request = { |
| 97 | + query: {} as never, |
| 98 | + token: 'token', |
| 99 | + variables: {first: 1}, |
| 100 | + unauthorizedHandler: {type: 'token_refresh' as const, handler: async () => ({token: 'refreshed'})}, |
| 101 | + } |
| 102 | + |
| 103 | + test("sends the request to the organization's Organizations API and returns the result", async () => { |
| 104 | + vi.mocked(businessPlatformOrganizationsRequestDoc).mockResolvedValue({ok: true}) |
| 105 | + |
| 106 | + await expect(organizationStoresRequestDoc(organization, request)).resolves.toEqual({ok: true}) |
| 107 | + expect(businessPlatformOrganizationsRequestDoc).toHaveBeenCalledWith({...request, organizationId: '299069'}) |
| 108 | + }) |
| 109 | + |
| 110 | + test('replaces a refused request with the access error', async () => { |
| 111 | + vi.mocked(isServiceAccountTokenSet).mockReturnValue(true) |
| 112 | + vi.mocked(businessPlatformOrganizationsRequestDoc).mockRejectedValue(handledClientError(401)) |
| 113 | + |
| 114 | + await expect(organizationStoresRequestDoc(organization, request)).rejects.toThrow( |
| 115 | + "This service account can't manage dev stores in A Better Looking Shop.", |
| 116 | + ) |
| 117 | + }) |
| 118 | + |
| 119 | + test('rethrows every other error unchanged', async () => { |
| 120 | + const notFound = handledClientError(404) |
| 121 | + vi.mocked(businessPlatformOrganizationsRequestDoc).mockRejectedValue(notFound) |
| 122 | + |
| 123 | + await expect(organizationStoresRequestDoc(organization, request)).rejects.toBe(notFound) |
| 124 | + }) |
| 125 | +}) |
0 commit comments