Skip to content

Commit

Permalink
fix(core): Prevent use of unrecognized currency codes in RequestContext
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 13, 2023
1 parent 0ebf0fb commit fee503f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/core/e2e/channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
CREATE_ROLE,
GET_CHANNELS,
GET_CUSTOMER_LIST,
GET_PRODUCT_LIST,
GET_PRODUCT_WITH_VARIANTS,
ME,
UPDATE_CHANNEL,
Expand Down Expand Up @@ -436,6 +437,21 @@ describe('Channels', () => {
});
}, 'availableCurrencyCodes must include the defaultCurrencyCode (AUD)'),
);

it(
'specifying an unsupported currencyCode throws',
assertThrowsWithMessage(async () => {
await adminClient.query<Codegen.GetProductListQuery, Codegen.GetProductListQueryVariables>(
GET_PRODUCT_LIST,
{
options: {
take: 1,
},
},
{ currencyCode: 'JPY' },
);
}, 'The currency "JPY" is not available in the current Channel'),
);
});
});

Expand Down
24 changes: 12 additions & 12 deletions packages/core/e2e/product-prices.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ describe('Product prices', () => {
expect(product?.variants[0]?.currencyCode).toEqual(CurrencyCode.GBP);
});

it('uses default if unrecognised currency code passed in query string', async () => {
const { product } = await adminClient.query(
GetProductWithVariantsDocument,
{
id: multiPriceProduct.id,
},
{ currencyCode: 'JPY' },
);

expect(product?.variants[0]?.price).toEqual(1200);
expect(product?.variants[0]?.currencyCode).toEqual(CurrencyCode.USD);
});
it(
'throws if unrecognised currency code passed in query string',
assertThrowsWithMessage(async () => {
await adminClient.query(
GetProductWithVariantsDocument,
{
id: multiPriceProduct.id,
},
{ currencyCode: 'JPY' },
);
}, 'The currency "JPY" is not available in the current Channel'),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ms from 'ms';

import { ApiType, getApiType } from '../../../api/common/get-api-type';
import { RequestContext } from '../../../api/common/request-context';
import { UserInputError } from '../../../common/index';
import { idsAreEqual } from '../../../common/utils';
import { ConfigService } from '../../../config/config.service';
import { CachedSession, CachedSessionUser } from '../../../config/session-cache/session-cache-strategy';
Expand Down Expand Up @@ -138,7 +139,13 @@ export class RequestContextService {
}

private getCurrencyCode(req: Request, channel: Channel): CurrencyCode | undefined {
return (req.query && (req.query.currencyCode as CurrencyCode)) ?? channel.defaultCurrencyCode;
const queryCurrencyCode = req.query && (req.query.currencyCode as CurrencyCode);
if (queryCurrencyCode && !channel.availableCurrencyCodes.includes(queryCurrencyCode)) {
throw new UserInputError('error.currency-not-available-in-channel', {
currencyCode: queryCurrencyCode,
});
}
return queryCurrencyCode ?? channel.defaultCurrencyCode;
}

/**
Expand Down

0 comments on commit fee503f

Please sign in to comment.