-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1186 from contentstack/test/CS-42859
test: Unit test cases added for new marketplace sdk adapter [CS-42859]
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
packages/contentstack-utilities/test/unit/contentstack-marketplace-sdk.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { expect } from '@oclif/test'; | ||
import { fancy } from '@contentstack/cli-dev-dependencies'; | ||
|
||
import authHandler from '../../src/auth-handler'; | ||
import configStore from '../../src/config-handler'; | ||
import marketplaceSDKClient, { marketplaceSDKInitiator } from '../../src/contentstack-marketplace-sdk'; | ||
|
||
describe('MarketplaceSDKInitiator class', () => { | ||
const host = 'test.app-api.io'; | ||
const endpoint = `http://${host}/marketplace`; | ||
|
||
describe('createAppSDKClient method', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configStore, 'get', (...[key]: (string | any)[]) => { | ||
return { | ||
authorisationType: 'BASIC', | ||
authtoken: 'TEST-AUTH-TKN', | ||
}[key]; | ||
}) | ||
.spy(configStore, 'get') | ||
.it('should create sdk instance with given host', async ({ spy }) => { | ||
marketplaceSDKInitiator.init({ analyticsInfo: 'TEST-DATA' }); | ||
const appSdk = await marketplaceSDKClient({ host }); | ||
|
||
expect(appSdk).has.haveOwnProperty('login'); | ||
expect(appSdk).has.haveOwnProperty('logout'); | ||
expect(appSdk).has.haveOwnProperty('marketplace'); | ||
expect(spy.get.callCount).to.be.equals(2); | ||
}); | ||
}); | ||
|
||
describe('SDK retryCondition & refreshToken', () => { | ||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configStore, 'get', (...[key]: (string | any)[]) => { | ||
return { | ||
authorisationType: 'BASIC', | ||
authtoken: 'TEST-AUTH-TKN', | ||
}[key]; | ||
}) | ||
.nock(endpoint, (api) => api.persist().get(`/manifests`).reply(401)) | ||
.it("should throw 'Session timed out error' if auth type is 'BASIC'", async () => { | ||
const appSdk = await marketplaceSDKClient({ endpoint, management_token: '', retryDelay: 300, retryLimit: 1 }); | ||
try { | ||
await appSdk.marketplace('UID').findAllApps(); | ||
} catch (error) { | ||
expect(error.errorCode).to.be.contain('401'); | ||
expect(error).is.haveOwnProperty('message'); | ||
expect(error.errorMessage).to.contain('Session timed out, please login to proceed'); | ||
} | ||
}); | ||
|
||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configStore, 'get', (...[key]: (string | any)[]) => { | ||
return { | ||
authorisationType: 'OAUTH', | ||
oauthAccessToken: 'TEST-AUTH-TKN', | ||
}[key]; | ||
}) | ||
.stub(authHandler, 'compareOAuthExpiry', async () => void 0) | ||
.nock(endpoint, (api) => api.get(`/manifests`).reply(401)) | ||
.nock(endpoint, (api) => api.get(`/manifests`).reply(200, [])) | ||
.spy(authHandler, 'compareOAuthExpiry') | ||
.it("should refresh token if auth type is 'OAUTH'", async ({ spy }) => { | ||
const appSdk = await marketplaceSDKClient({ endpoint, retryLimit: 1, retryDelay: 300 }); | ||
const apps = await appSdk.marketplace('UID').findAllApps(); | ||
|
||
expect(apps.items).deep.equal([]); | ||
expect(apps.items.length).to.be.equals(0); | ||
expect(spy.compareOAuthExpiry.callCount).to.be.equals(2); | ||
}); | ||
|
||
fancy | ||
.stdout({ print: process.env.PRINT === 'true' || false }) | ||
.stub(configStore, 'get', (...[key]: (string | any)[]) => { | ||
return { | ||
authorisationType: 'OAUTH', | ||
oauthAccessToken: 'TEST-AUTH-TKN', | ||
}[key]; | ||
}) | ||
.nock(endpoint, (api) => api.get(`/manifests`).reply(500)) | ||
.it('should not refresh the token if status code is not among [401, 429, 408]', async () => { | ||
const appSdk = await marketplaceSDKClient({ endpoint, management_token: '' }); | ||
try { | ||
await appSdk.marketplace('UID').findAllApps(); | ||
} catch (error) { | ||
expect(error.status).to.be.equals(500); | ||
} | ||
}); | ||
}); | ||
}); |