@unito/integration-cli 0.57.2 → 0.58.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.eslintrc.d.ts +6 -0
- package/dist/.eslintrc.js +17 -2
- package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/src/index.ts +1 -3
- package/dist/src/commands/dev.d.ts +4 -0
- package/dist/src/commands/dev.js +40 -4
- package/dist/src/commands/init.js +2 -2
- package/dist/src/commands/login.js +1 -1
- package/dist/src/commands/oauth2.js +27 -21
- package/dist/src/commands/test.js +28 -6
- package/dist/src/errors.d.ts +8 -0
- package/dist/src/errors.js +35 -18
- package/dist/src/resources/configuration.d.ts +1 -1
- package/dist/src/resources/configuration.js +4 -6
- package/dist/src/resources/credentials.d.ts +3 -0
- package/dist/src/resources/credentials.js +26 -0
- package/dist/src/resources/integrations.d.ts +1 -0
- package/dist/src/resources/integrations.js +37 -2
- package/dist/src/resources/oauth2.d.ts +4 -0
- package/dist/src/resources/oauth2.js +28 -0
- package/dist/src/resources/template.d.ts +3 -0
- package/dist/src/resources/template.js +40 -0
- package/dist/src/services/integrationsPlatform.d.ts +1 -0
- package/dist/src/services/integrationsPlatform.js +5 -1
- package/dist/src/{oauth2Helper/oauth2Helper.d.ts → services/oauth2.d.ts} +37 -23
- package/dist/src/{oauth2Helper/oauth2Helper.js → services/oauth2.js} +112 -113
- package/dist/test/commands/dev.test.js +21 -0
- package/dist/test/commands/init.test.js +4 -4
- package/dist/test/commands/login.test.js +1 -0
- package/dist/test/commands/oauth2.test.js +20 -2
- package/dist/test/commands/publish.test.js +1 -0
- package/dist/test/commands/test.test.js +22 -0
- package/dist/test/commands/upgrade.test.js +1 -0
- package/dist/test/resources/integrations.test.js +37 -0
- package/dist/test/resources/oauth2.test.js +55 -0
- package/dist/test/resources/template.test.js +40 -0
- package/dist/test/services/oauth2.test.d.ts +1 -0
- package/dist/test/services/oauth2.test.js +159 -0
- package/oclif.manifest.json +64 -9
- package/package.json +8 -8
- package/dist/integrationGenerator/errors.d.ts +0 -2
- package/dist/integrationGenerator/errors.js +0 -6
- package/dist/integrationGenerator/index.d.ts +0 -2
- package/dist/integrationGenerator/index.js +0 -5
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.dockerignore +0 -3
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.eslintrc.js +0 -74
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.nvmrc +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.prettierignore +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.prettierrc +0 -7
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/.unito.json +0 -1
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/Dockerfile +0 -38
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/README.md +0 -21
- package/dist/integrationGenerator/integrationBoilerplate/integrationBoilerplate/src/index.ts +0 -94
- package/dist/integrationGenerator/integrationBoilerplate/package.json +0 -43
- package/dist/integrationGenerator/integrationBoilerplate/src/logger.ts +0 -55
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/additionalLoggingContext.ts +0 -22
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/correlationId.ts +0 -13
- package/dist/integrationGenerator/integrationBoilerplate/src/middlewares/credentials.ts +0 -38
- package/dist/integrationGenerator/integrationBoilerplate/src/request.ts +0 -59
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/index.ts +0 -11
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/me.ts +0 -15
- package/dist/integrationGenerator/integrationBoilerplate/src/routes/root.ts +0 -12
- package/dist/integrationGenerator/integrationBoilerplate/tsconfig.json +0 -37
- package/dist/integrationGenerator/src/index.d.ts +0 -1
- package/dist/integrationGenerator/src/index.js +0 -5
- package/dist/integrationGenerator/src/resources/index.d.ts +0 -1
- package/dist/integrationGenerator/src/resources/index.js +0 -5
- package/dist/integrationGenerator/src/resources/integration.d.ts +0 -9
- package/dist/integrationGenerator/src/resources/integration.js +0 -60
- package/dist/integrationGenerator/test/resources/integration.test.js +0 -50
- package/dist/src/oauth2Helper/types.d.ts +0 -22
- package/dist/src/oauth2Helper/types.js +0 -2
- package/dist/src/services/oauth2Helper.d.ts +0 -4
- package/dist/src/services/oauth2Helper.js +0 -35
- package/dist/test/oauth2Helper/oauth2Helper.test.js +0 -150
- package/dist/test/services/oauth2Helper.test.js +0 -85
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.dockerignore +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.eslintrc.js +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.nvmrc +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.prettierignore +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.prettierrc +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/.unito.json +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/Dockerfile +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate → boilerplate}/README.md +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/package.json +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/logger.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/additionalLoggingContext.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/correlationId.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/middlewares/credentials.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/request.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/index.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/me.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/src/routes/root.ts +0 -0
- /package/dist/{integrationGenerator/integrationBoilerplate/integrationBoilerplate → boilerplate}/tsconfig.json +0 -0
- /package/dist/{integrationGenerator/test/resources/integration.test.d.ts → test/resources/integrations.test.d.ts} +0 -0
- /package/dist/test/{oauth2Helper/oauth2Helper.test.d.ts → resources/oauth2.test.d.ts} +0 -0
- /package/dist/test/{services/oauth2Helper.test.d.ts → resources/template.test.d.ts} +0 -0
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const strict_1 = tslib_1.__importDefault(require("node:assert/strict"));
|
|
5
|
-
const path = tslib_1.__importStar(require("path"));
|
|
6
|
-
const sinon = tslib_1.__importStar(require("sinon"));
|
|
7
|
-
const tmp_1 = tslib_1.__importDefault(require("tmp"));
|
|
8
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
9
|
-
const Integration = tslib_1.__importStar(require("../../src/resources/integration"));
|
|
10
|
-
describe('Integration Resource Generation', () => {
|
|
11
|
-
let tempDir;
|
|
12
|
-
const boilerplatePath = path.join(__dirname, '../../integrationBoilerplate');
|
|
13
|
-
afterEach(() => {
|
|
14
|
-
sinon.restore();
|
|
15
|
-
});
|
|
16
|
-
describe('generateIntegrationSkeleton', () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
tempDir = tmp_1.default.dirSync({ prefix: 'outputIntegrations-' }).name;
|
|
19
|
-
});
|
|
20
|
-
afterEach(async () => {
|
|
21
|
-
await fs_1.default.promises.rm(tempDir, { recursive: true });
|
|
22
|
-
});
|
|
23
|
-
it('should not create a new integration directory if it already exists', async () => {
|
|
24
|
-
const existingIntegrationName = 'existingIntegration';
|
|
25
|
-
await Integration.generateIntegrationSkeleton(existingIntegrationName, tempDir);
|
|
26
|
-
try {
|
|
27
|
-
await Integration.generateIntegrationSkeleton(existingIntegrationName, tempDir);
|
|
28
|
-
}
|
|
29
|
-
catch (e) {
|
|
30
|
-
(0, strict_1.default)(e.message.includes('already exists with this name'));
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
it('should create a new integration directory if it does not exist', async () => {
|
|
34
|
-
const integrationName = 'NewIntegration';
|
|
35
|
-
const result = await Integration.generateIntegrationSkeleton(integrationName, tempDir);
|
|
36
|
-
strict_1.default.equal(result, path.join(tempDir, integrationName.toLowerCase()));
|
|
37
|
-
(0, strict_1.default)(fs_1.default.existsSync(result));
|
|
38
|
-
const expectedFiles = await fs_1.default.promises.readdir(boilerplatePath);
|
|
39
|
-
expectedFiles.push('.npmrc');
|
|
40
|
-
const createdFiles = await fs_1.default.promises.readdir(result);
|
|
41
|
-
strict_1.default.deepEqual(createdFiles, expectedFiles.sort());
|
|
42
|
-
createdFiles.forEach(createdField => (0, strict_1.default)(expectedFiles.includes(createdField)));
|
|
43
|
-
});
|
|
44
|
-
it('generates an .npmrc file', async () => {
|
|
45
|
-
const result = await Integration.generateIntegrationSkeleton('foo', tempDir);
|
|
46
|
-
const npmrc = await fs_1.default.promises.readFile(`${result}/.npmrc`, 'utf-8');
|
|
47
|
-
(0, strict_1.default)(npmrc.includes('//npm.pkg.github.com/:_authToken=${UNITO_GITHUB_PKG_TOKEN}'));
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Oauth2 } from '../configurationTypes';
|
|
2
|
-
export interface Oauth2Credentials {
|
|
3
|
-
clientId: string;
|
|
4
|
-
clientSecret: string;
|
|
5
|
-
}
|
|
6
|
-
export interface AuthorizePayload extends Oauth2Credentials {
|
|
7
|
-
providerAuthorizationUrl: string;
|
|
8
|
-
scopes?: string;
|
|
9
|
-
providerAccessTokenUrl: string;
|
|
10
|
-
}
|
|
11
|
-
export interface CallbackPayload {
|
|
12
|
-
code: string;
|
|
13
|
-
}
|
|
14
|
-
export interface Oauth2Response {
|
|
15
|
-
accessToken: string;
|
|
16
|
-
refreshToken?: string;
|
|
17
|
-
}
|
|
18
|
-
export interface TokenPayload extends Oauth2Credentials {
|
|
19
|
-
providerTokenUrl: string;
|
|
20
|
-
code: string;
|
|
21
|
-
}
|
|
22
|
-
export type Oauth2Payload = Oauth2 & Omit<Oauth2Response, 'accessToken'>;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { Oauth2Response, Oauth2Payload } from '../oauth2Helper/types';
|
|
2
|
-
import { Environment } from '../resources/globalConfiguration';
|
|
3
|
-
export declare function performOAuth2Flow(applicationCredentials: Oauth2Payload, environment?: Environment): Promise<Oauth2Response>;
|
|
4
|
-
export declare function updateToken(applicationCredentials: Oauth2Payload): Promise<Oauth2Response>;
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.updateToken = exports.performOAuth2Flow = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const oauth2Helper_1 = tslib_1.__importDefault(require("../oauth2Helper/oauth2Helper"));
|
|
6
|
-
const errors_1 = require("../errors");
|
|
7
|
-
const globalConfiguration_1 = require("../resources/globalConfiguration");
|
|
8
|
-
async function performOAuth2Flow(applicationCredentials, environment = globalConfiguration_1.Environment.Production) {
|
|
9
|
-
const oauthHelper = new oauth2Helper_1.default(applicationCredentials, environment);
|
|
10
|
-
const serverUrl = await oauthHelper.startServer();
|
|
11
|
-
const healthCheck = await fetch(`${serverUrl}/health`);
|
|
12
|
-
if (healthCheck.status !== 200) {
|
|
13
|
-
throw new Error('OAuthServer did not start');
|
|
14
|
-
}
|
|
15
|
-
await fetch(`${serverUrl}/credentials/new/oauth2/authorize`);
|
|
16
|
-
await oauthHelper.callbackIsDone();
|
|
17
|
-
const response = await fetch(`${serverUrl}/credentials/new/oauth2/token`);
|
|
18
|
-
if (response.status !== 200) {
|
|
19
|
-
throw new errors_1.FailedToRetrieveAccessTokenError(await response.text());
|
|
20
|
-
}
|
|
21
|
-
const credentials = await response.json();
|
|
22
|
-
oauthHelper.stopServer();
|
|
23
|
-
if (!credentials.accessToken) {
|
|
24
|
-
throw new errors_1.FailedToRetrieveAccessTokenError('Access token was not returned');
|
|
25
|
-
}
|
|
26
|
-
return credentials;
|
|
27
|
-
}
|
|
28
|
-
exports.performOAuth2Flow = performOAuth2Flow;
|
|
29
|
-
/* istanbul ignore next */
|
|
30
|
-
// No point to test this function as it is just a wrapper around OAuth2Helper
|
|
31
|
-
async function updateToken(applicationCredentials) {
|
|
32
|
-
const oauthHelper = new oauth2Helper_1.default(applicationCredentials);
|
|
33
|
-
return oauthHelper.updateToken();
|
|
34
|
-
}
|
|
35
|
-
exports.updateToken = updateToken;
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
-
const strict_1 = tslib_1.__importDefault(require("node:assert/strict"));
|
|
6
|
-
const sinon_1 = tslib_1.__importDefault(require("sinon"));
|
|
7
|
-
const oauth2Helper_1 = tslib_1.__importStar(require("../../src/oauth2Helper/oauth2Helper")), oauth2Namespace = oauth2Helper_1;
|
|
8
|
-
const configurationTypes_1 = require("../../src/configurationTypes");
|
|
9
|
-
const errors_1 = require("../../src/errors");
|
|
10
|
-
describe('OAuth2Helper', () => {
|
|
11
|
-
let oauth2Helper;
|
|
12
|
-
let openSpy;
|
|
13
|
-
let fetchStub;
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
const authorizationInfo = {
|
|
16
|
-
clientId: 'your-client-id',
|
|
17
|
-
clientSecret: 'your-client-secret',
|
|
18
|
-
authorizationUrl: 'https://provider.com/oauth/authorize',
|
|
19
|
-
scopes: [{ name: 'scope1' }, { name: 'scope2' }],
|
|
20
|
-
tokenUrl: 'https://provider.com/oauth/token',
|
|
21
|
-
grantType: configurationTypes_1.GrantType.AUTHORIZATION_CODE,
|
|
22
|
-
requestContentType: configurationTypes_1.RequestContentType.URL_ENCODED,
|
|
23
|
-
responseContentType: configurationTypes_1.RequestContentType.JSON,
|
|
24
|
-
tokenRequestParameters: {
|
|
25
|
-
header: {
|
|
26
|
-
Authorization: 'custom',
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
};
|
|
30
|
-
oauth2Helper = new oauth2Helper_1.default(authorizationInfo);
|
|
31
|
-
fetchStub = sinon_1.default.stub().resolves({ json: sinon_1.default.stub().resolves({}), status: 200 });
|
|
32
|
-
sinon_1.default.stub(oauth2Helper, 'startServer').resolves('http://localhost:5050');
|
|
33
|
-
sinon_1.default.stub(oauth2Helper, 'stopServer');
|
|
34
|
-
sinon_1.default.replace(global, 'fetch', fetchStub);
|
|
35
|
-
sinon_1.default.replace(oauth2Namespace, 'open', { open: (_url) => undefined });
|
|
36
|
-
openSpy = sinon_1.default.spy(oauth2Namespace.open, 'open');
|
|
37
|
-
});
|
|
38
|
-
afterEach(() => {
|
|
39
|
-
sinon_1.default.restore();
|
|
40
|
-
});
|
|
41
|
-
describe('handleAuthorize', () => {
|
|
42
|
-
it('should open the authorization URL', async () => {
|
|
43
|
-
const req = {};
|
|
44
|
-
const res = { sendStatus: sinon_1.default.stub() };
|
|
45
|
-
await oauth2Helper['handleAuthorize'](req, res);
|
|
46
|
-
sinon_1.default.assert.calledOnce(openSpy);
|
|
47
|
-
sinon_1.default.assert.calledWith(openSpy, 'https://provider.com/oauth/authorize?client_id=your-client-id&redirect_uri=https%3A%2F%2Fintegrations-platform.unito.io%2Fcredentials%2Fnew%2Foauth2%2Fcallback-cli&state=eyJjbGlDYWxsYmFja1VybCI6Ii9jcmVkZW50aWFscy9uZXcvb2F1dGgyL2NhbGxiYWNrIn0%3D&scope=scope1+scope2&response_type=code');
|
|
48
|
-
sinon_1.default.assert.calledOnce(res.sendStatus);
|
|
49
|
-
});
|
|
50
|
-
it('should maintain the pre-existing query parameters in authorization URL', async () => {
|
|
51
|
-
oauth2Helper['providerAuthorizationUrl'] = 'https://provider.com/oauth/authorize?query1=value1&query2=value2';
|
|
52
|
-
const req = {};
|
|
53
|
-
const res = { sendStatus: sinon_1.default.stub() };
|
|
54
|
-
await oauth2Helper['handleAuthorize'](req, res);
|
|
55
|
-
sinon_1.default.assert.calledOnce(openSpy);
|
|
56
|
-
sinon_1.default.assert.calledWith(openSpy, 'https://provider.com/oauth/authorize?query1=value1&query2=value2&client_id=your-client-id&redirect_uri=https%3A%2F%2Fintegrations-platform.unito.io%2Fcredentials%2Fnew%2Foauth2%2Fcallback-cli&state=eyJjbGlDYWxsYmFja1VybCI6Ii9jcmVkZW50aWFscy9uZXcvb2F1dGgyL2NhbGxiYWNrIn0%3D&scope=scope1+scope2&response_type=code');
|
|
57
|
-
(0, strict_1.default)(openSpy.firstCall.args[0].includes('query1=value1'));
|
|
58
|
-
(0, strict_1.default)(openSpy.firstCall.args[0].includes('query2=value2'));
|
|
59
|
-
sinon_1.default.assert.calledOnce(res.sendStatus);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe('handleCallback', () => {
|
|
63
|
-
it('should store the authorization code', async () => {
|
|
64
|
-
const code = 'test-code';
|
|
65
|
-
const req = { body: { query: { code } } };
|
|
66
|
-
const res = { sendStatus: sinon_1.default.stub() };
|
|
67
|
-
await oauth2Helper['handleCallback'](req, res);
|
|
68
|
-
strict_1.default.equal(oauth2Helper['code'], code);
|
|
69
|
-
sinon_1.default.assert.calledOnce(res.sendStatus);
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
describe('handleToken', () => {
|
|
73
|
-
it('should retrieve the access token and return it in the response', async () => {
|
|
74
|
-
const req = { query: {} };
|
|
75
|
-
const res = { json: sinon_1.default.stub() };
|
|
76
|
-
await oauth2Helper['handleToken'](req, res);
|
|
77
|
-
sinon_1.default.assert.calledOnce(fetchStub);
|
|
78
|
-
sinon_1.default.assert.calledWith(fetchStub, 'https://provider.com/oauth/token', {
|
|
79
|
-
headers: {
|
|
80
|
-
'Content-type': 'application/x-www-form-urlencoded',
|
|
81
|
-
Authorization: 'custom',
|
|
82
|
-
},
|
|
83
|
-
body: sinon_1.default.match.string,
|
|
84
|
-
method: 'POST',
|
|
85
|
-
});
|
|
86
|
-
sinon_1.default.assert.calledOnce(res.json);
|
|
87
|
-
});
|
|
88
|
-
it('should handle errors and return 500 status', async () => {
|
|
89
|
-
const req = { query: {} };
|
|
90
|
-
const res = { status: sinon_1.default.stub().returns({ json: sinon_1.default.stub() }) };
|
|
91
|
-
fetchStub.rejects(new Error('Failed to retrieve access token'));
|
|
92
|
-
await oauth2Helper['handleToken'](req, res);
|
|
93
|
-
sinon_1.default.assert.calledWith(res.status, 500);
|
|
94
|
-
});
|
|
95
|
-
it('should return an error 400 if the request content type is not supported', async () => {
|
|
96
|
-
const req = { query: {} };
|
|
97
|
-
const res = { status: sinon_1.default.stub().returns({ json: sinon_1.default.stub() }) };
|
|
98
|
-
oauth2Helper['requestContentType'] = 'random';
|
|
99
|
-
await oauth2Helper['handleToken'](req, res);
|
|
100
|
-
sinon_1.default.assert.calledWith(res.status, 400);
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
describe('updateToken', () => {
|
|
104
|
-
it('should throw an error when no refresh token is available', async () => {
|
|
105
|
-
const response = oauth2Helper.updateToken();
|
|
106
|
-
await strict_1.default.rejects(response, errors_1.NoRefreshTokenError);
|
|
107
|
-
});
|
|
108
|
-
it('should retrieve the access token when a refresh token is available', async () => {
|
|
109
|
-
const refreshToken = 'test-refresh-token';
|
|
110
|
-
oauth2Helper['refreshToken'] = refreshToken;
|
|
111
|
-
const accessToken = 'test-access-token';
|
|
112
|
-
const fetchResponse = { access_token: accessToken, refresh_token: refreshToken };
|
|
113
|
-
fetchStub.resolves({ status: 200, json: sinon_1.default.stub().resolves(fetchResponse) });
|
|
114
|
-
const result = await oauth2Helper.updateToken();
|
|
115
|
-
strict_1.default.deepEqual(result, { accessToken, refreshToken });
|
|
116
|
-
sinon_1.default.assert.calledOnce(fetchStub);
|
|
117
|
-
});
|
|
118
|
-
it('should throw an error when failing to refresh the access token', async () => {
|
|
119
|
-
const refreshToken = 'test-refresh-token';
|
|
120
|
-
oauth2Helper['refreshToken'] = refreshToken;
|
|
121
|
-
fetchStub.rejects(new Error('Failed to retrieve access token'));
|
|
122
|
-
const response = oauth2Helper.updateToken();
|
|
123
|
-
await strict_1.default.rejects(response, errors_1.FailedToRetrieveAccessTokenError);
|
|
124
|
-
sinon_1.default.assert.calledOnce(fetchStub);
|
|
125
|
-
});
|
|
126
|
-
it("should throw an error if we don't support the request content type", async () => {
|
|
127
|
-
const refreshToken = 'test-refresh-token';
|
|
128
|
-
oauth2Helper['refreshToken'] = refreshToken;
|
|
129
|
-
oauth2Helper['requestContentType'] = 'random';
|
|
130
|
-
const response = oauth2Helper.updateToken();
|
|
131
|
-
await strict_1.default.rejects(response, errors_1.InvalidRequestContentTypeError);
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
describe('encodeBody', () => {
|
|
135
|
-
it('should encode body data as URL-encoded when content type is URL_ENCODED', () => {
|
|
136
|
-
const bodyData = { key1: 'value1', key2: 'value2' };
|
|
137
|
-
const contentType = configurationTypes_1.RequestContentType.URL_ENCODED;
|
|
138
|
-
const expectedEncodedBody = 'key1=value1&key2=value2';
|
|
139
|
-
const encodedBody = oauth2Helper['encodeBody'](bodyData, contentType);
|
|
140
|
-
strict_1.default.equal(encodedBody, expectedEncodedBody);
|
|
141
|
-
});
|
|
142
|
-
it('should encode body data as JSON when content type is JSON', () => {
|
|
143
|
-
const bodyData = { key1: 'value1', key2: 'value2' };
|
|
144
|
-
const contentType = configurationTypes_1.RequestContentType.JSON;
|
|
145
|
-
const expectedEncodedBody = JSON.stringify(bodyData);
|
|
146
|
-
const encodedBody = oauth2Helper['encodeBody'](bodyData, contentType);
|
|
147
|
-
strict_1.default.equal(encodedBody, expectedEncodedBody);
|
|
148
|
-
});
|
|
149
|
-
});
|
|
150
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const strict_1 = tslib_1.__importDefault(require("node:assert/strict"));
|
|
5
|
-
const sinon_1 = tslib_1.__importDefault(require("sinon"));
|
|
6
|
-
const oauth2Helper_1 = tslib_1.__importDefault(require("../../src/oauth2Helper/oauth2Helper"));
|
|
7
|
-
const configurationTypes_1 = require("../../src/configurationTypes");
|
|
8
|
-
const oauth2HelperService = tslib_1.__importStar(require("../../src/services/oauth2Helper"));
|
|
9
|
-
const errors_1 = require("../../src/errors");
|
|
10
|
-
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
|
|
11
|
-
describe('OAuth2Helper', () => {
|
|
12
|
-
let fetchStub;
|
|
13
|
-
const authorizationInfo = {
|
|
14
|
-
clientId: 'your-client-id',
|
|
15
|
-
clientSecret: 'your-client-secret',
|
|
16
|
-
authorizationUrl: 'https://provider.com/oauth/authorize',
|
|
17
|
-
scopes: [{ name: 'scope1' }, { name: 'scope2' }],
|
|
18
|
-
tokenUrl: 'https://provider.com/oauth/token',
|
|
19
|
-
grantType: configurationTypes_1.GrantType.AUTHORIZATION_CODE,
|
|
20
|
-
requestContentType: configurationTypes_1.RequestContentType.URL_ENCODED,
|
|
21
|
-
responseContentType: configurationTypes_1.RequestContentType.JSON,
|
|
22
|
-
};
|
|
23
|
-
beforeEach(() => {
|
|
24
|
-
sinon_1.default.stub(oauth2Helper_1.default.prototype, 'startServer').resolves('http://localhost:5050');
|
|
25
|
-
sinon_1.default.stub(oauth2Helper_1.default.prototype, 'stopServer');
|
|
26
|
-
sinon_1.default.stub(oauth2Helper_1.default.prototype, 'callbackIsDone');
|
|
27
|
-
sinon_1.default.stub(inquirer_1.default, 'prompt');
|
|
28
|
-
});
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
sinon_1.default.restore();
|
|
31
|
-
});
|
|
32
|
-
describe('performOAuth2Flow', () => {
|
|
33
|
-
it('should perform the oauth flow', async () => {
|
|
34
|
-
fetchStub = sinon_1.default
|
|
35
|
-
.stub()
|
|
36
|
-
.onFirstCall()
|
|
37
|
-
.resolves({ status: 200 })
|
|
38
|
-
.onSecondCall()
|
|
39
|
-
.resolves({ status: 200 })
|
|
40
|
-
.onThirdCall()
|
|
41
|
-
.resolves({
|
|
42
|
-
status: 200,
|
|
43
|
-
json: () => {
|
|
44
|
-
return { accessToken: 'token', refresToken: 'refreshToken' };
|
|
45
|
-
},
|
|
46
|
-
});
|
|
47
|
-
sinon_1.default.replace(global, 'fetch', fetchStub);
|
|
48
|
-
await oauth2HelperService.performOAuth2Flow(authorizationInfo);
|
|
49
|
-
strict_1.default.equal(fetchStub.getCall(0).args.at(0), 'http://localhost:5050/health');
|
|
50
|
-
strict_1.default.equal(fetchStub.getCall(1).args.at(0), 'http://localhost:5050/credentials/new/oauth2/authorize');
|
|
51
|
-
strict_1.default.equal(fetchStub.getCall(2).args.at(0), 'http://localhost:5050/credentials/new/oauth2/token');
|
|
52
|
-
});
|
|
53
|
-
it('raises a FailedToRetrieveAccessTokenError', async () => {
|
|
54
|
-
fetchStub = sinon_1.default
|
|
55
|
-
.stub()
|
|
56
|
-
.onFirstCall()
|
|
57
|
-
.resolves({ status: 200 })
|
|
58
|
-
.onSecondCall()
|
|
59
|
-
.resolves({ status: 200 })
|
|
60
|
-
.onThirdCall()
|
|
61
|
-
.resolves({ status: 500, text: () => 'error' });
|
|
62
|
-
sinon_1.default.replace(global, 'fetch', fetchStub);
|
|
63
|
-
const response = oauth2HelperService.performOAuth2Flow(authorizationInfo);
|
|
64
|
-
await strict_1.default.rejects(response, errors_1.FailedToRetrieveAccessTokenError);
|
|
65
|
-
});
|
|
66
|
-
it('raises a FailedToRetrieveAccessTokenError when the accessToken is not returned', async () => {
|
|
67
|
-
fetchStub = sinon_1.default
|
|
68
|
-
.stub()
|
|
69
|
-
.onFirstCall()
|
|
70
|
-
.resolves({ status: 200 })
|
|
71
|
-
.onSecondCall()
|
|
72
|
-
.resolves({ status: 200 })
|
|
73
|
-
.onThirdCall()
|
|
74
|
-
.resolves({
|
|
75
|
-
status: 200,
|
|
76
|
-
json: () => {
|
|
77
|
-
return {};
|
|
78
|
-
},
|
|
79
|
-
});
|
|
80
|
-
sinon_1.default.replace(global, 'fetch', fetchStub);
|
|
81
|
-
const response = oauth2HelperService.performOAuth2Flow(authorizationInfo);
|
|
82
|
-
await strict_1.default.rejects(response, errors_1.FailedToRetrieveAccessTokenError);
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
});
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|