From 010f95065ae385723fb172c791f2ab2bc298e1d7 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 19 Jan 2024 18:51:31 +0530 Subject: [PATCH 1/2] test: Added locale test suit and delete test suit to sanity folder --- package.json | 5 +- sanity-report.mjs | 31 ++++--- test/sanity-check/api/create-test.js | 0 test/sanity-check/api/delete-test.js | 68 ++++++++++++++ test/sanity-check/api/locale-test.js | 127 +++++++++++++++++++++++++++ test/sanity-check/sanity.js | 2 + 6 files changed, 221 insertions(+), 12 deletions(-) create mode 100644 test/sanity-check/api/create-test.js create mode 100644 test/sanity-check/api/delete-test.js create mode 100644 test/sanity-check/api/locale-test.js diff --git a/package.json b/package.json index 6eede519..d4b6711b 100644 --- a/package.json +++ b/package.json @@ -30,8 +30,8 @@ "buildnativescript": "webpack --config webpack/webpack.nativescript.js --mode production", "buildweb": "webpack --config webpack/webpack.web.js --mode production", "test": "npm run test:api && npm run test:unit", - "test:sanity": "BABEL_ENV=test nyc --reporter=html --reporter=text mocha --require @babel/register ./test/sanity-check/sanity.js -t 30000 --reporter mochawesome --require babel-polyfill", - "test:sanity-report": "node sanity-report.mjs", + "test:sanity": "BABEL_ENV=test nyc --reporter=html mocha --require @babel/register ./test/sanity-check/sanity.js -t 30000 --reporter mochawesome --require babel-polyfill --reporter-options reportDir=mochawesome-report,reportFilename=mochawesome.json && marge mochawesome-report/mochawesome.json -f sanity-report.html --inline", + "test:sanity-report": "marge mochawesome-report/mochawesome.json -f sanity-report.html --inline && node sanity-report.mjs", "test:api": "BABEL_ENV=test nyc --reporter=html --reporter=text mocha --require @babel/register ./test/test.js -t 30000 --reporter mochawesome --require babel-polyfill", "test:unit": "BABEL_ENV=test nyc --reporter=html --reporter=text mocha --require @babel/register ./test/unit/index.js -t 30000 --reporter mochawesome --require babel-polyfill", "test:unit:report:json": "BABEL_ENV=test nyc --reporter=clover --reporter=text mocha --require @babel/register ./test/unit/index.js -t 30000 --reporter json --reporter-options output=report.json --require babel-polyfill", @@ -92,6 +92,7 @@ "jest": "^28.1.0", "jsdoc": "^4.0.2", "mocha": "^9.2.2", + "mocha-html-reporter": "^0.0.1", "mochawesome": "^7.1.3", "multiparty": "^4.2.3", "nock": "^10.0.6", diff --git a/sanity-report.mjs b/sanity-report.mjs index 224457ae..2014ae87 100644 --- a/sanity-report.mjs +++ b/sanity-report.mjs @@ -6,29 +6,32 @@ dotenv.config() const mochawesomeJsonOutput = fs.readFileSync('./mochawesome-report/mochawesome.json', 'utf-8') const mochawesomeReport = JSON.parse(mochawesomeJsonOutput) +const report = `./mochawesome-report/sanity-report.html` const totalSuites = mochawesomeReport.stats.suites const totalTests = mochawesomeReport.stats.tests const passedTests = mochawesomeReport.stats.passes const failedTests = mochawesomeReport.stats.failures const pendingTests = mochawesomeReport.stats.pending -const durationInSeconds = mochawesomeReport.stats.duration / 1000 +let durationInSeconds = mochawesomeReport.stats.duration / 1000 +const durationInMinutes = Math.floor(durationInSeconds / 60) +durationInSeconds %= 60 console.log(`Total Suites: ${totalSuites}`) console.log(`Total Tests: ${totalTests}`) console.log(`Passed Tests: ${passedTests}`) console.log(`Failed Tests: ${failedTests}`) console.log(`Pending Tests: ${pendingTests}`) -console.log(`Total Duration: ${durationInSeconds.toFixed(2)} seconds`) +console.log(`Total Duration: ${durationInMinutes}m ${durationInSeconds.toFixed(2)}s`) const slackMessage = ` *Test Summary* -Total Suites: ${totalSuites} -Total Tests: ${totalTests} -Passed Tests: ${passedTests} -Failed Tests: ${failedTests} -Pending Tests: ${pendingTests} -Total Duration: ${durationInSeconds.toFixed(2)} seconds +• Total Suites: *${totalSuites}* +• Total Tests: *${totalTests}* +• Passed Tests: *${passedTests}* +• Failed Tests: *${failedTests}* +• Pending Tests: *${pendingTests}* +• Total Duration: *${durationInMinutes}m ${durationInSeconds.toFixed(2)}s* ` const app = new Slack.App({ @@ -36,12 +39,20 @@ const app = new Slack.App({ signingSecret: process.env.SLACK_SIGNING_SECRET }) -async function publishMessage (text) { +async function publishMessage (text, report) { await app.client.chat.postMessage({ token: process.env.SLACK_BOT_TOKEN, channel: process.env.SLACK_CHANNEL, text: text }) + await app.client.files.upload({ + token: process.env.SLACK_BOT_TOKEN, + channels: process.env.SLACK_CHANNEL, + initial_comment: '*Here is the report generated*', + filetype: 'html', + filename: 'sanity-report.html', + file: fs.createReadStream(report) + }) } -publishMessage(slackMessage) +publishMessage(slackMessage, report) diff --git a/test/sanity-check/api/create-test.js b/test/sanity-check/api/create-test.js new file mode 100644 index 00000000..e69de29b diff --git a/test/sanity-check/api/delete-test.js b/test/sanity-check/api/delete-test.js new file mode 100644 index 00000000..de5f8739 --- /dev/null +++ b/test/sanity-check/api/delete-test.js @@ -0,0 +1,68 @@ +import { expect } from 'chai' +import { describe, it, setup } from 'mocha' +import { jsonReader } from '../utility/fileOperations/readwrite' +import { environmentCreate, environmentProdCreate } from '../mock/environment.js' +import { contentstackClient } from '../utility/ContentstackClient.js' + +let client = {} + +describe('Delete Environment api Test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + it('should delete an environment', done => { + makeEnvironment(environmentCreate.environment.name) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Environment deleted successfully.') + done() + }) + .catch(done) + }) + + it('should delete the prod environment', done => { + makeEnvironment(environmentProdCreate.environment.name) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Environment deleted successfully.') + done() + }) + .catch(done) + }) +}) + +describe('Delete Locale api Test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + + it('should delete language: Hindi - India', done => { + makeLocale('hi-in') + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Language removed successfully.') + done() + }) + .catch(done) + }) + + it('should delete language: English - Austria', done => { + makeLocale('en-at') + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Language removed successfully.') + done() + }) + .catch(done) + }) +}) + +function makeEnvironment (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).environment(uid) +} + +function makeLocale (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).locale(uid) +} diff --git a/test/sanity-check/api/locale-test.js b/test/sanity-check/api/locale-test.js new file mode 100644 index 00000000..913a5188 --- /dev/null +++ b/test/sanity-check/api/locale-test.js @@ -0,0 +1,127 @@ +import { expect } from 'chai' +import { describe, it, setup } from 'mocha' +import { jsonReader } from '../utility/fileOperations/readwrite' +import { contentstackClient } from '../utility/ContentstackClient.js' + +let client = {} + +describe('Locale api Test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + + it('should add a language English - Austria', done => { + makeLocale() + .create({ locale: { code: 'en-at' } }) + .then((locale) => { + expect(locale.code).to.be.equal('en-at') + expect(locale.name).to.be.equal('English - Austria') + expect(locale.fallback_locale).to.be.equal('en-us') + expect(locale.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should add a language Hindi - India', done => { + makeLocale() + .create({ locale: { code: 'hi-in' } }) + .then((locale) => { + expect(locale.code).to.be.equal('hi-in') + expect(locale.name).to.be.equal('Hindi - India') + expect(locale.fallback_locale).to.be.equal('en-us') + expect(locale.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should add a language Marathi - India with Fallback en-at', done => { + makeLocale() + .create({ locale: { code: 'mr-in', fallback_locale: 'en-at' } }) + .then((locale) => { + expect(locale.code).to.be.equal('mr-in') + expect(locale.name).to.be.equal('Marathi - India') + expect(locale.fallback_locale).to.be.equal('en-at') + expect(locale.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should get a all languages', done => { + makeLocale() + .query() + .find() + .then((locales) => { + locales.items.forEach((locale) => { + expect(locale.code).to.be.not.equal(null) + expect(locale.name).to.be.not.equal(null) + expect(locale.uid).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + + it('should query a language Hindi - India', done => { + makeLocale() + .query({ query: { name: 'Hindi - India' } }) + .find() + .then((locales) => { + locales.items.forEach((locale) => { + expect(locale.code).to.be.equal('hi-in') + expect(locale.name).to.be.equal('Hindi - India') + expect(locale.fallback_locale).to.be.equal('en-us') + expect(locale.uid).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + + it('should get a language Hindi - India', done => { + makeLocale('hi-in') + .fetch() + .then((locale) => { + expect(locale.code).to.be.equal('hi-in') + expect(locale.name).to.be.equal('Hindi - India') + expect(locale.fallback_locale).to.be.equal('en-us') + expect(locale.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should get and update a language Hindi - India', done => { + makeLocale('hi-in') + .fetch() + .then((locale) => { + locale.fallback_locale = 'en-at' + return locale.update() + }) + .then((locale) => { + expect(locale.code).to.be.equal('hi-in') + expect(locale.name).to.be.equal('Hindi - India') + expect(locale.fallback_locale).to.be.equal('en-at') + expect(locale.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should delete language: Hindi - India', done => { + makeLocale('mr-in') + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Language removed successfully.') + done() + }) + .catch(done) + }) +}) + +function makeLocale (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).locale(uid) +} diff --git a/test/sanity-check/sanity.js b/test/sanity-check/sanity.js index 960b4567..13843af7 100644 --- a/test/sanity-check/sanity.js +++ b/test/sanity-check/sanity.js @@ -1,6 +1,7 @@ require('./api/user-test') require('./api/organization-test') require('./api/stack-test') +require('./api/locale-test') require('./api/environment-test') require('./api/contentType-test') require('./api/asset-test') @@ -10,3 +11,4 @@ require('./api/branchAlias-test') require('./api/contentType-delete-test') require('./api/taxonomy-test') require('./api/terms-test') +require('./api/delete-test') From 0c4641c3f6ece5bf6b1db6f00b79ad4aa4614b14 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 19 Jan 2024 19:03:20 +0530 Subject: [PATCH 2/2] chore: updated package-lock file --- package-lock.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/package-lock.json b/package-lock.json index 4c8de3e7..e723019e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,6 +45,7 @@ "jest": "^28.1.0", "jsdoc": "^4.0.2", "mocha": "^9.2.2", + "mocha-html-reporter": "^0.0.1", "mochawesome": "^7.1.3", "multiparty": "^4.2.3", "nock": "^10.0.6", @@ -10213,6 +10214,15 @@ "url": "https://opencollective.com/mochajs" } }, + "node_modules/mocha-html-reporter": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/mocha-html-reporter/-/mocha-html-reporter-0.0.1.tgz", + "integrity": "sha512-AqkAOBOm5/V9arNsb69H+5NhFKM8w/M00882czB9S07dc/eWhodBuGUcp6ZbZBapKZ+OY8YVv7+aAIWUuSeEoQ==", + "dev": true, + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/mocha/node_modules/cliui": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", @@ -21441,6 +21451,12 @@ } } }, + "mocha-html-reporter": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/mocha-html-reporter/-/mocha-html-reporter-0.0.1.tgz", + "integrity": "sha512-AqkAOBOm5/V9arNsb69H+5NhFKM8w/M00882czB9S07dc/eWhodBuGUcp6ZbZBapKZ+OY8YVv7+aAIWUuSeEoQ==", + "dev": true + }, "mochawesome": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/mochawesome/-/mochawesome-7.1.3.tgz",