Skip to content

Commit 4c3a0aa

Browse files
Merge pull request #73 from contentstack/staging
hotfix/region-support-fix
2 parents ad5bcea + 868d984 commit 4c3a0aa

9 files changed

+110
-44
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fileignoreconfig:
66
- filename: test/unit/mock/execution-mock.js
77
checksum: 89d239d37c9d8d0cdb6ac61553a7d2e2d9115a10207f7c0b387c3565c9cb6564
88
- filename: package-lock.json
9-
checksum: 0f9b6180aa8b66fdd055ef99f74f269febb07e90bbd64badf1039a4d375850b6
9+
checksum: bc3cbd474a69321d97be9557ef74a056b7fab4111869d496b0d0403c1c84b971
1010
- filename: docdash-template/fixtures/documents/probe.js
1111
checksum: e841ecf889d0e82367c53c48ee0b3be8bd68d7babf4777a87ced769f29686ac4
1212
- filename: docdash-template/.travis.yml

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
3+
## [v1.2.7](https://github.com/contentstack/contentstack-marketplace-sdk/tree/v1.2.6) (2024-05-15)
4+
- Fixed base URL path logic in contentstackClient to handle when region and host not provided
5+
26
## [v1.2.6](https://github.com/contentstack/contentstack-marketplace-sdk/tree/v1.2.6) (2024-03-03)
37
- Update sanity tests
48

lib/contentstackClient.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export default function contentstackClient ({ http }) {
3131

3232
if (region && region !== Region.NA) {
3333
baseUrlPath = `https://${region}-api.contentstack.com:443/v3/user-session`
34+
} else if (!region && http?.defaults?.host) {
35+
baseUrlPath = `https://${http.defaults.host}:443/v3/user-session`
36+
} else {
37+
baseUrlPath = `https://api.contentstack.io:443/v3/user-session`
3438
}
3539

3640
function login (requestBody, params = {}) {

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/marketplace-sdk",
3-
"version": "1.2.6",
3+
"version": "1.2.7",
44
"description": "The Contentstack Marketplace SDK is used to manage the content of your Contentstack marketplace apps",
55
"main": "./dist/node/contentstack-marketplace.js",
66
"browser": "./dist/web/contentstack-marketplace.js",
@@ -92,6 +92,6 @@
9292
"webpack-merge": "4.2.2"
9393
},
9494
"dependencies": {
95-
"axios": "^1.8.2"
95+
"axios": "^1.8.3"
9696
}
9797
}

sanity-report-dev11.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const user4 = process.env.USER4;
1010

1111
const mochawesomeJsonOutput = fs.readFileSync(
1212
"./mochawesome-report/mochawesome.json",
13-
"utf-8"
13+
"utf8"
1414
);
1515
const mochawesomeReport = JSON.parse(mochawesomeJsonOutput);
1616

sanity-report.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import fs from 'fs'
55

66
dotenv.config()
77

8-
const mochawesomeJsonOutput = fs.readFileSync('./mochawesome-report/mochawesome.json', 'utf-8')
8+
const mochawesomeJsonOutput = fs.readFileSync('./mochawesome-report/mochawesome.json', 'utf8')
99
const mochawesomeReport = JSON.parse(mochawesomeJsonOutput)
1010
const report = `./mochawesome-report/sanity-report.html`
1111

@@ -25,8 +25,16 @@ console.log(`Failed Tests: ${failedTests}`)
2525
console.log(`Pending Tests: ${pendingTests}`)
2626
console.log(`Total Duration: ${durationInMinutes}m ${durationInSeconds.toFixed(2)}s`)
2727

