Skip to content

Commit

Permalink
feat: ✨ teams implementation with unit and api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithad0703 committed Oct 27, 2023
1 parent aac5cf4 commit dba8f48
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/organization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href='https://www.contentstack.com/docs/guide/organization'>Organizations.</a>.
* @namespace Organization
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions lib/organization/team/index.js
Original file line number Diff line number Diff line change
@@ -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
}
78 changes: 78 additions & 0 deletions test/api/team-test.js
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
7 changes: 6 additions & 1 deletion test/unit/mock/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -845,6 +849,7 @@ export {
auditLogItemMock,
taxonomyMock,
termsMock,
teamsMock,
mockCollection,
entryMockCollection,
checkSystemFields
Expand Down
23 changes: 22 additions & 1 deletion test/unit/taxonomy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 = {}) {
Expand Down
104 changes: 104 additions & 0 deletions test/unit/team-test.js
Original file line number Diff line number Diff line change
@@ -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')
}

0 comments on commit dba8f48

Please sign in to comment.