diff --git a/test/sanity-check/api/managementToken-test.js b/test/sanity-check/api/managementToken-test.js new file mode 100644 index 00000000..b676b195 --- /dev/null +++ b/test/sanity-check/api/managementToken-test.js @@ -0,0 +1,146 @@ +import { expect } from 'chai' +import { describe, it, setup } from 'mocha' +import { jsonReader } from '../utility/fileOperations/readwrite.js' +import { createManagementToken, createManagementToken2 } from '../mock/managementToken.js' +import { contentstackClient } from '../utility/ContentstackClient.js' + +let client = {} + +let tokenUidProd = '' +let tokenUidDev = '' +describe('Management Token api Test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + + it('should add a Management Token', done => { + makeManagementToken() + .create(createManagementToken) + .then((token) => { + tokenUidDev = token.uid + expect(token.name).to.be.equal(createManagementToken.token.name) + expect(token.description).to.be.equal(createManagementToken.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should add a Management Token for production', done => { + makeManagementToken() + .create(createManagementToken2) + .then((token) => { + tokenUidProd = token.uid + expect(token.name).to.be.equal(createManagementToken2.token.name) + expect(token.description).to.be.equal(createManagementToken2.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should get a Management Token from uid', done => { + makeManagementToken(tokenUidProd) + .fetch() + .then((token) => { + expect(token.name).to.be.equal(createManagementToken2.token.name) + expect(token.description).to.be.equal(createManagementToken2.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should query to get all Management Token', done => { + makeManagementToken() + .query() + .find() + .then((tokens) => { + tokens.items.forEach((token) => { + expect(token.name).to.be.not.equal(null) + expect(token.description).to.be.not.equal(null) + expect(token.scope[0].module).to.be.not.equal(null) + expect(token.uid).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + + it('should query to get a Management Token from name', done => { + makeManagementToken() + .query({ query: { name: createManagementToken.token.name } }) + .find() + .then((tokens) => { + tokens.items.forEach((token) => { + expect(token.name).to.be.equal(createManagementToken.token.name) + expect(token.description).to.be.equal(createManagementToken.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + + it('should fetch and update a Management Token from uid', done => { + makeManagementToken(tokenUidProd) + .fetch() + .then((token) => { + token.name = 'Update Production Name' + token.description = 'Update Production description' + token.scope = createManagementToken2.token.scope + return token.update() + }) + .then((token) => { + expect(token.name).to.be.equal('Update Production Name') + expect(token.description).to.be.equal('Update Production description') + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should update a Management Token from uid', done => { + const token = makeManagementToken(tokenUidProd) + Object.assign(token, createManagementToken2.token) + token.update() + .then((token) => { + expect(token.name).to.be.equal(createManagementToken2.token.name) + expect(token.description).to.be.equal(createManagementToken2.token.description) + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) + expect(token.uid).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should delete a Management Token from uid', done => { + makeManagementToken(tokenUidProd) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Management Token deleted successfully.') + done() + }) + .catch(done) + }) + + it('should delete a Management Token from uid 2', done => { + makeManagementToken(tokenUidDev) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Management Token deleted successfully.') + done() + }) + .catch(done) + }) +}) + +function makeManagementToken (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).managementToken(uid) +} diff --git a/test/sanity-check/mock/managementToken.js b/test/sanity-check/mock/managementToken.js new file mode 100644 index 00000000..263b786f --- /dev/null +++ b/test/sanity-check/mock/managementToken.js @@ -0,0 +1,72 @@ +const createManagementToken = { + token: { + name: 'Dev Token', + description: 'This is a sample management token.', + scope: [ + { + module: 'content_type', + acl: { + read: true, + write: true + } + }, + { + module: 'branch', + branches: [ + 'main' + ], + acl: { + read: true + } + }, + { + module: 'branch_alias', + branch_aliases: [ + 'sb1_alias' + ], + acl: { + read: true + } + } + ], + expires_on: '2024-12-10', + is_email_notification_enabled: true + } +} +const createManagementToken2 = { + token: { + name: 'Prod Token', + description: 'This is a sample management token.', + scope: [ + { + module: 'content_type', + acl: { + read: true, + write: true + } + }, + { + module: 'branch', + branches: [ + 'main' + ], + acl: { + read: true + } + }, + { + module: 'branch_alias', + branch_aliases: [ + 'sb1_alias' + ], + acl: { + read: true + } + } + ], + expires_on: '2024-12-10', + is_email_notification_enabled: true + } +} + +export { createManagementToken, createManagementToken2 } diff --git a/test/sanity-check/sanity.js b/test/sanity-check/sanity.js index 09c1aa64..d1643fb6 100644 --- a/test/sanity-check/sanity.js +++ b/test/sanity-check/sanity.js @@ -7,7 +7,7 @@ require('./api/branch-test') require('./api/branchAlias-test') require('./api/role-test') require('./api/deliveryToken-test') -// require('./api/managementToken-test') +require('./api/managementToken-test') require('./api/contentType-test') require('./api/asset-test') require('./api/extension-test')