|
| 1 | +import { expect } from 'chai' |
| 2 | +import { describe, it, setup } from 'mocha' |
| 3 | +import { jsonReader } from '../utility/fileOperations/readwrite.js' |
| 4 | +import { createManagementToken, createManagementToken2 } from '../mock/managementToken.js' |
| 5 | +import { contentstackClient } from '../utility/ContentstackClient.js' |
| 6 | + |
| 7 | +let client = {} |
| 8 | + |
| 9 | +let tokenUidProd = '' |
| 10 | +let tokenUidDev = '' |
| 11 | +describe('Management Token api Test', () => { |
| 12 | + setup(() => { |
| 13 | + const user = jsonReader('loggedinuser.json') |
| 14 | + client = contentstackClient(user.authtoken) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should add a Management Token', done => { |
| 18 | + makeManagementToken() |
| 19 | + .create(createManagementToken) |
| 20 | + .then((token) => { |
| 21 | + tokenUidDev = token.uid |
| 22 | + expect(token.name).to.be.equal(createManagementToken.token.name) |
| 23 | + expect(token.description).to.be.equal(createManagementToken.token.description) |
| 24 | + expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) |
| 25 | + expect(token.uid).to.be.not.equal(null) |
| 26 | + done() |
| 27 | + }) |
| 28 | + .catch(done) |
| 29 | + }) |
| 30 | + |
| 31 | + it('should add a Management Token for production', done => { |
| 32 | + makeManagementToken() |
| 33 | + .create(createManagementToken2) |
| 34 | + .then((token) => { |
| 35 | + tokenUidProd = token.uid |
| 36 | + expect(token.name).to.be.equal(createManagementToken2.token.name) |
| 37 | + expect(token.description).to.be.equal(createManagementToken2.token.description) |
| 38 | + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) |
| 39 | + expect(token.uid).to.be.not.equal(null) |
| 40 | + done() |
| 41 | + }) |
| 42 | + .catch(done) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should get a Management Token from uid', done => { |
| 46 | + makeManagementToken(tokenUidProd) |
| 47 | + .fetch() |
| 48 | + .then((token) => { |
| 49 | + expect(token.name).to.be.equal(createManagementToken2.token.name) |
| 50 | + expect(token.description).to.be.equal(createManagementToken2.token.description) |
| 51 | + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) |
| 52 | + expect(token.uid).to.be.not.equal(null) |
| 53 | + done() |
| 54 | + }) |
| 55 | + .catch(done) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should query to get all Management Token', done => { |
| 59 | + makeManagementToken() |
| 60 | + .query() |
| 61 | + .find() |
| 62 | + .then((tokens) => { |
| 63 | + tokens.items.forEach((token) => { |
| 64 | + expect(token.name).to.be.not.equal(null) |
| 65 | + expect(token.description).to.be.not.equal(null) |
| 66 | + expect(token.scope[0].module).to.be.not.equal(null) |
| 67 | + expect(token.uid).to.be.not.equal(null) |
| 68 | + }) |
| 69 | + done() |
| 70 | + }) |
| 71 | + .catch(done) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should query to get a Management Token from name', done => { |
| 75 | + makeManagementToken() |
| 76 | + .query({ query: { name: createManagementToken.token.name } }) |
| 77 | + .find() |
| 78 | + .then((tokens) => { |
| 79 | + tokens.items.forEach((token) => { |
| 80 | + expect(token.name).to.be.equal(createManagementToken.token.name) |
| 81 | + expect(token.description).to.be.equal(createManagementToken.token.description) |
| 82 | + expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module) |
| 83 | + expect(token.uid).to.be.not.equal(null) |
| 84 | + }) |
| 85 | + done() |
| 86 | + }) |
| 87 | + .catch(done) |
| 88 | + }) |
| 89 | + |
| 90 | + it('should fetch and update a Management Token from uid', done => { |
| 91 | + makeManagementToken(tokenUidProd) |
| 92 | + .fetch() |
| 93 | + .then((token) => { |
| 94 | + token.name = 'Update Production Name' |
| 95 | + token.description = 'Update Production description' |
| 96 | + token.scope = createManagementToken2.token.scope |
| 97 | + return token.update() |
| 98 | + }) |
| 99 | + .then((token) => { |
| 100 | + expect(token.name).to.be.equal('Update Production Name') |
| 101 | + expect(token.description).to.be.equal('Update Production description') |
| 102 | + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) |
| 103 | + expect(token.uid).to.be.not.equal(null) |
| 104 | + done() |
| 105 | + }) |
| 106 | + .catch(done) |
| 107 | + }) |
| 108 | + |
| 109 | + it('should update a Management Token from uid', done => { |
| 110 | + const token = makeManagementToken(tokenUidProd) |
| 111 | + Object.assign(token, createManagementToken2.token) |
| 112 | + token.update() |
| 113 | + .then((token) => { |
| 114 | + expect(token.name).to.be.equal(createManagementToken2.token.name) |
| 115 | + expect(token.description).to.be.equal(createManagementToken2.token.description) |
| 116 | + expect(token.scope[0].module).to.be.equal(createManagementToken2.token.scope[0].module) |
| 117 | + expect(token.uid).to.be.not.equal(null) |
| 118 | + done() |
| 119 | + }) |
| 120 | + .catch(done) |
| 121 | + }) |
| 122 | + |
| 123 | + it('should delete a Management Token from uid', done => { |
| 124 | + makeManagementToken(tokenUidProd) |
| 125 | + .delete() |
| 126 | + .then((data) => { |
| 127 | + expect(data.notice).to.be.equal('Management Token deleted successfully.') |
| 128 | + done() |
| 129 | + }) |
| 130 | + .catch(done) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should delete a Management Token from uid 2', done => { |
| 134 | + makeManagementToken(tokenUidDev) |
| 135 | + .delete() |
| 136 | + .then((data) => { |
| 137 | + expect(data.notice).to.be.equal('Management Token deleted successfully.') |
| 138 | + done() |
| 139 | + }) |
| 140 | + .catch(done) |
| 141 | + }) |
| 142 | +}) |
| 143 | + |
| 144 | +function makeManagementToken (uid = null) { |
| 145 | + return client.stack({ api_key: process.env.API_KEY }).managementToken(uid) |
| 146 | +} |
0 commit comments