Skip to content

Commit 57e0dfb

Browse files
added unit test cases for stack role mapping
1 parent b05f0c5 commit 57e0dfb

File tree

4 files changed

+105
-5
lines changed

4 files changed

+105
-5
lines changed

test/unit/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ require('./taxonomy-test')
3737
require('./terms-test')
3838
require('./team-test')
3939
require('./team-users-test')
40+
require('./team-stack-role-mapping-test')

test/unit/mock/objects.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,16 @@ const teamUsersMock = {
788788
userId: 'UID'
789789
}
790790
}
791+
const stackRoleMappingMock = {
792+
stackRoleMappings: [
793+
{
794+
stackApiKey: 'stackApiKey',
795+
roles: [
796+
'roles_uid'
797+
]
798+
}
799+
]
800+
}
791801
function mockCollection (mockData, type) {
792802
const mock = {
793803
...cloneDeep(noticeMock),
@@ -860,6 +870,7 @@ export {
860870
termsMock,
861871
teamsMock,
862872
teamUsersMock,
873+
stackRoleMappingMock,
863874
mockCollection,
864875
entryMockCollection,
865876
checkSystemFields
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import Axios from 'axios'
2+
import { expect } from 'chai'
3+
import { describe, it } from 'mocha'
4+
import MockAdapter from 'axios-mock-adapter'
5+
import { StackRoleMappings } from '../../lib/organization/team/stackRoleMapping'
6+
import { stackRoleMappingMock } from './mock/objects'
7+
8+
describe('Contentstack Team Stack Role Mapping test', () => {
9+
it('should fetch all the roles', done => {
10+
var mock = new MockAdapter(Axios)
11+
mock.onGet(`/organizations/organization_uid/teams/team_uid/stack_role_mappings`).reply(200, stackRoleMappingMock)
12+
makeStackRoleMapping().fetchAll()
13+
.then((roles) => {
14+
console.log('🚀 ~ file: team-stack-role-mapping-test.js:14 ~ .then ~ roles:', roles)
15+
done()
16+
})
17+
.catch(done)
18+
})
19+
it('should add roles when correct data is passed', done => {
20+
const addStackRoleMappingMock = {
21+
stackRoleMapping: {
22+
stackApiKey: 'stackApiKey',
23+
roles: [
24+
'role_uid'
25+
]
26+
}
27+
}
28+
var mock = new MockAdapter(Axios)
29+
mock.onPost(`/organizations/organization_uid/teams/team_uid/stack_role_mappings`).reply(200, addStackRoleMappingMock)
30+
const addRole = {
31+
stackApiKey: 'stackApiKey',
32+
roles: [
33+
'role_uid'
34+
35+
]
36+
}
37+
makeStackRoleMapping()
38+
.add(addRole)
39+
.then((response) => {
40+
expect(response.stackRoleMapping).not.to.be.equal(undefined)
41+
expect(response.stackRoleMapping.roles[0]).to.be.equal(addRole.roles[0])
42+
expect(response.stackRoleMapping.stackApiKey).to.be.equal(addRole.stackApiKey)
43+
done()
44+
})
45+
.catch(done)
46+
})
47+
it('should update stack role mapping when stack api key and updateData are passed', done => {
48+
const updateStackRoleMappingMock = {
49+
stackRoleMapping: {
50+
stackApiKey: 'STACKAPIKEY',
51+
roles: [
52+
'role_uid1',
53+
'role_uid2'
54+
]
55+
}
56+
}
57+
var mock = new MockAdapter(Axios)
58+
mock.onPut(`/organizations/organization_uid/teams/team_uid/stack_role_mappings/STACKAPIKEY`).reply(200, updateStackRoleMappingMock)
59+
const stackRoleMappings = {
60+
roles: [
61+
'role_uid1',
62+
'role_uid2'
63+
]
64+
}
65+
makeStackRoleMapping({ stackApiKey: 'STACKAPIKEY' }).update(stackRoleMappings)
66+
.then((response) => {
67+
expect(response.stackRoleMapping).not.to.be.equal(undefined)
68+
expect(response.stackRoleMapping.roles[0]).to.be.equal(stackRoleMappings.roles[0])
69+
expect(response.stackRoleMapping.stackApiKey).to.be.equal('STACKAPIKEY')
70+
done()
71+
})
72+
.catch(done)
73+
})
74+
it('should delete stack role mapping when stack api key is passed', done => {
75+
var mock = new MockAdapter(Axios)
76+
mock.onDelete(`/organizations/organization_uid/teams/team_uid/stack_role_mappings/STACKAPIKEY`).reply(200, { status: 204 })
77+
makeStackRoleMapping({ stackApiKey: 'STACKAPIKEY' }).delete()
78+
.then((response) => {
79+
expect(response.status).to.be.equal(204)
80+
done()
81+
})
82+
.catch(done)
83+
})
84+
})
85+
86+
function makeStackRoleMapping (data = {}) {
87+
return new StackRoleMappings(Axios, { organizationUid: 'organization_uid', teamUid: 'team_uid', ...data })
88+
}

test/unit/team-users-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import MockAdapter from 'axios-mock-adapter'
55
import { TeamUsers } from '../../lib/organization/team/teamUsers'
66
import { teamUsersMock, noticeMock } from './mock/objects'
77

8-
describe('Contentstack Team test', () => {
8+
describe('Contentstack Team Users test', () => {
99
it('should query and find all users', done => {
1010
var mock = new MockAdapter(Axios)
1111
mock.onGet(`/organizations/organization_uid/teams/team_uid/users`).reply(200, teamUsersMock)
12-
makeTeams().query().find()
12+
makeTeamUsers().query().find()
1313
.then((users) => {
1414
users.items.forEach((user) => {
1515
expect(user.uidId).to.be.not.equal(null)
@@ -24,7 +24,7 @@ describe('Contentstack Team test', () => {
2424
const usersMail = {
2525
emails: ['email@email.com']
2626
}
27-
makeTeams()
27+
makeTeamUsers()
2828
.add(usersMail)
2929
.then((team) => {
3030
expect(team.userId).to.be.equal('UID')
@@ -35,7 +35,7 @@ describe('Contentstack Team test', () => {
3535
it('should remove the user when uid is passed', done => {
3636
var mock = new MockAdapter(Axios)
3737
mock.onDelete(`/organizations/organization_uid/teams/team_uid/users/UID`).reply(200, { ...noticeMock })
38-
makeTeams({ userId: 'UID' }, noticeMock)
38+
makeTeamUsers({ userId: 'UID' }, noticeMock)
3939
.remove()
4040
.then((user) => {
4141
expect(user.notice).to.be.equal(noticeMock.notice)
@@ -45,6 +45,6 @@ describe('Contentstack Team test', () => {
4545
})
4646
})
4747

48-
function makeTeams (data = {}) {
48+
function makeTeamUsers (data = {}) {
4949
return new TeamUsers(Axios, { organizationUid: 'organization_uid', teamUid: 'team_uid', ...data })
5050
}

0 commit comments

Comments
 (0)