From 9f4bb151e3b7a25bdc5023c27ae6164e0697247c Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 31 Jan 2024 15:53:38 +0530 Subject: [PATCH 1/2] test: added basic sanity for role --- test/sanity-check/api/organization-test.js | 6 +- test/sanity-check/api/role-test.js | 148 +++++++++++++++++++++ test/sanity-check/mock/role.js | 82 ++++++++++++ test/sanity-check/sanity.js | 2 +- 4 files changed, 233 insertions(+), 5 deletions(-) create mode 100644 test/sanity-check/api/role-test.js create mode 100644 test/sanity-check/mock/role.js diff --git a/test/sanity-check/api/organization-test.js b/test/sanity-check/api/organization-test.js index 7e1f2b8f..da25c4b9 100644 --- a/test/sanity-check/api/organization-test.js +++ b/test/sanity-check/api/organization-test.js @@ -1,6 +1,6 @@ import { expect } from 'chai' import { describe, it, setup } from 'mocha' -import { jsonReader } from '../utility/fileOperations/readwrite' +import { jsonReader, jsonWrite } from '../utility/fileOperations/readwrite' import { contentstackClient } from '../utility/ContentstackClient' var user = {} @@ -79,6 +79,7 @@ describe('Organization api test', () => { organization.roles() .then((roles) => { for (const i in roles.items) { + jsonWrite(roles.items, 'orgRoles.json') expect(roles.items[i].uid).to.not.equal(null, 'Role uid cannot be null') expect(roles.items[i].name).to.not.equal(null, 'Role name cannot be null') expect(roles.items[i].org_uid).to.be.equal(organization.uid, 'Role org_uid not match') @@ -102,7 +103,4 @@ describe('Organization api test', () => { }) .catch(done) }) - - // addUser - // Resend invitation }) diff --git a/test/sanity-check/api/role-test.js b/test/sanity-check/api/role-test.js new file mode 100644 index 00000000..d1a190c1 --- /dev/null +++ b/test/sanity-check/api/role-test.js @@ -0,0 +1,148 @@ +import { expect } from 'chai' +import { describe, it, setup } from 'mocha' +import role from '../mock/role.js' +import { jsonReader, jsonWrite } from '../utility/fileOperations/readwrite' +import { contentstackClient } from '../utility/ContentstackClient.js' +import dotenv from 'dotenv' + +dotenv.config() +let client = {} +let roleUID = '' + +describe('Role api test', () => { + setup(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + + it('should get all role in stack', done => { + getRole() + .fetchAll() + .then((roles) => { + jsonWrite(roles.items, 'roles.json') + for (const index in roles.items) { + const role1 = roles.items[index] + expect(role1.uid).to.not.equal(null, 'Role uid cannot be null') + } + done() + }) + .catch(done) + }) + + it('should get 1 role in stack with limit', done => { + getRole() + .fetchAll({ limit: 2 }) + .then((roles) => { + expect(roles.items.length).to.not.equal(1) + done() + }) + .catch(done) + }) + + it('should get role in stack with skip first', done => { + getRole() + .fetchAll({ skip: 1 }) + .then((roles) => { + expect(roles.items.lenth).to.not.equal(1, 'Role fetch with limit 1 not work') + done() + }) + .catch(done) + }) + + it('should create new role in stack', done => { + getRole() + .create(role) + .then((roles) => { + roleUID = roles.uid + expect(roles.name).to.be.equal(role.role.name, 'Role name not match') + expect(roles.description).to.be.equal(role.role.description, 'Role description not match') + done() + }) + .catch(done) + }) + + it('should get role in stack', done => { + getRole(roleUID) + .fetch() + .then((roles) => { + jsonWrite(roles, 'role.json') + expect(roles.name).to.be.equal(role.role.name, 'Role name not match') + expect(roles.description).to.be.equal(role.role.description, 'Role description not match') + expect(roles.stack.api_key).to.be.equal(process.env.API_KEY, 'Role stack uid not match') + done() + }) + .catch(done) + }) + + it('should update role in stack', done => { + getRole(roleUID) + .fetch({ include_rules: true, include_permissions: true }) + .then((roles) => { + roles.name = 'Update test name' + roles.description = 'Update description' + return roles.update() + }) + .then((roles) => { + expect(roles.name).to.be.equal('Update test name', 'Role name not match') + expect(roles.description).to.be.equal('Update description', 'Role description not match') + done() + }) + .catch(done) + }) + + it('should get all Roles with query', done => { + getRole() + .query() + .find() + .then((response) => { + for (const index in response.items) { + const role = response.items[index] + expect(role.name).to.not.equal(null) + expect(role.uid).to.not.equal(null) + } + done() + }) + .catch(done) + }) + + it('should get query Role', done => { + getRole() + .query({ query: { name: 'Developer' } }) + .find() + .then((response) => { + for (const index in response.items) { + const stack = response.items[index] + expect(stack.name).to.be.equal('Developer') + } + done() + }) + .catch(done) + }) + + it('should find one role', done => { + getRole() + .query({ name: 'Developer' }) + .findOne() + .then((response) => { + const stack = response.items[0] + expect(response.items.length).to.be.equal(1) + expect(stack.name).to.be.not.equal(null) + done() + }) + .catch(done) + }) + + it('should delete role in stack', done => { + getRole(roleUID) + .delete() + .then((roles) => { + expect(roles.notice).to.be.equal('The role deleted successfully.') + done() + }) + .catch(done) + }) +}) + +function getRole (uid = null) { + return client.stack({ api_key: process.env.API_KEY }).role(uid) +} diff --git a/test/sanity-check/mock/role.js b/test/sanity-check/mock/role.js new file mode 100644 index 00000000..7364ae8d --- /dev/null +++ b/test/sanity-check/mock/role.js @@ -0,0 +1,82 @@ +const role = { + role: { + name: 'testRole', + description: 'This is a test role.', + rules: [ + { + module: 'branch', + branches: [ + 'main' + ], + acl: { + read: true + } + }, + { + module: 'branch_alias', + branch_aliases: [ + 'staging_alias' + ], + acl: { + read: true + } + }, + { + module: 'content_type', + content_types: [ + '$all' + ], + acl: { + read: true, + sub_acl: { + read: true + } + } + }, + { + module: 'asset', + assets: [ + '$all' + ], + acl: { + read: true, + update: true, + publish: true, + delete: true + } + }, + { + module: 'folder', + folders: [ + '$all' + ], + acl: { + read: true, + sub_acl: { + read: true + } + } + }, + { + module: 'environment', + environments: [ + '$all' + ], + acl: { + read: true + } + }, + { + module: 'locale', + locales: [ + 'en-us' + ], + acl: { + read: true + } + } + ] + } +} + +export default role diff --git a/test/sanity-check/sanity.js b/test/sanity-check/sanity.js index e9e30512..09c1aa64 100644 --- a/test/sanity-check/sanity.js +++ b/test/sanity-check/sanity.js @@ -5,7 +5,7 @@ require('./api/locale-test') require('./api/environment-test') require('./api/branch-test') require('./api/branchAlias-test') -// require('./api/role-test') +require('./api/role-test') require('./api/deliveryToken-test') // require('./api/managementToken-test') require('./api/contentType-test') From 8b8e2a5b750c7b40261c7353a07b837fe1ef8c7c Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 31 Jan 2024 15:55:11 +0530 Subject: [PATCH 2/2] test: added basic sanity for management token test suit --- test/sanity-check/api/managementToken-test.js | 146 ++++++++++++++++++ test/sanity-check/mock/managementToken.js | 72 +++++++++ test/sanity-check/sanity.js | 2 +- 3 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 test/sanity-check/api/managementToken-test.js create mode 100644 test/sanity-check/mock/managementToken.js 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')