From c36e6b3d566cde7d8aa4dcdf041c3d8a2b7f92ab Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Tue, 28 Apr 2020 12:51:37 -0700 Subject: [PATCH] Added test cases all shopping api's (#85) * v2.7.7 * added test for shopping api --- package.json | 2 +- src/shopping.js | 11 ++- test/buildURL.test.js | 12 +-- test/common.test.js | 8 +- test/findItemsByKeyword.test.js | 8 +- test/{findingApi.test.js => finding.test.js} | 35 +++---- test/index.test.js | 41 ++++++-- test/shopping.test.js | 98 ++++++++++++++++++++ 8 files changed, 171 insertions(+), 44 deletions(-) rename test/{findingApi.test.js => finding.test.js} (74%) create mode 100644 test/shopping.test.js diff --git a/package.json b/package.json index c8fe134..9ecbeac 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "./src/index.js", "homepage": "https://github.com/pajaydev/ebay-node-api", "scripts": { - "lint": "eslint src/*.js", + "lint": "eslint src/*.js test/*.js", "test": "mocha && npm run lint", "docs": "docsify init ./docs", "serve-docs": "docsify serve docs", diff --git a/src/shopping.js b/src/shopping.js index 568b5fa..ed016b8 100644 --- a/src/shopping.js +++ b/src/shopping.js @@ -6,6 +6,7 @@ const makeString = require('make-string'); const getAllCategories = function (categoryID) { const requestURL = `${urlObject.buildShoppingUrl(this.options, 'GetCategoryInfo')}&${stringifyUrl({ 'CategoryID': categoryID || -1 })}`; + console.log(requestURL); return getRequest(requestURL).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console @@ -13,9 +14,10 @@ const getAllCategories = function (categoryID) { }; const getUserDetails = function (input) { - if (!input || typeof input !== 'object') throw new Error('Invalid input'); + if (!input || typeof input !== 'object') throw new Error('invalid_request_error -> Invalid input'); if (!input.userId) throw new Error('invalid_request_error -> userId is null or invalid'); const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetUserProfile')}&${stringifyUrl(input)}`; + console.log(requestUrl); return getRequest(requestUrl).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console @@ -23,11 +25,12 @@ const getUserDetails = function (input) { }; const getItemStatus = function (itemIds) { - if (!itemIds) throw new Error('invalid_request_error -> itemIds is null or invalid'); + if (!itemIds) throw new Error('invalid_request_error -> Item ID is null or invalid'); const paramsObj = { 'ItemID': makeString(itemIds, { braces: 'false', quotes: 'no' }) }; const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetItemStatus')}&${stringifyUrl(paramsObj)}`; + console.log(requestUrl); return getRequest(requestUrl).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console @@ -35,9 +38,10 @@ const getItemStatus = function (itemIds) { }; const getShippingCosts = function (input) { - if (!input || typeof input !== 'object') throw new Error('Invalid input'); + if (!input || typeof input !== 'object') throw new Error('invalid_request_error -> Invalid input'); if (!input.itemId) throw new Error('invalid_request_error -> Item id is null or invalid'); const url = `${urlObject.buildShoppingUrl(this.options, 'GetShippingCosts')}&${stringifyUrl(input)} `; + console.log(url); return getRequest(url).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console @@ -52,6 +56,7 @@ const getShippingCosts = function (input) { const getMultipleItems = function (options) { if (!options || !options.itemId) throw new Error('invalid_request_error -> Item ID is null or invalid'); const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetMultipleItems')}&${stringifyUrl({ 'itemId': makeString(options.itemId, { braces: 'false', quotes: 'no' }) })}`; + console.log(requestUrl); return getRequest(requestUrl).then((data) => { return JSON.parse(data); }, console.error // eslint-disable-line no-console diff --git a/test/buildURL.test.js b/test/buildURL.test.js index 8cf2cdb..454294c 100644 --- a/test/buildURL.test.js +++ b/test/buildURL.test.js @@ -7,7 +7,7 @@ describe('test building url methods', () => { it('test search url', () => { - let expected_search_url = 'https://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&keywords=iphone&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US'; + let expectedSearchUrl = 'https://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=testID&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&keywords=iphone&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&paginationInput.entriesPerPage=6&GLOBAL-ID=EBAY-US'; let options = { name: 'iphone', operationName: 'findItemsByKeywords', @@ -17,22 +17,22 @@ describe('test building url methods', () => { globalID: 'EBAY-US', baseSvcUrl: 'svcs.ebay.com' }; - expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expected_search_url); + expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expectedSearchUrl); }); it('test Shopping url without selector', () => { - let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON'; + let expectedSearchUrl = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON'; let options = { name: 'iphone', param: 'keywords', clientID: 'testID', baseUrl: 'open.api.ebay.com' }; - expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url); + expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expectedSearchUrl); }); it('test Shopping url including selector', () => { - let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true'; + let expectedSearchUrl = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true'; let options = { name: 'iphone', param: 'keywords', @@ -40,6 +40,6 @@ describe('test building url methods', () => { includeSelector: true, baseUrl: 'open.api.ebay.com' }; - expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url); + expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expectedSearchUrl); }); }); diff --git a/test/common.test.js b/test/common.test.js index 3b2b8e6..1e37949 100644 --- a/test/common.test.js +++ b/test/common.test.js @@ -5,15 +5,15 @@ const { parseObj } = require('../src/common-utils/index'); describe('test common util methods', () => { it('test parse object to query params', () => { - const expected_param = '&keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; + const expectedParam = '&keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; const options = { keywords: 'iphone', categoryId: '111', sortOrder: 'PricePlusShippingLowest' }; const emptyOptions = {}; - expect(parseObj(options)).to.be.equal(expected_param); + expect(parseObj(options)).to.be.equal(expectedParam); expect(parseObj(emptyOptions)).to.be.equal(''); - expect(parseObj(options, 'userName=ebay')).to.be.equal(`userName=ebay${expected_param}`); + expect(parseObj(options, 'userName=ebay')).to.be.equal(`userName=ebay${expectedParam}`); }); -}); \ No newline at end of file +}); diff --git a/test/findItemsByKeyword.test.js b/test/findItemsByKeyword.test.js index ff5ec56..ed095d6 100644 --- a/test/findItemsByKeyword.test.js +++ b/test/findItemsByKeyword.test.js @@ -1,5 +1,5 @@ const nock = require('nock'); -const eBay = require('../src/index'); +const Ebay = require('../src/index'); let expect = require('chai').expect; describe('Test find items by keyword method', () => { @@ -22,9 +22,9 @@ describe('Test find items by keyword method', () => { }); it('test input parameter in findItemsByKeyword method', () => { - let ebay = new eBay({ + let ebay = new Ebay({ clientID: 'ClientId' - }) - expect(() => { ebay.findItemsByKeywords() }).to.throw('Keyword is missing, Keyword is required'); + }); + expect(() => { ebay.findItemsByKeywords(); }).to.throw('Keyword is missing, Keyword is required'); }); }); diff --git a/test/findingApi.test.js b/test/finding.test.js similarity index 74% rename from test/findingApi.test.js rename to test/finding.test.js index f09998c..7b57c81 100644 --- a/test/findingApi.test.js +++ b/test/finding.test.js @@ -1,7 +1,8 @@ +'use strict'; const expect = require('chai').expect; const should = require('chai').should(); const nock = require('nock'); -const eBay = require('../src/index'); +const Ebay = require('../src/index'); const { constructAdditionalParams } = require('../src/findingApi'); const nockFindingApi = nock('https://svcs.ebay.com/'); @@ -9,14 +10,14 @@ describe('test ebay finding Api', () => { describe('test findingApi methods with required params', () => { it('test findItemsByCategory with required params', () => { - let ebay = new eBay({ + let ebay = new Ebay({ clientID: 'ClientId' }); expect(() => { ebay.findItemsByCategory(); }).to.throw('Category ID is null or invalid'); }); it('test findCompletedItemswith required params', () => { - let ebay = new eBay({ + let ebay = new Ebay({ clientID: 'ClientId' }); expect(() => { ebay.findCompletedItems(''); }).to.throw('Keyword or category ID are required.'); @@ -25,20 +26,20 @@ describe('test ebay finding Api', () => { describe('test constructAdditionalParams', () => { it('test constructAdditionalParams with required params', () => { - let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; + let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; const options = { keywords: 'iphone', categoryId: '111', sortOrder: 'PricePlusShippingLowest' }; const emptyOptions = {}; - expect(constructAdditionalParams(options)).to.be.equal(expected_param); + expect(constructAdditionalParams(options)).to.be.equal(expectedParam); expect(constructAdditionalParams(emptyOptions)).to.be.equal(''); }); it('test constructAdditionalParams with affiliate params', () => { - let expected_param_with_affiliate = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&affiliate.trackingId=1234567899&affiliate.networkId=123'; - let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; + let expectedParamWithAffiliate = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&affiliate.trackingId=1234567899&affiliate.networkId=123'; + let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest'; const options = { keywords: 'iphone', categoryId: '111', @@ -55,14 +56,14 @@ describe('test ebay finding Api', () => { sortOrder: 'PricePlusShippingLowest' }; const emptyOptions = {}; - expect(constructAdditionalParams(options)).to.be.equal(expected_param_with_affiliate); - expect(constructAdditionalParams(optionsWithNoAffiliate)).to.be.equal(expected_param); + expect(constructAdditionalParams(options)).to.be.equal(expectedParamWithAffiliate); + expect(constructAdditionalParams(optionsWithNoAffiliate)).to.be.equal(expectedParam); expect(constructAdditionalParams(emptyOptions)).to.be.equal(''); }); it('test constructAdditionalParams with additional params', () => { - let expected_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true'; - let expected_pag_param = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2'; + let expectedParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true'; + let expectedPaginationParam = 'keywords=iphone&categoryId=111&sortOrder=PricePlusShippingLowest&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=SoldItemsOnly&itemFilter(1).value=true&paginationInput.entriesPerPage=2'; const options = { keywords: 'iphone', categoryId: '111', @@ -78,18 +79,18 @@ describe('test ebay finding Api', () => { SoldItemsOnly: true, entriesPerPage: 2 }; - expect(constructAdditionalParams(options)).to.be.equal(expected_param); - expect(constructAdditionalParams(optionsWithPagination)).to.be.equal(expected_pag_param); + expect(constructAdditionalParams(options)).to.be.equal(expectedParam); + expect(constructAdditionalParams(optionsWithPagination)).to.be.equal(expectedPaginationParam); }); }); describe('test all get apis', () => { - it("test findItemsAdvanced", () => { - let ebay = new eBay({ + it('test findItemsAdvanced', () => { + let ebay = new Ebay({ clientID: 'ABCD' }); nockFindingApi.get('/services/search/FindingService/v1?SECURITY-APPNAME=ABCD&OPERATION-NAME=findItemsAdvanced&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&paginationInput.entriesPerPage=2&keywords=ipad&itemFilter(0).name=ExpeditedShippingType&itemFilter(0).value=OneDayShipping&outputSelector(0)=SellerInfo&outputSelector(1)=PictureURLLarge&GLOBAL-ID=EBAY-US') - .reply(200, { "findItemsAdvancedResponse": [{ "ack": ["Success"] }] }); + .reply(200, { 'findItemsAdvancedResponse': [{ 'ack': ['Success'] }] }); return ebay.findItemsAdvanced({ entriesPerPage: 2, keywords: 'ipad', @@ -101,4 +102,4 @@ describe('test ebay finding Api', () => { }); }); }); -}); \ No newline at end of file +}); diff --git a/test/index.test.js b/test/index.test.js index c633c81..bffd415 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,31 +1,54 @@ let expect = require('chai').expect; let should = require('chai').should(); -let eBay = require('../src/index'); +let Ebay = require('../src/index'); describe('check all the options provided is valid or not - Ebay Constructor ', () => { it('check input is provided or not', () => { expect(() => { - new eBay(); + new Ebay(); }).to.throw('Options is missing, please provide the input'); }); it('should have client ID', () => { - let ebayApi = new eBay({ clientID: '12345' }); + let ebayApi = new Ebay({ clientID: '12345' }); ebayApi.options.should.have.property('clientID'); }); it('should not have client ID', () => { expect(() => { - new eBay({}); + new Ebay({}); }).to.throw('Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/'); }); it('check instance of Ebay', () => { - let ebayApi = new eBay({ clientID: '12345' }); - expect(ebayApi).to.be.a.instanceOf(eBay); + let ebayApi = new Ebay({ clientID: '12345' }); + expect(ebayApi).to.be.a.instanceOf(Ebay); }); -}); - - + it('test default params', () => { + const ebay = new Ebay({ + clientID: 'ClientId' + }); + const expected = { + clientID: 'ClientId', + env: 'PROD', + baseUrl: 'api.ebay.com', + baseSvcUrl: 'svcs.ebay.com', + globalID: 'EBAY-US', + siteId: '0' + }; + expect(ebay.options).to.deep.equal(expected); + }); + it('test site id, env and country code', () => { + const ebay = new Ebay({ + clientID: 'ClientId', + siteId: 3, + env: 'SANDBOX', + countryCode: 'EBAY_UK' + }); + expect(ebay.options.siteId).to.equals(3); + expect(ebay.options.env).to.equals('SANDBOX'); + expect(ebay.options.globalID).to.equals('EBAY_UK'); + }); +}); diff --git a/test/shopping.test.js b/test/shopping.test.js new file mode 100644 index 0000000..d55db4b --- /dev/null +++ b/test/shopping.test.js @@ -0,0 +1,98 @@ +'use strict'; + +const expect = require('chai').expect; +const nock = require('nock'); +const Ebay = require('../src/index'); + +describe('test shopping api', () => { + describe('test all error scenarios', () => { + it('test input params', () => { + let ebay = new Ebay({ + clientID: 'ClientId' + }); + expect(() => { ebay.getSingleItem(); }).to.throw('invalid_request_error -> Item ID is null or invalid'); + expect(() => { ebay.getMultipleItems(); }).to.throw('invalid_request_error -> Item ID is null or invalid'); + expect(() => { ebay.getMultipleItems([]); }).to.throw('invalid_request_error -> Item ID is null or invalid'); + expect(() => { ebay.getShippingCosts(); }).to.throw('invalid_request_error -> Invalid input'); + expect(() => { ebay.getUserDetails(); }).to.throw('invalid_request_error -> Invalid input'); + expect(() => { ebay.getAllCategories(); }).to.be.not.throw; + expect(() => { ebay.getItemStatus(); }).to.throw('invalid_request_error -> Item ID is null or invalid'); + }); + }); + describe('test shopping api calls', () => { + it('test getSingle Item method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetSingleItem&version=967&siteid=0&responseencoding=JSON&ItemID=12345') + .reply(200, { getSingleItem: true }); + ebay.getSingleItem('12345').then((data) => { + expect(data).to.deep.equal({ getSingleItem: true }); + }); + }); + + it('test getMutiple items method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetMultipleItems&version=967&siteid=0&responseencoding=JSON&itemId=12345,4567') + .reply(200, { getMultipleItems: true }); + ebay.getMultipleItems({ itemId: ['12345', '4567'] }).then((data) => { + expect(data).to.deep.equal({ getMultipleItems: true }); + }); + }); + + it('test getUserDetails method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetUserProfile&version=967&siteid=0&responseencoding=JSON&userId=test') + .reply(200, { getUserDetails: true }); + ebay.getUserDetails({ userId: 'test' }).then((data) => { + expect(data).to.deep.equal({ getUserDetails: true }); + }); + }); + + it('test getShippingCosts method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetShippingCosts&version=967&siteid=0&responseencoding=JSON&itemId=153265274986&destinationCountryCode=US&destinationPostalCode=95128') + .reply(200, { getShippingCosts: true }); + ebay.getShippingCosts({ + itemId: '153265274986', destinationCountryCode: 'US', + destinationPostalCode: '95128' + }).then((data) => { + expect(data).to.deep.equal({ getShippingCosts: true }); + }); + }); + + it('test getAllCategories method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetCategoryInfo&version=967&siteid=0&responseencoding=JSON&CategoryID=1234') + .reply(200, { getAllCategories: true }); + ebay.getAllCategories(1234).then((data) => { + expect(data).to.deep.equal({ getAllCategories: true }); + }); + }); + + it('test getItemStatus method', () => { + const ebay = new Ebay({ + clientID: 'ABCXXX123' + }); + nock('https://api.ebay.com') + .get('/Shopping?appid=ABCXXX123&callname=GetItemStatus&version=967&siteid=0&responseencoding=JSON&ItemID=12345') + .reply(200, { getItemStatus: true }); + ebay.getItemStatus(12345).then((data) => { + expect(data).to.deep.equal({ getItemStatus: true }); + }); + }); + }); +});