From 1c2597ed4e3ccd651fe69fc0d0d0e1c49da62a24 Mon Sep 17 00:00:00 2001 From: mantovanig Date: Tue, 16 Feb 2021 00:41:35 +0100 Subject: [PATCH] test: integration tests + vscode devcontainer --- .devcontainer/Dockerfile | 16 + .devcontainer/devcontainer.json | 29 ++ __tests__/healthcheck.test.js | 7 + __tests__/mburger.test.js | 109 ++++++ __tests__/section.test.js | 0 mburger.js | 663 ++++++++++++++++---------------- 6 files changed, 497 insertions(+), 327 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 __tests__/healthcheck.test.js create mode 100644 __tests__/mburger.test.js delete mode 100644 __tests__/section.test.js diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..3f98a86 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,16 @@ +# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/javascript-node/.devcontainer/base.Dockerfile + +# [Choice] Node.js version: 14, 12, 10 +ARG VARIANT="14-buster" +FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT} + +# [Optional] Uncomment this section to install additional OS packages. +# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ +# && apt-get -y install --no-install-recommends + +# [Optional] Uncomment if you want to install an additional version of node using nvm +# ARG EXTRA_NODE_VERSION=10 +# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}" + +# [Optional] Uncomment if you want to install more global node modules +# RUN su node -c "npm install -g " \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..c249bce --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,29 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: +// https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/javascript-node +{ + "name": "Node.js", + "build": { + "dockerfile": "Dockerfile", + // Update 'VARIANT' to pick a Node version: 10, 12, 14 + "args": { "VARIANT": "12" } + }, + + // Set *default* container specific settings.json values on container create. + "settings": { + "terminal.integrated.shell.osx": "/bin/zsh" + }, + + // Add the IDs of extensions you want installed when the container is created. + "extensions": [ + "dbaeumer.vscode-eslint" + ], + + // Use 'forwardPorts' to make a list of ports inside the container available locally. + // "forwardPorts": [], + + // Use 'postCreateCommand' to run commands after the container is created. + // "postCreateCommand": "yarn install", + + // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. + "remoteUser": "node" +} diff --git a/__tests__/healthcheck.test.js b/__tests__/healthcheck.test.js new file mode 100644 index 0000000..02030ad --- /dev/null +++ b/__tests__/healthcheck.test.js @@ -0,0 +1,7 @@ +describe('healthcheck', () => { + it('1 equals 1', () => { + expect(1).toBe(1); + }); +}); + + diff --git a/__tests__/mburger.test.js b/__tests__/mburger.test.js new file mode 100644 index 0000000..15458d1 --- /dev/null +++ b/__tests__/mburger.test.js @@ -0,0 +1,109 @@ +import { createClient } from "../mburger"; + +const MBURGER_API_KEY = "7f24ae161f9070567363630870fb90d58ab67fcd"; + +describe("MBurger SDK", () => { + it("create a MBurger client", () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + expect(Boolean(client["getSection"])).toBeTruthy(); + expect(Boolean(client["getBlocks"])).toBeTruthy(); + expect(Boolean(client["getBlock"])).toBeTruthy(); + }); + + it("Try to create a client without an api_key", () => { + expect(() => { + createClient(); + }).toThrow(); + }); + + it("Get a block", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + const res = await client.getBlock({ + block_id: "1782", + }); + + expect(res).toBeInstanceOf(Array); + }); + + it("Get a block with a specific locale", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + const res = await client.getBlock({ + block_id: "1782", + + locale: "en", + }); + + // TODO(mantovanig): missing assertion on locale + expect(res).toBeInstanceOf(Array); + }); + + it("Get a error with an invalid block id", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + expect(async () => await client.getBlock({ block_id: "9999" })).rejects.toThrow(TypeError); + }); + + it("Get a section", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + const res = await client.getSection({ + section_id: "321640", + }); + + expect(res.meta.id).toBe(321640); + }); + + it("Get a error with an invalid section id", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + expect(async () => await client.getSection({ section_id: "9999" })).rejects.toThrow(TypeError); + }); + + it("Get blocks array", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + const res = await client.getBlocks({ + block_ids: [1782, 1787], + }); + + expect(res.Product.meta.id).toBe(1782); + expect(res.Categories.meta.id).toBe(1787); + }) + + it("Get a error calling getBlocks without block_ids", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + expect(async () => await client.getBlocks()).rejects.toThrow(TypeError); + }) + + it("Get a error calling getBlocks with id that does not exist", async () => { + const client = createClient({ + api_key: MBURGER_API_KEY, + }); + + const res = await client.getBlocks({ + block_ids: [9999], + }); + + expect(res).toStrictEqual({}); + }) +}); diff --git a/__tests__/section.test.js b/__tests__/section.test.js deleted file mode 100644 index e69de29..0000000 diff --git a/mburger.js b/mburger.js index 25341c9..4fe55af 100644 --- a/mburger.js +++ b/mburger.js @@ -11,14 +11,13 @@ * */ - -const axios = require('axios'); -const qs = require('qs'); +const axios = require("axios"); +const qs = require("qs"); const host = "https://mburger.cloud/api/"; const headers = { - 'Accept': 'application/json', - 'X-MBurger-Version': 3, + Accept: "application/json", + "X-MBurger-Version": 3, }; // TODO. Implement caching @@ -40,343 +39,353 @@ const headers = { * */ export function createClient(params) { - if (!params.api_key) { - throw new TypeError('You have to initialize the Client with an API Key. Visit support.mburger.cloud for more informations'); - } - - return MBurgerInstance( - axios.create({ - baseURL: host, - headers: { - ...{'X-MBurger-Token': params.api_key}, - ...headers - } - }) + if (!params.api_key) { + throw new TypeError( + "You have to initialize the Client with an API Key. Visit support.mburger.cloud for more informations" ); + } + + return MBurgerInstance( + axios.create({ + baseURL: host, + headers: { + ...{ "X-MBurger-Token": params.api_key }, + ...headers, + }, + }) + ); } export function MBurgerInstance(axiosInstance) { + /** + * Retrieve a single section. + * + * @constructor + * @param {integer} params.section_id - ID of the requested Section from MBurger. + * @param {string} params.locale - Country code of the required locale. + * @param {boolean} params.original_media=false - Indicate if you want the original media or the converted ones. + * @param {boolean} params.force_locale_fallback=false - Set the parameters force_locale_fallback as indicated in the documentation. + * @param {boolean} params.use_slug=false - Declare if you want to use the section slug instead of the ID to retrieve data. + * @returns {object} + * @example + * // Import MBurger SDK + * const mburger = require('mburger'); + * + * // Init the connection + * const instance = mburger.createClient({ + * api_key: '1234567890' + * }); + * + * // Retrieve data from the section 10088 + * instance.getSection({ + * section_id: 10088, + * locale: 'it', + * force_locale_fallback: false + * }).then(result => console.log(result)); + */ + async function getSection(params) { + if (!params.section_id) { + throw new TypeError("You have to speficy a section_id. Visit support.mburger.cloud for more informations"); + } - /** - * Retrieve a single section. - * - * @constructor - * @param {integer} params.section_id - ID of the requested Section from MBurger. - * @param {string} params.locale - Country code of the required locale. - * @param {boolean} params.original_media=false - Indicate if you want the original media or the converted ones. - * @param {boolean} params.force_locale_fallback=false - Set the parameters force_locale_fallback as indicated in the documentation. - * @param {boolean} params.use_slug=false - Declare if you want to use the section slug instead of the ID to retrieve data. - * @returns {object} - * @example - * // Import MBurger SDK - * const mburger = require('mburger'); - * - * // Init the connection - * const instance = mburger.createClient({ - * api_key: '1234567890' - * }); - * - * // Retrieve data from the section 10088 - * instance.getSection({ - * section_id: 10088, - * locale: 'it', - * force_locale_fallback: false - * }).then(result => console.log(result)); - */ - async function getSection(params) { - if (!params.section_id) { - throw new TypeError('You have to speficy a section_id. Visit support.mburger.cloud for more informations'); - } - - let path = 'sections/' + params.section_id; - - let query = { - 'include': 'elements' - }; - - if (params.locale) { - query.locale = params.locale; - } - - if (params.original_media) { - query.original_media = params.original_media; - } - - if (params.force_locale_fallback) { - query.force_locale_fallback = params.force_locale_fallback; - } - - if (params.use_slug) { - query.use_slug = params.use_slug; - } - - return new Promise((resolve) => { - axiosInstance.get(host + path, { - params: query, - headers: headers - }) - .then((response) => { - let section = {}; - section.body = {}; - section.meta = {} - - for (let key in response.data.body.elements) { - section.body[key] = response.data.body.elements[key].value; - } - - section.meta.id = response.data.body.id; - section.meta.updated_at = response.data.body.updated_at; - section.meta.available_at = response.data.body.available_at; - section.meta.order = response.data.body.order; - section.meta.in_evidence = response.data.body.in_evidence; - section.meta.visible = response.data.body.visible; - section.meta.all_locales = response.data.body.all_locales; - - resolve(section); - }, (error) => { - console.log(error); - throw new TypeError('Error #1 while executing Promise of getSection.'); - }); - }) + let path = "sections/" + params.section_id; + + let query = { + include: "elements", + }; + + if (params.locale) { + query.locale = params.locale; } - /** - * Retrieve a single block. - * - * @constructor - * @param {integer} params.block_id - ID of the requested Block. - * @param {string} params.locale - Country code of the required locale. - * @param {boolean} params.original_media=false - Indicate if you want the original media or the converted ones - * @param {object} params.extra_params={} - The parameters you want to pass to the MBurger params variable. Check our API Reference for more informations. - * @param {boolean} params.order_desc=true - Express if you want the data in ascendent or descendent order. - * @param {boolean} params.force_locale_fallback=false - Set the parameters force_locale_fallback as indicated in the documentation. - * @param {boolean} params.filter={} - Set the filters as indicated in the official documentation. - * @returns {object} - * @example - * // Import MBurger SDK - * const mburger = require('mburger'); - * - * // Init the connection - * const instance = mburger.createClient({ - * api_key: '1234567890' - * }); - * - * // Retrieve a specific block - * instance.getBlock({ - * section_id: 884, - * locale: 'it', - * original_media: false, - * filter: { - * 'value': 'chiave 1, chiave 2' - * } - * }).then(result => console.log(result)); - * - */ - async function getBlock(params) { - - if (!params.block_id) { - throw new TypeError('You have to speficy a block_id. Visit support.mburger.cloud for more informations'); - } - - let path = 'blocks/' + params.block_id + '/sections'; - - let query = { - include: 'elements', - sort: 'order', - }; - - if (params.force_locale_fallback) { - query.force_locale_fallback = params.force_locale_fallback - } - - if (params.locale) { - query.locale = params.locale - } - - if (params.original_media) { - query.original_media = params.original_media - } - - if (params.order_desc) { - query.sort = '-order'; - } - - if (params.filter) { - query.filter = []; - for (let [key, value] of Object.entries(params.filter)) { - query.filter[key] = value; - } - } + if (params.original_media) { + query.original_media = params.original_media; + } - if (params.extra_params) { - query = { - ...query, - ...params.extra_params - } - } - - return new Promise((resolve) => { - axiosInstance.get(host + path, { - params: query, - headers: headers, - paramsSerializer: params => { - return qs.stringify(params) - } - }) - .then((response) => { - let items = response.data.body.items.map(value => { - let section = {}; - section.body = {}; - section.meta = {} - - for (let key in value.elements) { - section.body[key] = value.elements[key].value; - } - - section.meta.id = value.id; - section.meta.updated_at = value.updated_at; - section.meta.available_at = value.available_at; - section.meta.order = value.order; - section.meta.in_evidence = value.in_evidence; - section.meta.visible = value.visible; - section.meta.all_locales = value.all_locales; - - return section; - }); - - resolve(items); - }, (error) => { - console.log(error); - throw new TypeError('Error #2 while executing Promise of getBlock.'); - }); - }) + if (params.force_locale_fallback) { + query.force_locale_fallback = params.force_locale_fallback; + } + + if (params.use_slug) { + query.use_slug = params.use_slug; } - /** - * Retrieve multiple blocks. - * - * @constructor - * @param {array} params.block_ids - ID of the requested Blocks. - * @param {string} params.locale - Country code of the required locale. - * @param {boolean} params.filter={} - Set the filters as indicated in the official documentation. - * @returns {object} - * @example - * // Import MBurger SDK - * const mburger = require('mburger'); - * - * // Init the connection - * const instance = mburger.createClient({ - * api_key: '123457890' - * }); - * - * // Retrieve data from the blocks 798 and 799 - * instance.getBlocks({ - * block_ids: [798, 799], - * locale: 'it', - * filter: { - * 'value': 'chiave 1, chiave 2' - * } - * }).then(result => console.log(result)); - * - */ - async function getBlocks(params) { - - let path = 'blocks'; - - if (!params.block_ids) { - throw new TypeError('You have to speficy the block_ids value (array). Visit support.mburger.cloud for more informations'); - } - - let query = { - include: 'sections.elements', - sort: 'order', - }; - - if (params.locale) { - query.locale = params.locale - } - - if (params.filter) { - query.filter = []; - for (let [key, value] of Object.entries(params.filter)) { - query.filter[key] = value; + return new Promise((resolve, rejects) => { + axiosInstance + .get(host + path, { + params: query, + headers: headers, + }) + .then( + (response) => { + let section = {}; + section.body = {}; + section.meta = {}; + + for (let key in response.data.body.elements) { + section.body[key] = response.data.body.elements[key].value; } - } - if (!query.filter) { - query.filter = []; - } + section.meta.id = response.data.body.id; + section.meta.updated_at = response.data.body.updated_at; + section.meta.available_at = response.data.body.available_at; + section.meta.order = response.data.body.order; + section.meta.in_evidence = response.data.body.in_evidence; + section.meta.visible = response.data.body.visible; + section.meta.all_locales = response.data.body.all_locales; + + resolve(section); + }, + (error) => { + rejects(new TypeError("Error #1 while executing Promise of getSection.")); + } + ); + }); + } + + /** + * Retrieve a single block. + * + * @constructor + * @param {integer} params.block_id - ID of the requested Block. + * @param {string} params.locale - Country code of the required locale. + * @param {boolean} params.original_media=false - Indicate if you want the original media or the converted ones + * @param {object} params.extra_params={} - The parameters you want to pass to the MBurger params variable. Check our API Reference for more informations. + * @param {boolean} params.order_desc=true - Express if you want the data in ascendent or descendent order. + * @param {boolean} params.force_locale_fallback=false - Set the parameters force_locale_fallback as indicated in the documentation. + * @param {boolean} params.filter={} - Set the filters as indicated in the official documentation. + * @returns {object} + * @example + * // Import MBurger SDK + * const mburger = require('mburger'); + * + * // Init the connection + * const instance = mburger.createClient({ + * api_key: '1234567890' + * }); + * + * // Retrieve a specific block + * instance.getBlock({ + * section_id: 884, + * locale: 'it', + * original_media: false, + * filter: { + * 'value': 'chiave 1, chiave 2' + * } + * }).then(result => console.log(result)); + * + */ + async function getBlock(params) { + if (!params.block_id) { + throw new TypeError("You have to speficy a block_id. Visit support.mburger.cloud for more informations"); + } - query.filter['id'] = params.block_ids.join(); + let path = "blocks/" + params.block_id + "/sections"; - return new Promise((resolve) => { - axiosInstance.get(host + path, { - params: query, - headers: headers, - paramsSerializer: params => { - return qs.stringify(params) - } - }) - .then((response) => { - let blocks = response.data.body.items.map((block, i) => { - return block; - }); - - let out = {}; - let sections = blocks.map((value, i) => { - let sections = value.sections; - - return sections.map((value, i) => { - let item = {}; - item.body = {}; - item.meta = {}; - - for (let key in value.elements) { - item.body[key] = value.elements[key].value; - } - - item.meta = { - all_locales: value.all_locales, - available_at: value.available_at, - id: value.id, - in_evidence: value.in_evidence, - order: value.order, - updated_at: value.updated_at, - visible: value.visible, - } - - return item; - }); - }); - - let metas = blocks.map((value, i) => { - return { - 'id': value.id, - 'available_at': value.available_at, - 'updated_at': value.updated_at, - 'in_evidence': value.in_evidence, - 'order': value.order, - 'all_locales': value.all_locales - }; - }); - - for (let [i, value] of Object.entries(blocks)) { - out[value.title] = {}; - out[value.title]['body'] = sections[i]; - out[value.title]['meta'] = metas[i]; - } - - resolve(out); - }, (error) => { - console.log(error); - throw new TypeError('Error #3 while executing Promise of getBlocks.'); - }); + let query = { + include: "elements", + sort: "order", + }; + + if (params.force_locale_fallback) { + query.force_locale_fallback = params.force_locale_fallback; + } + + if (params.locale) { + query.locale = params.locale; + } + + if (params.original_media) { + query.original_media = params.original_media; + } + + if (params.order_desc) { + query.sort = "-order"; + } + + if (params.filter) { + query.filter = []; + for (let [key, value] of Object.entries(params.filter)) { + query.filter[key] = value; + } + } + + if (params.extra_params) { + query = { + ...query, + ...params.extra_params, + }; + } + + return new Promise((resolve, rejects) => { + axiosInstance + .get(host + path, { + params: query, + headers: headers, + paramsSerializer: (params) => { + return qs.stringify(params); + }, }) + .then( + (response) => { + let items = response.data.body.items.map((value) => { + let section = {}; + section.body = {}; + section.meta = {}; + + for (let key in value.elements) { + section.body[key] = value.elements[key].value; + } + + section.meta.id = value.id; + section.meta.updated_at = value.updated_at; + section.meta.available_at = value.available_at; + section.meta.order = value.order; + section.meta.in_evidence = value.in_evidence; + section.meta.visible = value.visible; + section.meta.all_locales = value.all_locales; + + return section; + }); + + resolve(items); + }, + (error) => { + rejects(new TypeError("Error #2 while executing Promise of getBlock.")); + } + ); + }); + } + + /** + * Retrieve multiple blocks. + * + * @constructor + * @param {array} params.block_ids - ID of the requested Blocks. + * @param {string} params.locale - Country code of the required locale. + * @param {boolean} params.filter={} - Set the filters as indicated in the official documentation. + * @returns {object} + * @example + * // Import MBurger SDK + * const mburger = require('mburger'); + * + * // Init the connection + * const instance = mburger.createClient({ + * api_key: '123457890' + * }); + * + * // Retrieve data from the blocks 798 and 799 + * instance.getBlocks({ + * block_ids: [798, 799], + * locale: 'it', + * filter: { + * 'value': 'chiave 1, chiave 2' + * } + * }).then(result => console.log(result)); + * + */ + async function getBlocks(params) { + let path = "blocks"; + + if (!params.block_ids) { + throw new TypeError( + "You have to speficy the block_ids value (array). Visit support.mburger.cloud for more informations" + ); + } + + let query = { + include: "sections.elements", + sort: "order", + }; + + if (params.locale) { + query.locale = params.locale; + } + + if (params.filter) { + query.filter = { id: params.block_ids.join() }; + for (let [key, value] of Object.entries(params.filter)) { + query.filter[key] = value; + } } - return { - getSection: getSection, - getBlock: getBlock, - getBlocks: getBlocks + if (!query.filter) { + query.filter = {}; } -} \ No newline at end of file + + query.filter["id"] = params.block_ids.join(); + + return new Promise((resolve, rejects) => { + axiosInstance + .get(host + path, { + params: query, + headers: headers, + paramsSerializer: (params) => { + return qs.stringify(params); + }, + }) + .then( + (response) => { + let blocks = response.data.body.items.map((block, i) => { + return block; + }); + + let out = {}; + let sections = blocks.map((value, i) => { + let sections = value.sections; + + return sections.map((value, i) => { + let item = {}; + item.body = {}; + item.meta = {}; + + for (let key in value.elements) { + item.body[key] = value.elements[key].value; + } + + item.meta = { + all_locales: value.all_locales, + available_at: value.available_at, + id: value.id, + in_evidence: value.in_evidence, + order: value.order, + updated_at: value.updated_at, + visible: value.visible, + }; + + return item; + }); + }); + + let metas = blocks.map((value, i) => { + return { + id: value.id, + available_at: value.available_at, + updated_at: value.updated_at, + in_evidence: value.in_evidence, + order: value.order, + all_locales: value.all_locales, + }; + }); + + for (let [i, value] of Object.entries(blocks)) { + out[value.title] = {}; + out[value.title]["body"] = sections[i]; + out[value.title]["meta"] = metas[i]; + } + + resolve(out); + }, + (error) => { + rejects(new TypeError("Error #3 while executing Promise of getBlocks.")); + } + ); + }); + } + + return { + getSection: getSection, + getBlock: getBlock, + getBlocks: getBlocks, + }; +}