28+
const host = process.env.DEFAULTHOST || ''
29+
let region = 'NA'
30+
31+
const match = host.match(/^([^-]+(?:-[^-]+)*)-api/)
32+
if (match && match[1]) {
33+
region = match[1].toUpperCase()
34+
}
35+
2836
const slackMessage = `
29-
*JavaScript Marketplace SDK Report*
37+
*JavaScript Marketplace SDK Report - ${region}*
3038
• Total Suites: *${totalSuites}*
3139
• Total Tests: *${totalTests}*
3240
• Passed Tests: *${passedTests}*

test/unit/ContentstackClient-test.js

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,31 @@ import { expect } from 'chai'
44
import { describe, it, beforeEach } from 'mocha'
55
import MockAdapter from 'axios-mock-adapter'
66
import Region from '../../lib/core/region'
7-
var host = 'http://localhost/'
7+
var host = 'localhost'
8+
9+
describe('Region Test ', () => {
10+
it('Contentstack Client login success with region NA', done => {
11+
var mock = new MockAdapter(axios)
12+
axios.defaults.region = Region.NA
13+
mock.onPost('https://api.contentstack.io:443/v3/user-session').reply(200, {
14+
user: {
15+
authtoken: 'Test Auth'
16+
}
17+
})
18+
19+
ContentstackClient({ http: axios })
20+
.login()
21+
.then((response) => {
22+
expect(response.user.authtoken).to.be.equal('Test Auth')
23+
done()
24+
})
25+
.catch(done)
26+
})
27+
})
828

929
describe('Contentstack Client', () => {
1030
beforeEach(function () {
11-
host = 'http://localhost/'
31+
host = 'localhost'
1232
axios.defaults.host = host
1333
axios.defaults.adapter = 'http'
1434
})
@@ -21,7 +41,7 @@ describe('Contentstack Client', () => {
2141

2242
it('Contentstack Client login success', done => {
2343
var mock = new MockAdapter(axios)
24-
mock.onPost('/user-session').reply(200, {
44+
mock.onPost('https://localhost:443/v3/user-session').reply(200, {
2545
user: {
2646
authtoken: 'Test Auth'
2747
}
@@ -37,7 +57,7 @@ describe('Contentstack Client', () => {
3757

3858
it('Contentstack Client Logout with Authtoken', done => {
3959
var mock = new MockAdapter(axios)
40-
mock.onDelete('/user-session').reply(200, {
60+
mock.onDelete('https://localhost:443/v3/user-session').reply(200, {
4161
notice: 'You\'ve logged out successfully'
4262
})
4363
ContentstackClient({ http: axios })
@@ -51,7 +71,7 @@ describe('Contentstack Client', () => {
5171

5272
it('Contentstack Client Logout', done => {
5373
var mock = new MockAdapter(axios)
54-
mock.onDelete('/user-session').reply(200, {
74+
mock.onDelete('https://localhost:443/v3/user-session').reply(200, {
5575
notice: 'You\'ve logged out successfully'
5676
})
5777
axios.defaults.headers = {
@@ -101,20 +121,57 @@ describe('Contentstack Client', () => {
101121
done()
102122
})
103123

104-
it('Contentstack Client login success with region', done => {
105-
var mock = new MockAdapter(axios)
106-
axios.defaults.region = Region.AZURE_NA
107-
mock.onPost('https://azure-na-api.contentstack.com:443/v3/user-session').reply(200, {
108-
user: {
109-
authtoken: 'Test Auth'
110-
}
124+
it('Contentstack Client login success with region AZURE-NA', done => {
125+
var mock = new MockAdapter(axios)
126+
axios.defaults.region = Region.AZURE_NA
127+
mock.onPost('https://azure-na-api.contentstack.com:443/v3/user-session').reply(200, {
128+
user: {
129+
authtoken: 'Test Auth'
130+
}
131+
})
132+
133+
ContentstackClient({ http: axios })
134+
.login()
135+
.then((response) => {
136+
expect(response.user.authtoken).to.be.equal('Test Auth')
137+
done()
111138
})
112-
ContentstackClient({ http: axios })
113-
.login()
114-
.then((response) => {
115-
expect(response.user.authtoken).to.be.equal('Test Auth')
116-
done()
117-
})
118-
.catch(done)
139+
.catch(done)
140+
})
141+
142+
it('Contentstack Client login success with region AZURE-EU', done => {
143+
var mock = new MockAdapter(axios)
144+
axios.defaults.region = Region.AZURE_EU
145+
mock.onPost('https://azure-eu-api.contentstack.com:443/v3/user-session').reply(200, {
146+
user: {
147+
authtoken: 'Test Auth'
148+
}
149+
})
150+
151+
ContentstackClient({ http: axios })
152+
.login()
153+
.then((response) => {
154+
expect(response.user.authtoken).to.be.equal('Test Auth')
155+
done()
156+
})
157+
.catch(done)
158+
})
159+
160+
it('Contentstack Client login success with region GCP-NA', done => {
161+
var mock = new MockAdapter(axios)
162+
axios.defaults.region = Region.GCP_NA
163+
mock.onPost('https://gcp-na-api.contentstack.com:443/v3/user-session').reply(200, {
164+
user: {
165+
authtoken: 'Test Auth'
166+
}
167+
})
168+
169+
ContentstackClient({ http: axios })
170+
.login()
171+
.then((response) => {
172+
expect(response.user.authtoken).to.be.equal('Test Auth')
173+
done()
174+
})
175+
.catch(done)
119176
})
120177
})

test/unit/concurrency-Queue-test.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,15 @@ describe('Concurrency queue test', () => {
130130
})
131131

132132
it('Refresh Token on 401 with 1000 concurrent request', done => {
133-
var mock = new MockAdapter(axios)
134-
mock.onPost('/user-session').reply(200, {
135-
token
136-
})
137-
const axiosClient = client({
138-
baseURL: `${host}:${port}`,
139-
authorization: 'Bearer <token_value>',
133+
const axiosClient = client({
134+
baseURL: `${host}:${port}`,
135+
authorization: 'Bearer <token_value>',
140136
logHandler: logHandlerStub,
141137
refreshToken: () => {
142-
return new Promise((resolve, reject) => {
143-
return contentstackClient({ http: axios }).login().then((res) => {
144-
resolve({ authorization: res.token })
145-
}).catch((error) => {
146-
reject(error)
138+
return Axios.post(`${host}:${port}/user-session`)
139+
.then((res) => {
140+
return { authorization: res.data.token }
147141
})
148-
})
149142
}
150143
})
151144
Promise.all(sequence(1003).map(() => axiosClient.axiosInstance.get('/unauthorized')))

0 commit comments

Comments
 (0)