Skip to content

Commit

Permalink
added unit test cases for stack role mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithad0703 committed Nov 7, 2023
1 parent 864db84 commit 231711f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ require('./taxonomy-test')
require('./terms-test')
require('./team-test')
require('./team-users-test')
require('./team-stack-role-mapping-test')
11 changes: 11 additions & 0 deletions test/unit/mock/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,16 @@ const teamUsersMock = {
userId: 'UID'
}
}
const stackRoleMappingMock = {
stackRoleMappings: [
{
stackApiKey: 'stackApiKey',
roles: [
'roles_uid'
]
}
]
}
function mockCollection (mockData, type) {
const mock = {
...cloneDeep(noticeMock),
Expand Down Expand Up @@ -860,6 +870,7 @@ export {
termsMock,
teamsMock,
teamUsersMock,
stackRoleMappingMock,
mockCollection,
entryMockCollection,
checkSystemFields
Expand Down
88 changes: 88 additions & 0 deletions test/unit/team-stack-role-mapping-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import Axios from 'axios'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import MockAdapter from 'axios-mock-adapter'
import { StackRoleMappings } from '../../lib/organization/team/stackRoleMapping'
import { stackRoleMappingMock } from './mock/objects'

describe('Contentstack Team Stack Role Mapping test', () => {
it('should fetch all the roles', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/organizations/organization_uid/teams/team_uid/stack_role_mappings`).reply(200, stackRoleMappingMock)
makeStackRoleMapping().fetchAll()
.then((roles) => {
console.log('🚀 ~ file: team-stack-role-mapping-test.js:14 ~ .then ~ roles:', roles)
done()
})
.catch(done)
})
it('should add roles when correct data is passed', done => {
const addStackRoleMappingMock = {
stackRoleMapping: {
stackApiKey: 'stackApiKey',
roles: [
'role_uid'
]
}
}
var mock = new MockAdapter(Axios)
mock.onPost(`/organizations/organization_uid/teams/team_uid/stack_role_mappings`).reply(200, addStackRoleMappingMock)
const addRole = {
stackApiKey: 'stackApiKey',
roles: [
'role_uid'

]
}
makeStackRoleMapping()
.add(addRole)
.then((response) => {
expect(response.stackRoleMapping).not.to.be.equal(undefined)
expect(response.stackRoleMapping.roles[0]).to.be.equal(addRole.roles[0])
expect(response.stackRoleMapping.stackApiKey).to.be.equal(addRole.stackApiKey)
done()
})
.catch(done)
})
it('should update stack role mapping when stack api key and updateData are passed', done => {
const updateStackRoleMappingMock = {
stackRoleMapping: {
stackApiKey: 'STACKAPIKEY',
roles: [
'role_uid1',
'role_uid2'
]
}
}
var mock = new MockAdapter(Axios)
mock.onPut(`/organizations/organization_uid/teams/team_uid/stack_role_mappings/STACKAPIKEY`).reply(200, updateStackRoleMappingMock)
const stackRoleMappings = {
roles: [
'role_uid1',
'role_uid2'
]
}
makeStackRoleMapping({ stackApiKey: 'STACKAPIKEY' }).update(stackRoleMappings)
.then((response) => {
expect(response.stackRoleMapping).not.to.be.equal(undefined)
expect(response.stackRoleMapping.roles[0]).to.be.equal(stackRoleMappings.roles[0])
expect(response.stackRoleMapping.stackApiKey).to.be.equal('STACKAPIKEY')
done()
})
.catch(done)
})
it('should delete stack role mapping when stack api key is passed', done => {
var mock = new MockAdapter(Axios)
mock.onDelete(`/organizations/organization_uid/teams/team_uid/stack_role_mappings/STACKAPIKEY`).reply(200, { status: 204 })
makeStackRoleMapping({ stackApiKey: 'STACKAPIKEY' }).delete()
.then((response) => {
expect(response.status).to.be.equal(204)
done()
})
.catch(done)
})
})

function makeStackRoleMapping (data = {}) {
return new StackRoleMappings(Axios, { organizationUid: 'organization_uid', teamUid: 'team_uid', ...data })
}
10 changes: 5 additions & 5 deletions test/unit/team-users-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import MockAdapter from 'axios-mock-adapter'
import { TeamUsers } from '../../lib/organization/team/teamUsers'
import { teamUsersMock, noticeMock } from './mock/objects'

describe('Contentstack Team test', () => {
describe('Contentstack Team Users test', () => {
it('should query and find all users', done => {
var mock = new MockAdapter(Axios)
mock.onGet(`/organizations/organization_uid/teams/team_uid/users`).reply(200, teamUsersMock)
makeTeams().query().find()
makeTeamUsers().query().find()
.then((users) => {
users.items.forEach((user) => {
expect(user.uidId).to.be.not.equal(null)
Expand All @@ -24,7 +24,7 @@ describe('Contentstack Team test', () => {
const usersMail = {
emails: ['[email protected]']
}
makeTeams()
makeTeamUsers()
.add(usersMail)
.then((team) => {
expect(team.userId).to.be.equal('UID')
Expand All @@ -35,7 +35,7 @@ describe('Contentstack Team test', () => {
it('should remove the user when uid is passed', done => {
var mock = new MockAdapter(Axios)
mock.onDelete(`/organizations/organization_uid/teams/team_uid/users/UID`).reply(200, { ...noticeMock })
makeTeams({ userId: 'UID' }, noticeMock)
makeTeamUsers({ userId: 'UID' }, noticeMock)
.remove()
.then((user) => {
expect(user.notice).to.be.equal(noticeMock.notice)
Expand All @@ -45,6 +45,6 @@ describe('Contentstack Team test', () => {
})
})

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

0 comments on commit 231711f

Please sign in to comment.