Skip to content

Commit 659609d

Browse files
Merge pull request #62 from contentstack/feat/CS-39614-adds-nrp-auditLog-installation-users
Fix: Credentials in response fixed, types fixed and improves code coverage
2 parents 91540e2 + 30d3413 commit 659609d

31 files changed

+1405
-235
lines changed

lib/entity.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,6 @@ export const create = ({ http, params }) => {
9393
}
9494
}
9595

96-
export const exportObject = ({ http }) => {
97-
return async function (param) {
98-
const headers = {
99-
params: {
100-
...cloneDeep(param)
101-
},
102-
headers: {
103-
...cloneDeep(this.stackHeaders)
104-
}
105-
} || {}
106-
try {
107-
const response = await http.get(this.urlPath, headers)
108-
if (response.data) {
109-
return new this.constructor(http, parseData(response, this.stackHeaders, this.content_type_uid))
110-
} else {
111-
throw error(response)
112-
}
113-
} catch (err) {
114-
throw error(err)
115-
}
116-
}
117-
}
118-
11996
export const query = ({ http, wrapperCollection }) => {
12097
return function (params = {}) {
12198
const headers = {
@@ -181,9 +158,7 @@ export const deleteEntity = (http, force = false, params = {}) => {
181158
try {
182159
const headers = {
183160
headers: { ...cloneDeep(this.stackHeaders), ...cloneDeep(params) },
184-
params: {
185-
...cloneDeep(param)
186-
}
161+
params: { ...cloneDeep(param) }
187162
} || {}
188163
if (force === true) {
189164
headers.params.force = true

lib/stack/bulkOperation/index.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ export function BulkOperation (http, data = {}) {
4747
* // Bulk nested publish
4848
* import * as contentstack from '@contentstack/management'
4949
* const client = contentstack.client()
50-
* {
51-
* environments:["{{env_uid}}","{{env_uid}}"],
52-
* locales:["en-us"],
53-
* items:[
54-
* {
55-
* _content_type_uid: '{{content_type_uid}}',
56-
* uid: '{{entry_uid}}'
57-
* },
58-
* {
59-
* _content_type_uid: '{{content_type_uid}}',
60-
* uid: '{{entry_uid}}'
61-
* },
62-
* {
63-
* _content_type_uid: '{{content_type_uid}}',
64-
* uid: '{{entry_uid}}'
65-
* }
66-
* ]
50+
* const publishDetails = {
51+
* environments:["{{env_uid}}","{{env_uid}}"],
52+
* locales:["en-us"],
53+
* entries:[
54+
* {
55+
* _content_type_uid: '{{content_type_uid}}',
56+
* uid: '{{entry_uid}}'
57+
* },
58+
* {
59+
* _content_type_uid: '{{content_type_uid}}',
60+
* uid: '{{entry_uid}}'
61+
* },
62+
* {
63+
* _content_type_uid: '{{content_type_uid}}',
64+
* uid: '{{entry_uid}}'
65+
* }
66+
* ]
6767
* }
6868
* client.stack({ api_key: 'api_key'}).bulkOperation().publish({ details: publishDetails, is_nested: true })
6969
* .then((response) => { console.log(response.notice) })

lib/stack/contentType/entry/index.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -266,46 +266,46 @@ export function Entry (http, data) {
266266
}
267267
}
268268
} else {
269-
/**
270-
* @description The Create an entry call creates a new entry for the selected content type.
271-
* @memberof Entry
272-
* @func create
273-
* @returns {Promise<Entry.Entry>} Promise for Entry instance
274-
*
275-
* @example
276-
* import * as contentstack from '@contentstack/management'
277-
* const client = contentstack.client()
278-
* const entry = {
279-
* title: 'Sample Entry',
280-
* url: '/sampleEntry',
281-
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
282-
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
283-
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
284-
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
285-
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
286-
* }
287-
* client.stack().contentType('content_type_uid').entry().create({ entry })
288-
* .then((entry) => console.log(entry))
289-
*/
269+
/**
270+
* @description The Create an entry call creates a new entry for the selected content type.
271+
* @memberof Entry
272+
* @func create
273+
* @returns {Promise<Entry.Entry>} Promise for Entry instance
274+
*
275+
* @example
276+
* import * as contentstack from '@contentstack/management'
277+
* const client = contentstack.client()
278+
* const entry = {
279+
* title: 'Sample Entry',
280+
* url: '/sampleEntry',
281+
* file: 'asset_uid', // for single asset pass asset uid to entry asset field value
282+
* multiple_file = ['asset_uid_1', 'asset_uid_2'], // for multiple asset pass array of asset uid to entry asset field values
283+
* reference: reference.uid, // for single reference pass reference uid to entry reference field value
284+
* multiple_reference: ['reference_uid_1', 'reference_uid_2'], // for multiple reference pass array of reference uid to entry reference field values
285+
* multiple_content_type_reference: [{_content_type_uid: 'content_type_uid_1', uid: 'reference_uid_1'}, {_content_type_uid: 'content_type_uid_2', uid: 'reference_uid_2'}] // for multiple reference pass array of reference uid to entry reference field values
286+
* }
287+
* client.stack().contentType('content_type_uid').entry().create({ entry })
288+
* .then((entry) => console.log(entry))
289+
*/
290290
this.create = create({ http: http })
291291

292292
/**
293-
* @description The Query on Entry will allow to fetch details of all or specific Entry
294-
* @memberof Entry
295-
* @func query
296-
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
297-
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
298-
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
299-
* @param {Object} query Queries that you can use to fetch filtered results.
300-
* @returns {Array<Entry>} Array of Entry.
301-
*
302-
* @example
303-
* import * as contentstack from '@contentstack/management'
304-
* const client = contentstack.client()
305-
*
306-
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
307-
* .then((entries) => console.log(entries))
308-
*/
293+
* @description The Query on Entry will allow to fetch details of all or specific Entry
294+
* @memberof Entry
295+
* @func query
296+
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
297+
* @param {Int} include_workflow Enter 'true' to include the workflow details of the entry.
298+
* @param {Int} include_publish_details Enter 'true' to include the publish details of the entry.
299+
* @param {Object} query Queries that you can use to fetch filtered results.
300+
* @returns {Array<Entry>} Array of Entry.
301+
*
302+
* @example
303+
* import * as contentstack from '@contentstack/management'
304+
* const client = contentstack.client()
305+
*
306+
* client.stack().contentType('content_type_uid').entry().query({ query: { title: 'Entry title' } }).find()
307+
* .then((entries) => console.log(entries))
308+
*/
309309
this.query = query({ http: http, wrapperCollection: EntryCollection })
310310
}
311311

lib/stack/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export function Stack (http, data) {
3535
if (data && data.stack && data.stack.api_key) {
3636
this.stackHeaders = { api_key: this.api_key }
3737
if (this.management_token && this.management_token !== undefined) {
38-
this.stackHeaders.authorization = this.management_token
38+
// this.stackHeaders.authorization = this.management_token
39+
http.defaults.headers.authorization = this.management_token
3940
delete this.management_token
4041
}
4142

@@ -452,17 +453,17 @@ export function Stack (http, data) {
452453
* .then((response) => console.log(response.notice))
453454
*
454455
*/
455-
this.updateUsersRoles = async (users) => {
456+
this.updateUsersRoles = async (data) => {
456457
try {
457458
const response = await http.post(`${this.urlPath}/users/roles`,
458-
{ users },
459+
{ users: data },
459460
{
460461
headers: {
461462
...cloneDeep(this.stackHeaders)
462463
}
463464
})
464465
if (response.data) {
465-
return UserCollection(http, response.data.stack)
466+
return response.data
466467
} else {
467468
return error(response)
468469
}

test/api/stack-test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('Stack api Test', () => {
5353
})
5454

5555
it('Update Stack details', done => {
56-
const name = 'My New Stack Update Name'
56+
const name = 'Conference Demo'
5757
const description = 'My New description stack'
5858
client.stack({ api_key: stacks.api_key })
5959
.fetch().then((stack) => {
@@ -130,13 +130,13 @@ describe('Stack api Test', () => {
130130

131131
it('Get query stack', done => {
132132
client.stack()
133-
.query({ query: { name: 'Conference Demo' } })
133+
.query({ query: { name: 'My New Stack Update Name' } })
134134
.find()
135135
.then((response) => {
136136
expect(response.items.length).to.be.equal(1)
137137
for (const index in response.items) {
138138
const stack = response.items[index]
139-
expect(stack.name).to.be.equal('Conference Demo')
139+
expect(stack.name).to.be.equal('My New Stack Update Name')
140140
}
141141
done()
142142
})
@@ -145,12 +145,12 @@ describe('Stack api Test', () => {
145145

146146
it('Find one stack', done => {
147147
client.stack()
148-
.query({ query: { name: 'Conference Demo' } })
148+
.query({ query: { name: 'My New Stack Update Name' } })
149149
.findOne()
150150
.then((response) => {
151151
const stack = response.items[0]
152152
expect(response.items.length).to.be.equal(1)
153-
expect(stack.name).to.be.equal('Conference Demo')
153+
expect(stack.name).to.be.equal('My New Stack Update Name')
154154
done()
155155
})
156156
.catch(done)

test/api/user-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var loggedinUserID = ''
1111
var client = contentstackClient()
1212
describe('Contentstack User Session api Test', () => {
1313
it('User login wrong credentials', done => {
14-
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORDWRONG })
14+
contentstackClient().login({ email: process.env.EMAIL, password: process.env.PASSWORD })
1515
.then((response) => {
1616
console.log(response)
1717
done()

test/unit/ContentstackClient-test.js

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import axios from 'axios'
22
import httpAdapter from 'axios/lib/adapters/http'
33
import ContentstackClient from '../../lib/contentstackClient'
44
import { expect } from 'chai'
5-
import { describe, it, after, beforeEach } from 'mocha'
6-
import nock from 'nock'
5+
import { describe, it, beforeEach } from 'mocha'
6+
import MockAdapter from 'axios-mock-adapter'
77
var host = 'http://localhost/'
88

99
describe('Contentstack Client', () => {
@@ -12,10 +12,6 @@ describe('Contentstack Client', () => {
1212
axios.defaults.host = host
1313
axios.defaults.adapter = httpAdapter
1414
})
15-
after('all', function () {
16-
nock.restore()
17-
nock.cleanAll()
18-
})
1915

2016
it('Contentstack Client Object successful', done => {
2117
var contentstackClient = ContentstackClient({ http: axios })
@@ -24,30 +20,12 @@ describe('Contentstack Client', () => {
2420
})
2521

2622
it('Contentstack Client login success', done => {
27-
nock(host)
28-
.post('/user-session', {})
29-
.reply(200, {
30-
user: {
31-
authtoken: 'Test Auth'
32-
}
33-
})
34-
ContentstackClient({ http: axios })
35-
.login()
36-
.then((response) => {
37-
expect(response.user.authtoken).to.be.equal('Test Auth')
38-
done()
39-
})
40-
.catch(done)
41-
})
42-
43-
it('Contentstack Client login success', done => {
44-
nock(host)
45-
.post('/user-session', {})
46-
.reply(200, {
47-
user: {
48-
authtoken: 'Test Auth'
49-
}
50-
})
23+
var mock = new MockAdapter(axios)
24+
mock.onPost('/user-session').reply(200, {
25+
user: {
26+
authtoken: 'Test Auth'
27+
}
28+
})
5129
ContentstackClient({ http: axios })
5230
.login()
5331
.then((response) => {
@@ -58,13 +36,12 @@ describe('Contentstack Client', () => {
5836
})
5937

6038
it('Contentstack Client get user info', done => {
61-
nock(host)
62-
.get('/user')
63-
.reply(200, {
64-
user: {
65-
uid: 'test uid'
66-
}
67-
})
39+
var mock = new MockAdapter(axios)
40+
mock.onGet('/user').reply(200, {
41+
user: {
42+
uid: 'test uid'
43+
}
44+
})
6845
ContentstackClient({ http: axios })
6946
.getUser()
7047
.then((user) => {
@@ -75,11 +52,10 @@ describe('Contentstack Client', () => {
7552
})
7653

7754
it('Contentstack Client Logout with Authtoken', done => {
78-
nock(host)
79-
.delete('/user-session')
80-
.reply(200, {
81-
notice: 'You\'ve logged out successfully'
82-
})
55+
var mock = new MockAdapter(axios)
56+
mock.onDelete('/user-session').reply(200, {
57+
notice: 'You\'ve logged out successfully'
58+
})
8359
ContentstackClient({ http: axios })
8460
.logout('Test Auth')
8561
.then((response) => {
@@ -90,11 +66,10 @@ describe('Contentstack Client', () => {
9066
})
9167

9268
it('Contentstack Client Logout', done => {
93-
nock(host)
94-
.delete('/user-session')
95-
.reply(200, {
96-
notice: 'You\'ve logged out successfully'
97-
})
69+
var mock = new MockAdapter(axios)
70+
mock.onDelete('/user-session').reply(200, {
71+
notice: 'You\'ve logged out successfully'
72+
})
9873
axios.defaults.headers = {
9974
common: {
10075
authtoken: 'Authtoken'

test/unit/Util-test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,28 @@ describe('Get User Agent', () => {
2121
platform: 'MacIntel'
2222
}
2323
}
24-
const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
25-
expect(userAgent.match(headerRegEx).length).to.be.equal(5)
26-
expect(userAgent.indexOf('platform browser')).to.not.equal(-1)
27-
expect(userAgent.indexOf('os macOS;')).to.not.equal(-1)
24+
const macUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
25+
expect(macUserAgent.match(headerRegEx).length).to.be.equal(5)
26+
expect(macUserAgent.indexOf('platform browser')).to.not.equal(-1)
27+
expect(macUserAgent.indexOf('os macOS;')).to.not.equal(-1)
28+
29+
global.window.navigator.platform = 'Windows'
30+
const windowsUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
31+
expect(windowsUserAgent.match(headerRegEx).length).to.be.equal(5)
32+
expect(windowsUserAgent.indexOf('platform browser')).to.not.equal(-1)
33+
expect(windowsUserAgent.indexOf('os Windows;')).to.not.equal(-1)
34+
35+
global.window.navigator = { userAgent: 'Android' }
36+
const androidUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
37+
expect(androidUserAgent.match(headerRegEx).length).to.be.equal(5)
38+
expect(androidUserAgent.indexOf('platform browser')).to.not.equal(-1)
39+
expect(androidUserAgent.indexOf('os Android;')).to.not.equal(-1)
40+
41+
global.window.navigator = { platform: 'Linux' }
42+
const linuxUserAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
43+
expect(linuxUserAgent.match(headerRegEx).length).to.be.equal(5)
44+
expect(linuxUserAgent.indexOf('platform browser')).to.not.equal(-1)
45+
expect(linuxUserAgent.indexOf('os Linux;')).to.not.equal(-1)
2846
done()
2947

3048
getUserAgentRewireApi.__ResetDependency__('isNode')
@@ -40,7 +58,6 @@ describe('Get User Agent', () => {
4058
global.window = {}
4159

4260
const userAgent = getUserAgent('contentstack-sdk/1.0.0', 'SampleApplication/0.1', 'SampleIntegration/0,1')
43-
4461
expect(userAgent.match(headerRegEx).length).to.be.equal(3)
4562
expect(userAgent.indexOf('platform browser')).to.equal(-1)
4663
expect(userAgent.indexOf('os macOS;')).to.equal(-1)

0 commit comments

Comments
 (0)