|
| 1 | +import { expect } from 'chai' |
| 2 | +import { describe, it, setup } from 'mocha' |
| 3 | +import role from '../mock/role.js' |
| 4 | +import { jsonReader, jsonWrite } from '../utility/fileOperations/readwrite' |
| 5 | +import { contentstackClient } from '../utility/ContentstackClient.js' |
| 6 | +import dotenv from 'dotenv' |
| 7 | + |
| 8 | +dotenv.config() |
| 9 | +let client = {} |
| 10 | +let roleUID = '' |
| 11 | + |
| 12 | +describe('Role api test', () => { |
| 13 | + setup(() => { |
| 14 | + const user = jsonReader('loggedinuser.json') |
| 15 | + client = contentstackClient(user.authtoken) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should get all role in stack', done => { |
| 19 | + getRole() |
| 20 | + .fetchAll() |
| 21 | + .then((roles) => { |
| 22 | + jsonWrite(roles.items, 'roles.json') |
| 23 | + for (const index in roles.items) { |
| 24 | + const role1 = roles.items[index] |
| 25 | + expect(role1.uid).to.not.equal(null, 'Role uid cannot be null') |
| 26 | + } |
| 27 | + done() |
| 28 | + }) |
| 29 | + .catch(done) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should get 1 role in stack with limit', done => { |
| 33 | + getRole() |
| 34 | + .fetchAll({ limit: 2 }) |
| 35 | + .then((roles) => { |
| 36 | + expect(roles.items.length).to.not.equal(1) |
| 37 | + done() |
| 38 | + }) |
| 39 | + .catch(done) |
| 40 | + }) |
| 41 | + |
| 42 | + it('should get role in stack with skip first', done => { |
| 43 | + getRole() |
| 44 | + .fetchAll({ skip: 1 }) |
| 45 | + .then((roles) => { |
| 46 | + expect(roles.items.lenth).to.not.equal(1, 'Role fetch with limit 1 not work') |
| 47 | + done() |
| 48 | + }) |
| 49 | + .catch(done) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should create new role in stack', done => { |
| 53 | + getRole() |
| 54 | + .create(role) |
| 55 | + .then((roles) => { |
| 56 | + roleUID = roles.uid |
| 57 | + expect(roles.name).to.be.equal(role.role.name, 'Role name not match') |
| 58 | + expect(roles.description).to.be.equal(role.role.description, 'Role description not match') |
| 59 | + done() |
| 60 | + }) |
| 61 | + .catch(done) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should get role in stack', done => { |
| 65 | + getRole(roleUID) |
| 66 | + .fetch() |
| 67 | + .then((roles) => { |
| 68 | + jsonWrite(roles, 'role.json') |
| 69 | + expect(roles.name).to.be.equal(role.role.name, 'Role name not match') |
| 70 | + expect(roles.description).to.be.equal(role.role.description, 'Role description not match') |
| 71 | + expect(roles.stack.api_key).to.be.equal(process.env.API_KEY, 'Role stack uid not match') |
| 72 | + done() |
| 73 | + }) |
| 74 | + .catch(done) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should update role in stack', done => { |
| 78 | + getRole(roleUID) |
| 79 | + .fetch({ include_rules: true, include_permissions: true }) |
| 80 | + .then((roles) => { |
| 81 | + roles.name = 'Update test name' |
| 82 | + roles.description = 'Update description' |
| 83 | + return roles.update() |
| 84 | + }) |
| 85 | + .then((roles) => { |
| 86 | + expect(roles.name).to.be.equal('Update test name', 'Role name not match') |
| 87 | + expect(roles.description).to.be.equal('Update description', 'Role description not match') |
| 88 | + done() |
| 89 | + }) |
| 90 | + .catch(done) |
| 91 | + }) |
| 92 | + |
| 93 | + it('should get all Roles with query', done => { |
| 94 | + getRole() |
| 95 | + .query() |
| 96 | + .find() |
| 97 | + .then((response) => { |
| 98 | + for (const index in response.items) { |
| 99 | + const role = response.items[index] |
| 100 | + expect(role.name).to.not.equal(null) |
| 101 | + expect(role.uid).to.not.equal(null) |
| 102 | + } |
| 103 | + done() |
| 104 | + }) |
| 105 | + .catch(done) |
| 106 | + }) |
| 107 | + |
| 108 | + it('should get query Role', done => { |
| 109 | + getRole() |
| 110 | + .query({ query: { name: 'Developer' } }) |
| 111 | + .find() |
| 112 | + .then((response) => { |
| 113 | + for (const index in response.items) { |
| 114 | + const stack = response.items[index] |
| 115 | + expect(stack.name).to.be.equal('Developer') |
| 116 | + } |
| 117 | + done() |
| 118 | + }) |
| 119 | + .catch(done) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should find one role', done => { |
| 123 | + getRole() |
| 124 | + .query({ name: 'Developer' }) |
| 125 | + .findOne() |
| 126 | + .then((response) => { |
| 127 | + const stack = response.items[0] |
| 128 | + expect(response.items.length).to.be.equal(1) |
| 129 | + expect(stack.name).to.be.not.equal(null) |
| 130 | + done() |
| 131 | + }) |
| 132 | + .catch(done) |
| 133 | + }) |
| 134 | + |
| 135 | + it('should delete role in stack', done => { |
| 136 | + getRole(roleUID) |
| 137 | + .delete() |
| 138 | + .then((roles) => { |
| 139 | + expect(roles.notice).to.be.equal('The role deleted successfully.') |
| 140 | + done() |
| 141 | + }) |
| 142 | + .catch(done) |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +function getRole (uid = null) { |
| 147 | + return client.stack({ api_key: process.env.API_KEY }).role(uid) |
| 148 | +} |
0 commit comments