Skip to content

Commit

Permalink
Update tests and docs for new allowSecretKey config option
Browse files Browse the repository at this point in the history
  • Loading branch information
robbieaverill committed Sep 3, 2021
1 parent e5cc131 commit b526325
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The following configuration options are available to be defined as the third arg
* `timeoutMs`: The number of milliseconds before a request will time out. Default: 60000.
* `axiosConfig`: An optional object containing configuration options for axios, if used.
* `headers`: A list of request headers. Defining headers here will override the defaults.
* `allowSecretKey`: Commerce.js will prevent you from using a secret API key for authorization. Use this option to override. Default: false.

## Upgrading

Expand Down
7 changes: 5 additions & 2 deletions src/commerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ class Commerce {
throw new Error('⚠️ Invalid public key given to Commerce.js client');
}

if (!config.allowSecretKey && apiKey.toLowerCase().substring(0, 3) === 'sk_') {
if (
!config.allowSecretKey &&
apiKey.toLowerCase().substring(0, 3) === 'sk_'
) {
throw new Error(
'Secret key provided. You must use a public key with Commerce.js, or explicitly allow secret keys in the config object.',
'Secret key provided. You must use a public key with Commerce.js, or use `allowSecretKey` in the config object.',
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/tests/cart-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ beforeEach(() => {
const commerceImpl = {
options: {
url: 'http://localhost/',
publicKey: 'test',
apiKey: 'test',
version: 'v1',
cartLifetime: 30,
},
Expand Down
8 changes: 7 additions & 1 deletion src/tests/commerce-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ describe('Commerce', () => {
expect(() => {
new Commerce('sk_9w8h598s4t9st');
}).toThrowError(
'Secret key provided. You must use a public key with Commerce.js!',
'Secret key provided. You must use a public key with Commerce.js, or use `allowSecretKey` in the config object.',
);
});

it('allows use of a secret key', () => {
expect(() => {
new Commerce('sk_9w8h598s4t9st', false, { allowSecretKey: true });
}).not.toThrowError();
});

it('ensures that cart lifetime is positive', () => {
expect(() => {
new Commerce('pk_9w8h598s4t9st', true, { cartLifetime: -1 });
Expand Down

0 comments on commit b526325

Please sign in to comment.