@unito/integration-cli
Version:
Integration CLI
84 lines (83 loc) • 5.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const strict_1 = tslib_1.__importDefault(require("node:assert/strict"));
const sinon_1 = tslib_1.__importDefault(require("sinon"));
const oauth2_1 = tslib_1.__importDefault(require("../../src/services/oauth2"));
const configurationTypes_1 = require("../../src/configurationTypes");
const oauth2HelperResource = tslib_1.__importStar(require("../../src/resources/oauth2"));
const errors_1 = require("../../src/errors");
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
describe('OAuth2Helper', () => {
let fetchStub;
const authorizationInfo = {
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
authorizationUrl: 'https://provider.com/oauth/authorize',
scopes: [{ name: 'scope1' }, { name: 'scope2' }],
tokenUrl: 'https://provider.com/oauth/token',
grantType: configurationTypes_1.GrantType.AUTHORIZATION_CODE,
requestContentType: configurationTypes_1.RequestContentType.URL_ENCODED,
responseContentType: configurationTypes_1.RequestContentType.JSON,
};
beforeEach(() => {
sinon_1.default.stub(oauth2_1.default.prototype, 'startServer').resolves('http://localhost:5050');
sinon_1.default.stub(oauth2_1.default.prototype, 'stopServer');
sinon_1.default.stub(oauth2_1.default.prototype, 'authorize');
sinon_1.default.stub(inquirer_1.default, 'prompt');
});
afterEach(() => {
sinon_1.default.restore();
});
describe('performOAuth2Flow', () => {
it('should perform the oauth flow', async () => {
fetchStub = sinon_1.default.stub().onFirstCall().resolves({ status: 200 }).onSecondCall().resolves({ status: 200 });
sinon_1.default.replace(global, 'fetch', fetchStub);
sinon_1.default.stub(oauth2_1.default.prototype, 'callbackIsDone').resolves({ accessToken: 'token' });
await oauth2HelperResource.performOAuth2Flow(authorizationInfo);
strict_1.default.equal(fetchStub.getCall(0).args.at(0), 'http://localhost:5050/health');
});
it('raises a FailedToRetrieveAccessTokenError', async () => {
fetchStub = sinon_1.default.stub().onFirstCall().resolves({ status: 200 }).onSecondCall().resolves({ status: 200 });
sinon_1.default.replace(global, 'fetch', fetchStub);
sinon_1.default.stub(oauth2_1.default.prototype, 'callbackIsDone').resolves({});
const response = oauth2HelperResource.performOAuth2Flow(authorizationInfo);
await strict_1.default.rejects(response, errors_1.FailedToRetrieveAccessTokenError);
});
it('raises a FailedToRetrieveAccessTokenError when the accessToken is not returned', async () => {
fetchStub = sinon_1.default.stub().onFirstCall().resolves({ status: 200 }).onSecondCall().resolves({ status: 200 });
sinon_1.default.replace(global, 'fetch', fetchStub);
sinon_1.default.stub(oauth2_1.default.prototype, 'callbackIsDone').resolves({});
const response = oauth2HelperResource.performOAuth2Flow(authorizationInfo);
await strict_1.default.rejects(response, errors_1.FailedToRetrieveAccessTokenError);
});
});
describe('updateToken', () => {
let updateTokenStub;
beforeEach(() => {
updateTokenStub = sinon_1.default.stub(oauth2_1.default.prototype, 'updateToken');
});
it('should create OAuth2Service with credential payload and call updateToken', async () => {
const mockResponse = { accessToken: 'new-token', refreshToken: 'new-refresh-token' };
updateTokenStub.resolves(mockResponse);
const credentialPayload = { domain: 'test-domain', customVar: 'test-value' };
const refreshToken = 'test-refresh-token';
const result = await oauth2HelperResource.updateToken(authorizationInfo, refreshToken, credentialPayload);
// Assert that the OAuth2Service was instantiated with the credential payload
// and that updateToken was called with the refresh token
sinon_1.default.assert.calledOnce(updateTokenStub);
sinon_1.default.assert.calledWith(updateTokenStub, refreshToken);
strict_1.default.deepEqual(result, mockResponse);
});
it('should work without credential payload', async () => {
const mockResponse = { accessToken: 'new-token', refreshToken: 'new-refresh-token' };
updateTokenStub.resolves(mockResponse);
const refreshToken = 'test-refresh-token';
const result = await oauth2HelperResource.updateToken(authorizationInfo, refreshToken);
// Should still work when no credential payload is provided
sinon_1.default.assert.calledOnce(updateTokenStub);
sinon_1.default.assert.calledWith(updateTokenStub, refreshToken);
strict_1.default.deepEqual(result, mockResponse);
});
});
});