Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Unit test cases added for new marketplace sdk adapter [CS-42859] #1186

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});
});
});
Loading