Skip to content

Commit

Permalink
feat: ✨ stackrolemapping implementation and api test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithad0703 committed Nov 6, 2023
1 parent fec3852 commit 99fc8c1
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .jsdoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"lib/stack/roles/index.js",
"lib/stack/webhook/index.js",
"lib/stack/workflow/index.js",
"lib/stack/workflow/publishRules/index.js"
"lib/stack/workflow/publishRules/index.js",
"lib/organization/team/stackRoleMapping/index.js̀"

],
"excludePattern": "(node_modules/|jsdocs)"
Expand Down
10 changes: 10 additions & 0 deletions lib/organization/team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
fetchAll
} from '../../entity'
import { TeamUsers } from './teamUsers'
import { StackRoleMappings } from './stackRoleMapping'

export function Teams (http, data) {
this.organizationUid = data.organizationUid
Expand Down Expand Up @@ -78,6 +79,15 @@ export function Teams (http, data) {
}
return new TeamUsers(http, data)
}

this.stackRoleMappings = (stackApiKey = null) => {
data.organizationUid = this.organizationUid
data.teamUid = this.uid
if (stackApiKey) {
data.stackApiKey = stackApiKey
}
return new StackRoleMappings(http, data)
}
} else {
/**
* @description The fetch call on team will delete the existing team.
Expand Down
95 changes: 95 additions & 0 deletions lib/organization/team/stackRoleMapping/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @namespace StackRoleMappings
*/
import cloneDeep from 'lodash/cloneDeep'
import {
create,
update,
deleteEntity,
fetchAll
} from '../../../entity'

export function StackRoleMappings (http, data) {
console.log("🚀 ~ file: index.js:13 ~ StackRoleMappings ~ data:", data)
const _urlPath = `/organizations/${data.organizationUid}/teams/${data.teamUid}/stack_role_mappings`
if (data && data.stackApiKey) {
Object.assign(this, cloneDeep(data))

this.urlPath = `${_urlPath}/${this.stackApiKey}`
/**
* @description The update stackRoleMappings call is used to update the roles.
* @memberof StackRoleMappings
* @func update
* @returns {Promise<StackRoleMappings.StackRoleMappings>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const updateRoles = {
* roles: [
* 'roles_uid1',
* 'roles_uid2'
* ]
* }
* client.organization('organizationUid').teams('teamUid').stackRoleMappings('stackApiKey').update(updateRoles)
* .then((response) => console.log(response))
*/
this.update = update(http, 'stackRoleMapping')

/**
* @description The delete stackRoleMappings call is used to delete the roles.
* @memberof StackRoleMappings
* @func delete
* @returns {Promise<StackRoleMappings.StackRoleMappings>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organizationUid').teams('teamUid').stackRoleMappings('stackApiKey').delete()
* .then((response) => console.log(response))
*/
this.delete = deleteEntity(http)
} else {
// const _urlPath = `/organizations/${data.organizationUid}/teams/${data.teamUid}/stack_role_mappings`
this.urlPath = _urlPath
/**
* @description The add stackRoleMappings call is used to add the roles.
* @memberof StackRoleMappings
* @func add
* @returns {Promise<StackRoleMappings.StackRoleMappings>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const addRole = {
* 'stackApiKey: 'stackApiKey',
* 'roles': [
* 'role_uid'
* ]
* }
* client.organization('organizationUid').teams('teamUid').stackRoleMappings().add(addRole)
* .then((response) => console.log(response))
*/
this.add = create({ http })

/**
* @description The fetchAll stackRoleMappings call is used to fetchAll the roles.
* @memberof StackRoleMappings
* @func fetchAll
* @returns {Promise<StackRoleMappings.StackRoleMappings>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll
* .then((response) => console.log(response))
*/
this.fetchAll = fetchAll(http, stackRoleMappingsCollection)
}
}
export function stackRoleMappingsCollection (http, data) {
const obj = cloneDeep(data.stackRoleMappings) || []
const stackRoleMappingCollection = obj.map((stackRoleMappings) => {
return new StackRoleMappings(http, { stackRoleMappings: stackRoleMappings })
})
return stackRoleMappingCollection
}
74 changes: 74 additions & 0 deletions test/api/team-stack-role-mapping-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, it, beforeEach } from 'mocha'
import { expect } from 'chai'
import { jsonReader } from '../utility/fileOperations/readwrite'
import { contentstackClient } from '../utility/ContentstackClient.js'

let client = {}

const stackApiKey = 'stackApiKey'
const organizationUid = 'organizationUid'
const teamUid = 'teamUid'

describe('Teams API Test', () => {
beforeEach(() => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
})
it('should fetch all stackRoleMappings', (done) => {
makestackRoleMappings(organizationUid, teamUid).fetchAll().then((response) => {
console.log('🚀 ~ file: team-stack-role-mapping-test.js:19 ~ makestackRoleMappings ~ response:', response)
response.items.forEach((stackRoleMapping) => {
console.log(stackRoleMapping)
})
expect(response).to.be.not.equal(null)
done()
})
.catch(done)
})
it('should add roles', async () => {
try {
const stackRoleMappings = {
stackApiKey: 'stackApiKey',
roles: [
'role_uid'

]
}
await makestackRoleMappings(organizationUid, teamUid).add(stackRoleMappings).then((response) => {
console.log('🚀 ~ file: team-stack-role-mapping-test.js:36 ~ awaitmakestackRoleMappings ~ response:', response)
})
} catch (err) {
console.log(err)
}
})
it('should update roles', async () => {
try {
const stackRoleMappings = {
roles: [
'role_uid1',
'role_uid2'
]
}
await makestackRoleMappings(organizationUid, teamUid, stackApiKey).update(stackRoleMappings).then((response) => {
console.log('🚀 ~ file: team-stack-role-mapping-test.js:31 ~ makestackRoleMappings ~ response:', response)
})
} catch (err) {
console.log(err.errors)
}
})
it('should delete roles', async () => {
try {
await makestackRoleMappings(organizationUid, teamUid, stackApiKey).delete().then((response) => {
console.log('🚀 ~ file: team-stack-role-mapping-test.js:31 ~ makestackRoleMappings ~ response:', response)
})
} catch (err) {
console.log(err.errors)
}
})
})

function makestackRoleMappings (organizationUid, teamUid, stackApiKey = null) {
return client.organization(organizationUid).teams(teamUid).stackRoleMappings(stackApiKey)
}

// delete done
2 changes: 1 addition & 1 deletion test/api/team-users-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Teams API Test', () => {
const user = jsonReader('loggedinuser.json')
client = contentstackClient(user.authtoken)
})
it.only('should add the user when user\'s mail is passed', done => {
it('should add the user when user\'s mail is passed', done => {
const usersMail = {
emails: ['[email protected]']
}
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ require('./api/taxonomy-test')
require('./api/terms-test')
require('./api/team-test')
require('./api/team-users-test')
require('./api/team-stack-role-mapping-test')

0 comments on commit 99fc8c1

Please sign in to comment.