From dba8f48a269d70c94374c0d3686de3be1e781c3f Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 27 Oct 2023 14:07:03 +0530 Subject: [PATCH 01/32] feat: :sparkles: teams implementation with unit and api tests --- lib/organization/index.js | 9 +++ lib/organization/team/index.js | 40 +++++++++++++ test/api/team-test.js | 78 +++++++++++++++++++++++++ test/test.js | 1 + test/unit/mock/objects.js | 7 ++- test/unit/taxonomy-test.js | 23 +++++++- test/unit/team-test.js | 104 +++++++++++++++++++++++++++++++++ 7 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 lib/organization/team/index.js create mode 100644 test/api/team-test.js create mode 100644 test/unit/team-test.js diff --git a/lib/organization/index.js b/lib/organization/index.js index 5d35409f..3d51755f 100644 --- a/lib/organization/index.js +++ b/lib/organization/index.js @@ -7,6 +7,7 @@ import { StackCollection } from '../stack' import { UserCollection } from '../user' import { App } from '../app' import { AppRequest } from '../app/request' +import { Teams, TeamsCollection } from './team' /** * Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about Organizations.. * @namespace Organization @@ -18,6 +19,14 @@ export function Organization (http, data) { Object.assign(this, cloneDeep(data.organization)) this.urlPath = `/organizations/${this.uid}` + this.teams = (team_uid = null) => { + delete data.organization + data.organization_uid = this.uid + if (team_uid) { + data.team = { uid: team_uid } + } + return new Teams(http, data) + } /** * @description The fetch Organization call fetches Organization details. * @memberof Organization diff --git a/lib/organization/team/index.js b/lib/organization/team/index.js new file mode 100644 index 00000000..891415a3 --- /dev/null +++ b/lib/organization/team/index.js @@ -0,0 +1,40 @@ +import cloneDeep from 'lodash/cloneDeep' +import { + create, + fetch, + update, + query, + deleteEntity, + parseData, + fetchAll +} from '../../entity' + +export function Teams (http, data) { + this.organization_uid = data.organization_uid + + this.urlPath = `/organizations/${this.organization_uid}/teams` + + if (data && data.team) { + Object.assign(this, cloneDeep(data.team)) + console.log('org uid 2', this.organization_uid) + + this.urlPath = `/organizations/${this.organization_uid}/teams/${this.uid}` + + this.update = update(http, 'team') + + this.delete = deleteEntity(http) + + this.fetch = fetch(http, 'team') + } else { + this.create = create({ http }) + this.query = fetchAll(http, TeamsCollection) + // this.query = query({ http: http, wrapperCollection: TeamsCollection }) + } +} +export function TeamsCollection (http, teamsData) { + const obj = cloneDeep(teamsData.teams) || [] + const teamsCollection = obj.map((team) => { + return new Teams(http, { team: team }) + }) + return teamsCollection +} diff --git a/test/api/team-test.js b/test/api/team-test.js new file mode 100644 index 00000000..16d2dae5 --- /dev/null +++ b/test/api/team-test.js @@ -0,0 +1,78 @@ +import { describe, it, beforeEach } from 'mocha' +import { expect } from 'chai' +import { jsonReader } from '../utility/fileOperations/readwrite' +// import * as contentstack from '../../lib/contentstack.js' +import { contentstackClient } from '../utility/ContentstackClient.js' + +var client = {} + +var org_uid = 'blt242b133b4e9bd736' +const team_uid = '6539eed4b502f0a73415188e' +const team = { + name: 'team_name', + users: [], + stackRoleMapping: [], + organizationRole: 'organization_role_uid' +} + +describe('Teams API Test', () => { + beforeEach(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + it('Query and get all teams', async () => { + try { + const response = await client.organization(org_uid).teams().query() + console.log('res', response) + } catch (err) { + console.log(err) + } + }) + it('fetch test', async () => { + try { + const response = await client.organization(org_uid).teams(team_uid).fetch() + console.log('res2', await response) + } catch (err) { + console.log(err) + } + }) + // it('create test', async () => { + // try { + // const response = await makeTeams(org_uid).create({ + // name: 't2', + // users: [], + // stackRoleMapping: [], + // organizationRole: 'blt09e5dfced326aaea' }) + // console.log('res2', response) + // } catch (err) { + // console.log(err) + // } + // }) + // it('delete test', async () => { + // try { + // const response = await makeTeams(org_uid, '').delete() + // console.log('res2', response) + // } catch (err) { + // console.log(err) + // } + // }) + // it('update test', async () => { + // try { + // var response = await makeTeams(org_uid, team_uid).fetch() + // .then((team) => { + // team.name = 'updated' + // console.log('tttt', team) + // return response.update() + // }) + // // const res = response.update() + // console.log('update', response) + // // console.log('update12', res) + // } catch (err) { + // console.log(err) + // } + // }) +}) + +function makeTeams (org_uid, team_uid = null) { + return client.organization(org_uid).teams(team_uid) +} diff --git a/test/test.js b/test/test.js index d7e30522..5cb5e213 100644 --- a/test/test.js +++ b/test/test.js @@ -27,3 +27,4 @@ require('./api/contentType-delete-test') require('./api/delete-test') require('./api/taxonomy-test') require('./api/terms-test') +require('./api/team-test') diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index 1c82cc1a..a30a2b51 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -774,7 +774,11 @@ const termsMock = { referenced_entries_count: 4 }] } - +const teamsMock = { + organization_uid: 'organization_uid', + uid: 'UID', + name: 'name' +} function mockCollection (mockData, type) { const mock = { ...cloneDeep(noticeMock), @@ -845,6 +849,7 @@ export { auditLogItemMock, taxonomyMock, termsMock, + teamsMock, mockCollection, entryMockCollection, checkSystemFields diff --git a/test/unit/taxonomy-test.js b/test/unit/taxonomy-test.js index b5f09386..06f4d300 100644 --- a/test/unit/taxonomy-test.js +++ b/test/unit/taxonomy-test.js @@ -3,7 +3,7 @@ import { expect } from 'chai' import { describe, it } from 'mocha' import MockAdapter from 'axios-mock-adapter' import { Taxonomy } from '../../lib/stack/taxonomy' -import { systemUidMock, stackHeadersMock, taxonomyMock, noticeMock } from './mock/objects' +import { systemUidMock, stackHeadersMock, taxonomyMock, noticeMock, termsMock } from './mock/objects' describe('Contentstack Taxonomy test', () => { it('taxonomy create test', done => { @@ -121,6 +121,27 @@ describe('Contentstack Taxonomy test', () => { expect(taxonomy.query).to.be.equal(undefined) done() }) + + it('term create test', done => { + var mock = new MockAdapter(Axios) + mock.onPost(`/taxonomies/taxonomy_uid/terms`).reply(200, { + term: { + ...termsMock + } + }) + makeTaxonomy({ + taxonomy: { + uid: 'taxonomy_uid' + }, + stackHeaders: stackHeadersMock + }).terms() + .create() + .then((term) => { + console.log(term) + done() + }) + .catch(done) + }) }) function makeTaxonomy (data = {}) { diff --git a/test/unit/team-test.js b/test/unit/team-test.js new file mode 100644 index 00000000..6660a35b --- /dev/null +++ b/test/unit/team-test.js @@ -0,0 +1,104 @@ +import Axios from 'axios' +import { expect } from 'chai' +import { describe, it } from 'mocha' +import MockAdapter from 'axios-mock-adapter' +import { Teams } from '../../lib/organization/team' +import { systemUidMock, teamsMock, noticeMock } from './mock/objects' + +describe('Contentstack Team test', () => { + it('team create test', done => { + var mock = new MockAdapter(Axios) + mock.onPost(`/organizations/organization_uid/teams`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams() + .create() + .then((team) => { + checkTeams(team) + done() + }) + .catch(done) + }) + it('Team fetch test', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams/UID`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .fetch() + .then((team) => { + console.log('fetch', team) + checkTeams(team) + done() + }) + .catch(done) + }) + it('Teams query test', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams`).reply(200, { + teams: [ + teamsMock + ] + }) + makeTeams() + .query() + .then((teams) => { + console.log(teams) + checkTeams(teams.items[0]) + done() + }) + .catch(done) + }) + it('Team update test', done => { + var mock = new MockAdapter(Axios) + mock.onPut(`/organizations/organization_uid/teams/UID`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .update() + .then((team) => { + checkTeams(team) + done() + }) + .catch(done) + }) + it('team delete test', done => { + var mock = new MockAdapter(Axios) + mock.onDelete(`/organizations/organization_uid/teams/UID`).reply(200, { + ...noticeMock + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .delete() + .then((team) => { + expect(team.notice).to.be.equal(noticeMock.notice) + done() + }) + .catch(done) + }) +}) + +function makeTeams (data = {}) { + return new Teams(Axios, { organization_uid: 'organization_uid', ...data }) +} + +function checkTeams (teams) { + expect(teams.name).to.be.equal('name') +} From 393d8c0eb4b0bb12b42bef56ac45673a8d94e7c6 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 1 Nov 2023 18:40:03 +0530 Subject: [PATCH 02/32] docs: added teams documentation --- lib/organization/team/index.js | 112 ++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 14 deletions(-) diff --git a/lib/organization/team/index.js b/lib/organization/team/index.js index 891415a3..d3edd4c1 100644 --- a/lib/organization/team/index.js +++ b/lib/organization/team/index.js @@ -3,38 +3,122 @@ import { create, fetch, update, - query, deleteEntity, - parseData, fetchAll } from '../../entity' +import { TeamUsers } from './teamUsers' export function Teams (http, data) { - this.organization_uid = data.organization_uid + this.organizationUid = data.organizationUid + this.urlPath = `/organizations/${this.organizationUid}/teams` + if (data && data.uid) { + Object.assign(this, cloneDeep(data)) - this.urlPath = `/organizations/${this.organization_uid}/teams` - - if (data && data.team) { - Object.assign(this, cloneDeep(data.team)) - console.log('org uid 2', this.organization_uid) - - this.urlPath = `/organizations/${this.organization_uid}/teams/${this.uid}` + this.urlPath = `/organizations/${this.organizationUid}/teams/${this.uid}` + /** + * @description The update call on team will allow to update details of team. + * @memberof Teams + * @func update + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * client.organization(s'organizationUid').teams('teamUid') + * .then((response) => console.log(response)) + * + */ this.update = update(http, 'team') + /** + * @description The delete call on team will delete the existing team. + * @memberof Teams + * @func delete + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * client.organization('organizationUid').teams('teamUid').delete() + * .then((response) => console.log(response)) + * + */ this.delete = deleteEntity(http) + /** + * @description The fetch call on team will delete the existing team. + * @memberof Teams + * @func fetch + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * client.organization('organizationUid').teams('teamUid').fetch() + * .then((response) => console.log(response)) + * + */ this.fetch = fetch(http, 'team') + + /** + * @description The users call on team will get users details. + * @memberof Teams + * @func users + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * client.organization('organizationUid').teams('teamUid').users().fetchAll() + * .then((response) => console.log(response)) + * + */ + this.users = (userId = null) => { + data.organizationUid = this.organizationUid + data.teamUid = this.uid + if (userId) { + data.userId = userId + } + return new TeamUsers(http, data) + } } else { + /** + * @description The fetch call on team will delete the existing team. + * @memberof Teams + * @func create + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * const team = { + * name: 'name', + * organizationUid: 'organization_uid', + * users: [], + * stackRoleMapping: [], + * organizationRole: 'organizationRole' + * } + * client.organization('organizationUid').teams().create(team) + * .then((response) => console.log(response)) + * + */ this.create = create({ http }) - this.query = fetchAll(http, TeamsCollection) - // this.query = query({ http: http, wrapperCollection: TeamsCollection }) + + /** + * @description The fetchAll on team will allow to fetch details of all teams. + * @memberof Teams + * @func fetchAll + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.organization('organizationUid').teams().fetchAll() + * .then((response) => console.log(response)) + */ + this.fetchAll = fetchAll(http, TeamsCollection) } } export function TeamsCollection (http, teamsData) { - const obj = cloneDeep(teamsData.teams) || [] + const obj = cloneDeep(teamsData) || [] const teamsCollection = obj.map((team) => { - return new Teams(http, { team: team }) + return new Teams(http, team) }) return teamsCollection } From c575b4b790dc8c83afdbdaa73b54cd695ce60b8b Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 1 Nov 2023 18:41:36 +0530 Subject: [PATCH 03/32] test: :white_check_mark: updated api and unit test cases for teams support --- test/api/team-test.js | 105 ++++++++++++++++---------------------- test/unit/mock/objects.js | 8 ++- test/unit/team-test.js | 75 ++++++++++----------------- 3 files changed, 76 insertions(+), 112 deletions(-) diff --git a/test/api/team-test.js b/test/api/team-test.js index 16d2dae5..9845c726 100644 --- a/test/api/team-test.js +++ b/test/api/team-test.js @@ -1,78 +1,61 @@ import { describe, it, beforeEach } from 'mocha' import { expect } from 'chai' import { jsonReader } from '../utility/fileOperations/readwrite' -// import * as contentstack from '../../lib/contentstack.js' import { contentstackClient } from '../utility/ContentstackClient.js' -var client = {} +let client = {} -var org_uid = 'blt242b133b4e9bd736' -const team_uid = '6539eed4b502f0a73415188e' -const team = { - name: 'team_name', - users: [], - stackRoleMapping: [], - organizationRole: 'organization_role_uid' -} +const organizationUid = 'organizationUid' +const teamUid = 'teamUid' +const deleteUid = 'deleteUid' describe('Teams API Test', () => { beforeEach(() => { const user = jsonReader('loggedinuser.json') client = contentstackClient(user.authtoken) }) - it('Query and get all teams', async () => { - try { - const response = await client.organization(org_uid).teams().query() - console.log('res', response) - } catch (err) { - console.log(err) - } + it('should get all the teams when correct organization uid is passed', async () => { + const response = await makeTeams(organizationUid).fetchAll() + expect(response.items[0].organizationUid).to.be.equal(organizationUid) + expect(response.items[0].name).not.to.be.equal(null) + expect(response.items[0].created_by).not.to.be.equal(null) + expect(response.items[0].updated_by).not.to.be.equal(null) + expect(response.items[0].users).to.be.an('array') + }) + it('should fetch the team when correct organization uid and team uid is passed', async () => { + const response = await makeTeams(organizationUid, teamUid).fetch() + expect(response.uid).to.be.equal(teamUid) + expect(response.organizationUid).to.be.equal(organizationUid) + expect(response.name).not.to.be.equal(null) + expect(response.created_by).not.to.be.equal(null) + expect(response.updated_by).not.to.be.equal(null) + expect(response.users).to.be.an('array') + }) + it('should create new team when required object is passed', async () => { + const response = await makeTeams(organizationUid).create({ + name: 'test_team', + users: [], + stackRoleMapping: [], + organizationRole: 'organizationRoleUid' }) + expect(response.name).not.to.be.equal(null) + expect(response.stackRoleMapping).not.to.be.equal(null) + expect(response.organizationRole).not.to.be.equal(null) + expect(response.users).to.be.an('array') + }) + it('should delete team when correct organization uid and team uid is passed', async () => { + const response = await makeTeams(organizationUid, deleteUid).delete() + expect(response.status).not.to.be.equal(204) }) - it('fetch test', async () => { - try { - const response = await client.organization(org_uid).teams(team_uid).fetch() - console.log('res2', await response) - } catch (err) { - console.log(err) - } + it('should update team when updating data is passed', async () => { + const response = await makeTeams(organizationUid, teamUid).fetch() + .then((team) => { + team.name = 'updateName' + return team.update() + }) + console.log('update', response) }) - // it('create test', async () => { - // try { - // const response = await makeTeams(org_uid).create({ - // name: 't2', - // users: [], - // stackRoleMapping: [], - // organizationRole: 'blt09e5dfced326aaea' }) - // console.log('res2', response) - // } catch (err) { - // console.log(err) - // } - // }) - // it('delete test', async () => { - // try { - // const response = await makeTeams(org_uid, '').delete() - // console.log('res2', response) - // } catch (err) { - // console.log(err) - // } - // }) - // it('update test', async () => { - // try { - // var response = await makeTeams(org_uid, team_uid).fetch() - // .then((team) => { - // team.name = 'updated' - // console.log('tttt', team) - // return response.update() - // }) - // // const res = response.update() - // console.log('update', response) - // // console.log('update12', res) - // } catch (err) { - // console.log(err) - // } - // }) }) -function makeTeams (org_uid, team_uid = null) { - return client.organization(org_uid).teams(team_uid) +function makeTeams (organizationUid, teamUid = null) { + return client.organization(organizationUid).teams(teamUid) } diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index a30a2b51..cc610e57 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -775,9 +775,13 @@ const termsMock = { }] } const teamsMock = { - organization_uid: 'organization_uid', uid: 'UID', - name: 'name' + name: 'name', + organizationUid: 'organization_uid', + users: [], + stackRoleMapping: [], + organizationRole: 'organizationRole' + } function mockCollection (mockData, type) { const mock = { diff --git a/test/unit/team-test.js b/test/unit/team-test.js index 6660a35b..2ff96cce 100644 --- a/test/unit/team-test.js +++ b/test/unit/team-test.js @@ -6,69 +6,50 @@ import { Teams } from '../../lib/organization/team' import { systemUidMock, teamsMock, noticeMock } from './mock/objects' describe('Contentstack Team test', () => { - it('team create test', done => { + it('should get all the teams when correct organization uid is passed', done => { var mock = new MockAdapter(Axios) - mock.onPost(`/organizations/organization_uid/teams`).reply(200, { - team: { - ...teamsMock - } - }) - makeTeams() - .create() - .then((team) => { - checkTeams(team) + mock.onGet(`/organizations/organization_uid/teams`).reply(200, [teamsMock]) + makeTeams().fetchAll() + .then((teams) => { + expect(teams.items[0].uid).to.be.equal('UID') + checkTeams(teams.items[0]) done() }) .catch(done) }) - it('Team fetch test', done => { + it('should fetch the team when correct organization uid and team uid is passed', done => { var mock = new MockAdapter(Axios) - mock.onGet(`/organizations/organization_uid/teams/UID`).reply(200, { - team: { - ...teamsMock - } - }) - makeTeams({ - team: { - ...systemUidMock - } - }) + mock.onGet(`/organizations/organization_uid/teams/UID`).reply(200, { ...teamsMock }) + makeTeams({ ...systemUidMock }) .fetch() .then((team) => { - console.log('fetch', team) checkTeams(team) done() }) .catch(done) }) - it('Teams query test', done => { + it('should create new team when required object is passedt', done => { var mock = new MockAdapter(Axios) - mock.onGet(`/organizations/organization_uid/teams`).reply(200, { - teams: [ - teamsMock - ] - }) + mock.onPost(`/organizations/organization_uid/teams`).reply(200, { ...teamsMock }) makeTeams() - .query() - .then((teams) => { - console.log(teams) - checkTeams(teams.items[0]) + .create({ + name: 'name', + organizationUid: 'organization_uid', + users: [], + stackRoleMapping: [], + organizationRole: 'organizationRole' + }) + .then((team) => { + checkTeams(team) done() }) .catch(done) }) - it('Team update test', done => { + it('should update team when updating data is passed', done => { var mock = new MockAdapter(Axios) - mock.onPut(`/organizations/organization_uid/teams/UID`).reply(200, { - team: { - ...teamsMock - } - }) - makeTeams({ - team: { - ...systemUidMock - } + mock.onPut(`/organizations/organization_uid/teams/UID`).reply(200, { ...teamsMock }) + makeTeams({ ...systemUidMock }) .update() .then((team) => { checkTeams(team) @@ -76,16 +57,12 @@ describe('Contentstack Team test', () => { }) .catch(done) }) - it('team delete test', done => { + it('should delete team when correct organization uid and team uid is passed', done => { var mock = new MockAdapter(Axios) mock.onDelete(`/organizations/organization_uid/teams/UID`).reply(200, { ...noticeMock }) - makeTeams({ - team: { - ...systemUidMock - } - }) + makeTeams({ ...systemUidMock }) .delete() .then((team) => { expect(team.notice).to.be.equal(noticeMock.notice) @@ -96,7 +73,7 @@ describe('Contentstack Team test', () => { }) function makeTeams (data = {}) { - return new Teams(Axios, { organization_uid: 'organization_uid', ...data }) + return new Teams(Axios, { organizationUid: 'organization_uid', ...data }) } function checkTeams (teams) { From 797ba45d448080f9d5a5d4349884ddc6b673e6d5 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Wed, 1 Nov 2023 18:43:24 +0530 Subject: [PATCH 04/32] feat: :sparkles: teams users implementation and api test cases --- lib/organization/team/teamUsers/index.js | 72 ++++++++++++++++++++++++ test/api/team-users-test.js | 45 +++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 lib/organization/team/teamUsers/index.js create mode 100644 test/api/team-users-test.js diff --git a/lib/organization/team/teamUsers/index.js b/lib/organization/team/teamUsers/index.js new file mode 100644 index 00000000..fad5e435 --- /dev/null +++ b/lib/organization/team/teamUsers/index.js @@ -0,0 +1,72 @@ +import cloneDeep from 'lodash/cloneDeep' +import { + create, + deleteEntity, + query +} from '../../../entity' + +export function TeamUsers (http, data) { + if (data && data.userId) { + Object.assign(this, cloneDeep(data)) + + const _urlPath = `/organizations/${this.organizationUid}/teams/${this.teamUid}/users/${data.userId}` + if (this.organizationUid) this.urlPath = _urlPath + + /** + * @description The Remove teamUser call is used to remove an existing user of that team. + * @memberof TeamUsers + * @func remove + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.organization('organizationUid').teams('teamUid').users('userId').remove() + * .then((response) => console.log(response)) + * + */ + this.remove = deleteEntity(http) + } else { + this.urlPath = `/organizations/${data.organizationUid}/teams/${data.teamUid}/users` + + /** + * @description The Add teamUser call is used to add an user the team. + * @memberof TeamUsers + * @func add + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * const usersMail = { + * emails: ['emailId1','emailId2' ] + * } + * client.organization('organizationUid').teams('teamUid').users('userId').add(usersMail) + * .then((response) => console.log(response)) + * + */ + this.add = create({ http }) + + /** + * @description The Query on teamUser will allow to fetch details of all teamUsers. + * @memberof TeamUsers + * @func query + * @returns {Promise} Response Object. + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * const usersMail = { + * emails: ['emailId1','emailId2' ]} + * client.organization('organizationUid').teams('teamUid').users('userId').query().find() + * .then((response) => console.log(response)) + * + */ + this.query = query({ http: http, wrapperCollection: UsersCollection }) + } +} +export function UsersCollection (http, data) { + const obj = cloneDeep(data.users) || [] + const usersCollection = obj.map((user) => { + return new TeamUsers(http, { userId: user }) + }) + return usersCollection +} diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js new file mode 100644 index 00000000..63ddb8a2 --- /dev/null +++ b/test/api/team-users-test.js @@ -0,0 +1,45 @@ +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 organizationUid = 'organizationUid' +const teamUid = 'teamUid' +const userId = 'userId' + +describe('Teams API Test', () => { + beforeEach(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + it('should add the user when user\'s mail is passed', async () => { + const usersMail = { + emails: ['email@email.com'] + } + makeUsers(organizationUid, teamUid).add(usersMail).then((response) => { + expect(response).to.be.equal(null) + }) + }) + it('should remove the user when uid is passed', async () => { + makeUsers(organizationUid, teamUid, userId).remove().then((response) => { + expect(response).to.be.equal(null) + expect(response.status).to.be.equal(204) + }) + }) + it('should fetch all users', async () => { + makeUsers(organizationUid, teamUid) + .query() + .find() + .then((response) => { + response.items.forEach((termResponse) => { + expect(termResponse.uidId).to.be.not.equal(null) + }) + }) + }) +}) + +function makeUsers (organizationUid, teamUid, userId = null) { + return client.organization(organizationUid).teams(teamUid).users(userId) +} From fec38520742747baa5d86dea1f38e77235213802 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 2 Nov 2023 13:12:20 +0530 Subject: [PATCH 05/32] test: :white_check_mark: added unit test cases for team users --- lib/organization/index.js | 24 ++++++++++++----- test/api/team-users-test.js | 11 +++++--- test/test.js | 1 + test/unit/index.js | 2 ++ test/unit/mock/objects.js | 8 +++++- test/unit/team-users-test.js | 50 ++++++++++++++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 11 deletions(-) create mode 100644 test/unit/team-users-test.js diff --git a/lib/organization/index.js b/lib/organization/index.js index 3d51755f..fb55bcf7 100644 --- a/lib/organization/index.js +++ b/lib/organization/index.js @@ -7,7 +7,7 @@ import { StackCollection } from '../stack' import { UserCollection } from '../user' import { App } from '../app' import { AppRequest } from '../app/request' -import { Teams, TeamsCollection } from './team' +import { Teams } from './team' /** * Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about Organizations.. * @namespace Organization @@ -19,11 +19,23 @@ export function Organization (http, data) { Object.assign(this, cloneDeep(data.organization)) this.urlPath = `/organizations/${this.uid}` - this.teams = (team_uid = null) => { - delete data.organization - data.organization_uid = this.uid - if (team_uid) { - data.team = { uid: team_uid } + /** + * @description The teams call fetches teams details. + * @memberof Organization + * @func teams + * @returns {Promise} Promise for Organization instance + * @example + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client() + * + * client.organization('organization_uid').teams('teamsUid').fetch() + * .then((organization) => console.log(organization)) + * + */ + this.teams = (teamUid = null) => { + data.organizationUid = this.uid + if (teamUid) { + data.uid = teamUid } return new Teams(http, data) } diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index 63ddb8a2..9bdbd3d8 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -14,27 +14,30 @@ describe('Teams API Test', () => { const user = jsonReader('loggedinuser.json') client = contentstackClient(user.authtoken) }) - it('should add the user when user\'s mail is passed', async () => { + it.only('should add the user when user\'s mail is passed', done => { const usersMail = { emails: ['email@email.com'] } makeUsers(organizationUid, teamUid).add(usersMail).then((response) => { expect(response).to.be.equal(null) }) + .catch(done) }) - it('should remove the user when uid is passed', async () => { + it('should remove the user when uid is passed', done => { makeUsers(organizationUid, teamUid, userId).remove().then((response) => { expect(response).to.be.equal(null) expect(response.status).to.be.equal(204) + done() }) + .catch(done) }) it('should fetch all users', async () => { makeUsers(organizationUid, teamUid) .query() .find() .then((response) => { - response.items.forEach((termResponse) => { - expect(termResponse.uidId).to.be.not.equal(null) + response.items.forEach((user) => { + expect(user.uidId).to.be.not.equal(null) }) }) }) diff --git a/test/test.js b/test/test.js index 5cb5e213..7f152fa4 100644 --- a/test/test.js +++ b/test/test.js @@ -28,3 +28,4 @@ require('./api/delete-test') require('./api/taxonomy-test') require('./api/terms-test') require('./api/team-test') +require('./api/team-users-test') diff --git a/test/unit/index.js b/test/unit/index.js index 2877b750..68d35803 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -35,3 +35,5 @@ require('./authorization-test') require('./auditLog-test') require('./taxonomy-test') require('./terms-test') +require('./team-test') +require('./team-users-test') diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index cc610e57..a5acf20e 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -781,7 +781,12 @@ const teamsMock = { users: [], stackRoleMapping: [], organizationRole: 'organizationRole' - +} +const teamUsersMock = { + users: ['user1', 'user2', 'UID'], + addUser: { + userId: 'UID' + } } function mockCollection (mockData, type) { const mock = { @@ -854,6 +859,7 @@ export { taxonomyMock, termsMock, teamsMock, + teamUsersMock, mockCollection, entryMockCollection, checkSystemFields diff --git a/test/unit/team-users-test.js b/test/unit/team-users-test.js new file mode 100644 index 00000000..b9346790 --- /dev/null +++ b/test/unit/team-users-test.js @@ -0,0 +1,50 @@ +import Axios from 'axios' +import { expect } from 'chai' +import { describe, it } from 'mocha' +import MockAdapter from 'axios-mock-adapter' +import { TeamUsers } from '../../lib/organization/team/teamUsers' +import { teamUsersMock, noticeMock } from './mock/objects' + +describe('Contentstack Team 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() + .then((users) => { + users.items.forEach((user) => { + expect(user.uidId).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + it('should add the user when user\'s mail is passed', done => { + var mock = new MockAdapter(Axios) + mock.onPost(`/organizations/organization_uid/teams/team_uid/users`).reply(200, teamUsersMock.addUser) + const usersMail = { + emails: ['email@email.com'] + } + makeTeams() + .add(usersMail) + .then((team) => { + expect(team.userId).to.be.equal('UID') + done() + }) + .catch(done) + }) + 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) + .remove() + .then((user) => { + expect(user.notice).to.be.equal(noticeMock.notice) + done() + }) + .catch(done) + }) +}) + +function makeTeams (data = {}) { + return new TeamUsers(Axios, { organizationUid: 'organization_uid', teamUid: 'team_uid', ...data }) +} From 99fc8c1ca509557cd35216d5eb5a3f8446ca2e01 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 6 Nov 2023 12:34:22 +0530 Subject: [PATCH 06/32] feat: :sparkles: stackrolemapping implementation and api test cases --- .jsdoc.json | 3 +- lib/organization/team/index.js | 10 ++ .../team/stackRoleMapping/index.js | 95 +++++++++++++++++++ test/api/team-stack-role-mapping-test.js | 74 +++++++++++++++ test/api/team-users-test.js | 2 +- test/test.js | 1 + 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 lib/organization/team/stackRoleMapping/index.js create mode 100644 test/api/team-stack-role-mapping-test.js diff --git a/.jsdoc.json b/.jsdoc.json index 6da32180..d0d78b59 100644 --- a/.jsdoc.json +++ b/.jsdoc.json @@ -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)" diff --git a/lib/organization/team/index.js b/lib/organization/team/index.js index d3edd4c1..9792d0f2 100644 --- a/lib/organization/team/index.js +++ b/lib/organization/team/index.js @@ -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 @@ -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. diff --git a/lib/organization/team/stackRoleMapping/index.js b/lib/organization/team/stackRoleMapping/index.js new file mode 100644 index 00000000..78a0a703 --- /dev/null +++ b/lib/organization/team/stackRoleMapping/index.js @@ -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} 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} 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} 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} 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 +} diff --git a/test/api/team-stack-role-mapping-test.js b/test/api/team-stack-role-mapping-test.js new file mode 100644 index 00000000..8b1e5bc8 --- /dev/null +++ b/test/api/team-stack-role-mapping-test.js @@ -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 diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index 9bdbd3d8..d31616d4 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -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@email.com'] } diff --git a/test/test.js b/test/test.js index 7f152fa4..8b4ec6b2 100644 --- a/test/test.js +++ b/test/test.js @@ -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') From c8425fb6560346b60d58975d9bd3f0507f44201b Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 6 Nov 2023 14:39:59 +0530 Subject: [PATCH 07/32] test: changes made in update implementation and its test case --- lib/organization/team/index.js | 25 ++++++++++++++++++++++--- test/api/team-test.js | 28 ++++++++++++++++++---------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/organization/team/index.js b/lib/organization/team/index.js index 9792d0f2..b3a6bb7f 100644 --- a/lib/organization/team/index.js +++ b/lib/organization/team/index.js @@ -2,12 +2,12 @@ import cloneDeep from 'lodash/cloneDeep' import { create, fetch, - update, deleteEntity, fetchAll } from '../../entity' import { TeamUsers } from './teamUsers' import { StackRoleMappings } from './stackRoleMapping' +import error from '../../core/contentstackError' export function Teams (http, data) { this.organizationUid = data.organizationUid @@ -25,11 +25,30 @@ export function Teams (http, data) { * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() - * client.organization(s'organizationUid').teams('teamUid') + * const updateData = { + * name: 'updatedname', + * users: [ + * { + * email: 'abc@abc.com' + * } + * ], + * organizationRole: 'blt09e5dfced326aaea', + * stackRoleMapping: [] + * } + * client.organization(s'organizationUid').teams('teamUid').update(updateData) * .then((response) => console.log(response)) * */ - this.update = update(http, 'team') + this.update = async (updateData, params = {}) => { + try { + const response = await http.put(this.urlPath, updateData, { params }) + if (response.data) { + return response.data + } + } catch (err) { + throw error(err) + } + } /** * @description The delete call on team will delete the existing team. diff --git a/test/api/team-test.js b/test/api/team-test.js index 9845c726..825c1af8 100644 --- a/test/api/team-test.js +++ b/test/api/team-test.js @@ -20,7 +20,6 @@ describe('Teams API Test', () => { expect(response.items[0].name).not.to.be.equal(null) expect(response.items[0].created_by).not.to.be.equal(null) expect(response.items[0].updated_by).not.to.be.equal(null) - expect(response.items[0].users).to.be.an('array') }) it('should fetch the team when correct organization uid and team uid is passed', async () => { const response = await makeTeams(organizationUid, teamUid).fetch() @@ -29,30 +28,39 @@ describe('Teams API Test', () => { expect(response.name).not.to.be.equal(null) expect(response.created_by).not.to.be.equal(null) expect(response.updated_by).not.to.be.equal(null) - expect(response.users).to.be.an('array') }) it('should create new team when required object is passed', async () => { const response = await makeTeams(organizationUid).create({ - name: 'test_team', + name: 'test_team11111', users: [], stackRoleMapping: [], - organizationRole: 'organizationRoleUid' }) + organizationRole: 'blt09e5dfced326aaea' }) + expect(response.uid).not.to.be.equal(null) expect(response.name).not.to.be.equal(null) expect(response.stackRoleMapping).not.to.be.equal(null) expect(response.organizationRole).not.to.be.equal(null) - expect(response.users).to.be.an('array') }) it('should delete team when correct organization uid and team uid is passed', async () => { const response = await makeTeams(organizationUid, deleteUid).delete() - expect(response.status).not.to.be.equal(204) + expect(response.status).to.be.equal(204) }) it('should update team when updating data is passed', async () => { - const response = await makeTeams(organizationUid, teamUid).fetch() + const updateData = { + name: 'name', + users: [ + { + email: 'abc@abc.com' + } + ], + organizationRole: '', + stackRoleMapping: [] + } + await makeTeams(organizationUid, teamUid).update(updateData) .then((team) => { - team.name = 'updateName' - return team.update() + expect(team.name).to.be.equal(updateData.name) + expect(team.createdByUserName).not.to.be.equal(undefined) + expect(team.updatedByUserName).not.to.be.equal(undefined) }) - console.log('update', response) }) }) From cd9dea7aac40ead9bcfe4bae32bcbc9ff772f64e Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 6 Nov 2023 19:03:11 +0530 Subject: [PATCH 08/32] test: update stackrolemapping implementation and api test cases --- .../team/stackRoleMapping/index.js | 35 +++++++++++++------ test/api/team-stack-role-mapping-test.js | 19 +++++----- test/api/team-users-test.js | 1 - 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/organization/team/stackRoleMapping/index.js b/lib/organization/team/stackRoleMapping/index.js index 78a0a703..531167b0 100644 --- a/lib/organization/team/stackRoleMapping/index.js +++ b/lib/organization/team/stackRoleMapping/index.js @@ -3,19 +3,17 @@ */ import cloneDeep from 'lodash/cloneDeep' import { - create, - update, deleteEntity, fetchAll } from '../../../entity' +import error from '../../../core/contentstackError' 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}` + if (this.organizationUid) this.urlPath = `${_urlPath}/${this.stackApiKey}` /** * @description The update stackRoleMappings call is used to update the roles. * @memberof StackRoleMappings @@ -33,7 +31,16 @@ export function StackRoleMappings (http, data) { * client.organization('organizationUid').teams('teamUid').stackRoleMappings('stackApiKey').update(updateRoles) * .then((response) => console.log(response)) */ - this.update = update(http, 'stackRoleMapping') + this.update = async (updateData, params = {}) => { + try { + const response = await http.put(this.urlPath, updateData, { params }) + if (response.data) { + return response.data + } + } catch (err) { + throw error(err) + } + } /** * @description The delete stackRoleMappings call is used to delete the roles. @@ -49,7 +56,6 @@ export function StackRoleMappings (http, data) { */ 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. @@ -69,7 +75,16 @@ export function StackRoleMappings (http, data) { * client.organization('organizationUid').teams('teamUid').stackRoleMappings().add(addRole) * .then((response) => console.log(response)) */ - this.add = create({ http }) + this.add = async (updateData, params = {}) => { + try { + const response = await http.post(this.urlPath, updateData, { params }) + if (response.data) { + return response.data + } + } catch (err) { + throw error(err) + } + } /** * @description The fetchAll stackRoleMappings call is used to fetchAll the roles. @@ -87,9 +102,9 @@ export function StackRoleMappings (http, data) { } } export function stackRoleMappingsCollection (http, data) { - const obj = cloneDeep(data.stackRoleMappings) || [] + const obj = cloneDeep(data) || [] const stackRoleMappingCollection = obj.map((stackRoleMappings) => { - return new StackRoleMappings(http, { stackRoleMappings: stackRoleMappings }) + return stackRoleMappings(http, { stackRoleMappings: stackRoleMappings }) }) - return stackRoleMappingCollection + return new StackRoleMappings(http, stackRoleMappingCollection) } diff --git a/test/api/team-stack-role-mapping-test.js b/test/api/team-stack-role-mapping-test.js index 8b1e5bc8..e52597bf 100644 --- a/test/api/team-stack-role-mapping-test.js +++ b/test/api/team-stack-role-mapping-test.js @@ -14,16 +14,14 @@ describe('Teams API Test', () => { 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) + it('should fetch all stackRoleMappings', async () => { + try { + makestackRoleMappings(organizationUid, teamUid).fetchAll().then((response) => { + console.log('🚀 ~ file: team-stack-role-mapping-test.js:19 ~ makestackRoleMappings ~ response:', response.stackRoleMappings[0].roles) }) - expect(response).to.be.not.equal(null) - done() - }) - .catch(done) + } catch (err) { + console.log("🚀 ~ file: team-stack-role-mapping-test.js:21 ~ it.only ~ err:", err) + } }) it('should add roles', async () => { try { @@ -31,7 +29,6 @@ describe('Teams API Test', () => { stackApiKey: 'stackApiKey', roles: [ 'role_uid' - ] } await makestackRoleMappings(organizationUid, teamUid).add(stackRoleMappings).then((response) => { @@ -72,3 +69,5 @@ function makestackRoleMappings (organizationUid, teamUid, stackApiKey = null) { } // delete done +// update done +// add done diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index d31616d4..861a9045 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -25,7 +25,6 @@ describe('Teams API Test', () => { }) it('should remove the user when uid is passed', done => { makeUsers(organizationUid, teamUid, userId).remove().then((response) => { - expect(response).to.be.equal(null) expect(response.status).to.be.equal(204) done() }) From 864db844f082ec4c448c9104e6de2b4434010b07 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 09:44:02 +0530 Subject: [PATCH 09/32] added assertions to check the response --- test/api/team-stack-role-mapping-test.js | 85 +++++++++++------------- 1 file changed, 40 insertions(+), 45 deletions(-) diff --git a/test/api/team-stack-role-mapping-test.js b/test/api/team-stack-role-mapping-test.js index e52597bf..092b31eb 100644 --- a/test/api/team-stack-role-mapping-test.js +++ b/test/api/team-stack-role-mapping-test.js @@ -14,60 +14,55 @@ describe('Teams API Test', () => { const user = jsonReader('loggedinuser.json') client = contentstackClient(user.authtoken) }) - it('should fetch all stackRoleMappings', async () => { - try { - makestackRoleMappings(organizationUid, teamUid).fetchAll().then((response) => { - console.log('🚀 ~ file: team-stack-role-mapping-test.js:19 ~ makestackRoleMappings ~ response:', response.stackRoleMappings[0].roles) + it('should fetch all stackRoleMappings', done => { + makestackRoleMappings(organizationUid, teamUid).fetchAll().then((response) => { + response.items.forEach((stackRoleMapping) => { + console.log(stackRoleMapping) }) - } catch (err) { - console.log("🚀 ~ file: team-stack-role-mapping-test.js:21 ~ it.only ~ err:", err) - } + 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 add roles', done => { + const stackRoleMappings = { + stackApiKey: 'stackApiKey', + roles: [ + 'role_uid' + ] } + makestackRoleMappings(organizationUid, teamUid).add(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(stackRoleMappings.stackApiKey) + done() + }) + .catch(done) }) - 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 update roles', done => { + const stackRoleMappings = { + roles: [ + 'role_uid1', + 'role_uid2' + ] } + makestackRoleMappings(organizationUid, teamUid, 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 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) - } + it('should delete roles', done => { + makestackRoleMappings(organizationUid, teamUid, stackApiKey).delete().then((response) => { + expect(response.status).to.be.equal(204) + done() + }) + .catch(done) }) }) function makestackRoleMappings (organizationUid, teamUid, stackApiKey = null) { return client.organization(organizationUid).teams(teamUid).stackRoleMappings(stackApiKey) } - -// delete done -// update done -// add done From 231711ff004db82122b93d521baacc751a8f0396 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 10:30:12 +0530 Subject: [PATCH 10/32] added unit test cases for stack role mapping --- test/unit/index.js | 1 + test/unit/mock/objects.js | 11 +++ test/unit/team-stack-role-mapping-test.js | 88 +++++++++++++++++++++++ test/unit/team-users-test.js | 10 +-- 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 test/unit/team-stack-role-mapping-test.js diff --git a/test/unit/index.js b/test/unit/index.js index 68d35803..1eccb05e 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -37,3 +37,4 @@ require('./taxonomy-test') require('./terms-test') require('./team-test') require('./team-users-test') +require('./team-stack-role-mapping-test') diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index a5acf20e..a8dbf9a9 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -788,6 +788,16 @@ const teamUsersMock = { userId: 'UID' } } +const stackRoleMappingMock = { + stackRoleMappings: [ + { + stackApiKey: 'stackApiKey', + roles: [ + 'roles_uid' + ] + } + ] +} function mockCollection (mockData, type) { const mock = { ...cloneDeep(noticeMock), @@ -860,6 +870,7 @@ export { termsMock, teamsMock, teamUsersMock, + stackRoleMappingMock, mockCollection, entryMockCollection, checkSystemFields diff --git a/test/unit/team-stack-role-mapping-test.js b/test/unit/team-stack-role-mapping-test.js new file mode 100644 index 00000000..b7c2e4fa --- /dev/null +++ b/test/unit/team-stack-role-mapping-test.js @@ -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 }) +} diff --git a/test/unit/team-users-test.js b/test/unit/team-users-test.js index b9346790..14ac8433 100644 --- a/test/unit/team-users-test.js +++ b/test/unit/team-users-test.js @@ -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) @@ -24,7 +24,7 @@ describe('Contentstack Team test', () => { const usersMail = { emails: ['email@email.com'] } - makeTeams() + makeTeamUsers() .add(usersMail) .then((team) => { expect(team.userId).to.be.equal('UID') @@ -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) @@ -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 }) } From 4fcf2171b1f097953215a2290ee7f4ddc6f7bc92 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 16:30:51 +0530 Subject: [PATCH 11/32] replaced query with fetchAll function --- lib/organization/team/stackRoleMapping/index.js | 16 ++++++++++++---- lib/organization/team/teamUsers/index.js | 4 ++-- test/api/team-stack-role-mapping-test.js | 5 +---- test/api/team-users-test.js | 5 ++--- test/unit/team-users-test.js | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/organization/team/stackRoleMapping/index.js b/lib/organization/team/stackRoleMapping/index.js index 531167b0..2d34d5f4 100644 --- a/lib/organization/team/stackRoleMapping/index.js +++ b/lib/organization/team/stackRoleMapping/index.js @@ -3,8 +3,7 @@ */ import cloneDeep from 'lodash/cloneDeep' import { - deleteEntity, - fetchAll + deleteEntity } from '../../../entity' import error from '../../../core/contentstackError' @@ -98,11 +97,20 @@ export function StackRoleMappings (http, data) { * client.organization('organizationUid').teams('teamUid').stackRoleMappings().fetchAll * .then((response) => console.log(response)) */ - this.fetchAll = fetchAll(http, stackRoleMappingsCollection) + this.fetchAll = async () => { + try { + const response = await http.get(this.urlPath) + if (response.data) { + return response.data + } + } catch (err) { + throw error(err) + } + } } } export function stackRoleMappingsCollection (http, data) { - const obj = cloneDeep(data) || [] + const obj = cloneDeep(data.stackRoleMappings) || [] const stackRoleMappingCollection = obj.map((stackRoleMappings) => { return stackRoleMappings(http, { stackRoleMappings: stackRoleMappings }) }) diff --git a/lib/organization/team/teamUsers/index.js b/lib/organization/team/teamUsers/index.js index fad5e435..b56b4c21 100644 --- a/lib/organization/team/teamUsers/index.js +++ b/lib/organization/team/teamUsers/index.js @@ -2,7 +2,7 @@ import cloneDeep from 'lodash/cloneDeep' import { create, deleteEntity, - query + fetchAll } from '../../../entity' export function TeamUsers (http, data) { @@ -60,7 +60,7 @@ export function TeamUsers (http, data) { * .then((response) => console.log(response)) * */ - this.query = query({ http: http, wrapperCollection: UsersCollection }) + this.fetchAll = fetchAll(http, UsersCollection) } } export function UsersCollection (http, data) { diff --git a/test/api/team-stack-role-mapping-test.js b/test/api/team-stack-role-mapping-test.js index 092b31eb..c94bc318 100644 --- a/test/api/team-stack-role-mapping-test.js +++ b/test/api/team-stack-role-mapping-test.js @@ -16,10 +16,7 @@ describe('Teams API Test', () => { }) it('should fetch all stackRoleMappings', done => { makestackRoleMappings(organizationUid, teamUid).fetchAll().then((response) => { - response.items.forEach((stackRoleMapping) => { - console.log(stackRoleMapping) - }) - expect(response).to.be.not.equal(null) + expect(response.stackRoleMappings).to.be.not.equal(undefined) done() }) .catch(done) diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index 861a9045..674cfb57 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -9,7 +9,7 @@ const organizationUid = 'organizationUid' const teamUid = 'teamUid' const userId = 'userId' -describe('Teams API Test', () => { +describe('Teams Users API Test', () => { beforeEach(() => { const user = jsonReader('loggedinuser.json') client = contentstackClient(user.authtoken) @@ -32,8 +32,7 @@ describe('Teams API Test', () => { }) it('should fetch all users', async () => { makeUsers(organizationUid, teamUid) - .query() - .find() + .fetchAll() .then((response) => { response.items.forEach((user) => { expect(user.uidId).to.be.not.equal(null) diff --git a/test/unit/team-users-test.js b/test/unit/team-users-test.js index 14ac8433..63302d9c 100644 --- a/test/unit/team-users-test.js +++ b/test/unit/team-users-test.js @@ -9,7 +9,7 @@ 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) - makeTeamUsers().query().find() + makeTeamUsers().fetchAll() .then((users) => { users.items.forEach((user) => { expect(user.uidId).to.be.not.equal(null) From 95d5f86aa793893d682f1927f9153569f30c6486 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 16:35:15 +0530 Subject: [PATCH 12/32] changing folder name to be same as class name --- .jsdoc.json | 5 +++-- lib/organization/index.js | 2 +- lib/organization/{team => teams}/index.js | 2 +- .../stackRoleMapping => teams/stackRoleMappings}/index.js | 0 lib/organization/{team => teams}/teamUsers/index.js | 0 test/unit/team-stack-role-mapping-test.js | 2 +- test/unit/team-test.js | 2 +- test/unit/team-users-test.js | 2 +- 8 files changed, 8 insertions(+), 7 deletions(-) rename lib/organization/{team => teams}/index.js (98%) rename lib/organization/{team/stackRoleMapping => teams/stackRoleMappings}/index.js (100%) rename lib/organization/{team => teams}/teamUsers/index.js (100%) diff --git a/.jsdoc.json b/.jsdoc.json index d0d78b59..c0363440 100644 --- a/.jsdoc.json +++ b/.jsdoc.json @@ -33,8 +33,9 @@ "lib/stack/webhook/index.js", "lib/stack/workflow/index.js", "lib/stack/workflow/publishRules/index.js", - "lib/organization/team/stackRoleMapping/index.js̀" - + "lib/organization/teams/index.js", + "lib/organization/teams/stackRoleMappings/index.js", + "lib/organization/teams/teamUsers/index.js" ], "excludePattern": "(node_modules/|jsdocs)" }, diff --git a/lib/organization/index.js b/lib/organization/index.js index fb55bcf7..725bd65a 100644 --- a/lib/organization/index.js +++ b/lib/organization/index.js @@ -7,7 +7,7 @@ import { StackCollection } from '../stack' import { UserCollection } from '../user' import { App } from '../app' import { AppRequest } from '../app/request' -import { Teams } from './team' +import { Teams } from './teams' /** * Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about Organizations.. * @namespace Organization diff --git a/lib/organization/team/index.js b/lib/organization/teams/index.js similarity index 98% rename from lib/organization/team/index.js rename to lib/organization/teams/index.js index b3a6bb7f..15475389 100644 --- a/lib/organization/team/index.js +++ b/lib/organization/teams/index.js @@ -6,7 +6,7 @@ import { fetchAll } from '../../entity' import { TeamUsers } from './teamUsers' -import { StackRoleMappings } from './stackRoleMapping' +import { StackRoleMappings } from './stackRoleMappings' import error from '../../core/contentstackError' export function Teams (http, data) { diff --git a/lib/organization/team/stackRoleMapping/index.js b/lib/organization/teams/stackRoleMappings/index.js similarity index 100% rename from lib/organization/team/stackRoleMapping/index.js rename to lib/organization/teams/stackRoleMappings/index.js diff --git a/lib/organization/team/teamUsers/index.js b/lib/organization/teams/teamUsers/index.js similarity index 100% rename from lib/organization/team/teamUsers/index.js rename to lib/organization/teams/teamUsers/index.js diff --git a/test/unit/team-stack-role-mapping-test.js b/test/unit/team-stack-role-mapping-test.js index b7c2e4fa..14dff3b2 100644 --- a/test/unit/team-stack-role-mapping-test.js +++ b/test/unit/team-stack-role-mapping-test.js @@ -2,7 +2,7 @@ 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 { StackRoleMappings } from '../../lib/organization/teams/stackRoleMappings' import { stackRoleMappingMock } from './mock/objects' describe('Contentstack Team Stack Role Mapping test', () => { diff --git a/test/unit/team-test.js b/test/unit/team-test.js index 2ff96cce..9c2ee28c 100644 --- a/test/unit/team-test.js +++ b/test/unit/team-test.js @@ -2,7 +2,7 @@ import Axios from 'axios' import { expect } from 'chai' import { describe, it } from 'mocha' import MockAdapter from 'axios-mock-adapter' -import { Teams } from '../../lib/organization/team' +import { Teams } from '../../lib/organization/teams' import { systemUidMock, teamsMock, noticeMock } from './mock/objects' describe('Contentstack Team test', () => { diff --git a/test/unit/team-users-test.js b/test/unit/team-users-test.js index 63302d9c..5bb9a4f1 100644 --- a/test/unit/team-users-test.js +++ b/test/unit/team-users-test.js @@ -2,7 +2,7 @@ import Axios from 'axios' import { expect } from 'chai' import { describe, it } from 'mocha' import MockAdapter from 'axios-mock-adapter' -import { TeamUsers } from '../../lib/organization/team/teamUsers' +import { TeamUsers } from '../../lib/organization/teams/teamUsers' import { teamUsersMock, noticeMock } from './mock/objects' describe('Contentstack Team Users test', () => { From 59827bf4cb814cfdf6f57779d494039ed7a41eca Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 7 Nov 2023 18:12:24 +0530 Subject: [PATCH 13/32] ci: :green_heart: fix unit test github action --- .github/workflows/unit-test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 5bb82fba..ecddab1e 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -23,6 +23,5 @@ jobs: reporter: mocha-json # Format of test results - name: Coverage report uses: lucassabreu/comment-coverage-clover@main - with: - name: Unit test Coverage report + with: file: coverage/clover.xml \ No newline at end of file From e79409fd48a501a6cb878239216776fc509f9c60 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 7 Nov 2023 18:20:40 +0530 Subject: [PATCH 14/32] ci: fix unit test github action --- .github/workflows/unit-test.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index ecddab1e..cb626701 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -1,7 +1,10 @@ name: Unit Test & Reports on: pull_request: - push: + branches: + - master + - main + - next jobs: build-test: name: Build & Test From 83c136cb91b8f9bb3b11fb381c077a193f2fed45 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 18:24:25 +0530 Subject: [PATCH 15/32] feat: types support for teams --- lib/organization/teams/index.js | 4 +-- test/typescript/teams.ts | 57 +++++++++++++++++++++++++++++++++ types/organization.d.ts | 3 ++ types/teams/index.d.ts | 28 ++++++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 test/typescript/teams.ts create mode 100644 types/teams/index.d.ts diff --git a/lib/organization/teams/index.js b/lib/organization/teams/index.js index 15475389..7585fb9d 100644 --- a/lib/organization/teams/index.js +++ b/lib/organization/teams/index.js @@ -39,9 +39,9 @@ export function Teams (http, data) { * .then((response) => console.log(response)) * */ - this.update = async (updateData, params = {}) => { + this.update = async (updateData) => { try { - const response = await http.put(this.urlPath, updateData, { params }) + const response = await http.put(this.urlPath, updateData) if (response.data) { return response.data } diff --git a/test/typescript/teams.ts b/test/typescript/teams.ts new file mode 100644 index 00000000..ad26c859 --- /dev/null +++ b/test/typescript/teams.ts @@ -0,0 +1,57 @@ +import { expect } from "chai"; +import { Organization } from "../../types/organization"; + +let teamUid = '' +export function testTeams (organization: Organization) { + describe('Contentstack Teams test', () => { + test('should fetch all the teams', done => { + organization.teams().fetchAll() + .then((teams) => { + expect(teams[0].organizationUid).not.to.be.equal(undefined) + expect(teams[0].name).not.to.be.equal(null) + expect(teams[0].created_by).not.to.be.equal(null) + expect(teams[0].updated_by).not.to.be.equal(null) + done() + }) + .catch(done) + }) + test('should fetch the team when correct organization uid and team uid is passed', done => { + organization.teams(teamUid).fetch() + .then((teams) => { + expect(teams.uid).to.be.equal(teamUid) + expect(teams.organizationUid).not.to.be.equal(undefined) + expect(teams.name).not.to.be.equal(null) + expect(teams.created_by).not.to.be.equal(null) + expect(teams.updated_by).not.to.be.equal(null) + done() + }) + .catch(done) + }) + test('should create new team when required object is passed', done => { + const createData = { + name: 'test_team', + users: [], + stackRoleMapping: [], + organizationRole: '' + } + organization.teams().create(createData) + .then((teams) => { + expect(teams.uid).not.to.be.equal(null) + expect(teams.name).not.to.be.equal(null) + expect(teams.stackRoleMapping).not.to.be.equal(null) + expect(teams.organizationRole).not.to.be.equal(null) + done() + }) + .catch(done) + }) + test('should delete team when correct organization uid and team uid is passed', done => { + organization.teams(teamUid).delete() + .then((teams) => { + expect(teams.status).to.be.equal(204) + done() + }) + .catch(done) + }) + }) +} + diff --git a/types/organization.d.ts b/types/organization.d.ts index 0612b25f..3dc040c3 100644 --- a/types/organization.d.ts +++ b/types/organization.d.ts @@ -7,6 +7,7 @@ import { AnyProperty, SystemFields } from './utility/fields' import { ContentstackCollection, Response } from './contentstackCollection' import { App, Apps } from './app' import { AppRequest } from './app/request' +import { Team, Teams } from './teams' export interface Organizations { fetchAll(params?: AnyProperty): Promise> @@ -25,6 +26,8 @@ export interface Organization extends SystemFields { app(): Apps app(uid: string): App appRequest(): AppRequest + teams(): Teams + teams(uid: string): Team } export interface OrganizationInvite { diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts new file mode 100644 index 00000000..26c18eaa --- /dev/null +++ b/types/teams/index.d.ts @@ -0,0 +1,28 @@ +import { AnyProperty, SystemFields } from "../utility/fields"; +import { ContentstackCollection } from '../contentstackCollection' +import { Creatable, SystemFunction } from "../utility/operations"; +import { User, Users } from "./teamUsers"; +import { StackRoleMapping, StackRoleMappings } from "./stackRoleMapping"; + +export interface Team extends SystemFields, SystemFunction { + update(data?:TeamData, param?: { includeUserDetails?: boolean}): Promise + user(): User + users(uid: string): Users + stackRoleMappings(): StackRoleMappings + stackRoleMappings(uid: string): StackRoleMapping +} + +export interface Teams extends Creatable { +} + +export interface Teams { + fetchAll(params?: AnyProperty): Promise> +} + +export interface TeamData extends AnyProperty { + name: string, + users: any, + stackRoleMapping: any, + organizationRole: string +} + From e66b74e021a87aa49f8fb89547ea414faf6c4c4a Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 7 Nov 2023 18:29:37 +0530 Subject: [PATCH 16/32] chore: :arrow_up: update axios lib --- .talismanrc | 2 +- package-lock.json | 18 +++++++++--------- package.json | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.talismanrc b/.talismanrc index b61827c6..adb09795 100644 --- a/.talismanrc +++ b/.talismanrc @@ -1,5 +1,5 @@ threshold: medium fileignoreconfig: - filename: package-lock.json - checksum: ef5d374553f431b5a952069f46184ec7e49efd7d72143e1a1642994758db4359 + checksum: 9a7bec9513834a0fc7db31b9a312ec35980600415c04f0d404e1370cfce1ef1b version: "" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index eb8d451e..aaa58d6e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@contentstack/management", - "version": "1.10.2", + "version": "1.11.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@contentstack/management", - "version": "1.10.2", + "version": "1.11.0", "license": "MIT", "dependencies": { - "axios": "^1.5.1", + "axios": "^1.6.0", "form-data": "^3.0.1", "lodash": "^4.17.21", "qs": "^6.11.2" @@ -3617,9 +3617,9 @@ } }, "node_modules/axios": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.5.1.tgz", - "integrity": "sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "dependencies": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", @@ -15160,9 +15160,9 @@ "dev": true }, "axios": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.5.1.tgz", - "integrity": "sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==", + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.0.tgz", + "integrity": "sha512-EZ1DYihju9pwVB+jg67ogm+Tmqc6JmhamRN6I4Zt8DfZu5lbcQGw3ozH9lFejSJgs/ibaef3A9PMXPLeefFGJg==", "requires": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", diff --git a/package.json b/package.json index 3ab84b5e..742e27bb 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "author": "Contentstack", "license": "MIT", "dependencies": { - "axios": "^1.5.1", + "axios": "^1.6.0", "form-data": "^3.0.1", "lodash": "^4.17.21", "qs": "^6.11.2" From 9e59400d9643b542c19c593443aeea3301685132 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 19:15:55 +0530 Subject: [PATCH 17/32] added valid assertions for the test --- test/api/team-test.js | 4 ++-- test/unit/taxonomy-test.js | 4 +++- test/unit/team-stack-role-mapping-test.js | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/api/team-test.js b/test/api/team-test.js index 825c1af8..4cbb6408 100644 --- a/test/api/team-test.js +++ b/test/api/team-test.js @@ -31,10 +31,10 @@ describe('Teams API Test', () => { }) it('should create new team when required object is passed', async () => { const response = await makeTeams(organizationUid).create({ - name: 'test_team11111', + name: 'test_team', users: [], stackRoleMapping: [], - organizationRole: 'blt09e5dfced326aaea' }) + organizationRole: 'organizationRole' }) expect(response.uid).not.to.be.equal(null) expect(response.name).not.to.be.equal(null) expect(response.stackRoleMapping).not.to.be.equal(null) diff --git a/test/unit/taxonomy-test.js b/test/unit/taxonomy-test.js index 06f4d300..c5f67190 100644 --- a/test/unit/taxonomy-test.js +++ b/test/unit/taxonomy-test.js @@ -137,7 +137,9 @@ describe('Contentstack Taxonomy test', () => { }).terms() .create() .then((term) => { - console.log(term) + expect(term.taxonomy_uid).to.be.not.equal(undefined) + expect(term.uid).to.be.equal('UID') + expect(term.name).to.be.equal('name') done() }) .catch(done) diff --git a/test/unit/team-stack-role-mapping-test.js b/test/unit/team-stack-role-mapping-test.js index 14dff3b2..96fe642d 100644 --- a/test/unit/team-stack-role-mapping-test.js +++ b/test/unit/team-stack-role-mapping-test.js @@ -10,8 +10,8 @@ describe('Contentstack Team Stack Role Mapping test', () => { 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) + .then((response) => { + expect(response.stackRoleMappings).to.be.not.equal(undefined) done() }) .catch(done) From 46c09768ee56ab3e0e1406c5364910b35e930548 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Tue, 7 Nov 2023 19:17:18 +0530 Subject: [PATCH 18/32] teams test cases for types support --- test/typescript/teams.ts | 20 ++++++++++++++++++++ types/teams/index.d.ts | 22 +++++++++++----------- types/teams/stackRoleMapping/index.d.ts | 17 +++++++++++++++++ types/teams/teamUsers/index.d.ts | 15 +++++++++++++++ 4 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 types/teams/stackRoleMapping/index.d.ts create mode 100644 types/teams/teamUsers/index.d.ts diff --git a/test/typescript/teams.ts b/test/typescript/teams.ts index ad26c859..a7b95e59 100644 --- a/test/typescript/teams.ts +++ b/test/typescript/teams.ts @@ -52,6 +52,26 @@ export function testTeams (organization: Organization) { }) .catch(done) }) + test('should update team when update data is passed', done => { + const updateData = { + name: 'name', + users: [ + { + email: 'abc@abc.com' + } + ], + organizationRole: 'blt09e5dfced326aaea', + stackRoleMapping: [] + } + organization.teams(teamUid).update(updateData) + .then((team) => { + expect(team.name).to.be.equal(updateData.name) + expect(team.createdByUserName).not.to.be.equal(undefined) + expect(team.updatedByUserName).not.to.be.equal(undefined) + done() + }) + .catch(done) + }) }) } diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index 26c18eaa..a9e18291 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -2,27 +2,27 @@ import { AnyProperty, SystemFields } from "../utility/fields"; import { ContentstackCollection } from '../contentstackCollection' import { Creatable, SystemFunction } from "../utility/operations"; import { User, Users } from "./teamUsers"; -import { StackRoleMapping, StackRoleMappings } from "./stackRoleMapping"; +import { StackRoleMapping, StackRoleMappings, StackRoleMappingData } from "./stackRoleMapping"; -export interface Team extends SystemFields, SystemFunction { - update(data?:TeamData, param?: { includeUserDetails?: boolean}): Promise - user(): User +export interface Team extends TeamData { + update(data: TeamData, param?: { includeUserDetails?: boolean}): Promise + users(): User users(uid: string): Users stackRoleMappings(): StackRoleMappings stackRoleMappings(uid: string): StackRoleMapping + fetch(): Promise + delete(): Promise } export interface Teams extends Creatable { -} - -export interface Teams { fetchAll(params?: AnyProperty): Promise> } export interface TeamData extends AnyProperty { - name: string, - users: any, - stackRoleMapping: any, - organizationRole: string + uid?: string, + name?: string, + users?: any, + stackRoleMapping?: StackRoleMappingData[] | [], + organizationRole?: string } diff --git a/types/teams/stackRoleMapping/index.d.ts b/types/teams/stackRoleMapping/index.d.ts new file mode 100644 index 00000000..d93153e2 --- /dev/null +++ b/types/teams/stackRoleMapping/index.d.ts @@ -0,0 +1,17 @@ +import { AnyProperty, SystemFields } from "../../utility/fields"; +import { ContentstackCollection } from '../../contentstackCollection' +import { SystemFunction } from "../../utility/operations"; + +export interface StackRoleMapping extends SystemFields, SystemFunction { + update(data: { stackRoleMapping: StackRoleMappingData }): Promise +} + +export interface StackRoleMappings { + fetchAll(params?: AnyProperty): Promise> + add(data: { stackRoleMapping: StackRoleMappingData }): Promise +} + +export interface StackRoleMappingData extends AnyProperty { + stackApiKey?: string, + roles: string[] +} diff --git a/types/teams/teamUsers/index.d.ts b/types/teams/teamUsers/index.d.ts new file mode 100644 index 00000000..8619cd0d --- /dev/null +++ b/types/teams/teamUsers/index.d.ts @@ -0,0 +1,15 @@ +import { AnyProperty, SystemFields } from "../../utility/fields"; +import { Creatable, Queryable, SystemFunction } from "../../utility/operations"; + +export interface User extends SystemFields, SystemFunction { +} + +export interface Users extends Creatable { +} + +export interface Users extends Queryable { +} + +export interface UserData extends AnyProperty { + users: string[] +} From e5b08071247846bfa5992eeb147588fabf42c563 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 9 Nov 2023 17:28:58 +0530 Subject: [PATCH 19/32] types support for team Users and api test cases --- test/typescript/index.test.ts | 5 ++++- test/typescript/teamUsers.ts | 36 ++++++++++++++++++++++++++++++++ test/typescript/teams.ts | 5 +++-- types/teams/index.d.ts | 4 ++-- types/teams/teamUsers/index.d.ts | 16 +++++++------- 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 test/typescript/teamUsers.ts diff --git a/test/typescript/index.test.ts b/test/typescript/index.test.ts index 51e48762..e046404d 100644 --- a/test/typescript/index.test.ts +++ b/test/typescript/index.test.ts @@ -19,6 +19,8 @@ import { orgAppRequest } from './app-request'; import { authorization } from './authorization'; import { testTaxonomy } from './taxonomy'; import { testTerm } from './terms'; +import { testTeams } from './teams'; +import { testTeamUsers } from './teamUsers'; dotenv.config() jest.setTimeout(10000); @@ -49,7 +51,8 @@ describe('Typescript API test', () => { authorization(org.app(process.env.APP_UID as string).authorization()) orgAppRequest(org.appRequest()) deleteApp(org) - + testTeams(org) + testTeamUsers(org) const stack = client.stack({api_key: process.env.APIKEY as string}) createBranch(stack) diff --git a/test/typescript/teamUsers.ts b/test/typescript/teamUsers.ts new file mode 100644 index 00000000..45245a3f --- /dev/null +++ b/test/typescript/teamUsers.ts @@ -0,0 +1,36 @@ +import { expect } from "chai"; +import { Organization } from "../../types/organization"; + +let teamUid = 'teamUid' +export function testTeamUsers (organization: Organization) { + describe('Contentstack Team Users test', () => { + test('should add the user when user\'s mail is passed', done => { + const usersMail = { + emails: ['email@email.com'] + } + organization.teams(teamUid).users().add(usersMail).then((response) => { + expect(response).not.to.be.equal(null) + done() + }) + .catch(done) + }) + test('should remove the user when uid is passed', done => { + const user_id = 'user_id' + organization.teams(teamUid).users(user_id).remove().then((response) => { + expect(response.status).to.be.equal(204) + done() + }) + .catch(done) + }) + test('should fetch all users', done => { + organization.teams(teamUid).users() + .fetchAll() + .then((response) => { + expect(response.items[0]).not.to.be.equal(undefined) + done() + }) + .catch(done) + }) + }) +} + diff --git a/test/typescript/teams.ts b/test/typescript/teams.ts index a7b95e59..158fe4a1 100644 --- a/test/typescript/teams.ts +++ b/test/typescript/teams.ts @@ -60,12 +60,13 @@ export function testTeams (organization: Organization) { email: 'abc@abc.com' } ], - organizationRole: 'blt09e5dfced326aaea', + organizationRole: '', stackRoleMapping: [] - } + } organization.teams(teamUid).update(updateData) .then((team) => { expect(team.name).to.be.equal(updateData.name) + expect(team.organizationRole).to.be.equal(updateData.organizationRole) expect(team.createdByUserName).not.to.be.equal(undefined) expect(team.updatedByUserName).not.to.be.equal(undefined) done() diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index a9e18291..6a8d1433 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -6,8 +6,8 @@ import { StackRoleMapping, StackRoleMappings, StackRoleMappingData } from "./sta export interface Team extends TeamData { update(data: TeamData, param?: { includeUserDetails?: boolean}): Promise - users(): User - users(uid: string): Users + users(): Users + users(uid: string): User stackRoleMappings(): StackRoleMappings stackRoleMappings(uid: string): StackRoleMapping fetch(): Promise diff --git a/types/teams/teamUsers/index.d.ts b/types/teams/teamUsers/index.d.ts index 8619cd0d..7ec8feea 100644 --- a/types/teams/teamUsers/index.d.ts +++ b/types/teams/teamUsers/index.d.ts @@ -1,15 +1,15 @@ -import { AnyProperty, SystemFields } from "../../utility/fields"; -import { Creatable, Queryable, SystemFunction } from "../../utility/operations"; +import { AnyProperty } from "../../utility/fields"; -export interface User extends SystemFields, SystemFunction { +export interface Users extends UserData { + add(data:UserData): Promise + fetchAll(params?: { includeUserDetails: boolean, include_count: boolean}): Promise } -export interface Users extends Creatable { -} - -export interface Users extends Queryable { +export interface User { + remove(): Promise } export interface UserData extends AnyProperty { - users: string[] + emails?: string[] + users?: string[] } From e85be9ff53e5ea900ab559b3ea1a85eb79f23c43 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 9 Nov 2023 17:58:50 +0530 Subject: [PATCH 20/32] interface changed from User to TeamUser in types --- types/teams/index.d.ts | 14 +++++++------- types/teams/teamUsers/index.d.ts | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index 6a8d1433..da9c8f38 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -1,13 +1,13 @@ -import { AnyProperty, SystemFields } from "../utility/fields"; +import { AnyProperty } from "../utility/fields"; import { ContentstackCollection } from '../contentstackCollection' -import { Creatable, SystemFunction } from "../utility/operations"; -import { User, Users } from "./teamUsers"; -import { StackRoleMapping, StackRoleMappings, StackRoleMappingData } from "./stackRoleMapping"; +import { Creatable } from "../utility/operations"; +import { TeamUser, TeamUsers, TeamUserData } from "./teamUsers"; +import { StackRoleMapping, StackRoleMappings, StackRoleMappingData } from "./stackRoleMappings"; export interface Team extends TeamData { update(data: TeamData, param?: { includeUserDetails?: boolean}): Promise - users(): Users - users(uid: string): User + users(): TeamUsers + users(uid: string): TeamUser stackRoleMappings(): StackRoleMappings stackRoleMappings(uid: string): StackRoleMapping fetch(): Promise @@ -21,7 +21,7 @@ export interface Teams extends Creatable { export interface TeamData extends AnyProperty { uid?: string, name?: string, - users?: any, + users?: TeamUserData | string[] | [], stackRoleMapping?: StackRoleMappingData[] | [], organizationRole?: string } diff --git a/types/teams/teamUsers/index.d.ts b/types/teams/teamUsers/index.d.ts index 7ec8feea..a992eef0 100644 --- a/types/teams/teamUsers/index.d.ts +++ b/types/teams/teamUsers/index.d.ts @@ -1,15 +1,15 @@ import { AnyProperty } from "../../utility/fields"; -export interface Users extends UserData { - add(data:UserData): Promise +export interface TeamUsers extends TeamUserData { + add(data:TeamUserData): Promise fetchAll(params?: { includeUserDetails: boolean, include_count: boolean}): Promise } -export interface User { +export interface TeamUser { remove(): Promise } -export interface UserData extends AnyProperty { +export interface TeamUserData extends AnyProperty { emails?: string[] users?: string[] } From 0aa565d3d2d582fd0c7635b2a2832b86cf52010b Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 9 Nov 2023 18:55:18 +0530 Subject: [PATCH 21/32] types support for stackRoleMapping and test cases --- test/typescript/index.test.ts | 3 ++ test/typescript/teamsStackRoleMappings.ts | 54 +++++++++++++++++++ types/teams/index.d.ts | 2 +- .../index.d.ts | 9 ++-- 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 test/typescript/teamsStackRoleMappings.ts rename types/teams/{stackRoleMapping => stackRoleMappings}/index.d.ts (56%) diff --git a/test/typescript/index.test.ts b/test/typescript/index.test.ts index e046404d..e7546da4 100644 --- a/test/typescript/index.test.ts +++ b/test/typescript/index.test.ts @@ -21,6 +21,7 @@ import { testTaxonomy } from './taxonomy'; import { testTerm } from './terms'; import { testTeams } from './teams'; import { testTeamUsers } from './teamUsers'; +import { testTeamStackRoleMapping } from './teamsStackRoleMappings'; dotenv.config() jest.setTimeout(10000); @@ -53,6 +54,8 @@ describe('Typescript API test', () => { deleteApp(org) testTeams(org) testTeamUsers(org) + testTeamStackRoleMapping(org) + const stack = client.stack({api_key: process.env.APIKEY as string}) createBranch(stack) diff --git a/test/typescript/teamsStackRoleMappings.ts b/test/typescript/teamsStackRoleMappings.ts new file mode 100644 index 00000000..a5798241 --- /dev/null +++ b/test/typescript/teamsStackRoleMappings.ts @@ -0,0 +1,54 @@ +import { expect } from "chai"; +import { Organization } from "../../types/organization"; + +let teamUid = 'teamUid' +let stackApiKey = 'stackApiKey' +export function testTeamStackRoleMapping (organization: Organization) { + describe('Contentstack Teams Stack Role Mapping test', () => { + it('should fetch all stackRoleMappings', done => { + organization.teams(teamUid).stackRoleMappings().fetchAll().then((response) => { + expect(response).to.be.not.equal(undefined) + done() + }) + .catch(done) + }) + it('should add roles', done => { + const stackRoleMappings = { + stackApiKey: 'stackApiKey', + roles: [ + 'role_uid' + ] + } + organization.teams(teamUid).stackRoleMappings(stackApiKey).add(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(stackRoleMappings.stackApiKey) + done() + }) + .catch(done) + }) + it('should update roles', done => { + const stackRoleMappings = { + roles: [ + 'role_uid1', + 'role_uid2' + ] + } + organization.teams(teamUid).stackRoleMappings(stackApiKey).update(stackRoleMappings).then((response) => { + expect(response).not.to.be.equal(undefined) + expect(response.StackRoleMappingData.roles[0]).to.be.equal(stackRoleMappings.roles[0]) + expect(response.StackRoleMappingData.stackApiKey).to.be.equal(stackApiKey) + done() + }) + .catch(done) + }) + it('should delete roles', done => { + organization.teams(teamUid).stackRoleMappings(stackApiKey).delete().then((response) => { + expect(response.status).to.be.equal(204) + done() + }) + .catch(done) + }) + }) +} + diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index da9c8f38..4d980ea0 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -9,7 +9,7 @@ export interface Team extends TeamData { users(): TeamUsers users(uid: string): TeamUser stackRoleMappings(): StackRoleMappings - stackRoleMappings(uid: string): StackRoleMapping + stackRoleMappings(stackApiKey: string): StackRoleMapping fetch(): Promise delete(): Promise } diff --git a/types/teams/stackRoleMapping/index.d.ts b/types/teams/stackRoleMappings/index.d.ts similarity index 56% rename from types/teams/stackRoleMapping/index.d.ts rename to types/teams/stackRoleMappings/index.d.ts index d93153e2..78496b20 100644 --- a/types/teams/stackRoleMapping/index.d.ts +++ b/types/teams/stackRoleMappings/index.d.ts @@ -2,16 +2,17 @@ import { AnyProperty, SystemFields } from "../../utility/fields"; import { ContentstackCollection } from '../../contentstackCollection' import { SystemFunction } from "../../utility/operations"; -export interface StackRoleMapping extends SystemFields, SystemFunction { - update(data: { stackRoleMapping: StackRoleMappingData }): Promise +export interface StackRoleMapping extends StackRoleMappingData { + update(data:StackRoleMappingData): Promise<{StackRoleMappingData: StackRoleMappingData}> } -export interface StackRoleMappings { +export interface StackRoleMappings extends StackRoleMappingData { fetchAll(params?: AnyProperty): Promise> - add(data: { stackRoleMapping: StackRoleMappingData }): Promise + add(data: StackRoleMappingData): Promise } export interface StackRoleMappingData extends AnyProperty { stackApiKey?: string, roles: string[] } + From 8098385f5c93dee0cf708b15ef451d99d4108334 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 10 Nov 2023 16:01:00 +0530 Subject: [PATCH 22/32] stackRoleMapping test cases in types support --- test/typescript/teamsStackRoleMappings.ts | 14 +++++++++----- types/teams/stackRoleMappings/index.d.ts | 2 +- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/test/typescript/teamsStackRoleMappings.ts b/test/typescript/teamsStackRoleMappings.ts index a5798241..03362d9b 100644 --- a/test/typescript/teamsStackRoleMappings.ts +++ b/test/typescript/teamsStackRoleMappings.ts @@ -19,7 +19,7 @@ export function testTeamStackRoleMapping (organization: Organization) { 'role_uid' ] } - organization.teams(teamUid).stackRoleMappings(stackApiKey).add(stackRoleMappings).then((response) => { + organization.teams(teamUid).stackRoleMappings().add(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(stackRoleMappings.stackApiKey) @@ -34,13 +34,17 @@ export function testTeamStackRoleMapping (organization: Organization) { 'role_uid2' ] } - organization.teams(teamUid).stackRoleMappings(stackApiKey).update(stackRoleMappings).then((response) => { + try { + organization.teams(teamUid).stackRoleMappings(stackApiKey).update(stackRoleMappings).then((response) => + { expect(response).not.to.be.equal(undefined) - expect(response.StackRoleMappingData.roles[0]).to.be.equal(stackRoleMappings.roles[0]) - expect(response.StackRoleMappingData.stackApiKey).to.be.equal(stackApiKey) + expect(response.StackRoleMapping).to.be.equal(stackRoleMappings) + expect(response.StackRoleMapping.stackApiKey).to.be.equal(stackApiKey) done() }) - .catch(done) + } catch(err) { + done() + } }) it('should delete roles', done => { organization.teams(teamUid).stackRoleMappings(stackApiKey).delete().then((response) => { diff --git a/types/teams/stackRoleMappings/index.d.ts b/types/teams/stackRoleMappings/index.d.ts index 78496b20..f85c7447 100644 --- a/types/teams/stackRoleMappings/index.d.ts +++ b/types/teams/stackRoleMappings/index.d.ts @@ -3,7 +3,7 @@ import { ContentstackCollection } from '../../contentstackCollection' import { SystemFunction } from "../../utility/operations"; export interface StackRoleMapping extends StackRoleMappingData { - update(data:StackRoleMappingData): Promise<{StackRoleMappingData: StackRoleMappingData}> + update(data:StackRoleMappingData): Promise } export interface StackRoleMappings extends StackRoleMappingData { From a582e8acdfa5fde0247bac60bdb6ce508fa5935e Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 10 Nov 2023 17:03:07 +0530 Subject: [PATCH 23/32] stackRoleMapping update test case on types support --- test/typescript/teamsStackRoleMappings.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/typescript/teamsStackRoleMappings.ts b/test/typescript/teamsStackRoleMappings.ts index 03362d9b..60cf5257 100644 --- a/test/typescript/teamsStackRoleMappings.ts +++ b/test/typescript/teamsStackRoleMappings.ts @@ -27,24 +27,20 @@ export function testTeamStackRoleMapping (organization: Organization) { }) .catch(done) }) - it('should update roles', done => { + it('should update roles', (done) => { const stackRoleMappings = { roles: [ 'role_uid1', 'role_uid2' ] } - try { - organization.teams(teamUid).stackRoleMappings(stackApiKey).update(stackRoleMappings).then((response) => - { + organization.teams(teamUid).stackRoleMappings(stackApiKey).update(stackRoleMappings).then((response) => { expect(response).not.to.be.equal(undefined) - expect(response.StackRoleMapping).to.be.equal(stackRoleMappings) - expect(response.StackRoleMapping.stackApiKey).to.be.equal(stackApiKey) + expect(response.stackRoleMapping.roles).to.be.eql(stackRoleMappings.roles) + expect(response.stackRoleMapping.stackApiKey).to.be.equal(stackApiKey) done() }) - } catch(err) { - done() - } + .catch(done) }) it('should delete roles', done => { organization.teams(teamUid).stackRoleMappings(stackApiKey).delete().then((response) => { From a3ec18a392201baccc7556795fcb782024e5d47c Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 10 Nov 2023 17:47:24 +0530 Subject: [PATCH 24/32] docs: :memo: updated version and changeLog --- CHANGELOG.md | 3 +++ package.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bdee9d8..c5469cfa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-15) + - Feature + - Teams API support ## [v1.12.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.12.0) (2023-10-17) - Feature - Types support for Taxonomy feature diff --git a/package.json b/package.json index 59888433..af403433 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/management", - "version": "1.12.0", + "version": "1.13.0", "description": "The Content Management API is used to manage the content of your Contentstack account", "main": "./dist/node/contentstack-management.js", "browser": "./dist/web/contentstack-management.js", From 9344c09cf4e113b8af05f74e0b400b7e3989f216 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Fri, 10 Nov 2023 19:05:29 +0530 Subject: [PATCH 25/32] test: added test cases to improve coverage report --- lib/organization/teams/index.js | 14 ++++++++++- .../teams/stackRoleMappings/index.js | 7 ------ test/unit/organization-test.js | 19 +++++++++++++++ test/unit/team-test.js | 24 ++++++++++++++++++- test/unit/team-users-test.js | 2 +- 5 files changed, 56 insertions(+), 10 deletions(-) diff --git a/lib/organization/teams/index.js b/lib/organization/teams/index.js index 7585fb9d..38ab9fbd 100644 --- a/lib/organization/teams/index.js +++ b/lib/organization/teams/index.js @@ -87,7 +87,7 @@ export function Teams (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * client.organization('organizationUid').teams('teamUid').users().fetchAll() - * .then((response) => console.log(response)) + * .then((response) => console.log(response)) * */ this.users = (userId = null) => { @@ -99,6 +99,18 @@ export function Teams (http, data) { return new TeamUsers(http, data) } + /** + * @description The stackRoleMappings call on team will get the stack role Mapping. + * @memberof Teams + * @func users + * @returns {Promise} 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.stackRoleMappings = (stackApiKey = null) => { data.organizationUid = this.organizationUid data.teamUid = this.uid diff --git a/lib/organization/teams/stackRoleMappings/index.js b/lib/organization/teams/stackRoleMappings/index.js index 2d34d5f4..235bc996 100644 --- a/lib/organization/teams/stackRoleMappings/index.js +++ b/lib/organization/teams/stackRoleMappings/index.js @@ -109,10 +109,3 @@ export function StackRoleMappings (http, data) { } } } -export function stackRoleMappingsCollection (http, data) { - const obj = cloneDeep(data.stackRoleMappings) || [] - const stackRoleMappingCollection = obj.map((stackRoleMappings) => { - return stackRoleMappings(http, { stackRoleMappings: stackRoleMappings }) - }) - return new StackRoleMappings(http, stackRoleMappingCollection) -} diff --git a/test/unit/organization-test.js b/test/unit/organization-test.js index 77d7a88c..be278013 100644 --- a/test/unit/organization-test.js +++ b/test/unit/organization-test.js @@ -263,6 +263,25 @@ describe('Organization Test', () => { }) .catch(done) }) + it('should get teams', done => { + const mock = new MockAdapter(Axios) + mock.onGet(`/organizations/UID`).reply(200, { + organization: { + ...orgMock + } + }) + makeOrganization({ organization: { + ...systemUidMock + } }) + .fetch() + .then((response) => { + const teams = response.teams() + expect(teams).to.not.equal(undefined) + expect(teams.organizationUid).to.be.equal('UID') + done() + }) + .catch(done) + }) }) function makeOrganization (params = {}) { diff --git a/test/unit/team-test.js b/test/unit/team-test.js index 9c2ee28c..ed8d14a2 100644 --- a/test/unit/team-test.js +++ b/test/unit/team-test.js @@ -3,7 +3,7 @@ import { expect } from 'chai' import { describe, it } from 'mocha' import MockAdapter from 'axios-mock-adapter' import { Teams } from '../../lib/organization/teams' -import { systemUidMock, teamsMock, noticeMock } from './mock/objects' +import { systemUidMock, teamsMock, noticeMock, teamUsersMock, stackRoleMappingMock } from './mock/objects' describe('Contentstack Team test', () => { it('should get all the teams when correct organization uid is passed', done => { @@ -70,6 +70,28 @@ describe('Contentstack Team test', () => { }) .catch(done) }) + it('should fetch all users', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams/UID/users`).reply(200, teamUsersMock) + makeTeams({ ...systemUidMock }).users().fetchAll() + .then((users) => { + users.items.forEach((user) => { + expect(user.uidId).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) + it('should fetch all the roles', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams/UID/stack_role_mappings`).reply(200, stackRoleMappingMock) + makeTeams({ ...systemUidMock }).stackRoleMappings().fetchAll() + .then((response) => { + expect(response.stackRoleMappings).to.be.not.equal(undefined) + done() + }) + .catch(done) + }) }) function makeTeams (data = {}) { diff --git a/test/unit/team-users-test.js b/test/unit/team-users-test.js index 5bb9a4f1..529eed25 100644 --- a/test/unit/team-users-test.js +++ b/test/unit/team-users-test.js @@ -6,7 +6,7 @@ import { TeamUsers } from '../../lib/organization/teams/teamUsers' import { teamUsersMock, noticeMock } from './mock/objects' describe('Contentstack Team Users test', () => { - it('should query and find all users', done => { + it('should fetch all users', done => { var mock = new MockAdapter(Axios) mock.onGet(`/organizations/organization_uid/teams/team_uid/users`).reply(200, teamUsersMock) makeTeamUsers().fetchAll() From 9ecb16f12ff7528e4469eeea4c6cd4bf2327bcbf Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 16 Nov 2023 12:37:33 +0530 Subject: [PATCH 26/32] feat: :sparkles: early access headers implementation --- lib/contentstack.js | 8 ++++++++ lib/core/contentstackHTTPClient.js | 4 ++++ test/unit/ContentstackHTTPClient-test.js | 13 +++++++++++++ test/unit/contentstack-test.js | 11 +++++++++++ 4 files changed, 36 insertions(+) diff --git a/lib/contentstack.js b/lib/contentstack.js index 5ecb13fa..cf716495 100644 --- a/lib/contentstack.js +++ b/lib/contentstack.js @@ -39,6 +39,11 @@ import httpClient from './core/contentstackHTTPClient.js' * import * as contentstack from '@contentstack/management' * const client = contentstack.client({ authtoken: 'value' }) * + * @prop {string=} params.early_access - Optional early_access is a token used for early access of new features in CMA requests. + * @example //Set the `early_access` + * import * as contentstack from '@contentstack/management' + * const client = contentstack.client({ early_access: ['ea1', 'ea2'] }) + * * @prop {string=} params.authorization - Optional authorization token is a read-write token used to make authorized CMA requests, but it is a user-specific token. * @example //Set the `authorization` * import * as contentstack from '@contentstack/management' @@ -177,6 +182,9 @@ export function client (params = {}) { if (params.authorization) { requiredHeaders.authorization = params.authorization } + if (params.early_access) { + requiredHeaders.early_access = params.early_access.join(',') + } params = { ...defaultParameter, ...clonedeep(params) diff --git a/lib/core/contentstackHTTPClient.js b/lib/core/contentstackHTTPClient.js index 1ad77e1a..30f8dfad 100644 --- a/lib/core/contentstackHTTPClient.js +++ b/lib/core/contentstackHTTPClient.js @@ -44,6 +44,10 @@ export default function contentstackHttpClient (options) { config.headers['accessToken'] = config.accessToken } + if (config.early_access) { + config.headers['x-header-ea'] = config.early_access + } + const protocol = config.insecure ? 'http' : 'https' let hostname = config.defaultHostName let port = config.port || 443 diff --git a/test/unit/ContentstackHTTPClient-test.js b/test/unit/ContentstackHTTPClient-test.js index 12e3a28d..1b78a290 100644 --- a/test/unit/ContentstackHTTPClient-test.js +++ b/test/unit/ContentstackHTTPClient-test.js @@ -154,4 +154,17 @@ describe('Contentstack HTTP Client', () => { expect(client.defaults.retryCondition('error')).to.be.equal(true) done() }) + it('should add x-header-ea in headers when early_access is passed', done => { + var axiosInstance = contentstackHTTPClient( + { + apiKey: 'apiKey', + accessToken: 'accessToken', + early_access: 'ea1,ea2' + }) + + expect(axiosInstance.defaults.headers.apiKey).to.be.equal('apiKey', 'Api not Equal to \'apiKey\'') + expect(axiosInstance.defaults.headers.accessToken).to.be.equal('accessToken', 'Api not Equal to \'accessToken\'') + expect(axiosInstance.defaults.headers['x-header-ea']).to.be.equal('ea1,ea2') + done() + }) }) diff --git a/test/unit/contentstack-test.js b/test/unit/contentstack-test.js index 18e0bd2c..067d6460 100644 --- a/test/unit/contentstack-test.js +++ b/test/unit/contentstack-test.js @@ -97,4 +97,15 @@ describe('Contentstack HTTP Client', () => { createClientRewireApi.__ResetDependency__('contentstackClient') done() }) + it('should have valid format of early_access headers when early_access is passed', done => { + createClientRewireApi.__Rewire__('client', { create: sinon.stub() }) + const createHttpClientStub = sinon.stub() + createClientRewireApi.__Rewire__('httpClient', createHttpClientStub) + createClientRewireApi.__Rewire__('contentstackClient', sinon.stub().returns({})) + client({ early_access: ['ea1', 'ea2'] }) + expect(createHttpClientStub.args[0][0].headers.early_access).to.be.eql('ea1,ea2', 'Early access does not match') + createClientRewireApi.__ResetDependency__('httpClient') + createClientRewireApi.__ResetDependency__('contentstackClient') + done() + }) }) From 2b04ac2b8793b875d32e8c0f767ef686e19caeb3 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 16 Nov 2023 14:31:16 +0530 Subject: [PATCH 27/32] types support for earlyaccess and update changeLog --- CHANGELOG.md | 3 ++- types/contentstackClient.d.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5469cfa..b19a1660 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Changelog -## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-15) +## [v1.13.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.13.0) (2023-11-21) - Feature - Teams API support + - Early Access Header support ## [v1.12.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.12.0) (2023-10-17) - Feature - Types support for Taxonomy feature diff --git a/types/contentstackClient.d.ts b/types/contentstackClient.d.ts index 67799d05..1e5eb191 100644 --- a/types/contentstackClient.d.ts +++ b/types/contentstackClient.d.ts @@ -24,6 +24,7 @@ export interface RetryDelayOption { export interface ContentstackToken { authorization?: string authtoken?: string + early_access: string[] } export interface ContentstackConfig extends AxiosRequestConfig, ContentstackToken { From 83b90aa22d01bfb94e3e5cf21b133049bfeeb52d Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Thu, 16 Nov 2023 14:35:34 +0530 Subject: [PATCH 28/32] fix: :bug: early_access made optional --- types/contentstackClient.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/contentstackClient.d.ts b/types/contentstackClient.d.ts index 1e5eb191..5374df27 100644 --- a/types/contentstackClient.d.ts +++ b/types/contentstackClient.d.ts @@ -24,7 +24,7 @@ export interface RetryDelayOption { export interface ContentstackToken { authorization?: string authtoken?: string - early_access: string[] + early_access?: string[] } export interface ContentstackConfig extends AxiosRequestConfig, ContentstackToken { From 496735abd8ef1e784acbb2a1899a33b5a3238f5f Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 20 Nov 2023 15:40:08 +0530 Subject: [PATCH 29/32] fix: :bug: added api_version param which is to be passed in headers --- lib/organization/teams/index.js | 4 ++-- test/typescript/teams.ts | 8 ++++---- types/teams/index.d.ts | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/organization/teams/index.js b/lib/organization/teams/index.js index 38ab9fbd..4f36d8ae 100644 --- a/lib/organization/teams/index.js +++ b/lib/organization/teams/index.js @@ -153,11 +153,11 @@ export function Teams (http, data) { * client.organization('organizationUid').teams().fetchAll() * .then((response) => console.log(response)) */ - this.fetchAll = fetchAll(http, TeamsCollection) + this.fetchAll = fetchAll(http, TeamsCollection, { api_version: 1.1 }) } } export function TeamsCollection (http, teamsData) { - const obj = cloneDeep(teamsData) || [] + const obj = cloneDeep(teamsData.teams) || [] const teamsCollection = obj.map((team) => { return new Teams(http, team) }) diff --git a/test/typescript/teams.ts b/test/typescript/teams.ts index 158fe4a1..d430df1b 100644 --- a/test/typescript/teams.ts +++ b/test/typescript/teams.ts @@ -7,10 +7,10 @@ export function testTeams (organization: Organization) { test('should fetch all the teams', done => { organization.teams().fetchAll() .then((teams) => { - expect(teams[0].organizationUid).not.to.be.equal(undefined) - expect(teams[0].name).not.to.be.equal(null) - expect(teams[0].created_by).not.to.be.equal(null) - expect(teams[0].updated_by).not.to.be.equal(null) + expect(teams.items[0].organizationUid).not.to.be.equal(undefined) + expect(teams.items[0].name).not.to.be.equal(null) + expect(teams.items[0].created_by).not.to.be.equal(null) + expect(teams.items[0].updated_by).not.to.be.equal(null) done() }) .catch(done) diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index 4d980ea0..c0bdbdc8 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -15,7 +15,7 @@ export interface Team extends TeamData { } export interface Teams extends Creatable { - fetchAll(params?: AnyProperty): Promise> + fetchAll(params?: AnyProperty): Promise> } export interface TeamData extends AnyProperty { From 78a3c182d5615e196d60d8a605f04488f3e927fd Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 20 Nov 2023 16:37:45 +0530 Subject: [PATCH 30/32] test: :white_check_mark: updated response fetchAll unit test case --- test/unit/team-test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/team-test.js b/test/unit/team-test.js index ed8d14a2..00f516a5 100644 --- a/test/unit/team-test.js +++ b/test/unit/team-test.js @@ -8,7 +8,10 @@ import { systemUidMock, teamsMock, noticeMock, teamUsersMock, stackRoleMappingMo describe('Contentstack Team test', () => { it('should get all the teams when correct organization uid is passed', done => { var mock = new MockAdapter(Axios) - mock.onGet(`/organizations/organization_uid/teams`).reply(200, [teamsMock]) + mock.onGet(`/organizations/organization_uid/teams`).reply(200, { + count: 17, + teams: [teamsMock] + }) makeTeams().fetchAll() .then((teams) => { expect(teams.items[0].uid).to.be.equal('UID') From 5cdd59d5a91a465dfb4a44d7e2ded133a2b2d355 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 20 Nov 2023 18:41:49 +0530 Subject: [PATCH 31/32] fix: :bug: create function response data update --- lib/entity.js | 11 +++++++++-- test/api/team-users-test.js | 3 ++- test/typescript/teamUsers.ts | 4 ++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index b3ed2a4f..7a9246b3 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -86,7 +86,14 @@ export const create = ({ http, params }) => { if (response.data) { return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid, this.taxonomy_uid)) } else { - throw error(response) + if (response.status >= 200 && response.status < 300) { + return { + status: response.status, + statusText: response.statusText + } + } else { + throw error(response) + } } } catch (err) { throw error(err) @@ -301,4 +308,4 @@ export const move = (http, type, force = false, params = {}) => { throw error(err) } } -} \ No newline at end of file +} diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index 674cfb57..b6d681c1 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -19,7 +19,8 @@ describe('Teams Users API Test', () => { emails: ['email@email.com'] } makeUsers(organizationUid, teamUid).add(usersMail).then((response) => { - expect(response).to.be.equal(null) + expect(response.status).to.be.equal(201) + done() }) .catch(done) }) diff --git a/test/typescript/teamUsers.ts b/test/typescript/teamUsers.ts index 45245a3f..c7059961 100644 --- a/test/typescript/teamUsers.ts +++ b/test/typescript/teamUsers.ts @@ -9,7 +9,7 @@ export function testTeamUsers (organization: Organization) { emails: ['email@email.com'] } organization.teams(teamUid).users().add(usersMail).then((response) => { - expect(response).not.to.be.equal(null) + expect(response.status).to.be.eql(201) done() }) .catch(done) @@ -24,7 +24,7 @@ export function testTeamUsers (organization: Organization) { }) test('should fetch all users', done => { organization.teams(teamUid).users() - .fetchAll() + .fetchAll() .then((response) => { expect(response.items[0]).not.to.be.equal(undefined) done() From 5251aeff68865f5414e44f19b895d1ebcf02f5fd Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 20 Nov 2023 19:49:12 +0530 Subject: [PATCH 32/32] refactor: :recycle: changes users in teams to teamUsers --- lib/organization/teams/index.js | 4 ++-- lib/organization/teams/teamUsers/index.js | 8 ++++---- test/api/team-users-test.js | 2 +- test/typescript/teamUsers.ts | 6 +++--- test/unit/team-test.js | 2 +- types/teams/index.d.ts | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/organization/teams/index.js b/lib/organization/teams/index.js index 4f36d8ae..1c2fa066 100644 --- a/lib/organization/teams/index.js +++ b/lib/organization/teams/index.js @@ -86,11 +86,11 @@ export function Teams (http, data) { * @example * import * as contentstack from '@contentstack/management' * const client = contentstack.client() - * client.organization('organizationUid').teams('teamUid').users().fetchAll() + * client.organization('organizationUid').teams('teamUid').teamUsers().fetchAll() * .then((response) => console.log(response)) * */ - this.users = (userId = null) => { + this.teamUsers = (userId = null) => { data.organizationUid = this.organizationUid data.teamUid = this.uid if (userId) { diff --git a/lib/organization/teams/teamUsers/index.js b/lib/organization/teams/teamUsers/index.js index b56b4c21..3f88438d 100644 --- a/lib/organization/teams/teamUsers/index.js +++ b/lib/organization/teams/teamUsers/index.js @@ -21,7 +21,7 @@ export function TeamUsers (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * - * client.organization('organizationUid').teams('teamUid').users('userId').remove() + * client.organization('organizationUid').teams('teamUid').teamUsers('userId').remove() * .then((response) => console.log(response)) * */ @@ -40,7 +40,7 @@ export function TeamUsers (http, data) { * const usersMail = { * emails: ['emailId1','emailId2' ] * } - * client.organization('organizationUid').teams('teamUid').users('userId').add(usersMail) + * client.organization('organizationUid').teams('teamUid').teamUsers('userId').add(usersMail) * .then((response) => console.log(response)) * */ @@ -56,7 +56,7 @@ export function TeamUsers (http, data) { * const client = contentstack.client() * const usersMail = { * emails: ['emailId1','emailId2' ]} - * client.organization('organizationUid').teams('teamUid').users('userId').query().find() + * client.organization('organizationUid').teams('teamUid').teamUsers('userId').query().find() * .then((response) => console.log(response)) * */ @@ -64,7 +64,7 @@ export function TeamUsers (http, data) { } } export function UsersCollection (http, data) { - const obj = cloneDeep(data.users) || [] + const obj = cloneDeep(data.teamUsers) || [] const usersCollection = obj.map((user) => { return new TeamUsers(http, { userId: user }) }) diff --git a/test/api/team-users-test.js b/test/api/team-users-test.js index b6d681c1..30c5d41d 100644 --- a/test/api/team-users-test.js +++ b/test/api/team-users-test.js @@ -43,5 +43,5 @@ describe('Teams Users API Test', () => { }) function makeUsers (organizationUid, teamUid, userId = null) { - return client.organization(organizationUid).teams(teamUid).users(userId) + return client.organization(organizationUid).teams(teamUid).teamUsers(userId) } diff --git a/test/typescript/teamUsers.ts b/test/typescript/teamUsers.ts index c7059961..1cfad9a9 100644 --- a/test/typescript/teamUsers.ts +++ b/test/typescript/teamUsers.ts @@ -8,7 +8,7 @@ export function testTeamUsers (organization: Organization) { const usersMail = { emails: ['email@email.com'] } - organization.teams(teamUid).users().add(usersMail).then((response) => { + organization.teams(teamUid).teamUsers().add(usersMail).then((response) => { expect(response.status).to.be.eql(201) done() }) @@ -16,14 +16,14 @@ export function testTeamUsers (organization: Organization) { }) test('should remove the user when uid is passed', done => { const user_id = 'user_id' - organization.teams(teamUid).users(user_id).remove().then((response) => { + organization.teams(teamUid).teamUsers(user_id).remove().then((response) => { expect(response.status).to.be.equal(204) done() }) .catch(done) }) test('should fetch all users', done => { - organization.teams(teamUid).users() + organization.teams(teamUid).teamUsers() .fetchAll() .then((response) => { expect(response.items[0]).not.to.be.equal(undefined) diff --git a/test/unit/team-test.js b/test/unit/team-test.js index 00f516a5..8e6e3545 100644 --- a/test/unit/team-test.js +++ b/test/unit/team-test.js @@ -76,7 +76,7 @@ describe('Contentstack Team test', () => { it('should fetch all users', done => { var mock = new MockAdapter(Axios) mock.onGet(`/organizations/organization_uid/teams/UID/users`).reply(200, teamUsersMock) - makeTeams({ ...systemUidMock }).users().fetchAll() + makeTeams({ ...systemUidMock }).teamUsers().fetchAll() .then((users) => { users.items.forEach((user) => { expect(user.uidId).to.be.not.equal(null) diff --git a/types/teams/index.d.ts b/types/teams/index.d.ts index c0bdbdc8..b0bb91de 100644 --- a/types/teams/index.d.ts +++ b/types/teams/index.d.ts @@ -6,8 +6,8 @@ import { StackRoleMapping, StackRoleMappings, StackRoleMappingData } from "./sta export interface Team extends TeamData { update(data: TeamData, param?: { includeUserDetails?: boolean}): Promise - users(): TeamUsers - users(uid: string): TeamUser + teamUsers(): TeamUsers + teamUsers(uid: string): TeamUser stackRoleMappings(): StackRoleMappings stackRoleMappings(stackApiKey: string): StackRoleMapping fetch(): Promise