Skip to content

Commit

Permalink
fix(iows): throw error on non-numeric buCode or productCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Dec 18, 2021
1 parent 9516e23 commit 254616e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
9 changes: 9 additions & 0 deletions source/lib/iows2.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ class IOWS2 {
productId = productId.substring(1);
}

assert(
/^\d+$/.test(productId),
`The given productId ${JSON.stringify(productId)} doesn’t look like a valid product id (06a5d687)`,
);
assert(
/^\d+$/.test(buCode),
`The given buCode ${JSON.stringify(buCode)} doesn’t look like a valid buCode id (b92bb3e4)`,
);

const url = this.buildUrl(this.baseUrl, this.countryCode, this.languageCode, buCode, productId, productType);
return this.fetch(url)
.catch(err => {
Expand Down
22 changes: 22 additions & 0 deletions source/lib/iows2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,28 @@ describe('IOWS2', () => {
})
});
});

['FG01283', 'false', 'undefined', '{}'].forEach(productId => {
it(`throws an error when productId is ${JSON.stringify(productId)}`, async () => {
return iows.getStoreProductAvailability('123', productId)
.then(() => { throw new Error('should not run') })
.catch(err => {
expect(err).to.be.instanceOf(AssertionError);
expect(err.message).to.contain('06a5d687');
})
});
});

['FG', 'false', 'undefined', '{}'].forEach(buCode => {
it(`throws an error when buCode is ${JSON.stringify(buCode)}`, async () => {
return iows.getStoreProductAvailability(buCode, '123')
.then(() => { throw new Error('should not run') })
.catch(err => {
expect(err).to.be.instanceOf(AssertionError);
expect(err.message).to.contain('b92bb3e4');
})
});
});
}); // argument validation

describe('successfull', () => {
Expand Down

0 comments on commit 254616e

Please sign in to comment.