Skip to content

Commit 5a3606c

Browse files
Merge pull request #123 from contentstack/test/cs-43498-basic-sanity-role
test: added basic sanity for role
2 parents f84ce58 + 9f4bb15 commit 5a3606c

File tree

4 files changed

+233
-5
lines changed

4 files changed

+233
-5
lines changed

test/sanity-check/api/organization-test.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai'
22
import { describe, it, setup } from 'mocha'
3-
import { jsonReader } from '../utility/fileOperations/readwrite'
3+
import { jsonReader, jsonWrite } from '../utility/fileOperations/readwrite'
44
import { contentstackClient } from '../utility/ContentstackClient'
55

66
var user = {}
@@ -79,6 +79,7 @@ describe('Organization api test', () => {
7979
organization.roles()
8080
.then((roles) => {
8181
for (const i in roles.items) {
82+
jsonWrite(roles.items, 'orgRoles.json')
8283
expect(roles.items[i].uid).to.not.equal(null, 'Role uid cannot be null')
8384
expect(roles.items[i].name).to.not.equal(null, 'Role name cannot be null')
8485
expect(roles.items[i].org_uid).to.be.equal(organization.uid, 'Role org_uid not match')
@@ -102,7 +103,4 @@ describe('Organization api test', () => {
102103
})
103104
.catch(done)
104105
})
105-
106-
// addUser
107-
// Resend invitation
108106
})

test/sanity-check/api/role-test.js

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}

test/sanity-check/mock/role.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const role = {
2+
role: {
3+
name: 'testRole',
4+
description: 'This is a test role.',
5+
rules: [
6+
{
7+
module: 'branch',
8+
branches: [
9+
'main'
10+
],
11+
acl: {
12+
read: true
13+
}
14+
},
15+
{
16+
module: 'branch_alias',
17+
branch_aliases: [
18+
'staging_alias'
19+
],
20+
acl: {
21+
read: true
22+
}
23+
},
24+
{
25+
module: 'content_type',
26+
content_types: [
27+
'$all'
28+
],
29+
acl: {
30+
read: true,
31+
sub_acl: {
32+
read: true
33+
}
34+
}
35+
},
36+
{
37+
module: 'asset',
38+
assets: [
39+
'$all'
40+
],
41+
acl: {
42+
read: true,
43+
update: true,
44+
publish: true,
45+
delete: true
46+
}
47+
},
48+
{
49+
module: 'folder',
50+
folders: [
51+
'$all'
52+
],
53+
acl: {
54+
read: true,
55+
sub_acl: {
56+
read: true
57+
}
58+
}
59+
},
60+
{
61+
module: 'environment',
62+
environments: [
63+
'$all'
64+
],
65+
acl: {
66+
read: true
67+
}
68+
},
69+
{
70+
module: 'locale',
71+
locales: [
72+
'en-us'
73+
],
74+
acl: {
75+
read: true
76+
}
77+
}
78+
]
79+
}
80+
}
81+
82+
export default role

test/sanity-check/sanity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require('./api/locale-test')
55
require('./api/environment-test')
66
require('./api/branch-test')
77
require('./api/branchAlias-test')
8-
// require('./api/role-test')
8+
require('./api/role-test')
99
require('./api/deliveryToken-test')
1010
// require('./api/managementToken-test')
1111
require('./api/contentType-test')

0 commit comments

Comments
 (0)