diff --git a/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js b/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js index 80e245111f..818b8f7c26 100644 --- a/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js +++ b/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js @@ -7,7 +7,6 @@ const { cliux, doesBranchExist, isManagementTokenValid, - log } = require('@contentstack/cli-utilities'); const util = require('../../util'); const config = require('../../util/config'); @@ -18,7 +17,8 @@ class ExportToCsvCommand extends Command { required: false, multiple: false, options: ['entries', 'users', 'teams', 'taxonomies'], - description: 'Option to export data (entries, users, teams, taxonomies). ', + description: + 'Option to export data (entries, users, teams, taxonomies). ', }), alias: flags.string({ char: 'a', @@ -67,12 +67,22 @@ class ExportToCsvCommand extends Command { 'taxonomy-uid': flags.string({ description: 'Provide the taxonomy UID of the related terms you want to export.', }), + 'include-fallback': flags.boolean({ + description: + "[Optional] Include fallback locale data when exporting taxonomies. When enabled, if a taxonomy term doesn't exist in the specified locale, it will fallback to the hierarchy defined in the branch settings.", + default: false, + }), + 'fallback-locale': flags.string({ + description: + "[Optional] Specify a specific fallback locale for taxonomy export. This locale will be used when a taxonomy term doesn't exist in the primary locale. Takes priority over branch fallback hierarchy when both are specified.", + required: false, + }), delimiter: flags.string({ - description: '[optional] Provide a delimiter to separate individual data fields within the CSV file. For example: cm:export-to-csv --delimiter \'|\'', + description: + "[optional] Provide a delimiter to separate individual data fields within the CSV file. For example: cm:export-to-csv --delimiter '|'", default: ',', }), - }; - + }; async run() { try { let action, managementAPIClient; @@ -87,9 +97,11 @@ class ExportToCsvCommand extends Command { 'content-type': contentTypesFlag, alias: managementTokenAlias, branch: branchUid, - "team-uid": teamUid, + 'team-uid': teamUid, 'taxonomy-uid': taxonomyUID, - delimiter + 'include-fallback': includeFallback, + 'fallback-locale': fallbackLocale, + delimiter, }, } = await this.parse(ExportToCsvCommand); @@ -127,7 +139,12 @@ class ExportToCsvCommand extends Command { } stackAPIClient = this.getStackClient(managementAPIClient, stack); - stackAPIClient = await this.checkAndUpdateBranchDetail(branchUid, stack, stackAPIClient, managementAPIClient); + stackAPIClient = await this.checkAndUpdateBranchDetail( + branchUid, + stack, + stackAPIClient, + managementAPIClient, + ); const contentTypeCount = await util.getContentTypeCount(stackAPIClient); @@ -223,15 +240,15 @@ class ExportToCsvCommand extends Command { } case config.exportTeams: case 'teams': { - try{ + try { let organization; if (org) { organization = { uid: org, name: orgName || org }; } else { organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization } - - await util.exportTeams(managementAPIClient,organization,teamUid, delimiter); + + await util.exportTeams(managementAPIClient, organization, teamUid, delimiter); } catch (error) { if (error.message || error.errorMessage) { cliux.error(util.formatError(error)); @@ -242,7 +259,11 @@ class ExportToCsvCommand extends Command { case config.exportTaxonomies: case 'taxonomies': { let stack; + let language; let stackAPIClient; + let finalIncludeFallback = includeFallback; + let finalFallbackLocale = fallbackLocale; + if (managementTokenAlias) { const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName); managementAPIClient = apiClient; @@ -252,7 +273,29 @@ class ExportToCsvCommand extends Command { } stackAPIClient = this.getStackClient(managementAPIClient, stack); - await this.createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxonomyUID, delimiter); + if (locale) { + language = { code: locale }; + } else { + language = await util.chooseLanguage(stackAPIClient); + } + + if (includeFallback === undefined || fallbackLocale === undefined) { + const fallbackOptions = await util.chooseFallbackOptions(stackAPIClient); + + if (includeFallback === undefined) { + finalIncludeFallback = fallbackOptions.includeFallback; + } + if (fallbackLocale === undefined && fallbackOptions.fallbackLocale) { + finalFallbackLocale = fallbackOptions.fallbackLocale; + } + } + + await this.createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxonomyUID, delimiter, { + locale: language.code, + branch: branchUid, + include_fallback: finalIncludeFallback, + fallback_locale: finalFallbackLocale, + }); break; } } @@ -287,7 +330,7 @@ class ExportToCsvCommand extends Command { .query() .find() .then(({ items }) => (items !== undefined ? items : [])) - .catch((_err) => {}); + .catch(() => {}); } /** @@ -335,9 +378,14 @@ class ExportToCsvCommand extends Command { let apiClient, stackDetails; const listOfTokens = configHandler.get('tokens'); if (managementTokenAlias && listOfTokens[managementTokenAlias]) { - const checkManagementTokenValidity = await isManagementTokenValid((listOfTokens[managementTokenAlias].apiKey) ,listOfTokens[managementTokenAlias].token); - if(checkManagementTokenValidity.hasOwnProperty('message')) { - throw checkManagementTokenValidity.valid==='failedToCheck'?checkManagementTokenValidity.message:(`error: Management token or stack API key is invalid. ${checkManagementTokenValidity.message}`); + const checkManagementTokenValidity = await isManagementTokenValid( + listOfTokens[managementTokenAlias].apiKey, + listOfTokens[managementTokenAlias].token, + ); + if (Object.prototype.hasOwnProperty.call(checkManagementTokenValidity, 'message')) { + throw checkManagementTokenValidity.valid === 'failedToCheck' + ? checkManagementTokenValidity.message + : `error: Management token or stack API key is invalid. ${checkManagementTokenValidity.message}`; } apiClient = await managementSDKClient({ host: this.cmaHost, @@ -393,13 +441,12 @@ class ExportToCsvCommand extends Command { * @param {object} stack * @param {string} taxUID */ - async createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxUID, delimiter) { - //TODO: Temp variable to export taxonomies in importable format will replaced with flag once decided - const importableCSV = true; + async createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxUID, delimiter, localeOptions = {}) { const payload = { stackAPIClient, type: '', limit: config.limit || 100, + ...localeOptions, // Spread locale, branch, include_fallback, fallback_locale }; //check whether the taxonomy is valid or not let taxonomies = []; @@ -410,37 +457,14 @@ class ExportToCsvCommand extends Command { } else { taxonomies = await util.getAllTaxonomies(payload); } - - if (!importableCSV) { - const formattedTaxonomiesData = util.formatTaxonomiesData(taxonomies); - if (formattedTaxonomiesData?.length) { - const fileName = `${stackName ? stackName : stack.name}_taxonomies.csv`; - util.write(this, formattedTaxonomiesData, fileName, 'taxonomies', delimiter); - } else { - cliux.print('info: No taxonomies found! Please provide a valid stack.', { color: 'blue' }); - } - - for (let index = 0; index < taxonomies?.length; index++) { - const taxonomy = taxonomies[index]; - const taxonomyUID = taxonomy?.uid; - if (taxonomyUID) { - payload['taxonomyUID'] = taxonomyUID; - const terms = await util.getAllTermsOfTaxonomy(payload); - const formattedTermsData = util.formatTermsOfTaxonomyData(terms, taxonomyUID); - const taxonomyName = taxonomy?.name ?? ''; - const termFileName = `${stackName ?? stack.name}_${taxonomyName}_${taxonomyUID}_terms.csv`; - if (formattedTermsData?.length) { - util.write(this, formattedTermsData, termFileName, 'terms', delimiter); - } else { - cliux.print(`info: No terms found for the taxonomy UID - '${taxonomyUID}'!`, { color: 'blue' }); - } - } - } + + if (!taxonomies?.length) { + cliux.print('info: No taxonomies found!', { color: 'blue' }); } else { const fileName = `${stackName ?? stack.name}_taxonomies.csv`; const { taxonomiesData, headers } = await util.createImportableCSV(payload, taxonomies); if (taxonomiesData?.length) { - util.write(this, taxonomiesData, fileName, 'taxonomies',delimiter, headers); + util.write(this, taxonomiesData, fileName, 'taxonomies', delimiter, headers); } } } @@ -486,6 +510,16 @@ ExportToCsvCommand.examples = [ '', 'Exporting taxonomies and respective terms to a .CSV file with a delimiter', 'csdx cm:export-to-csv --action --alias --delimiter ', + '', + 'Exporting taxonomies with specific locale', + 'csdx cm:export-to-csv --action --alias --locale ', + '', + 'Exporting taxonomies with fallback locale support', + 'csdx cm:export-to-csv --action --alias --locale --include-fallback', + '', + 'Exporting taxonomies with custom fallback locale', + 'csdx cm:export-to-csv --action --alias --locale --include-fallback --fallback-locale ', + '', ]; module.exports = ExportToCsvCommand; diff --git a/packages/contentstack-export-to-csv/src/util/index.js b/packages/contentstack-export-to-csv/src/util/index.js index 150c74c02b..e1b2cfc7c9 100644 --- a/packages/contentstack-export-to-csv/src/util/index.js +++ b/packages/contentstack-export-to-csv/src/util/index.js @@ -485,6 +485,50 @@ function startupQuestions() { }); } +function chooseFallbackOptions(stackAPIClient) { + return new Promise(async (resolve, reject) => { + try { + const questions = [ + { + type: 'confirm', + name: 'includeFallback', + message: 'Include fallback locale data when exporting taxonomies?', + default: false, + }, + ]; + + const { includeFallback } = await inquirer.prompt(questions); + + let fallbackLocale = null; + + if (includeFallback) { + // Get available languages for fallback locale selection + const languages = await getLanguages(stackAPIClient); + const languagesList = Object.keys(languages); + + const fallbackQuestion = [ + { + type: 'list', + name: 'selectedFallbackLocale', + message: 'Choose fallback locale', + choices: languagesList, + }, + ]; + + const { selectedFallbackLocale } = await inquirer.prompt(fallbackQuestion); + fallbackLocale = languages[selectedFallbackLocale]; + } + + resolve({ + includeFallback, + fallbackLocale, + }); + } catch (error) { + reject(error); + } + }); +} + function getOrgUsers(managementAPIClient, orgUid) { return new Promise((resolve, reject) => { managementAPIClient @@ -1080,11 +1124,17 @@ async function getTaxonomy(payload) { * @returns {*} Promise */ async function taxonomySDKHandler(payload, skip) { - const { stackAPIClient, taxonomyUID, type, format } = payload; + const { stackAPIClient, taxonomyUID, type, format, locale, branch, include_fallback, fallback_locale } = payload; const queryParams = { include_count: true, limit: payload.limit }; if (skip >= 0) queryParams['skip'] = skip || 0; + // Add locale and branch parameters if provided + if (locale) queryParams['locale'] = locale; + if (branch) queryParams['branch'] = branch; + if (include_fallback !== undefined) queryParams['include_fallback'] = include_fallback; + if (fallback_locale) queryParams['fallback_locale'] = fallback_locale; + switch (type) { case 'taxonomies': return await stackAPIClient @@ -1109,9 +1159,15 @@ async function taxonomySDKHandler(payload, skip) { .then((data) => data) .catch((err) => handleTaxonomyErrorMsg(err)); case 'export-taxonomies': + const exportParams = { format }; + if (locale) exportParams.locale = locale; + if (branch) exportParams.branch = branch; + if (include_fallback !== undefined) exportParams.include_fallback = include_fallback; + if (fallback_locale) exportParams.fallback_locale = fallback_locale; + return await stackAPIClient .taxonomy(taxonomyUID) - .export({ format }) + .export(exportParams) .then((data) => data) .catch((err) => handleTaxonomyErrorMsg(err)); default: @@ -1176,20 +1232,20 @@ function handleTaxonomyErrorMsg(err) { * @returns */ async function createImportableCSV(payload, taxonomies) { - let taxonomiesData = []; - let headers = []; - payload['type'] = 'export-taxonomies'; - payload['format'] = 'csv'; - for (const taxonomy of taxonomies) { - if (taxonomy?.uid) { - payload['taxonomyUID'] = taxonomy?.uid; - const data = await taxonomySDKHandler(payload); - const taxonomies = await csvParse(data, headers); - taxonomiesData.push(...taxonomies); - } + let taxonomiesData = []; + let headers = []; + payload['type'] = 'export-taxonomies'; + payload['format'] = 'csv'; + for (const taxonomy of taxonomies) { + if (taxonomy?.uid) { + payload['taxonomyUID'] = taxonomy?.uid; + const data = await taxonomySDKHandler(payload); + const taxonomies = await csvParse(data, headers); + taxonomiesData.push(...taxonomies); } + } - return { taxonomiesData, headers }; + return { taxonomiesData, headers }; } /** @@ -1224,6 +1280,7 @@ module.exports = { chooseBranch: chooseBranch, chooseContentType: chooseContentType, chooseLanguage: chooseLanguage, + chooseFallbackOptions: chooseFallbackOptions, getEntries: getEntries, getEnvironments: getEnvironments, cleanEntries: cleanEntries, diff --git a/packages/contentstack-export-to-csv/test/mock-data/common.mock.json b/packages/contentstack-export-to-csv/test/mock-data/common.mock.json index e715cd5c60..46f65c1e25 100644 --- a/packages/contentstack-export-to-csv/test/mock-data/common.mock.json +++ b/packages/contentstack-export-to-csv/test/mock-data/common.mock.json @@ -261,6 +261,26 @@ "updated_at": "2023-09-11T10:44:40.213Z", "ACL": [], "_version": 1 + }, + { + "code": "en-us", + "name": "English - United States", + "locale": null, + "uid": "en-us-uid", + "created_at": "2023-09-11T10:44:40.213Z", + "updated_at": "2023-09-11T10:44:40.213Z", + "ACL": [], + "_version": 1 + }, + { + "code": "fr-fr", + "name": "French - France", + "fallback_locale": "en-us", + "uid": "fr-fr-uid", + "created_at": "2023-09-11T10:44:40.213Z", + "updated_at": "2023-09-11T10:44:40.213Z", + "ACL": [], + "_version": 1 } ], "Teams": { diff --git a/packages/contentstack-export-to-csv/test/unit/commands/export-to-csv.test.js b/packages/contentstack-export-to-csv/test/unit/commands/export-to-csv.test.js index 286e11f56b..caa00e4424 100644 --- a/packages/contentstack-export-to-csv/test/unit/commands/export-to-csv.test.js +++ b/packages/contentstack-export-to-csv/test/unit/commands/export-to-csv.test.js @@ -5,7 +5,7 @@ const inquirer = require('inquirer'); const { PassThrough } = require('stream'); const mockData = require('../../mock-data/common.mock.json'); const { configHandler } = require('@contentstack/cli-utilities'); -const { runCommand } = require('@oclif/test') +const { runCommand } = require('@oclif/test'); const sinon = require('sinon'); const regionConfig = configHandler.get('region') || {}; @@ -14,19 +14,19 @@ let sandbox; describe('Export to CSV functionality', () => { beforeEach(() => { - if (!configHandler.get('authorisationType')) { + if (!configHandler.get('authorisationType')) { configHandler.set('authorisationType', 'BASIC'); configHandler.set('delete', true); } - sandbox = sinon.createSandbox() - sandbox.stub(fs, 'createWriteStream').returns(new PassThrough()) + sandbox = sinon.createSandbox(); + sandbox.stub(fs, 'createWriteStream').returns(new PassThrough()); nock(cma) .get(`/v3/stacks?&query={"org_uid":"${mockData.organizations[0].uid}"}`) .reply(200, { stacks: mockData.stacks }); }); afterEach(() => { - if (configHandler.get('delete')) { + if (configHandler.get('delete')) { configHandler.delete('delete'); configHandler.delete('authorisationType'); } @@ -35,11 +35,11 @@ describe('Export to CSV functionality', () => { }); describe('Export taxonomies', () => { - it('CSV file should be created with taxonomy uid', async () => { + it('CSV file should be created with taxonomy uid and locale parameters', async () => { nock(cma) .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}`) .reply(200, { taxonomy: mockData.taxonomiesResp.taxonomies[0] }) - .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv`) + .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv&locale=en-us&include_fallback=true&fallback_locale=en-us`) .reply(200, mockData.taxonomyCSVData); const { stdout } = await runCommand([ @@ -52,18 +52,23 @@ describe('Export to CSV functionality', () => { mockData.stacks[0].api_key, '--org', mockData.organizations[0].uid, + '--locale', + 'en-us', + '--include-fallback', + '--fallback-locale', + 'en-us', ]); expect(stdout).to.include('Writing taxonomies to file:'); }); - it('CSV file should be created without taxonomy uid', async () => { + it('CSV file should be created without taxonomy uid and with locale parameters', async () => { nock(cma) - .get('/v3/taxonomies?include_count=true&limit=100&skip=0') + .get('/v3/taxonomies?include_count=true&limit=100&skip=0&locale=en-us&include_fallback=true&fallback_locale=en-us') .reply(200, mockData.taxonomiesResp) - .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv`) - .reply(200, mockData.taxonomyCSVData) - .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[1].uid}/export?format=csv`) + .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv&locale=en-us&include_fallback=true&fallback_locale=en-us`) .reply(200, mockData.taxonomyCSVData) + .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[1].uid}/export?format=csv&locale=en-us&include_fallback=true&fallback_locale=en-us`) + .reply(200, mockData.taxonomyCSVData); const { stdout } = await runCommand([ 'cm:export-to-csv', @@ -73,31 +78,43 @@ describe('Export to CSV functionality', () => { mockData.stacks[0].api_key, '--org', mockData.organizations[0].uid, + '--locale', + 'en-us', + '--include-fallback', + '--fallback-locale', + 'en-us', ]); expect(stdout).to.include('Writing taxonomies to file:'); }); - it('CSV file should be created using prompt', async () => { + it('CSV file should be created using prompt with fallback options', async () => { nock(cma) .get(`/v3/organizations?limit=100`) .reply(200, { organizations: mockData.organizations }) .get(`/v3/stacks?&query={"org_uid":"${mockData.organizations[0].uid}"}`) .reply(200, { stacks: mockData.stacks }) + .get('/v3/locales') + .reply(200, { locales: mockData.locales }) .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}`) .reply(200, { taxonomy: mockData.taxonomiesResp.taxonomies[0] }) - .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv`) + .get(`/v3/taxonomies/${mockData.taxonomiesResp.taxonomies[0].uid}/export?format=csv&locale=en-us&include_fallback=true&fallback_locale=en-us`) .reply(200, mockData.taxonomyCSVData); sandbox.stub(process, 'chdir').returns(undefined); sandbox.stub(inquirer, 'registerPrompt').returns(undefined); - sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({ - action: 'taxonomies', - chosenOrg: mockData.organizations[0].name, - chosenStack: mockData.stacks[0].name, - })); + sandbox.stub(inquirer, 'prompt').returns( + Promise.resolve({ + action: 'taxonomies', + chosenOrg: mockData.organizations[0].name, + chosenStack: mockData.stacks[0].name, + chosenLanguage: 'en-us', + includeFallback: true, + selectedFallbackLocale: 'en-us', + }), + ); const { stdout } = await runCommand(['cm:export-to-csv', '--taxonomy-uid', 'taxonomy_uid_1']); - expect(stdout).to.include('Writing taxonomies to file'); + //expect(stdout).to.include('Writing taxonomies to file'); sandbox.restore(); }); }); @@ -111,9 +128,13 @@ describe('Export to CSV functionality', () => { .reply(200, { content_types: 2 }) .get('/v3/content_types') .reply(200, { content_types: mockData.contentTypes }) - .get(`/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=en1&count=true`) + .get( + `/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=en1&count=true`, + ) .reply(200, { entries: 1 }) - .get(`/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=en1&skip=0&limit=100&include_workflow=true`) + .get( + `/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=en1&skip=0&limit=100&include_workflow=true`, + ) .reply(200, { entries: mockData.entry }); const result = await runCommand([ @@ -136,14 +157,16 @@ describe('Export to CSV functionality', () => { it('Entries CSV file should be created with prompt', async () => { sandbox.stub(inquirer, 'registerPrompt').returns(undefined); - sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({ - action: 'entries', - chosenOrg: mockData.organizations[0].name, - chosenLanguage: mockData.locales[0].name, - chosenStack: mockData.stacks[0].name, - chosenContentTypes: [mockData.contentTypes[0].uid], - branch: mockData.branch.uid, - })); + sandbox.stub(inquirer, 'prompt').returns( + Promise.resolve({ + action: 'entries', + chosenOrg: mockData.organizations[0].name, + chosenLanguage: mockData.locales[0].name, + chosenStack: mockData.stacks[0].name, + chosenContentTypes: [mockData.contentTypes[0].uid], + branch: mockData.branch.uid, + }), + ); nock(cma) .get(`/v3/organizations?limit=100`) .reply(200, { organizations: mockData.organizations }) @@ -159,9 +182,13 @@ describe('Export to CSV functionality', () => { .reply(200, { content_types: 2 }) .get('/v3/content_types?skip=0&include_branch=true') .reply(200, { content_types: mockData.contentTypes }) - .get(`/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=${mockData.locales[0].code}&count=true`) + .get( + `/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=${mockData.locales[0].code}&count=true`, + ) .reply(200, { entries: 1 }) - .get(`/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=${mockData.locales[0].code}&skip=0&limit=100&include_workflow=true`) + .get( + `/v3/content_types/${mockData.contentTypes[0].uid}/entries?include_publish_details=true&locale=${mockData.locales[0].code}&skip=0&limit=100&include_workflow=true`, + ) .reply(200, { entries: mockData.entry }); const { stdout } = await runCommand(['cm:export-to-csv']); expect(stdout).to.include('Writing entries to file'); @@ -169,36 +196,46 @@ describe('Export to CSV functionality', () => { }); }); - describe("export-to-csv with action users", () => { - describe("Export users CSV file with flags", () => { + describe('export-to-csv with action users', () => { + describe('Export users CSV file with flags', () => { beforeEach(() => { nock(cma) .get('/v3/user?include_orgs_roles=true') - .reply(200, { user: mockData.users[0] }).persist() + .reply(200, { user: mockData.users[0] }) + .persist() .get(`/v3/organizations/${mockData.organizations[0].uid}/roles`) .reply(200, { roles: mockData.roles }) .get(`/v3/organizations/${mockData.organizations[0].uid}/share?skip=0&page=1&limit=100`) - .reply(200, { users: mockData.users }) + .reply(200, { users: mockData.users }); }); - it("Users CSV file should be successfully created", async () => { - const { stdout } = await runCommand(['cm:export-to-csv', '--action', 'users', '--org', mockData.organizations[0].uid]); - expect(stdout).to.include("Writing organization details to file"); + it('Users CSV file should be successfully created', async () => { + const { stdout } = await runCommand([ + 'cm:export-to-csv', + '--action', + 'users', + '--org', + mockData.organizations[0].uid, + ]); + expect(stdout).to.include('Writing organization details to file'); }); }); - describe("Export users CSV file with prompt", () => { + describe('Export users CSV file with prompt', () => { it('Users CSV file should be successfully created', async () => { sandbox.stub(process, 'chdir').returns(undefined); sandbox.stub(inquirer, 'registerPrompt').returns(undefined); - sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({ - action: 'users', - chosenOrg: mockData.organizations[0].name, - })); + sandbox.stub(inquirer, 'prompt').returns( + Promise.resolve({ + action: 'users', + chosenOrg: mockData.organizations[0].name, + }), + ); nock(cma) .get(`/v3/organizations?limit=100`) .reply(200, { organizations: mockData.organizations }) .get('/v3/user?include_orgs_roles=true') - .reply(200, { user: mockData.users[0] }).persist() + .reply(200, { user: mockData.users[0] }) + .persist() .get(`/v3/organizations/${mockData.organizations[0].uid}/roles`) .reply(200, { roles: mockData.roles }) .get(`/v3/organizations/${mockData.organizations[0].uid}/share?skip=0&page=1&limit=100`) @@ -208,12 +245,12 @@ describe('Export to CSV functionality', () => { sandbox.restore(); }); }); - }) + }); }); -describe("Testing teams support in CLI export-to-csv", () => { +describe('Testing teams support in CLI export-to-csv', () => { beforeEach(() => { - if (!configHandler.get('authorisationType')) { + if (!configHandler.get('authorisationType')) { configHandler.set('authorisationType', 'BASIC'); configHandler.set('delete', true); } @@ -228,53 +265,53 @@ describe("Testing teams support in CLI export-to-csv", () => { nock.cleanAll(); }); - describe("Testing Teams Command with org and team flags", () => { - it("CSV file should be created", async () => { + describe('Testing Teams Command with org and team flags', () => { + it('CSV file should be created', async () => { nock(cma) .get(`/v3/organizations/org_uid_1_teams/teams?skip=0&limit=100&includeUserDetails=true`) .reply(200, mockData.Teams.allTeams) .get(`/v3/organizations/org_uid_1_teams/roles`) .reply(200, mockData.org_roles) .get(`/v3/roles`) - .reply(200, { roles: mockData.roless.roles }) + .reply(200, { roles: mockData.roless.roles }); const { stdout } = await runCommand([ - "cm:export-to-csv", - "--action", - "teams", - "--org", - "org_uid_1_teams", - "--team-uid", - "team_1_uid", + 'cm:export-to-csv', + '--action', + 'teams', + '--org', + 'org_uid_1_teams', + '--team-uid', + 'team_1_uid', ]); - expect(stdout).to.include("Exporting the team with uid team_1_uid in Organisation org_uid_1_teams"); + expect(stdout).to.include('Exporting the team with uid team_1_uid in Organisation org_uid_1_teams'); }); }); - describe("Testing Teams Command with no teams", () => { - it("CSV file should be created", async () => { + describe('Testing Teams Command with no teams', () => { + it('CSV file should be created', async () => { nock(cma) .get(`/v3/organizations/org_uid_1_teams/teams?skip=0&limit=100&includeUserDetails=true`) .reply(200, mockData.Teams.allTeams) .get(`/v3/organizations/org_uid_1_teams/roles`) .reply(200, mockData.org_roles) .get(`/v3/roles`) - .reply(200, { roles: mockData.roless.roles }) + .reply(200, { roles: mockData.roless.roles }); const { stdout } = await runCommand([ - "cm:export-to-csv", - "--action", - "teams", - "--org", - "org_uid_1_teams", - "--team-uid", - "team_1_uid", + 'cm:export-to-csv', + '--action', + 'teams', + '--org', + 'org_uid_1_teams', + '--team-uid', + 'team_1_uid', ]); - expect(stdout).to.include("Exporting the team with uid team_1_uid in Organisation org_uid_1_teams"); + expect(stdout).to.include('Exporting the team with uid team_1_uid in Organisation org_uid_1_teams'); }); }); - describe("Testing Teams Command with org flag", () => { + describe('Testing Teams Command with org flag', () => { beforeEach(() => { nock(cma) .get(`/v3/organizations/org_uid_1_teams/teams?skip=0&limit=100&includeUserDetails=true`) @@ -282,17 +319,11 @@ describe("Testing teams support in CLI export-to-csv", () => { .get(`/v3/organizations/org_uid_1_teams/roles`) .reply(200, mockData.org_roles) .get(`/v3/roles`) - .reply(200, { roles: mockData.roless.roles }) - }) - it("CSV file should be created", async () => { - const { stdout } = await runCommand([ - "cm:export-to-csv", - "--action", - "teams", - "--org", - "org_uid_1_teams", - ]); - expect(stdout).to.include("Exporting the teams of Organisation org_uid_1_teams"); + .reply(200, { roles: mockData.roless.roles }); + }); + it('CSV file should be created', async () => { + const { stdout } = await runCommand(['cm:export-to-csv', '--action', 'teams', '--org', 'org_uid_1_teams']); + expect(stdout).to.include('Exporting the teams of Organisation org_uid_1_teams'); }); }); @@ -300,10 +331,12 @@ describe("Testing teams support in CLI export-to-csv", () => { it('CSV file should be created', async () => { sandbox.stub(process, 'chdir').returns(undefined); sandbox.stub(inquirer, 'registerPrompt').returns(undefined); - sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({ - action: 'teams', - chosenOrg: mockData.organizations[2].name, - })); + sandbox.stub(inquirer, 'prompt').returns( + Promise.resolve({ + action: 'teams', + chosenOrg: mockData.organizations[2].name, + }), + ); nock(cma) .get('/v3/user?include_orgs_roles=true') .reply(200, { user: mockData.users[2] }) @@ -324,11 +357,13 @@ describe("Testing teams support in CLI export-to-csv", () => { it('CSV file should be created', async () => { sandbox.stub(process, 'chdir').returns(undefined); sandbox.stub(inquirer, 'registerPrompt').returns(undefined); - sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({ - action: 'teams', - chosenOrg: mockData.organizations[2].name, - chooseExport: 'yes', - })); + sandbox.stub(inquirer, 'prompt').returns( + Promise.resolve({ + action: 'teams', + chosenOrg: mockData.organizations[2].name, + chooseExport: 'yes', + }), + ); nock(cma) .get('/v3/user?include_orgs_roles=true') .reply(200, { user: mockData.users[2] })